PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/pdo_mysql/tests/pdo_mysql_types_zerofill.php

http://github.com/facebook/hiphop-php
PHP | 116 lines | 90 code | 21 blank | 5 comment | 14 complexity | 22e4877f900bbbc4596d24156bb06767 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
  3. function test_type(&$db, $offset, $sql_type, $value, $ret_value = NULL, $pattern = NULL) {
  4. $db->exec('DROP TABLE IF EXISTS test');
  5. $sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
  6. @$db->exec($sql);
  7. if ($db->errorCode() != 0) {
  8. // not all MySQL Server versions and/or engines might support the type
  9. return true;
  10. }
  11. $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)');
  12. $stmt->bindValue(1, $offset);
  13. $stmt->bindValue(2, $value);
  14. try {
  15. if (!$stmt->execute()) {
  16. printf("[%03d + 1] INSERT failed, %s\n", $offset, var_export($stmt->errorInfo(), true));
  17. return false;
  18. }
  19. } catch (PDOException $e) {
  20. // This might be a SQL warning on signed values inserted in unsigned columns
  21. // Zerofill implies unsigned but the test plays with signed = negative values as well!
  22. return true;
  23. }
  24. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
  25. $stmt = $db->query('SELECT id, label FROM test');
  26. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  27. $stmt->closeCursor();
  28. if (!isset($row['id']) || !isset($row['label'])) {
  29. printf("[%03d + 2] Fetched result seems wrong, dumping result: %s\n", $offset, var_export($row, true));
  30. return false;
  31. }
  32. if ($row['id'] != $offset) {
  33. printf("[%03d + 3] Expecting %s got %s\n", $offset, $row['id']);
  34. return false;
  35. }
  36. if (!is_null($pattern)) {
  37. if (!preg_match($pattern, $row['label'])) {
  38. printf("[%03d + 5] Value seems wrong, accepting pattern %s got %s, check manually\n",
  39. $offset, $pattern, var_export($row['label'], true));
  40. return false;
  41. }
  42. } else {
  43. $exp = $value;
  44. if (!is_null($ret_value)) {
  45. // we expect a different return value than our input value
  46. // typically the difference is only the type
  47. $exp = $ret_value;
  48. }
  49. if ($row['label'] !== $exp) {
  50. printf("[%03d + 4] %s - input = %s/%s, output = %s/%s\n", $offset,
  51. $sql_type, var_export($exp, true), gettype($exp),
  52. var_export($row['label'], true), gettype($row['label']));
  53. return false;
  54. }
  55. }
  56. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
  57. $stmt = $db->query('SELECT id, label FROM test');
  58. $row_string = $stmt->fetch(PDO::FETCH_ASSOC);
  59. $stmt->closeCursor();
  60. if ($row['label'] != $row_string['label']) {
  61. printf("%s - STRINGIGY = %s, NATIVE = %s\n", $sql_type, var_export($row_string['label'], true), var_export($row['label'], true));
  62. return false;
  63. }
  64. return true;
  65. }
  66. $db = MySQLPDOTest::factory();
  67. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  68. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  69. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
  70. $stmt = $db->prepare('SELECT @@sql_mode AS _mode');
  71. $stmt->execute();
  72. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  73. $real_as_float = (false === stristr($row['_mode'], "REAL_AS_FLOAT")) ? false : true;
  74. test_type($db, 100, 'REAL ZEROFILL', -1.01, NULL, '/^[0]*0$/');
  75. test_type($db, 110, 'REAL ZEROFILL', 1.01, NULL, ($real_as_float) ? '/^[0]*1\.0.*$/' : '/^[0]*1\.01$/');
  76. test_type($db, 120, 'REAL UNSIGNED ZEROFILL', 1.01, NULL, ($real_as_float) ? '/^[0]*1\..*$/' : '/^[0]*1\.01$/');
  77. test_type($db, 130, 'DOUBLE ZEROFILL', -1.01, NULL, '/^[0]*0$/');
  78. test_type($db, 140, 'DOUBLE ZEROFILL', 1.01, NULL, '/^[0]*1\.01$/');
  79. test_type($db, 150, 'DOUBLE UNSIGNED ZEROFILL', 1.01, NULL, '/^[0]*1\.01$/');
  80. test_type($db, 160, 'FLOAT ZEROFILL', -1.01, NULL, '/^[0]*0$/');
  81. test_type($db, 170, 'FLOAT ZEROFILL', 1, NULL, '/^[0]*1$/');
  82. test_type($db, 180, 'FLOAT UNSIGNED ZEROFILL', -1, NULL, '/^[0]*0$/');
  83. test_type($db, 190, 'DECIMAL ZEROFILL', -1.01, NULL, '/^[0]*0$/');
  84. test_type($db, 200, 'DECIMAL ZEROFILL', 1.01, NULL, '/^[0]*1$/');
  85. test_type($db, 210, 'DECIMAL UNSIGNED ZEROFILL', 1.01, NULL, '/^[0]*1$/');
  86. test_type($db, 220, 'NUMERIC ZEROFILL', -1, NULL, '/^[0]*0$/');
  87. test_type($db, 230, 'NUMERIC ZEROFILL', 1, NULL, '/^[0]*1$/');
  88. test_type($db, 240, 'NUMERIC UNSIGNED ZEROFILL', 1.01, NULL, '/^[0]*1$/');
  89. echo "done!\n";
  90. ?>
  91. <?php
  92. require dirname(__FILE__) . '/mysql_pdo_test.inc';
  93. $db = MySQLPDOTest::factory();
  94. $db->exec('DROP TABLE IF EXISTS test');
  95. ?>