xref: /PHP-5.4/ext/pdo_pgsql/tests/copy_from.phpt (revision 2463e897)
1--TEST--
2PDO PgSQL pgsqlCopyFromArray and pgsqlCopyFromFile
3--SKIPIF--
4<?php # vim:se ft=php:
5if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6require dirname(__FILE__) . '/config.inc';
7require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
8PDOTest::skip();
9?>
10--FILE--
11<?php
12require 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
19try {
20
21echo "Preparing test file and array for CopyFrom tests\n";
22
23$tableRows = array();
24$tableRowsWithDifferentNullValues = array();
25
26for($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
37file_put_contents($filename, implode("\n",$tableRows));
38file_put_contents($filenameWithDifferentNullValues, implode("\n",$tableRowsWithDifferentNullValues));
39file_put_contents($filenameWithDifferentNullValuesAndSelectedFields, implode("\n",$tableRowsWithDifferentNullValuesAndSelectedFields));
40
41echo "Testing pgsqlCopyFromArray() with default parameters\n";
42$db->beginTransaction();
43var_dump($db->pgsqlCopyFromArray('test',$tableRows));
44
45$stmt = $db->query("select * from test");
46foreach($stmt as $r) {
47	var_dump($r);
48}
49$db->rollback();
50
51echo "Testing pgsqlCopyFromArray() with different field separator and not null indicator\n";
52$db->beginTransaction();
53var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValues,";","NULL"));
54$stmt = $db->query("select * from test");
55foreach($stmt as $r) {
56	var_dump($r);
57}
58$db->rollback();
59
60echo "Testing pgsqlCopyFromArray() with only selected fields\n";
61$db->beginTransaction();
62var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
63$stmt = $db->query("select * from test");
64foreach($stmt as $r) {
65	var_dump($r);
66}
67$db->rollback();
68
69echo "Testing pgsqlCopyFromArray() with error\n";
70$db->beginTransaction();
71var_dump($db->pgsqlCopyFromArray('test_error',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
72$db->rollback();
73
74
75echo "Testing pgsqlCopyFromFile() with default parameters\n";
76$db->beginTransaction();
77var_dump($db->pgsqlCopyFromFile('test',$filename));
78
79$stmt = $db->query("select * from test");
80foreach($stmt as $r) {
81	var_dump($r);
82}
83$db->rollback();
84
85echo "Testing pgsqlCopyFromFile() with different field separator and not null indicator\n";
86$db->beginTransaction();
87var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValues,";","NULL"));
88$stmt = $db->query("select * from test");
89foreach($stmt as $r) {
90	var_dump($r);
91}
92$db->rollback();
93
94echo "Testing pgsqlCopyFromFile() with only selected fields\n";
95$db->beginTransaction();
96var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
97$stmt = $db->query("select * from test");
98foreach($stmt as $r) {
99	var_dump($r);
100}
101$db->rollback();
102
103echo "Testing pgsqlCopyFromFile() with error\n";
104$db->beginTransaction();
105var_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}
113
114// Clean up
115foreach (array($filename, $filenameWithDifferentNullValues, $filenameWithDifferentNullValuesAndSelectedFields) as $f) {
116	@unlink($f);
117}
118?>
119--EXPECT--
120Preparing test file and array for CopyFrom tests
121Testing pgsqlCopyFromArray() with default parameters
122bool(true)
123array(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}
137array(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}
151array(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}
165Testing pgsqlCopyFromArray() with different field separator and not null indicator
166bool(true)
167array(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}
181array(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}
195array(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}
209Testing pgsqlCopyFromArray() with only selected fields
210bool(true)
211array(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}
225array(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}
239array(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}
253Testing pgsqlCopyFromArray() with error
254bool(false)
255Testing pgsqlCopyFromFile() with default parameters
256bool(true)
257array(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}
271array(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}
285array(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}
299Testing pgsqlCopyFromFile() with different field separator and not null indicator
300bool(true)
301array(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}
315array(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}
329array(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}
343Testing pgsqlCopyFromFile() with only selected fields
344bool(true)
345array(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}
359array(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}
373array(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}
387Testing pgsqlCopyFromFile() with error
388bool(false)
389