xref: /PHP-5.3/ext/pdo_pgsql/tests/copy_to.phpt (revision 9daa864c)
1--TEST--
2PDO PgSQL pgsqlCopyToArray and pgsqlCopyToFile
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
19$db->beginTransaction();
20try {
21
22echo "Preparing test table for CopyTo tests\n";
23$stmt = $db->prepare("INSERT INTO test (a, b, c) values (?, ?, ?)");
24
25for($i=0;$i<3;$i++) {
26	$firstParameter = $i;
27	$secondParameter = "test insert {$i}";
28	$thirdParameter = NULL;
29	$stmt->bindValue(1, $firstParameter);
30	$stmt->bindValue(2, $secondParameter);
31	$stmt->bindValue(3, $thirdParameter);
32	$stmt->execute();
33}
34
35$db->commit();
36
37echo "Testing pgsqlCopyToArray() with default parameters\n";
38var_dump($db->pgsqlCopyToArray('test'));
39echo "Testing pgsqlCopyToArray() with different field separator and not null indicator\n";
40var_dump($db->pgsqlCopyToArray('test',";","NULL"));
41echo "Testing pgsqlCopyToArray() with only selected fields\n";
42var_dump($db->pgsqlCopyToArray('test',";","NULL",'a,c'));
43
44echo "Testing pgsqlCopyToArray() with error\n";
45var_dump($db->pgsqlCopyToArray('test_error'));
46
47
48echo "Testing pgsqlCopyToFile() with default parameters\n";
49
50$filename="test_pgsqlCopyToFile.csv";
51var_dump($db->pgsqlCopyToFile('test',$filename));
52echo file_get_contents($filename);
53echo "Testing pgsqlCopyToFile() with different field separator and not null indicator\n";
54var_dump($db->pgsqlCopyToFile('test',$filename,";","NULL"));
55echo file_get_contents($filename);
56echo "Testing pgsqlCopyToFile() with only selected fields\n";
57var_dump($db->pgsqlCopyToFile('test',$filename,";","NULL",'a,c'));
58echo file_get_contents($filename);
59
60echo "Testing pgsqlCopyToFile() with error\n";
61var_dump($db->pgsqlCopyToFile('test_error',$filename));
62
63
64} catch (Exception $e) {
65	/* catch exceptions so that we can show the relative error */
66	echo "Exception! at line ", $e->getLine(), "\n";
67	var_dump($e->getMessage());
68}
69if(isset($filename)) {
70	@unlink($filename);
71}
72?>
73--EXPECT--
74Preparing test table for CopyTo tests
75Testing pgsqlCopyToArray() with default parameters
76array(3) {
77  [0]=>
78  string(19) "0	test insert 0	\N
79"
80  [1]=>
81  string(19) "1	test insert 1	\N
82"
83  [2]=>
84  string(19) "2	test insert 2	\N
85"
86}
87Testing pgsqlCopyToArray() with different field separator and not null indicator
88array(3) {
89  [0]=>
90  string(21) "0;test insert 0;NULL
91"
92  [1]=>
93  string(21) "1;test insert 1;NULL
94"
95  [2]=>
96  string(21) "2;test insert 2;NULL
97"
98}
99Testing pgsqlCopyToArray() with only selected fields
100array(3) {
101  [0]=>
102  string(7) "0;NULL
103"
104  [1]=>
105  string(7) "1;NULL
106"
107  [2]=>
108  string(7) "2;NULL
109"
110}
111Testing pgsqlCopyToArray() with error
112bool(false)
113Testing pgsqlCopyToFile() with default parameters
114bool(true)
1150	test insert 0	\N
1161	test insert 1	\N
1172	test insert 2	\N
118Testing pgsqlCopyToFile() with different field separator and not null indicator
119bool(true)
1200;test insert 0;NULL
1211;test insert 1;NULL
1222;test insert 2;NULL
123Testing pgsqlCopyToFile() with only selected fields
124bool(true)
1250;NULL
1261;NULL
1272;NULL
128Testing pgsqlCopyToFile() with error
129bool(false)