PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/facebook/hiphop-php
PHP | 172 lines | 126 code | 38 blank | 8 comment | 21 complexity | 3bc934f342d113985d3d5df470944d7c 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, $alternative_type = 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. if (!$stmt->execute()) {
  15. printf("[%03d + 1] INSERT failed, %s\n", $offset, var_export($stmt->errorInfo(), true));
  16. return false;
  17. }
  18. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
  19. $stmt = $db->query('SELECT id, label FROM test');
  20. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  21. $stmt->closeCursor();
  22. if (!isset($row['id']) || !isset($row['label'])) {
  23. printf("[%03d + 2] Fetched result seems wrong, dumping result: %s\n", $offset, var_export($row, true));
  24. return false;
  25. }
  26. if ($row['id'] != $offset) {
  27. printf("[%03d + 3] Expecting %s got %s\n", $offset, $row['id']);
  28. return false;
  29. }
  30. if (!is_null($pattern)) {
  31. if (!preg_match($pattern, $row['label'])) {
  32. printf("[%03d + 5] Value seems wrong, accepting pattern %s got %s, check manually\n",
  33. $offset, $pattern, var_export($row['label'], true));
  34. return false;
  35. }
  36. } else {
  37. $exp = $value;
  38. if (!is_null($ret_value)) {
  39. // we expect a different return value than our input value
  40. // typically the difference is only the type
  41. $exp = $ret_value;
  42. }
  43. if ($row['label'] !== $exp && !is_null($alternative_type) && gettype($row['label']) != $alternative_type) {
  44. printf("[%03d + 4] %s - input = %s/%s, output = %s/%s (alternative type: %s)\n", $offset,
  45. $sql_type, var_export($exp, true), gettype($exp),
  46. var_export($row['label'], true), gettype($row['label']),
  47. $alternative_type);
  48. return false;
  49. }
  50. }
  51. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
  52. $stmt = $db->query('SELECT id, label FROM test');
  53. $row_string = $stmt->fetch(PDO::FETCH_ASSOC);
  54. $stmt->closeCursor();
  55. if (is_null($pattern) && ($row['label'] != $row_string['label'])) {
  56. printf("%s - STRINGIGY = %s, NATIVE = %s\n", $sql_type, var_export($row_string['label'], true), var_export($row['label'], true));
  57. return false;
  58. } else if (!is_null($pattern) && !preg_match($pattern, $row_string['label'])) {
  59. printf("%s - STRINGIGY = %s, NATIVE = %s, pattern '%s'\n", $sql_type, var_export($row_string['label'], true), var_export($row['label'], true), $pattern);
  60. return false;
  61. }
  62. return true;
  63. }
  64. $db = MySQLPDOTest::factory();
  65. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  66. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  67. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
  68. /*
  69. test_type($db, 20, 'BIT(8)', 1);
  70. */
  71. $is_mysqlnd = MySQLPDOTest::isPDOMySQLnd();
  72. test_type($db, 30, 'TINYINT', -127, ($is_mysqlnd) ? -127: '-127');
  73. test_type($db, 40, 'TINYINT UNSIGNED', 255, ($is_mysqlnd) ? 255 : '255');
  74. test_type($db, 50, 'BOOLEAN', 1, ($is_mysqlnd) ? 1 : '1');
  75. test_type($db, 60, 'SMALLINT', -32768, ($is_mysqlnd) ? -32768 : '-32768');
  76. test_type($db, 70, 'SMALLINT UNSIGNED', 65535, ($is_mysqlnd) ? 65535 : '65535');
  77. test_type($db, 80, 'MEDIUMINT', -8388608, ($is_mysqlnd) ? -8388608 : '-8388608');
  78. test_type($db, 90, 'MEDIUMINT UNSIGNED', 16777215, ($is_mysqlnd) ? 16777215 : '16777215');
  79. test_type($db, 100, 'INT', -2147483648,
  80. ($is_mysqlnd) ? ((PHP_INT_SIZE > 4) ? (int)-2147483648 : (double)-2147483648) : '-2147483648',
  81. NULL, ($is_mysqlnd) ? 'integer' : NULL);
  82. test_type($db, 110, 'INT UNSIGNED', 4294967295, ($is_mysqlnd) ? ((PHP_INT_SIZE > 4) ? 4294967295 : '4294967295') : '4294967295');
  83. // no chance to return int with the current PDO version - we are forced to return strings
  84. test_type($db, 120, 'BIGINT', 1, ($is_mysqlnd) ? 1 : '1');
  85. // to avoid trouble with numeric ranges, lets pass the numbers as a string
  86. test_type($db, 130, 'BIGINT', '-9223372036854775808', NULL, '/^\-9[\.]*22/');
  87. test_type($db, 140, 'BIGINT UNSIGNED', '18446744073709551615', NULL, '/^1[\.]*844/');
  88. test_type($db, 150, 'REAL', -1.01, ($is_mysqlnd) ? -1.01 : '-1.01');
  89. test_type($db, 160, 'REAL UNSIGNED', 1.01, ($is_mysqlnd) ? 1.01 : '1.01');
  90. test_type($db, 170, 'DOUBLE', -1.01, ($is_mysqlnd) ? -1.01 : '-1.01');
  91. test_type($db, 180, 'DOUBLE UNSIGNED', 1.01, ($is_mysqlnd) ? 1.01 : '1.01');
  92. test_type($db, 210, 'FLOAT', -1.01, NULL, '/^\-1.0\d+/');
  93. test_type($db, 220, 'FLOAT UNSIGNED', 1.01, NULL, '/^1.0\d+/');
  94. test_type($db, 250, 'DECIMAL', -1.01, '-1');
  95. test_type($db, 260, 'DECIMAL UNSIGNED', 1.01, '1');
  96. test_type($db, 290, 'NUMERIC', -1.01, '-1');
  97. test_type($db, 300, 'NUMERIC UNSIGNED', 1.01, '1');
  98. test_type($db, 330, 'DATE', '2008-04-23');
  99. test_type($db, 340, 'TIME', '14:37:00');
  100. test_type($db, 350, 'TIMESTAMP', '2008-05-06 21:09:00');
  101. test_type($db, 360, 'DATETIME', '2008-03-23 14:38:00');
  102. test_type($db, 370, 'YEAR', 2008, ($is_mysqlnd) ? 2008 : '2008');
  103. test_type($db, 380, 'CHAR(1)', 'a');
  104. test_type($db, 390, 'CHAR(10)', '0123456789');
  105. test_type($db, 400, 'CHAR(255)', str_repeat('z', 255));
  106. test_type($db, 410, 'VARCHAR(1)', 'a');
  107. test_type($db, 420, 'VARCHAR(10)', '0123456789');
  108. test_type($db, 430, 'VARCHAR(255)', str_repeat('z', 255));
  109. test_type($db, 440, 'BINARY(1)', str_repeat('a', 1));
  110. test_type($db, 450, 'BINARY(255)', str_repeat('b', 255));
  111. test_type($db, 460, 'VARBINARY(1)', str_repeat('a', 1));
  112. test_type($db, 470, 'VARBINARY(255)', str_repeat('b', 255));
  113. test_type($db, 480, 'TINYBLOB', str_repeat('b', 255));
  114. test_type($db, 490, 'BLOB', str_repeat('b', 256));
  115. test_type($db, 500, 'MEDIUMBLOB', str_repeat('b', 256));
  116. test_type($db, 510, 'LONGBLOB', str_repeat('b', 256));
  117. test_type($db, 520, 'TINYTEXT', str_repeat('b', 255));
  118. test_type($db, 530, 'TINYTEXT BINARY', str_repeat('b', 255));
  119. test_type($db, 560, 'TEXT', str_repeat('b', 256));
  120. test_type($db, 570, 'TEXT BINARY', str_repeat('b', 256));
  121. test_type($db, 580, 'MEDIUMTEXT', str_repeat('b', 256));
  122. test_type($db, 590, 'MEDIUMTEXT BINARY', str_repeat('b', 256));
  123. test_type($db, 600, 'LONGTEXT', str_repeat('b', 256));
  124. test_type($db, 610, 'LONGTEXT BINARY', str_repeat('b', 256));
  125. test_type($db, 620, "ENUM('yes', 'no') DEFAULT 'yes'", 'no');
  126. test_type($db, 630, "SET('yes', 'no') DEFAULT 'yes'", 'no');
  127. test_type($db, 640, 'DECIMAL(3,2)', -1.01, '-1.01');
  128. echo "done!\n";
  129. ?>
  130. <?php
  131. require dirname(__FILE__) . '/mysql_pdo_test.inc';
  132. $db = MySQLPDOTest::factory();
  133. $db->exec('DROP TABLE IF EXISTS test');
  134. ?>