PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/hphp/test/zend/bad/ext-pdo_mysql/pdo_mysql_stmt_getcolumnmeta.php

https://bitbucket.org/gnanakeethan/hiphop-php
PHP | 295 lines | 227 code | 53 blank | 15 comment | 52 complexity | aa37126b43d400d3fed76f302e0a9cfd MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
  3. $db = MySQLPDOTest::factory();
  4. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
  5. MySQLPDOTest::createTestTable($db);
  6. try {
  7. $stmt = $db->prepare('SELECT id FROM test ORDER BY id ASC');
  8. // execute() has not been called yet
  9. // NOTE: no warning
  10. if (false !== ($tmp = $stmt->getColumnMeta(0)))
  11. printf("[002] Expecting false got %s\n", var_export($tmp, true));
  12. $stmt->execute();
  13. // Warning: PDOStatement::getColumnMeta() expects exactly 1 parameter, 0 given in
  14. if (false !== ($tmp = @$stmt->getColumnMeta()))
  15. printf("[003] Expecting false got %s\n", var_export($tmp, true));
  16. // invalid offset
  17. if (false !== ($tmp = @$stmt->getColumnMeta(-1)))
  18. printf("[004] Expecting false got %s\n", var_export($tmp, true));
  19. // Warning: PDOStatement::getColumnMeta() expects parameter 1 to be long, array given in
  20. if (false !== ($tmp = @$stmt->getColumnMeta(array())))
  21. printf("[005] Expecting false got %s\n", var_export($tmp, true));
  22. // Warning: PDOStatement::getColumnMeta() expects exactly 1 parameter, 2 given in
  23. if (false !== ($tmp = @$stmt->getColumnMeta(1, 1)))
  24. printf("[006] Expecting false got %s\n", var_export($tmp, true));
  25. $emulated = $stmt->getColumnMeta(0);
  26. printf("Testing native PS...\n");
  27. $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
  28. if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
  29. printf("[007] Unable to turn off emulated prepared statements\n");
  30. $stmt = $db->prepare('SELECT id FROM test ORDER BY id ASC');
  31. $stmt->execute();
  32. $native = $stmt->getColumnMeta(0);
  33. if (count($native) == 0) {
  34. printf("[008] Meta data seems wrong, %s / %s\n",
  35. var_export($native, true), var_export($emulated, true));
  36. }
  37. // invalid offset
  38. if (false !== ($tmp = $stmt->getColumnMeta(1)))
  39. printf("[009] Expecting false because of invalid offset got %s\n", var_export($tmp, true));
  40. function test_meta(&$db, $offset, $sql_type, $value, $native_type, $pdo_type) {
  41. $db->exec('DROP TABLE IF EXISTS test');
  42. $sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
  43. if (!($stmt = @$db->prepare($sql)) || (!@$stmt->execute())) {
  44. // Some engines and/or MySQL server versions might not support the data type
  45. return true;
  46. }
  47. if (!$db->exec(sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $value))) {
  48. printf("[%03d] + 1] Insert failed, %d - %s\n", $offset,
  49. $db->errorCode(), var_export($db->errorInfo(), true));
  50. return false;
  51. }
  52. $stmt = $db->prepare('SELECT id, label FROM test');
  53. $stmt->execute();
  54. $meta = $stmt->getColumnMeta(1);
  55. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  56. if (empty($meta)) {
  57. printf("[%03d + 2] getColumnMeta() failed, %d - %s\n", $offset,
  58. $stmt->errorCode(), var_export($stmt->errorInfo(), true));
  59. return false;
  60. }
  61. $elements = array('flags', 'table', 'name', 'len', 'precision', 'pdo_type');
  62. foreach ($elements as $k => $element)
  63. if (!isset($meta[$element])) {
  64. printf("[%03d + 3] Element %s missing, %s\n", $offset,
  65. $element, var_export($meta, true));
  66. return false;
  67. }
  68. if (($meta['table'] != 'test') || ($meta['name'] != 'label')) {
  69. printf("[%03d + 4] Table or field name is wrong, %s\n", $offset,
  70. var_export($meta, true));
  71. return false;
  72. }
  73. if (!is_null($native_type)) {
  74. if (!isset($meta['native_type'])) {
  75. printf("[%03d + 5] Element native_type missing, %s\n", $offset,
  76. var_export($meta, true));
  77. return false;
  78. }
  79. if (!is_array($native_type))
  80. $native_type = array($native_type);
  81. $found = false;
  82. foreach ($native_type as $k => $type) {
  83. if ($meta['native_type'] == $type) {
  84. $found = true;
  85. break;
  86. }
  87. }
  88. if (!$found) {
  89. printf("[%03d + 6] Expecting native type %s, %s\n", $offset,
  90. var_export($native_type, true), var_export($meta, true));
  91. return false;
  92. }
  93. }
  94. if (!is_null($pdo_type) && ($meta['pdo_type'] != $pdo_type)) {
  95. printf("[%03d + 6] Expecting PDO type %s got %s (%s)\n", $offset,
  96. $pdo_type, var_export($meta, true), var_export($meta['native_type']));
  97. return false;
  98. }
  99. return true;
  100. }
  101. $stmt = $db->prepare('SELECT @@sql_mode AS _mode');
  102. $stmt->execute();
  103. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  104. $real_as_float = (false === stristr($row['_mode'], "REAL_AS_FLOAT")) ? false : true;
  105. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
  106. $is_mysqlnd = MySQLPDOTest::isPDOMySQLnd();
  107. test_meta($db, 20, 'BIT(8)', 1, NULL, ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  108. test_meta($db, 30, 'TINYINT', -127, NULL, ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  109. test_meta($db, 40, 'TINYINT UNSIGNED', 255, NULL, ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  110. test_meta($db, 50, 'BOOLEAN', 1, NULL, ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  111. test_meta($db, 60, 'SMALLINT', -32768, 'SHORT', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  112. test_meta($db, 70, 'SMALLINT UNSIGNED', 65535, 'SHORT', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  113. test_meta($db, 80, 'MEDIUMINT', -8388608, 'INT24', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  114. test_meta($db, 90, 'MEDIUMINT UNSIGNED', 16777215, 'INT24', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  115. test_meta($db, 100, 'INT', -2147483648, 'LONG', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  116. test_meta($db, 110, 'INT UNSIGNED', 4294967295, 'LONG', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  117. test_meta($db, 120, 'BIGINT', -9223372036854775808, 'LONGLONG', ($is_mysqlnd) ? ((PHP_INT_SIZE == 4) ? PDO::PARAM_STR : PDO::PARAM_INT) : PDO::PARAM_STR);
  118. test_meta($db, 130, 'BIGINT UNSIGNED', 18446744073709551615, 'LONGLONG', ($is_mysqlnd) ? ((PHP_INT_SIZE == 4) ? PDO::PARAM_STR : PDO::PARAM_INT) : PDO::PARAM_STR);
  119. test_meta($db, 130, 'REAL', -1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  120. test_meta($db, 140, 'REAL UNSIGNED', 1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  121. test_meta($db, 150, 'REAL ZEROFILL', -1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  122. test_meta($db, 160, 'REAL UNSIGNED ZEROFILL', 1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  123. test_meta($db, 170, 'DOUBLE', -1.01, 'DOUBLE', PDO::PARAM_STR);
  124. test_meta($db, 180, 'DOUBLE UNSIGNED', 1.01, 'DOUBLE', PDO::PARAM_STR);
  125. test_meta($db, 190, 'DOUBLE ZEROFILL', -1.01, 'DOUBLE', PDO::PARAM_STR);
  126. test_meta($db, 200, 'DOUBLE UNSIGNED ZEROFILL', 1.01, 'DOUBLE', PDO::PARAM_STR);
  127. test_meta($db, 210, 'FLOAT', -1.01, 'FLOAT', PDO::PARAM_STR);
  128. test_meta($db, 220, 'FLOAT UNSIGNED', 1.01, 'FLOAT', PDO::PARAM_STR);
  129. test_meta($db, 230, 'FLOAT ZEROFILL', -1.01, 'FLOAT', PDO::PARAM_STR);
  130. test_meta($db, 240, 'FLOAT UNSIGNED ZEROFILL', 1.01, 'FLOAT', PDO::PARAM_STR);
  131. test_meta($db, 250, 'DECIMAL', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  132. test_meta($db, 260, 'DECIMAL UNSIGNED', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  133. test_meta($db, 270, 'DECIMAL ZEROFILL', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  134. test_meta($db, 280, 'DECIMAL UNSIGNED ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  135. test_meta($db, 290, 'NUMERIC', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  136. test_meta($db, 300, 'NUMERIC UNSIGNED', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  137. test_meta($db, 310, 'NUMERIC ZEROFILL', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  138. test_meta($db, 320, 'NUMERIC UNSIGNED ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  139. test_meta($db, 330, 'DATE', '2008-04-23', array('DATE', 'NEWDATE'), PDO::PARAM_STR);
  140. test_meta($db, 340, 'TIME', '14:37:00', 'TIME', PDO::PARAM_STR);
  141. test_meta($db, 350, 'TIMESTAMP', time(), 'TIMESTAMP', PDO::PARAM_STR);
  142. test_meta($db, 360, 'DATETIME', '2008-03-23 14:38:00', 'DATETIME', PDO::PARAM_STR);
  143. test_meta($db, 370, 'YEAR', '2008', NULL, ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  144. test_meta($db, 380, 'CHAR(1)', 'a', 'STRING', PDO::PARAM_STR);
  145. test_meta($db, 390, 'CHAR(10)', '0123456789', 'STRING', PDO::PARAM_STR);
  146. test_meta($db, 400, 'CHAR(255)', str_repeat('z', 255), 'STRING', PDO::PARAM_STR);
  147. test_meta($db, 410, 'VARCHAR(1)', 'a', 'VAR_STRING', PDO::PARAM_STR);
  148. test_meta($db, 420, 'VARCHAR(10)', '0123456789', 'VAR_STRING', PDO::PARAM_STR);
  149. test_meta($db, 430, 'VARCHAR(255)', str_repeat('z', 255), 'VAR_STRING', PDO::PARAM_STR);
  150. test_meta($db, 440, 'BINARY(1)', str_repeat('a', 1), 'STRING', PDO::PARAM_STR);
  151. test_meta($db, 450, 'BINARY(255)', str_repeat('b', 255), 'STRING', PDO::PARAM_STR);
  152. test_meta($db, 460, 'VARBINARY(1)', str_repeat('a', 1), 'VAR_STRING', PDO::PARAM_STR);
  153. test_meta($db, 470, 'VARBINARY(255)', str_repeat('b', 255), 'VAR_STRING', PDO::PARAM_STR);
  154. test_meta($db, 480, 'TINYBLOB', str_repeat('b', 255), 'BLOB', PDO::PARAM_STR);
  155. test_meta($db, 490, 'BLOB', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  156. test_meta($db, 500, 'MEDIUMBLOB', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  157. test_meta($db, 510, 'LONGBLOB', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  158. test_meta($db, 520, 'TINYTEXT', str_repeat('b', 255), 'BLOB', PDO::PARAM_STR);
  159. test_meta($db, 530, 'TINYTEXT BINARY', str_repeat('b', 255), 'BLOB', PDO::PARAM_STR);
  160. test_meta($db, 560, 'TEXT', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  161. test_meta($db, 570, 'TEXT BINARY', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  162. test_meta($db, 580, 'MEDIUMTEXT', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  163. test_meta($db, 590, 'MEDIUMTEXT BINARY', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  164. test_meta($db, 600, 'LONGTEXT', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  165. test_meta($db, 610, 'LONGTEXT BINARY', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
  166. test_meta($db, 620, "ENUM('yes', 'no') DEFAULT 'yes'", 'no', NULL, PDO::PARAM_STR);
  167. test_meta($db, 630, "SET('yes', 'no') DEFAULT 'yes'", 'no', NULL, PDO::PARAM_STR);
  168. /*
  169. | spatial_type
  170. */
  171. // unique key
  172. $db->exec('DROP TABLE IF EXISTS test');
  173. $sql = sprintf('CREATE TABLE test(id INT, label INT UNIQUE) ENGINE = %s', MySQLPDOTest::getTableEngine());
  174. if (($stmt = @$db->prepare($sql)) && @$stmt->execute()) {
  175. $db->exec('INSERT INTO test(id, label) VALUES (1, 2)');
  176. $stmt = $db->query('SELECT id, label FROM test');
  177. $meta = $stmt->getColumnMeta(1);
  178. if (!isset($meta['flags'])) {
  179. printf("[1000] No flags contained in metadata %s\n", var_export($meta, true));
  180. } else {
  181. $flags = $meta['flags'];
  182. $found = false;
  183. foreach ($flags as $k => $flag) {
  184. if ($flag == 'unique_key')
  185. $found = true;
  186. }
  187. if (!$found)
  188. printf("[1001] Flags seem wrong %s\n", var_export($meta, true));
  189. }
  190. }
  191. // primary key
  192. $db->exec('DROP TABLE IF EXISTS test');
  193. $sql = sprintf('CREATE TABLE test(id INT PRIMARY KEY NOT NULL AUTO_INCREMENT) ENGINE = %s', MySQLPDOTest::getTableEngine());
  194. if (($stmt = @$db->prepare($sql)) && @$stmt->execute()) {
  195. $db->exec('INSERT INTO test(id) VALUES (1)');
  196. $stmt = $db->query('SELECT id FROM test');
  197. $meta = $stmt->getColumnMeta(0);
  198. if (!isset($meta['flags'])) {
  199. printf("[1002] No flags contained in metadata %s\n", var_export($meta, true));
  200. } else {
  201. $flags = $meta['flags'];
  202. $found = false;
  203. foreach ($flags as $k => $flag) {
  204. if ($flag == 'primary_key')
  205. $found = true;
  206. }
  207. if (!$found)
  208. printf("[1003] Flags seem wrong %s\n", var_export($meta, true));
  209. }
  210. }
  211. // multiple key
  212. $db->exec('DROP TABLE IF EXISTS test');
  213. $sql = sprintf('CREATE TABLE test(id INT, label1 INT, label2 INT, INDEX idx1(label1, label2)) ENGINE = %s', MySQLPDOTest::getTableEngine());
  214. if (($stmt = @$db->prepare($sql)) && @$stmt->execute()) {
  215. $db->exec('INSERT INTO test(id, label1, label2) VALUES (1, 2, 3)');
  216. $stmt = $db->query('SELECT id, label1, label2 FROM test');
  217. $meta = $stmt->getColumnMeta(1);
  218. if (!isset($meta['flags'])) {
  219. printf("[1004] No flags contained in metadata %s\n", var_export($meta, true));
  220. } else {
  221. $flags = $meta['flags'];
  222. $found = false;
  223. foreach ($flags as $k => $flag) {
  224. if ($flag == 'multiple_key')
  225. $found = true;
  226. }
  227. if (!$found)
  228. printf("[1005] Flags seem wrong %s\n", var_export($meta, true));
  229. }
  230. }
  231. $stmt = $db->query('SELECT NULL AS col1');
  232. $meta = $stmt->getColumnMeta(0);
  233. if ('NULL' !== $meta['native_type'])
  234. printf("[1006] Expecting NULL got %s\n", $meta['native_type']);
  235. } catch (PDOException $e) {
  236. // we should never get here, we use warnings, but never trust a system...
  237. printf("[001] %s, [%s} %s\n",
  238. $e->getMessage(), $db->errorInfo(), implode(' ', $db->errorInfo()));
  239. }
  240. $db->exec('DROP TABLE IF EXISTS test');
  241. print "done!";
  242. ?>