connect.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. Default values are "localhost", "root",
  4. database "test" and empty password.
  5. Change the MYSQL_TEST environment values
  6. if you want to use another configuration
  7. */
  8. $driver = new mysqli_driver;
  9. $driver->report_mode = MYSQLI_REPORT_OFF;
  10. $host = getenv("MYSQL_TEST_HOST") ?: "127.0.0.1";
  11. $port = getenv("MYSQL_TEST_PORT") ?: 3306;
  12. $user = getenv("MYSQL_TEST_USER") ?: "root";
  13. $passwd = getenv("MYSQL_TEST_PASSWD") ?: "";
  14. $db = getenv("MYSQL_TEST_DB") ?: "test";
  15. $engine = getenv("MYSQL_TEST_ENGINE") ?: "InnoDB";
  16. $socket = getenv("MYSQL_TEST_SOCKET") ?: null;
  17. $skip_on_connect_failure = getenv("MYSQL_TEST_SKIP_CONNECT_FAILURE") ?: true;
  18. $connect_flags = (int)getenv("MYSQL_TEST_CONNECT_FLAGS") ?: 0;
  19. if ($socket) {
  20. ini_set('mysqli.default_socket', $socket);
  21. }
  22. /* Development setting: test experimental features and/or feature requests that never worked before? */
  23. $TEST_EXPERIMENTAL = (in_array(getenv("MYSQL_TEST_EXPERIMENTAL"), array(0, 1))) ?
  24. ((1 == getenv("MYSQL_TEST_EXPERIMENTAL")) ? true : false) :
  25. false;
  26. $IS_MYSQLND = stristr(mysqli_get_client_info(), "mysqlnd");
  27. if (!function_exists('sys_get_temp_dir')) {
  28. function sys_get_temp_dir() {
  29. if (!empty($_ENV['TMP']))
  30. return realpath( $_ENV['TMP'] );
  31. if (!empty($_ENV['TMPDIR']))
  32. return realpath( $_ENV['TMPDIR'] );
  33. if (!empty($_ENV['TEMP']))
  34. return realpath( $_ENV['TEMP'] );
  35. $temp_file = tempnam(md5(uniqid(rand(), TRUE)), '');
  36. if ($temp_file) {
  37. $temp_dir = realpath(dirname($temp_file));
  38. unlink($temp_file);
  39. return $temp_dir;
  40. }
  41. return FALSE;
  42. }
  43. }
  44. if (!function_exists('my_mysqli_connect')) {
  45. /**
  46. * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
  47. *
  48. * @param enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)?
  49. */
  50. function my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
  51. global $connect_flags;
  52. $flags = $enable_env_flags? $connect_flags:0;
  53. if ($flags !== 0) {
  54. $link = mysqli_init();
  55. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
  56. $link = false;
  57. } else {
  58. $link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
  59. }
  60. return $link;
  61. }
  62. /**
  63. * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
  64. *
  65. * @param enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
  66. */
  67. function my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags = 0, $enable_env_flags = true) {
  68. global $connect_flags;
  69. if ($enable_env_flags)
  70. $flags = $flags | $connect_flags;
  71. return mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags);
  72. }
  73. class my_mysqli extends mysqli {
  74. public function __construct($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
  75. global $connect_flags;
  76. $flags = ($enable_env_flags) ? $connect_flags : 0;
  77. if ($flags !== false) {
  78. parent::__construct();
  79. $this->real_connect($host, $user, $passwd, $db, $port, $socket, $flags);
  80. } else {
  81. parent::__construct($host, $user, $passwd, $db, $port, $socket);
  82. }
  83. }
  84. }
  85. function have_innodb($link) {
  86. if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) &&
  87. ($row = $res->fetch_row()) &&
  88. !empty($row)) {
  89. if ($row[1] == "DISABLED" || $row[1] == "NO") {
  90. return false;
  91. }
  92. return true;
  93. } else {
  94. /* MySQL 5.6.1+ */
  95. if ($res = $link->query("SHOW ENGINES")) {
  96. while ($row = $res->fetch_assoc()) {
  97. if (!isset($row['Engine']) || !isset($row['Support']))
  98. return false;
  99. if (('InnoDB' == $row['Engine']) &&
  100. (('YES' == $row['Support']) || ('DEFAULT' == $row['Support']))
  101. ) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. } else {
  107. return false;
  108. }
  109. }
  110. return false;
  111. }
  112. } else {
  113. printf("skip Eeeek/BUG/FIXME - connect.inc included twice! skipif bug?\n");
  114. }
  115. function handle_catchable_fatal($errno, $error, $file, $line) {
  116. static $errcodes = array();
  117. if (empty($errcodes)) {
  118. $constants = get_defined_constants();
  119. foreach ($constants as $name => $value) {
  120. if (substr($name, 0, 2) == "E_")
  121. $errcodes[$value] = $name;
  122. }
  123. }
  124. printf("[%s] %s in %s on line %s\n",
  125. (isset($errcodes[$errno])) ? $errcodes[$errno] : $errno,
  126. $error, $file, $line);
  127. return true;
  128. }
  129. ?>