copy_to.phpt 3.8 KB

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