copy_to.phpt 3.7 KB

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