PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/pdo_mysql/tests/mysql_pdo_test.inc

https://github.com/php/php-src
PHP | 177 lines | 129 code | 36 blank | 12 comment | 38 complexity | 0792c7e7b7d8cd0fba3e29179124402e MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. <?php
  2. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.inc');
  3. require_once(dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc');
  4. class MySQLPDOTest extends PDOTest {
  5. static function factory($classname = 'PDO', $drop_test_tables = false, $myattr = null, $mydsn = null) {
  6. $dsn = self::getDSN($mydsn);
  7. $user = PDO_MYSQL_TEST_USER;
  8. $pass = PDO_MYSQL_TEST_PASS;
  9. $attr = getenv('PDOTEST_ATTR');
  10. if (is_string($attr) && strlen($attr)) {
  11. $attr = unserialize($attr);
  12. } else {
  13. $attr = null;
  14. }
  15. if ($user === false)
  16. $user = NULL;
  17. if ($pass === false)
  18. $pass = NULL;
  19. $db = new $classname($dsn, $user, $pass, $attr);
  20. if (!$db) {
  21. die("Could not create PDO object (DSN=$dsn, user=$user)\n");
  22. }
  23. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  24. $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
  25. return $db;
  26. }
  27. static function createTestTable($db, $engine = null) {
  28. if (!$engine)
  29. $engine = PDO_MYSQL_TEST_ENGINE;
  30. $db->exec('DROP TABLE IF EXISTS test');
  31. $db->exec('CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE=' . $engine);
  32. $db->exec("INSERT INTO test(id, label) VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')");
  33. }
  34. static function getTableEngine() {
  35. return PDO_MYSQL_TEST_ENGINE;
  36. }
  37. static function getDSN($new_options = null, $addition = '') {
  38. if (!$new_options)
  39. return PDO_MYSQL_TEST_DSN . $addition;
  40. $old_options = array();
  41. $dsn = substr(PDO_MYSQL_TEST_DSN,
  42. strpos(PDO_MYSQL_TEST_DSN, ':') + 1,
  43. strlen(PDO_MYSQL_TEST_DSN));
  44. // no real parser - any excotic setting can fool us
  45. $parts = explode(';', $dsn);
  46. foreach ($parts as $k => $v) {
  47. $tmp = explode('=', $v);
  48. if (count($tmp) == 2)
  49. $old_options[$tmp[0]] = $tmp[1];
  50. }
  51. $options = $old_options;
  52. foreach ($new_options as $k => $v)
  53. $options[$k] = $v;
  54. $dsn = 'mysql:';
  55. foreach ($options as $k => $v)
  56. $dsn .= sprintf('%s=%s;', $k, $v);
  57. if ($addition)
  58. $dsn .= $addition;
  59. else
  60. $dsn = substr($dsn, 0, strlen($dsn) -1);
  61. return $dsn;
  62. }
  63. static function getClientVersion($db) {
  64. return self::extractVersion($db->getAttribute(PDO::ATTR_CLIENT_VERSION));
  65. }
  66. static function getServerVersion($db) {
  67. return self::extractVersion($db->getAttribute(PDO::ATTR_SERVER_VERSION));
  68. }
  69. static function extractVersion($version_string) {
  70. /*
  71. TODO:
  72. We're a bit in trouble: PDO_MYSQL returns version strings.
  73. That's wrong according to the manual. According to the manual
  74. integers should be returned. However, this code needs to work
  75. with stinky PDO_MYSQL and hopefully better PDO_MYSQLND.
  76. */
  77. // already an int value?
  78. if (is_int($version_string))
  79. return $version_string;
  80. // string but int value?
  81. $tmp = (int)$version_string;
  82. if (((string)$tmp) === $version_string)
  83. return $tmp;
  84. // stinky string which we need to parse
  85. $parts = explode('.', $version_string);
  86. if (count($parts) != 3)
  87. return -1;
  88. $version = (int)$parts[0] * 10000;
  89. $version+= (int)$parts[1] * 100;
  90. $version+= (int)$parts[2];
  91. return $version;
  92. }
  93. static function getTempDir() {
  94. if (!function_exists('sys_get_temp_dir')) {
  95. if (!empty($_ENV['TMP']))
  96. return realpath( $_ENV['TMP'] );
  97. if (!empty($_ENV['TMPDIR']))
  98. return realpath( $_ENV['TMPDIR'] );
  99. if (!empty($_ENV['TEMP']))
  100. return realpath( $_ENV['TEMP'] );
  101. $temp_file = tempnam(md5(uniqid(rand(), TRUE)), '');
  102. if ($temp_file) {
  103. $temp_dir = realpath(dirname($temp_file));
  104. unlink($temp_file);
  105. return $temp_dir;
  106. }
  107. return FALSE;
  108. } else {
  109. return sys_get_temp_dir();
  110. }
  111. }
  112. static function detect_transactional_mysql_engine($db) {
  113. foreach ($db->query("show variables like 'have%'") as $row) {
  114. if (!empty($row) && $row[1] == 'YES' && ($row[0] == 'have_innodb' || $row[0] == 'have_bdb')) {
  115. return str_replace("have_", "", $row[0]);
  116. }
  117. }
  118. /* MySQL 5.6.1+ */
  119. foreach ($db->query("SHOW ENGINES") as $row) {
  120. if (isset($row['engine']) && isset($row['support'])) {
  121. if ('InnoDB' == $row['engine'] && ('YES' == $row['support'] || 'DEFAULT' == $row['support']))
  122. return 'innodb';
  123. }
  124. }
  125. return false;
  126. }
  127. static function isPDOMySQLnd() {
  128. ob_start();
  129. phpinfo();
  130. $tmp = ob_get_contents();
  131. ob_end_clean();
  132. $tmp = stristr($tmp, "PDO Driver for MySQL => enabled");
  133. return (bool)preg_match('/Client API version.*mysqlnd/', $tmp);
  134. }
  135. static function dropTestTable($db = NULL) {
  136. if (is_null($db))
  137. $db = self::factory();
  138. $db->exec('DROP TABLE IF EXISTS test');
  139. }
  140. }
  141. ?>