123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- --TEST--
- PDO PgSQL pgsqlCopyFromArray and pgsqlCopyFromFile
- --EXTENSIONS--
- pdo
- pdo_pgsql
- --SKIPIF--
- <?php
- require __DIR__ . '/config.inc';
- require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
- PDOTest::skip();
- ?>
- --FILE--
- <?php
- require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
- $db = PDOTest::test_factory(__DIR__ . '/common.phpt');
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
- $db->exec('CREATE TABLE test (a integer not null primary key, b text, c integer)');
- echo "Preparing test file and array for CopyFrom tests\n";
- $tableRows = array();
- $tableRowsWithDifferentNullValues = array();
- for($i=0;$i<3;$i++) {
- $firstParameter = $i;
- $secondParameter = "test insert {$i}";
- $tableRows[] = "{$firstParameter}\t{$secondParameter}\t\\N";
- $tableRowsWithDifferentNullValues[] = "{$firstParameter};{$secondParameter};NULL";
- $tableRowsWithDifferentNullValuesAndSelectedFields[] = "{$firstParameter};NULL";
- }
- $filename = 'test_pgsqlCopyFromFile.csv';
- $filenameWithDifferentNullValues = 'test_pgsqlCopyFromFileWithDifferentNullValues.csv';
- $filenameWithDifferentNullValuesAndSelectedFields = 'test_pgsqlCopyFromFileWithDifferentNullValuesAndSelectedFields.csv';
- file_put_contents($filename, implode("\n",$tableRows));
- file_put_contents($filenameWithDifferentNullValues, implode("\n",$tableRowsWithDifferentNullValues));
- file_put_contents($filenameWithDifferentNullValuesAndSelectedFields, implode("\n",$tableRowsWithDifferentNullValuesAndSelectedFields));
- echo "Testing pgsqlCopyFromArray() with default parameters\n";
- $db->beginTransaction();
- var_dump($db->pgsqlCopyFromArray('test',$tableRows));
- $stmt = $db->query("select * from test");
- foreach($stmt as $r) {
- var_dump($r);
- }
- $db->rollback();
- echo "Testing pgsqlCopyFromArray() with different field separator and not null indicator\n";
- $db->beginTransaction();
- var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValues,";","NULL"));
- $stmt = $db->query("select * from test");
- foreach($stmt as $r) {
- var_dump($r);
- }
- $db->rollback();
- echo "Testing pgsqlCopyFromArray() with only selected fields\n";
- $db->beginTransaction();
- var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
- $stmt = $db->query("select * from test");
- foreach($stmt as $r) {
- var_dump($r);
- }
- $db->rollback();
- echo "Testing pgsqlCopyFromArray() with error\n";
- $db->beginTransaction();
- try {
- var_dump($db->pgsqlCopyFromArray('test_error',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
- } catch (Exception $e) {
- echo "Exception: {$e->getMessage()}\n";
- }
- $db->rollback();
- echo "Testing pgsqlCopyFromFile() with default parameters\n";
- $db->beginTransaction();
- var_dump($db->pgsqlCopyFromFile('test',$filename));
- $stmt = $db->query("select * from test");
- foreach($stmt as $r) {
- var_dump($r);
- }
- $db->rollback();
- echo "Testing pgsqlCopyFromFile() with different field separator and not null indicator\n";
- $db->beginTransaction();
- var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValues,";","NULL"));
- $stmt = $db->query("select * from test");
- foreach($stmt as $r) {
- var_dump($r);
- }
- $db->rollback();
- echo "Testing pgsqlCopyFromFile() with only selected fields\n";
- $db->beginTransaction();
- var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
- $stmt = $db->query("select * from test");
- foreach($stmt as $r) {
- var_dump($r);
- }
- $db->rollback();
- echo "Testing pgsqlCopyFromFile() with error\n";
- $db->beginTransaction();
- try {
- var_dump($db->pgsqlCopyFromFile('test_error',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
- } catch (Exception $e) {
- echo "Exception: {$e->getMessage()}\n";
- }
- $db->rollback();
- echo "Testing pgsqlCopyFromFile() with non existing file\n";
- $db->beginTransaction();
- try {
- var_dump($db->pgsqlCopyFromFile('test',"nonexisting/foo.csv",";","NULL",'a,c'));
- } catch (Exception $e) {
- echo "Exception: {$e->getMessage()}\n";
- }
- $db->rollback();
- // Clean up
- foreach (array($filename, $filenameWithDifferentNullValues, $filenameWithDifferentNullValuesAndSelectedFields) as $f) {
- @unlink($f);
- }
- ?>
- --EXPECTF--
- Preparing test file and array for CopyFrom tests
- Testing pgsqlCopyFromArray() with default parameters
- bool(true)
- array(6) {
- ["a"]=>
- int(0)
- [0]=>
- int(0)
- ["b"]=>
- string(13) "test insert 0"
- [1]=>
- string(13) "test insert 0"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(1)
- [0]=>
- int(1)
- ["b"]=>
- string(13) "test insert 1"
- [1]=>
- string(13) "test insert 1"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(2)
- [0]=>
- int(2)
- ["b"]=>
- string(13) "test insert 2"
- [1]=>
- string(13) "test insert 2"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- Testing pgsqlCopyFromArray() with different field separator and not null indicator
- bool(true)
- array(6) {
- ["a"]=>
- int(0)
- [0]=>
- int(0)
- ["b"]=>
- string(13) "test insert 0"
- [1]=>
- string(13) "test insert 0"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(1)
- [0]=>
- int(1)
- ["b"]=>
- string(13) "test insert 1"
- [1]=>
- string(13) "test insert 1"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(2)
- [0]=>
- int(2)
- ["b"]=>
- string(13) "test insert 2"
- [1]=>
- string(13) "test insert 2"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- Testing pgsqlCopyFromArray() with only selected fields
- bool(true)
- array(6) {
- ["a"]=>
- int(0)
- [0]=>
- int(0)
- ["b"]=>
- NULL
- [1]=>
- NULL
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(1)
- [0]=>
- int(1)
- ["b"]=>
- NULL
- [1]=>
- NULL
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(2)
- [0]=>
- int(2)
- ["b"]=>
- NULL
- [1]=>
- NULL
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- Testing pgsqlCopyFromArray() with error
- Exception: SQLSTATE[42P01]: Undefined table: 7 %s: %stest_error%s
- Testing pgsqlCopyFromFile() with default parameters
- bool(true)
- array(6) {
- ["a"]=>
- int(0)
- [0]=>
- int(0)
- ["b"]=>
- string(13) "test insert 0"
- [1]=>
- string(13) "test insert 0"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(1)
- [0]=>
- int(1)
- ["b"]=>
- string(13) "test insert 1"
- [1]=>
- string(13) "test insert 1"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(2)
- [0]=>
- int(2)
- ["b"]=>
- string(13) "test insert 2"
- [1]=>
- string(13) "test insert 2"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- Testing pgsqlCopyFromFile() with different field separator and not null indicator
- bool(true)
- array(6) {
- ["a"]=>
- int(0)
- [0]=>
- int(0)
- ["b"]=>
- string(13) "test insert 0"
- [1]=>
- string(13) "test insert 0"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(1)
- [0]=>
- int(1)
- ["b"]=>
- string(13) "test insert 1"
- [1]=>
- string(13) "test insert 1"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(2)
- [0]=>
- int(2)
- ["b"]=>
- string(13) "test insert 2"
- [1]=>
- string(13) "test insert 2"
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- Testing pgsqlCopyFromFile() with only selected fields
- bool(true)
- array(6) {
- ["a"]=>
- int(0)
- [0]=>
- int(0)
- ["b"]=>
- NULL
- [1]=>
- NULL
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(1)
- [0]=>
- int(1)
- ["b"]=>
- NULL
- [1]=>
- NULL
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- array(6) {
- ["a"]=>
- int(2)
- [0]=>
- int(2)
- ["b"]=>
- NULL
- [1]=>
- NULL
- ["c"]=>
- NULL
- [2]=>
- NULL
- }
- Testing pgsqlCopyFromFile() with error
- Exception: SQLSTATE[42P01]: Undefined table: 7 %s: %stest_error%s
- Testing pgsqlCopyFromFile() with non existing file
- Exception: SQLSTATE[HY000]: General error: 7 Unable to open the file
|