Back to index

php5  5.3.10
local_infile_tools.inc
Go to the documentation of this file.
00001 <?php
00002        /* Utility function for mysqli_set_local_infile*.phpt tests */
00003        function shutdown_clean($file) {
00004               if ($file) {
00005                      unlink($file);
00006               }
00007        }
00008 
00009        function check_local_infile_support($link, $engine, $table_name = 'test') {
00010 
00011               if (!$res = mysqli_query($link, 'SHOW VARIABLES LIKE "local_infile"'))
00012                      return "Cannot check if Server variable 'local_infile' is set to 'ON'";
00013 
00014               $row = mysqli_fetch_assoc($res);
00015               mysqli_free_result($res);
00016               if ('ON' != $row['Value'])
00017                      return sprintf("Server variable 'local_infile' seems not set to 'ON', found '%s'", $row['Value']);
00018 
00019               if (!mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name))) {
00020                      return "Failed to drop old test table";
00021               }
00022 
00023               if (!mysqli_query($link, $sql = sprintf('CREATE TABLE %s(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE=%s',
00024                      $table_name, $engine)))
00025                      return "Failed to create test table: $sql";
00026 
00027               $file = create_standard_csv(1, false);
00028               if (!$file) {
00029                      mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
00030                      return "Cannot create CSV file";
00031               }
00032 
00033               if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
00034                      INTO TABLE %s
00035                      FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
00036                      LINES TERMINATED BY '\n'",
00037                      mysqli_real_escape_string($link, $file),
00038                      $table_name))) {
00039                        if (1148 == mysqli_errno($link)) {
00040                               mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
00041                               return "Cannot test LOAD DATA LOCAL INFILE, [1148] The used command is not allowed with this MySQL version";
00042                        } else if ($link->errno) {
00043                               return $link->error;
00044                        }
00045               }
00046               mysqli_query($link, sprintf('DROP TABLE IF EXISTS %s', $table_name));
00047               return "";
00048        }
00049 
00050        function create_standard_csv($offset, $verbose = true) {
00051               // create a CVS file
00052               $file = tempnam(sys_get_temp_dir(), 'mysqli_test');
00053               if (!$fp = fopen($file, 'w')) {
00054                      if ($verbose)
00055                             printf("[%03d + 1] Cannot create CVS file '%s'\n", $offset, $file);
00056                      return NULL;
00057               } else {
00058                      /* Looks ugly? No, handy if you have crashes... */
00059                      register_shutdown_function("shutdown_clean", $file);
00060               }
00061 
00062               if ((version_compare(PHP_VERSION, '5.9.9', '>') == 1)) {
00063                      if (!fwrite($fp, (binary)"'97';'x';\n") ||
00064                             !fwrite($fp, (binary)"'98';'y';\n") ||
00065                             !fwrite($fp, (binary)"99;'z';\n")) {
00066                             if ($verbose)
00067                                    printf("[%03d + 2] Cannot write CVS file '%s'\n", $offset, $file);
00068                             return NULL;
00069                      }
00070               } else {
00071                      if (!fwrite($fp, "97;'x';\n") ||
00072                             !fwrite($fp, "98;'y';\n") ||
00073                             !fwrite($fp, "99;'z';\n")) {
00074                             if ($verbose)
00075                                    printf("[%03d + 3] Cannot write CVS file '%s'\n", $offset, $file);
00076                             return NULL;
00077                      }
00078               }
00079 
00080               fclose($fp);
00081 
00082               if (!chmod($file, 0644)) {
00083                      if ($verbose)
00084                             printf("[%03d + 4] Cannot change the file perms of '%s' from 0600 to 0644, MySQL might not be able to read it\n",
00085                                    $offset, $file);
00086                      return NULL;
00087               }
00088               return $file;
00089        }
00090 
00091        function try_handler($offset, $link, $file, $handler, $expected = null) {
00092 
00093               if ('default' == $handler) {
00094                      mysqli_set_local_infile_default($link);
00095               } else if (!mysqli_set_local_infile_handler($link, $handler)) {
00096                      printf("[%03d] Cannot set infile handler to '%s'\n", $offset, $handler);
00097                      return false;
00098               }
00099               printf("Callback set to '%s'\n", $handler);
00100 
00101               if (!mysqli_query($link, sprintf("DELETE FROM test"))) {
00102                      printf("[%03d] Cannot remove records, [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
00103                      return false;
00104               }
00105 
00106               if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
00107                      INTO TABLE test
00108                      FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
00109                      LINES TERMINATED BY '\n'",
00110                      mysqli_real_escape_string($link, $file)))) {
00111                      printf("[%03d] LOAD DATA failed, [%d] %s\n",
00112                             $offset + 2,
00113                             mysqli_errno($link), mysqli_error($link));
00114               }
00115 
00116               if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id")) {
00117                      printf("[%03d] [%d] %s\n", $offset + 3, mysqli_errno($link), mysqli_error($link));
00118                      return false;
00119               }
00120 
00121               if (!is_array($expected))
00122                      return true;
00123 
00124               foreach ($expected as $k => $values) {
00125                      if (!$tmp = mysqli_fetch_assoc($res)) {
00126                             printf("[%03d/%d] [%d] '%s'\n", $offset + 4, $k, mysqli_errno($link), mysqli_error($link));
00127                             return false;
00128                      }
00129                      if ($values['id'] != $tmp['id']) {
00130                             printf("[%03d/%d] Expecting %s got %s\n",
00131                                    $offset + 5, $k,
00132                                    $values['id'], $tmp['id']);
00133                                    return false;
00134                      }
00135                      if ($values['label'] != $tmp['label']) {
00136                             printf("[%03d/%d] Expecting %s got %s\n",
00137                                    $offset + 6, $k,
00138                                    $values['label'], $tmp['label']);
00139                                    return false;
00140                      }
00141               }
00142 
00143               if ($res && $tmp = mysqli_fetch_assoc($res)) {
00144                      printf("[%03d] More results than expected!\n", $offset + 7);
00145                      do {
00146                             var_dump($tmp);
00147                      } while ($tmp = mysqli_fetch_assoc($res));
00148                      return false;
00149               }
00150 
00151               if ($res)
00152                      mysqli_free_result($res);
00153 
00154               return true;
00155        }
00156 ?>