xref: /PHP-8.2/ext/pdo_pgsql/tests/copy_to.phpt (revision 39131219)
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 (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 (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'));
40echo "Testing pgsqlCopyToArray() with different field separator and not null indicator\n";
41var_dump($db->pgsqlCopyToArray('test',";","NULL"));
42echo "Testing pgsqlCopyToArray() with only selected fields\n";
43var_dump($db->pgsqlCopyToArray('test',";","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',$filename));
56echo file_get_contents($filename);
57echo "Testing pgsqlCopyToFile() with different field separator and not null indicator\n";
58var_dump($db->pgsqlCopyToFile('test',$filename,";","NULL"));
59echo file_get_contents($filename);
60echo "Testing pgsqlCopyToFile() with only selected fields\n";
61var_dump($db->pgsqlCopyToFile('test',$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', 'nonexistent/foo.csv'));
74} catch (Exception $e) {
75    echo "Exception: {$e->getMessage()}\n";
76}
77
78if(isset($filename)) {
79    @unlink($filename);
80}
81?>
82--EXPECTF--
83Preparing test table for CopyTo tests
84Testing pgsqlCopyToArray() with default parameters
85array(3) {
86  [0]=>
87  string(19) "0	test insert 0	\N
88"
89  [1]=>
90  string(19) "1	test insert 1	\N
91"
92  [2]=>
93  string(19) "2	test insert 2	\N
94"
95}
96Testing pgsqlCopyToArray() with different field separator and not null indicator
97array(3) {
98  [0]=>
99  string(21) "0;test insert 0;NULL
100"
101  [1]=>
102  string(21) "1;test insert 1;NULL
103"
104  [2]=>
105  string(21) "2;test insert 2;NULL
106"
107}
108Testing pgsqlCopyToArray() with only selected fields
109array(3) {
110  [0]=>
111  string(7) "0;NULL
112"
113  [1]=>
114  string(7) "1;NULL
115"
116  [2]=>
117  string(7) "2;NULL
118"
119}
120Testing pgsqlCopyToArray() with error
121Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
122Testing pgsqlCopyToFile() with default parameters
123bool(true)
1240	test insert 0	\N
1251	test insert 1	\N
1262	test insert 2	\N
127Testing pgsqlCopyToFile() with different field separator and not null indicator
128bool(true)
1290;test insert 0;NULL
1301;test insert 1;NULL
1312;test insert 2;NULL
132Testing pgsqlCopyToFile() with only selected fields
133bool(true)
1340;NULL
1351;NULL
1362;NULL
137Testing pgsqlCopyToFile() with error
138Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
139Testing pgsqlCopyToFile() to unwritable file
140Exception: SQLSTATE[HY000]: General error: 7 Unable to open the file for writing
141