xref: /php-src/ext/pdo_pgsql/tests/copy_to.phpt (revision 4f84b159)
1--TEST--
2PDO PgSQL pgsqlCopyToArray and pgsqlCopyToFile
3--EXTENSIONS--
4pdo
5pdo_pgsql
6--SKIPIF--
7<?php
8require __DIR__ . '/config.inc';
9require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
10PDOTest::skip();
11?>
12--FILE--
13<?php
14require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
15$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
16$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
17$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
18
19$db->exec('CREATE TABLE test_copy_to (a integer not null primary key, b text, c integer)');
20
21$db->beginTransaction();
22
23echo "Preparing test table for CopyTo tests\n";
24$stmt = $db->prepare("INSERT INTO test_copy_to (a, b, c) values (?, ?, ?)");
25
26for($i=0;$i<3;$i++) {
27    $firstParameter = $i;
28    $secondParameter = "test insert {$i}";
29    $thirdParameter = NULL;
30    $stmt->bindValue(1, $firstParameter);
31    $stmt->bindValue(2, $secondParameter);
32    $stmt->bindValue(3, $thirdParameter);
33    $stmt->execute();
34}
35
36$db->commit();
37
38echo "Testing pgsqlCopyToArray() with default parameters\n";
39var_dump($db->pgsqlCopyToArray('test_copy_to'));
40echo "Testing pgsqlCopyToArray() with different field separator and not null indicator\n";
41var_dump($db->pgsqlCopyToArray('test_copy_to',";","NULL"));
42echo "Testing pgsqlCopyToArray() with only selected fields\n";
43var_dump($db->pgsqlCopyToArray('test_copy_to',";","NULL",'a,c'));
44
45echo "Testing pgsqlCopyToArray() with error\n";
46try {
47    var_dump($db->pgsqlCopyToArray('test_error'));
48} catch (Exception $e) {
49    echo "Exception: {$e->getMessage()}\n";
50}
51
52echo "Testing pgsqlCopyToFile() with default parameters\n";
53
54$filename="test_pgsqlCopyToFile.csv";
55var_dump($db->pgsqlCopyToFile('test_copy_to',$filename));
56echo file_get_contents($filename);
57echo "Testing pgsqlCopyToFile() with different field separator and not null indicator\n";
58var_dump($db->pgsqlCopyToFile('test_copy_to',$filename,";","NULL"));
59echo file_get_contents($filename);
60echo "Testing pgsqlCopyToFile() with only selected fields\n";
61var_dump($db->pgsqlCopyToFile('test_copy_to',$filename,";","NULL",'a,c'));
62echo file_get_contents($filename);
63
64echo "Testing pgsqlCopyToFile() with error\n";
65try {
66    var_dump($db->pgsqlCopyToFile('test_error',$filename));
67} catch (Exception $e) {
68    echo "Exception: {$e->getMessage()}\n";
69}
70
71echo "Testing pgsqlCopyToFile() to unwritable file\n";
72try {
73    var_dump($db->pgsqlCopyToFile('test_copy_to', 'nonexistent/foo.csv'));
74} catch (Exception $e) {
75    echo "Exception: {$e->getMessage()}\n";
76}
77
78if(isset($filename)) {
79    @unlink($filename);
80}
81?>
82--CLEAN--
83<?php
84require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
85$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
86$db->exec('DROP TABLE test_copy_to');
87?>
88--EXPECTF--
89Preparing test table for CopyTo tests
90Testing pgsqlCopyToArray() with default parameters
91array(3) {
92  [0]=>
93  string(19) "0	test insert 0	\N
94"
95  [1]=>
96  string(19) "1	test insert 1	\N
97"
98  [2]=>
99  string(19) "2	test insert 2	\N
100"
101}
102Testing pgsqlCopyToArray() with different field separator and not null indicator
103array(3) {
104  [0]=>
105  string(21) "0;test insert 0;NULL
106"
107  [1]=>
108  string(21) "1;test insert 1;NULL
109"
110  [2]=>
111  string(21) "2;test insert 2;NULL
112"
113}
114Testing pgsqlCopyToArray() with only selected fields
115array(3) {
116  [0]=>
117  string(7) "0;NULL
118"
119  [1]=>
120  string(7) "1;NULL
121"
122  [2]=>
123  string(7) "2;NULL
124"
125}
126Testing pgsqlCopyToArray() with error
127Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
128Testing pgsqlCopyToFile() with default parameters
129bool(true)
1300	test insert 0	\N
1311	test insert 1	\N
1322	test insert 2	\N
133Testing pgsqlCopyToFile() with different field separator and not null indicator
134bool(true)
1350;test insert 0;NULL
1361;test insert 1;NULL
1372;test insert 2;NULL
138Testing pgsqlCopyToFile() with only selected fields
139bool(true)
1400;NULL
1411;NULL
1422;NULL
143Testing pgsqlCopyToFile() with error
144Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
145Testing pgsqlCopyToFile() to unwritable file
146Exception: SQLSTATE[HY000]: General error: 7 Unable to open the file for writing
147