Annotation of embedaddon/php/ext/mysqli/tests/local_infile_tools.inc, revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>