xref: /PHP-7.4/ext/pdo_pgsql/tests/copy_to.phpt (revision d07a6fde)
1--TEST--
2PDO PgSQL pgsqlCopyToArray and pgsqlCopyToFile
3--SKIPIF--
4<?php
5if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6require __DIR__ . '/config.inc';
7require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
8PDOTest::skip();
9?>
10--FILE--
11<?php
12require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
13$db = PDOTest::test_factory(__DIR__ . '/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$db->beginTransaction();
20
21echo "Preparing test table for CopyTo tests\n";
22$stmt = $db->prepare("INSERT INTO test (a, b, c) values (?, ?, ?)");
23
24for($i=0;$i<3;$i++) {
25	$firstParameter = $i;
26	$secondParameter = "test insert {$i}";
27	$thirdParameter = NULL;
28	$stmt->bindValue(1, $firstParameter);
29	$stmt->bindValue(2, $secondParameter);
30	$stmt->bindValue(3, $thirdParameter);
31	$stmt->execute();
32}
33
34$db->commit();
35
36echo "Testing pgsqlCopyToArray() with default parameters\n";
37var_dump($db->pgsqlCopyToArray('test'));
38echo "Testing pgsqlCopyToArray() with different field separator and not null indicator\n";
39var_dump($db->pgsqlCopyToArray('test',";","NULL"));
40echo "Testing pgsqlCopyToArray() with only selected fields\n";
41var_dump($db->pgsqlCopyToArray('test',";","NULL",'a,c'));
42
43echo "Testing pgsqlCopyToArray() with error\n";
44try {
45	var_dump($db->pgsqlCopyToArray('test_error'));
46} catch (Exception $e) {
47	echo "Exception: {$e->getMessage()}\n";
48}
49
50echo "Testing pgsqlCopyToFile() with default parameters\n";
51
52$filename="test_pgsqlCopyToFile.csv";
53var_dump($db->pgsqlCopyToFile('test',$filename));
54echo file_get_contents($filename);
55echo "Testing pgsqlCopyToFile() with different field separator and not null indicator\n";
56var_dump($db->pgsqlCopyToFile('test',$filename,";","NULL"));
57echo file_get_contents($filename);
58echo "Testing pgsqlCopyToFile() with only selected fields\n";
59var_dump($db->pgsqlCopyToFile('test',$filename,";","NULL",'a,c'));
60echo file_get_contents($filename);
61
62echo "Testing pgsqlCopyToFile() with error\n";
63try {
64	var_dump($db->pgsqlCopyToFile('test_error',$filename));
65} catch (Exception $e) {
66	echo "Exception: {$e->getMessage()}\n";
67}
68
69echo "Testing pgsqlCopyToFile() to unwritable file\n";
70try {
71	var_dump($db->pgsqlCopyToFile('test', 'nonexistent/foo.csv'));
72} catch (Exception $e) {
73	echo "Exception: {$e->getMessage()}\n";
74}
75
76if(isset($filename)) {
77	@unlink($filename);
78}
79?>
80--EXPECTF--
81Preparing test table for CopyTo tests
82Testing pgsqlCopyToArray() with default parameters
83array(3) {
84  [0]=>
85  string(19) "0	test insert 0	\N
86"
87  [1]=>
88  string(19) "1	test insert 1	\N
89"
90  [2]=>
91  string(19) "2	test insert 2	\N
92"
93}
94Testing pgsqlCopyToArray() with different field separator and not null indicator
95array(3) {
96  [0]=>
97  string(21) "0;test insert 0;NULL
98"
99  [1]=>
100  string(21) "1;test insert 1;NULL
101"
102  [2]=>
103  string(21) "2;test insert 2;NULL
104"
105}
106Testing pgsqlCopyToArray() with only selected fields
107array(3) {
108  [0]=>
109  string(7) "0;NULL
110"
111  [1]=>
112  string(7) "1;NULL
113"
114  [2]=>
115  string(7) "2;NULL
116"
117}
118Testing pgsqlCopyToArray() with error
119Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
120Testing pgsqlCopyToFile() with default parameters
121bool(true)
1220	test insert 0	\N
1231	test insert 1	\N
1242	test insert 2	\N
125Testing pgsqlCopyToFile() with different field separator and not null indicator
126bool(true)
1270;test insert 0;NULL
1281;test insert 1;NULL
1292;test insert 2;NULL
130Testing pgsqlCopyToFile() with only selected fields
131bool(true)
1320;NULL
1331;NULL
1342;NULL
135Testing pgsqlCopyToFile() with error
136Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
137Testing pgsqlCopyToFile() to unwritable file
138Exception: SQLSTATE[HY000]: General error: 7 Unable to open the file for writing
139