local_infile_tools.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /* Utility function for mysqli_set_local_infile*.phpt tests */
  3. function shutdown_clean($file) {
  4. if ($file) {
  5. unlink($file);
  6. }
  7. }
  8. function check_local_infile_support($link, $engine, $table_name = 'test') {
  9. if (!$res = mysqli_query($link, 'SHOW VARIABLES LIKE "local_infile"'))
  10. return "Cannot check if Server variable 'local_infile' is set to 'ON'";
  11. $row = mysqli_fetch_assoc($res);
  12. mysqli_free_result($res);
  13. if ('ON' != $row['Value'])
  14. return sprintf("Server variable 'local_infile' seems not set to 'ON', found '%s'", $row['Value']);
  15. if (!mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name))) {
  16. return "Failed to drop old test table";
  17. }
  18. if (!mysqli_query($link, $sql = sprintf('CREATE TABLE %s(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE=%s',
  19. $table_name, $engine)))
  20. return "Failed to create test table: $sql";
  21. $file = create_standard_csv(1, false);
  22. if (!$file) {
  23. mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
  24. return "Cannot create CSV file";
  25. }
  26. if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
  27. INTO TABLE %s
  28. FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
  29. LINES TERMINATED BY '\n'",
  30. mysqli_real_escape_string($link, $file),
  31. $table_name))) {
  32. if (1148 == mysqli_errno($link)) {
  33. mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
  34. return "Cannot test LOAD DATA LOCAL INFILE, [1148] The used command is not allowed with this MySQL version";
  35. } else if ($link->errno) {
  36. return $link->error;
  37. }
  38. }
  39. mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
  40. return "";
  41. }
  42. function create_standard_csv($offset, $verbose = true) {
  43. // create a CVS file
  44. $file = tempnam(sys_get_temp_dir(), 'mysqli_test');
  45. if (!$fp = fopen($file, 'w')) {
  46. if ($verbose)
  47. printf("[%03d + 1] Cannot create CVS file '%s'\n", $offset, $file);
  48. return NULL;
  49. } else {
  50. /* Looks ugly? No, handy if you have crashes... */
  51. register_shutdown_function("shutdown_clean", $file);
  52. }
  53. if ((version_compare(PHP_VERSION, '5.9.9', '>') == 1)) {
  54. if (!fwrite($fp, (binary)"'97';'x';\n") ||
  55. !fwrite($fp, (binary)"'98';'y';\n") ||
  56. !fwrite($fp, (binary)"99;'z';\n")) {
  57. if ($verbose)
  58. printf("[%03d + 2] Cannot write CVS file '%s'\n", $offset, $file);
  59. return NULL;
  60. }
  61. } else {
  62. if (!fwrite($fp, "97;'x';\n") ||
  63. !fwrite($fp, "98;'y';\n") ||
  64. !fwrite($fp, "99;'z';\n")) {
  65. if ($verbose)
  66. printf("[%03d + 3] Cannot write CVS file '%s'\n", $offset, $file);
  67. return NULL;
  68. }
  69. }
  70. fclose($fp);
  71. if (!chmod($file, 0644)) {
  72. if ($verbose)
  73. printf("[%03d + 4] Cannot change the file perms of '%s' from 0600 to 0644, MySQL might not be able to read it\n",
  74. $offset, $file);
  75. return NULL;
  76. }
  77. return $file;
  78. }
  79. function try_handler($offset, $link, $file, $handler, $expected = null) {
  80. if ('default' == $handler) {
  81. mysqli_set_local_infile_default($link);
  82. } else if (!mysqli_set_local_infile_handler($link, $handler)) {
  83. printf("[%03d] Cannot set infile handler to '%s'\n", $offset, $handler);
  84. return false;
  85. }
  86. printf("Callback set to '%s'\n", $handler);
  87. if (!mysqli_query($link, sprintf("DELETE FROM test"))) {
  88. printf("[%03d] Cannot remove records, [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
  89. return false;
  90. }
  91. if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
  92. INTO TABLE test
  93. FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
  94. LINES TERMINATED BY '\n'",
  95. mysqli_real_escape_string($link, $file)))) {
  96. printf("[%03d] LOAD DATA failed, [%d] %s\n",
  97. $offset + 2,
  98. mysqli_errno($link), mysqli_error($link));
  99. }
  100. if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id")) {
  101. printf("[%03d] [%d] %s\n", $offset + 3, mysqli_errno($link), mysqli_error($link));
  102. return false;
  103. }
  104. if (!is_array($expected))
  105. return true;
  106. foreach ($expected as $k => $values) {
  107. if (!$tmp = mysqli_fetch_assoc($res)) {
  108. printf("[%03d/%d] [%d] '%s'\n", $offset + 4, $k, mysqli_errno($link), mysqli_error($link));
  109. return false;
  110. }
  111. if ($values['id'] != $tmp['id']) {
  112. printf("[%03d/%d] Expecting %s got %s\n",
  113. $offset + 5, $k,
  114. $values['id'], $tmp['id']);
  115. return false;
  116. }
  117. if ($values['label'] != $tmp['label']) {
  118. printf("[%03d/%d] Expecting %s got %s\n",
  119. $offset + 6, $k,
  120. $values['label'], $tmp['label']);
  121. return false;
  122. }
  123. }
  124. if ($res && $tmp = mysqli_fetch_assoc($res)) {
  125. printf("[%03d] More results than expected!\n", $offset + 7);
  126. do {
  127. var_dump($tmp);
  128. } while ($tmp = mysqli_fetch_assoc($res));
  129. return false;
  130. }
  131. if ($res)
  132. mysqli_free_result($res);
  133. return true;
  134. }
  135. ?>