local_infile_tools.inc 5.5 KB

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