Annotation of embedaddon/php/ext/pdo_pgsql/tests/copy_from.phpt, revision 1.1.1.2
1.1 misho 1: --TEST--
2: PDO PgSQL pgsqlCopyFromArray and pgsqlCopyFromFile
3: --SKIPIF--
4: <?php # vim:se ft=php:
5: if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6: require dirname(__FILE__) . '/config.inc';
7: require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
8: PDOTest::skip();
9: ?>
10: --FILE--
11: <?php
12: require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
13: $db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
14: $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
15: $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
16:
17: $db->exec('CREATE TABLE test (a integer not null primary key, b text, c integer)');
18:
19: try {
20:
21: echo "Preparing test file and array for CopyFrom tests\n";
22:
23: $tableRows = array();
24: $tableRowsWithDifferentNullValues = array();
25:
26: for($i=0;$i<3;$i++) {
27: $firstParameter = $i;
28: $secondParameter = "test insert {$i}";
29: $tableRows[] = "{$firstParameter}\t{$secondParameter}\t\\N";
30: $tableRowsWithDifferentNullValues[] = "{$firstParameter};{$secondParameter};NULL";
31: $tableRowsWithDifferentNullValuesAndSelectedFields[] = "{$firstParameter};NULL";
32: }
33: $filename = 'test_pgsqlCopyFromFile.csv';
34: $filenameWithDifferentNullValues = 'test_pgsqlCopyFromFileWithDifferentNullValues.csv';
35: $filenameWithDifferentNullValuesAndSelectedFields = 'test_pgsqlCopyFromFileWithDifferentNullValuesAndSelectedFields.csv';
36:
37: file_put_contents($filename, implode("\n",$tableRows));
38: file_put_contents($filenameWithDifferentNullValues, implode("\n",$tableRowsWithDifferentNullValues));
39: file_put_contents($filenameWithDifferentNullValuesAndSelectedFields, implode("\n",$tableRowsWithDifferentNullValuesAndSelectedFields));
40:
41: echo "Testing pgsqlCopyFromArray() with default parameters\n";
42: $db->beginTransaction();
43: var_dump($db->pgsqlCopyFromArray('test',$tableRows));
44:
45: $stmt = $db->query("select * from test");
46: foreach($stmt as $r) {
47: var_dump($r);
48: }
49: $db->rollback();
50:
51: echo "Testing pgsqlCopyFromArray() with different field separator and not null indicator\n";
52: $db->beginTransaction();
53: var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValues,";","NULL"));
54: $stmt = $db->query("select * from test");
55: foreach($stmt as $r) {
56: var_dump($r);
57: }
58: $db->rollback();
59:
60: echo "Testing pgsqlCopyFromArray() with only selected fields\n";
61: $db->beginTransaction();
62: var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
63: $stmt = $db->query("select * from test");
64: foreach($stmt as $r) {
65: var_dump($r);
66: }
67: $db->rollback();
68:
69: echo "Testing pgsqlCopyFromArray() with error\n";
70: $db->beginTransaction();
71: var_dump($db->pgsqlCopyFromArray('test_error',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
72: $db->rollback();
73:
74:
75: echo "Testing pgsqlCopyFromFile() with default parameters\n";
76: $db->beginTransaction();
77: var_dump($db->pgsqlCopyFromFile('test',$filename));
78:
79: $stmt = $db->query("select * from test");
80: foreach($stmt as $r) {
81: var_dump($r);
82: }
83: $db->rollback();
84:
85: echo "Testing pgsqlCopyFromFile() with different field separator and not null indicator\n";
86: $db->beginTransaction();
87: var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValues,";","NULL"));
88: $stmt = $db->query("select * from test");
89: foreach($stmt as $r) {
90: var_dump($r);
91: }
92: $db->rollback();
93:
94: echo "Testing pgsqlCopyFromFile() with only selected fields\n";
95: $db->beginTransaction();
96: var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
97: $stmt = $db->query("select * from test");
98: foreach($stmt as $r) {
99: var_dump($r);
100: }
101: $db->rollback();
102:
103: echo "Testing pgsqlCopyFromFile() with error\n";
104: $db->beginTransaction();
105: var_dump($db->pgsqlCopyFromFile('test_error',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
106: $db->rollback();
107:
108: } catch (Exception $e) {
109: /* catch exceptions so that we can show the relative error */
110: echo "Exception! at line ", $e->getLine(), "\n";
111: var_dump($e->getMessage());
112: }
1.1.1.2 ! misho 113:
! 114: // Clean up
! 115: foreach (array($filename, $filenameWithDifferentNullValues, $filenameWithDifferentNullValuesAndSelectedFields) as $f) {
! 116: @unlink($f);
1.1 misho 117: }
118: ?>
119: --EXPECT--
120: Preparing test file and array for CopyFrom tests
121: Testing pgsqlCopyFromArray() with default parameters
122: bool(true)
123: array(6) {
124: ["a"]=>
125: int(0)
126: [0]=>
127: int(0)
128: ["b"]=>
129: string(13) "test insert 0"
130: [1]=>
131: string(13) "test insert 0"
132: ["c"]=>
133: NULL
134: [2]=>
135: NULL
136: }
137: array(6) {
138: ["a"]=>
139: int(1)
140: [0]=>
141: int(1)
142: ["b"]=>
143: string(13) "test insert 1"
144: [1]=>
145: string(13) "test insert 1"
146: ["c"]=>
147: NULL
148: [2]=>
149: NULL
150: }
151: array(6) {
152: ["a"]=>
153: int(2)
154: [0]=>
155: int(2)
156: ["b"]=>
157: string(13) "test insert 2"
158: [1]=>
159: string(13) "test insert 2"
160: ["c"]=>
161: NULL
162: [2]=>
163: NULL
164: }
165: Testing pgsqlCopyFromArray() with different field separator and not null indicator
166: bool(true)
167: array(6) {
168: ["a"]=>
169: int(0)
170: [0]=>
171: int(0)
172: ["b"]=>
173: string(13) "test insert 0"
174: [1]=>
175: string(13) "test insert 0"
176: ["c"]=>
177: NULL
178: [2]=>
179: NULL
180: }
181: array(6) {
182: ["a"]=>
183: int(1)
184: [0]=>
185: int(1)
186: ["b"]=>
187: string(13) "test insert 1"
188: [1]=>
189: string(13) "test insert 1"
190: ["c"]=>
191: NULL
192: [2]=>
193: NULL
194: }
195: array(6) {
196: ["a"]=>
197: int(2)
198: [0]=>
199: int(2)
200: ["b"]=>
201: string(13) "test insert 2"
202: [1]=>
203: string(13) "test insert 2"
204: ["c"]=>
205: NULL
206: [2]=>
207: NULL
208: }
209: Testing pgsqlCopyFromArray() with only selected fields
210: bool(true)
211: array(6) {
212: ["a"]=>
213: int(0)
214: [0]=>
215: int(0)
216: ["b"]=>
217: NULL
218: [1]=>
219: NULL
220: ["c"]=>
221: NULL
222: [2]=>
223: NULL
224: }
225: array(6) {
226: ["a"]=>
227: int(1)
228: [0]=>
229: int(1)
230: ["b"]=>
231: NULL
232: [1]=>
233: NULL
234: ["c"]=>
235: NULL
236: [2]=>
237: NULL
238: }
239: array(6) {
240: ["a"]=>
241: int(2)
242: [0]=>
243: int(2)
244: ["b"]=>
245: NULL
246: [1]=>
247: NULL
248: ["c"]=>
249: NULL
250: [2]=>
251: NULL
252: }
253: Testing pgsqlCopyFromArray() with error
254: bool(false)
255: Testing pgsqlCopyFromFile() with default parameters
256: bool(true)
257: array(6) {
258: ["a"]=>
259: int(0)
260: [0]=>
261: int(0)
262: ["b"]=>
263: string(13) "test insert 0"
264: [1]=>
265: string(13) "test insert 0"
266: ["c"]=>
267: NULL
268: [2]=>
269: NULL
270: }
271: array(6) {
272: ["a"]=>
273: int(1)
274: [0]=>
275: int(1)
276: ["b"]=>
277: string(13) "test insert 1"
278: [1]=>
279: string(13) "test insert 1"
280: ["c"]=>
281: NULL
282: [2]=>
283: NULL
284: }
285: array(6) {
286: ["a"]=>
287: int(2)
288: [0]=>
289: int(2)
290: ["b"]=>
291: string(13) "test insert 2"
292: [1]=>
293: string(13) "test insert 2"
294: ["c"]=>
295: NULL
296: [2]=>
297: NULL
298: }
299: Testing pgsqlCopyFromFile() with different field separator and not null indicator
300: bool(true)
301: array(6) {
302: ["a"]=>
303: int(0)
304: [0]=>
305: int(0)
306: ["b"]=>
307: string(13) "test insert 0"
308: [1]=>
309: string(13) "test insert 0"
310: ["c"]=>
311: NULL
312: [2]=>
313: NULL
314: }
315: array(6) {
316: ["a"]=>
317: int(1)
318: [0]=>
319: int(1)
320: ["b"]=>
321: string(13) "test insert 1"
322: [1]=>
323: string(13) "test insert 1"
324: ["c"]=>
325: NULL
326: [2]=>
327: NULL
328: }
329: array(6) {
330: ["a"]=>
331: int(2)
332: [0]=>
333: int(2)
334: ["b"]=>
335: string(13) "test insert 2"
336: [1]=>
337: string(13) "test insert 2"
338: ["c"]=>
339: NULL
340: [2]=>
341: NULL
342: }
343: Testing pgsqlCopyFromFile() with only selected fields
344: bool(true)
345: array(6) {
346: ["a"]=>
347: int(0)
348: [0]=>
349: int(0)
350: ["b"]=>
351: NULL
352: [1]=>
353: NULL
354: ["c"]=>
355: NULL
356: [2]=>
357: NULL
358: }
359: array(6) {
360: ["a"]=>
361: int(1)
362: [0]=>
363: int(1)
364: ["b"]=>
365: NULL
366: [1]=>
367: NULL
368: ["c"]=>
369: NULL
370: [2]=>
371: NULL
372: }
373: array(6) {
374: ["a"]=>
375: int(2)
376: [0]=>
377: int(2)
378: ["b"]=>
379: NULL
380: [1]=>
381: NULL
382: ["c"]=>
383: NULL
384: [2]=>
385: NULL
386: }
387: Testing pgsqlCopyFromFile() with error
1.1.1.2 ! misho 388: bool(false)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>