bug_33876.phpt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. --TEST--
  2. PDO PgSQL Bug #33876 (PDO misquotes/miscasts bool(false))
  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_SILENT);
  17. $db->exec("SET LC_MESSAGES='C'");
  18. $db->exec('CREATE TABLE test (foo varchar(5) NOT NULL, bar bool NOT NULL)');
  19. $db->exec("INSERT INTO test VALUES('false','f')");
  20. $db->exec("INSERT INTO test VALUES('true', 't')");
  21. $res = $db->prepare('SELECT foo from test where bar = ?');
  22. # this is the portable approach to binding a bool
  23. $res->bindValue(1, false, PDO::PARAM_BOOL);
  24. if (!$res->execute())
  25. print_r($res->errorInfo());
  26. else
  27. print_r($res->fetchAll(PDO::FETCH_ASSOC));
  28. # this is the portable approach to binding a bool
  29. $res->bindValue(1, true, PDO::PARAM_BOOL);
  30. if (!$res->execute())
  31. print_r($res->errorInfo());
  32. else
  33. print_r($res->fetchAll(PDO::FETCH_ASSOC));
  34. # true gets cast to string (because the implied default is string)
  35. # true-as-string is 1, so this "works"
  36. if (!$res->execute(array(true)))
  37. print_r($res->errorInfo());
  38. else
  39. print_r($res->fetchAll(PDO::FETCH_ASSOC));
  40. # Expected to fail; unless told otherwise, PDO assumes string inputs
  41. # false -> "" as string, which pgsql doesn't like
  42. if (!$res->execute(array(false)))
  43. print_r(normalizeErrorInfo($res->errorInfo()));
  44. else
  45. print_r($res->fetchAll(PDO::FETCH_ASSOC));
  46. # And now using emulator prepares
  47. echo "EMUL\n";
  48. $res = $db->prepare('SELECT foo from test where bar = ?', array(
  49. PDO::ATTR_EMULATE_PREPARES => true));
  50. # this is the portable approach to binding a bool
  51. $res->bindValue(1, false, PDO::PARAM_BOOL);
  52. if (!$res->execute())
  53. print_r($res->errorInfo());
  54. else
  55. print_r($res->fetchAll(PDO::FETCH_ASSOC));
  56. # this is the portable approach to binding a bool
  57. $res->bindValue(1, true, PDO::PARAM_BOOL);
  58. if (!$res->execute())
  59. print_r($res->errorInfo());
  60. else
  61. print_r($res->fetchAll(PDO::FETCH_ASSOC));
  62. # true gets cast to string (because the implied default is string)
  63. # true-as-string is 1, so this "works"
  64. if (!$res->execute(array(true)))
  65. print_r($res->errorInfo());
  66. else
  67. print_r($res->fetchAll(PDO::FETCH_ASSOC));
  68. # Expected to fail; unless told otherwise, PDO assumes string inputs
  69. # false -> "" as string, which pgsql doesn't like
  70. if (!$res->execute(array(false))) {
  71. print_r(normalizeErrorInfo($res->errorInfo()));
  72. } else {
  73. print_r($res->fetchAll(PDO::FETCH_ASSOC));
  74. }
  75. function normalizeErrorInfo(array $err): array {
  76. // Strip additional lines outputted by recent PgSQL versions
  77. $err[2] = trim(current(explode("\n", $err[2])));
  78. return $err;
  79. }
  80. ?>
  81. --EXPECTF--
  82. Array
  83. (
  84. [0] => Array
  85. (
  86. [foo] => false
  87. )
  88. )
  89. Array
  90. (
  91. [0] => Array
  92. (
  93. [foo] => true
  94. )
  95. )
  96. Array
  97. (
  98. [0] => Array
  99. (
  100. [foo] => true
  101. )
  102. )
  103. Array
  104. (
  105. [0] => 22P02
  106. [1] => 7
  107. [2] => %s: %sboolean%s
  108. )
  109. EMUL
  110. Array
  111. (
  112. [0] => Array
  113. (
  114. [foo] => false
  115. )
  116. )
  117. Array
  118. (
  119. [0] => Array
  120. (
  121. [foo] => true
  122. )
  123. )
  124. Array
  125. (
  126. [0] => Array
  127. (
  128. [foo] => true
  129. )
  130. )
  131. Array
  132. (
  133. [0] => 22P02
  134. [1] => 7
  135. [2] => %s: %sboolean%s
  136. )