local_infile_tools.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 (!fwrite($fp, "97;'x';\n") ||
  54. !fwrite($fp, "98;'y';\n") ||
  55. !fwrite($fp, "99;'z';\n")) {
  56. if ($verbose)
  57. printf("[%03d + 3] Cannot write CVS file '%s'\n", $offset, $file);
  58. return NULL;
  59. }
  60. fclose($fp);
  61. if (!chmod($file, 0644)) {
  62. if ($verbose)
  63. printf("[%03d + 4] Cannot change the file perms of '%s' from 0600 to 0644, MySQL might not be able to read it\n",
  64. $offset, $file);
  65. return NULL;
  66. }
  67. return $file;
  68. }
  69. function try_handler($offset, $link, $file, $handler, $expected = null) {
  70. if ('default' == $handler) {
  71. mysqli_set_local_infile_default($link);
  72. } else if (!mysqli_set_local_infile_handler($link, $handler)) {
  73. printf("[%03d] Cannot set infile handler to '%s'\n", $offset, $handler);
  74. return false;
  75. }
  76. printf("Callback set to '%s'\n", $handler);
  77. if (!mysqli_query($link, sprintf("DELETE FROM test"))) {
  78. printf("[%03d] Cannot remove records, [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
  79. return false;
  80. }
  81. if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
  82. INTO TABLE test
  83. FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
  84. LINES TERMINATED BY '\n'",
  85. mysqli_real_escape_string($link, $file)))) {
  86. printf("[%03d] LOAD DATA failed, [%d] %s\n",
  87. $offset + 2,
  88. mysqli_errno($link), mysqli_error($link));
  89. }
  90. if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id")) {
  91. printf("[%03d] [%d] %s\n", $offset + 3, mysqli_errno($link), mysqli_error($link));
  92. return false;
  93. }
  94. if (!is_array($expected))
  95. return true;
  96. foreach ($expected as $k => $values) {
  97. if (!$tmp = mysqli_fetch_assoc($res)) {
  98. printf("[%03d/%d] [%d] '%s'\n", $offset + 4, $k, mysqli_errno($link), mysqli_error($link));
  99. return false;
  100. }
  101. if ($values['id'] != $tmp['id']) {
  102. printf("[%03d/%d] Expecting %s got %s\n",
  103. $offset + 5, $k,
  104. $values['id'], $tmp['id']);
  105. return false;
  106. }
  107. if ($values['label'] != $tmp['label']) {
  108. printf("[%03d/%d] Expecting %s got %s\n",
  109. $offset + 6, $k,
  110. $values['label'], $tmp['label']);
  111. return false;
  112. }
  113. }
  114. if ($res && $tmp = mysqli_fetch_assoc($res)) {
  115. printf("[%03d] More results than expected!\n", $offset + 7);
  116. do {
  117. var_dump($tmp);
  118. } while ($tmp = mysqli_fetch_assoc($res));
  119. return false;
  120. }
  121. if ($res)
  122. mysqli_free_result($res);
  123. return true;
  124. }
  125. ?>