2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Library that provides common import functions that are used by import plugins
8 if (! defined('PHPMYADMIN')) {
13 * We need to know something about user
15 require_once './libraries/check_user_privileges.lib.php';
18 * We do this check, DROP DATABASE does not need to be confirmed elsewhere
20 define('PMA_CHK_DROP', 1);
23 * Check whether timeout is getting close
25 * @return boolean true if timeout is close
28 function PMA_checkTimeout()
30 global $timestamp, $maximum_time, $timeout_passed;
31 if ($maximum_time == 0) {
33 } elseif ($timeout_passed) {
35 /* 5 in next row might be too much */
36 } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
37 $timeout_passed = true;
45 * Detects what compression filse uses
47 * @param string filename to check
48 * @return string MIME type of compression, none for none
51 function PMA_detectCompression($filepath)
53 $file = @fopen
($filepath, 'rb');
57 $test = fread($file, 4);
60 if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
61 return 'application/gzip';
63 if ($len >= 3 && substr($test, 0, 3) == 'BZh') {
64 return 'application/bzip2';
66 if ($len >= 4 && $test == "PK\003\004") {
67 return 'application/zip';
73 * Runs query inside import buffer. This is needed to allow displaying
74 * of last SELECT, SHOW or HANDLER results and similar nice stuff.
76 * @param string query to run
77 * @param string query to display, this might be commented
78 * @param bool whether to use control user for queries
81 function PMA_importRunQuery($sql = '', $full = '', $controluser = false)
83 global $import_run_buffer, $go_sql, $complete_query, $display_query,
84 $sql_query, $my_die, $error, $reload,
85 $last_query_with_results,
86 $skip_queries, $executed_queries, $max_sql_len, $read_multiply,
87 $cfg, $sql_query_disabled, $db, $run_query, $is_superuser;
89 if (isset($import_run_buffer)) {
90 // Should we skip something?
91 if ($skip_queries > 0) {
94 if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
95 $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
96 if (!$sql_query_disabled) {
97 $sql_query .= $import_run_buffer['full'];
99 if (!$cfg['AllowUserDropDatabase']
101 && preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
102 $GLOBALS['message'] = PMA_Message
::error(__('"DROP DATABASE" statements are disabled.'));
106 if ($run_query && $GLOBALS['finished'] && empty($sql) && !$error && (
107 (!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
108 ($executed_queries == 1)
111 if (!$sql_query_disabled) {
112 $complete_query = $sql_query;
113 $display_query = $sql_query;
115 $complete_query = '';
118 $sql_query = $import_run_buffer['sql'];
119 // If a 'USE <db>' SQL-clause was found, set our current $db to the new one
120 list($db, $reload) = PMA_lookForUse($import_run_buffer['sql'], $db, $reload);
121 } elseif ($run_query) {
123 $result = PMA_query_as_controluser($import_run_buffer['sql']);
125 $result = PMA_DBI_try_query($import_run_buffer['sql']);
128 if ($result === false) { // execution failed
129 if (! isset($my_die)) {
132 $my_die[] = array('sql' => $import_run_buffer['full'], 'error' => PMA_DBI_getError());
134 if ($cfg['VerboseMultiSubmit']) {
138 if (!$cfg['IgnoreMultiSubmitErrors']) {
142 } elseif ($cfg['VerboseMultiSubmit']) {
143 $a_num_rows = (int)@PMA_DBI_num_rows
($result);
144 $a_aff_rows = (int)@PMA_DBI_affected_rows
();
145 if ($a_num_rows > 0) {
146 $msg .= __('Rows'). ': ' . $a_num_rows;
147 $last_query_with_results = $import_run_buffer['sql'];
148 } elseif ($a_aff_rows > 0) {
149 $message = PMA_Message
::affected_rows($a_aff_rows);
150 $msg .= $message->getMessage();
152 $msg .= __('MySQL returned an empty result set (i.e. zero rows).');
155 if (!$sql_query_disabled) {
156 $sql_query .= $msg . "\n";
159 // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
160 if ($result != false) {
161 list($db, $reload) = PMA_lookForUse($import_run_buffer['sql'], $db, $reload);
164 if ($result != false && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
168 } // end if not DROP DATABASE
169 } // end non empty query
170 elseif (!empty($import_run_buffer['full'])) {
172 $complete_query .= $import_run_buffer['full'];
173 $display_query .= $import_run_buffer['full'];
175 if (!$sql_query_disabled) {
176 $sql_query .= $import_run_buffer['full'];
180 // check length of query unless we decided to pass it to sql.php
181 // (if $run_query is false, we are just displaying so show
182 // the complete query in the textarea)
183 if (! $go_sql && $run_query) {
184 if ($cfg['VerboseMultiSubmit'] && ! empty($sql_query)) {
185 if (strlen($sql_query) > 50000 ||
$executed_queries > 50 ||
$max_sql_len > 1000) {
187 $sql_query_disabled = true;
190 if (strlen($sql_query) > 10000 ||
$executed_queries > 10 ||
$max_sql_len > 500) {
192 $sql_query_disabled = true;
196 } // end do query (no skip)
197 } // end buffer exists
199 // Do we have something to push into buffer?
200 if (!empty($sql) ||
!empty($full)) {
201 $import_run_buffer = array('sql' => $sql, 'full' => $full);
203 unset($GLOBALS['import_run_buffer']);
208 * Looks for the presence of USE to possibly change current db
210 * @param string buffer to examine
211 * @param string current db
212 * @param boolean reload
213 * @return array (current or new db, whether to reload)
216 function PMA_lookForUse($buffer, $db, $reload)
218 if (preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $buffer, $match)) {
219 $db = trim($match[1]);
220 $db = trim($db,';'); // for example, USE abc;
223 return(array($db, $reload));
228 * Returns next part of imported file/buffer
230 * @param integer size of buffer to read (this is maximal size
231 * function will return)
232 * @return string part of file/buffer
235 function PMA_importGetNextChunk($size = 32768)
237 global $compression, $import_handle, $charset_conversion, $charset_of_file,
240 // Add some progression while reading large amount of data
241 if ($read_multiply <= 8) {
242 $size *= $read_multiply;
248 // We can not read too much
249 if ($size > $GLOBALS['read_limit']) {
250 $size = $GLOBALS['read_limit'];
253 if (PMA_checkTimeout()) {
256 if ($GLOBALS['finished']) {
260 if ($GLOBALS['import_file'] == 'none') {
261 // Well this is not yet supported and tested, but should return content of textarea
262 if (strlen($GLOBALS['import_text']) < $size) {
263 $GLOBALS['finished'] = true;
264 return $GLOBALS['import_text'];
266 $r = substr($GLOBALS['import_text'], 0, $size);
267 $GLOBALS['offset'] +
= $size;
268 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
273 switch ($compression) {
274 case 'application/bzip2':
275 $result = bzread($import_handle, $size);
276 $GLOBALS['finished'] = feof($import_handle);
278 case 'application/gzip':
279 $result = gzread($import_handle, $size);
280 $GLOBALS['finished'] = feof($import_handle);
282 case 'application/zip':
283 $result = substr($GLOBALS['import_text'], 0, $size);
284 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
285 $GLOBALS['finished'] = empty($GLOBALS['import_text']);
288 $result = fread($import_handle, $size);
289 $GLOBALS['finished'] = feof($import_handle);
292 $GLOBALS['offset'] +
= $size;
294 if ($charset_conversion) {
295 return PMA_convert_string($charset_of_file, 'utf-8', $result);
298 * Skip possible byte order marks (I do not think we need more
299 * charsets, but feel free to add more, you can use wikipedia for
300 * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
302 * @todo BOM could be used for charset autodetection
304 if ($GLOBALS['offset'] == $size) {
306 if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
307 $result = substr($result, 3);
309 } elseif (strncmp($result, "\xFE\xFF", 2) == 0 ||
strncmp($result, "\xFF\xFE", 2) == 0) {
310 $result = substr($result, 2);
318 * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)
320 * This functions uses recursion to build the Excel column name.
322 * The column number (1-26) is converted to the responding ASCII character (A-Z) and returned.
324 * If the column number is bigger than 26 (= num of letters in alfabet),
325 * an extra character needs to be added. To find this extra character, the number is divided by 26
326 * and this value is passed to another instance of the same function (hence recursion).
327 * In that new instance the number is evaluated again, and if it is still bigger than 26, it is divided again
328 * and passed to another instance of the same function. This continues until the number is smaller than 26.
329 * Then the last called function returns the corresponding ASCII character to the function that called it.
330 * Each time a called function ends an extra character is added to the column name.
331 * When the first function is reached, the last character is addded and the complete column name is returned.
336 * @return string The column's "Excel" name
338 function PMA_getColumnAlphaName($num)
340 $A = 65; // ASCII value for capital "A"
344 $div = (int)($num / 26);
345 $remain = (int)($num %
26);
347 // subtract 1 of divided value in case the modulus is 0,
348 // this is necessary because A-Z has no 'zero'
353 // recursive function call
354 $col_name = PMA_getColumnAlphaName($div);
355 // use modulus as new column number
360 // use 'Z' if column number is 0,
361 // this is necessary because A-Z has no 'zero'
362 $col_name .= chr(($A +
26) - 1);
364 // convert column number to ASCII character
365 $col_name .= chr(($A +
$num) - 1);
372 * Returns the column number based on the Excel name.
373 * So "A" = 1, "Z" = 26, "AA" = 27, etc.
375 * Basicly this is a base26 (A-Z) to base10 (0-9) conversion.
376 * It iterates through all characters in the column name and
377 * calculates the corresponding value, based on character value
378 * (A = 1, ..., Z = 26) and position in the string.
382 * @param string $name (i.e. "A", or "BC", etc.)
383 * @return int The column number
385 function PMA_getColumnNumberFromName($name) {
387 $name = strtoupper($name);
388 $num_chars = strlen($name);
390 for ($i = 0; $i < $num_chars; ++
$i) {
391 // read string from back to front
392 $char_pos = ($num_chars - 1) - $i;
394 // convert capital character to ASCII value
395 // and subtract 64 to get corresponding decimal value
396 // ASCII value of "A" is 65, "B" is 66, etc.
397 // Decimal equivalent of "A" is 1, "B" is 2, etc.
398 $number = (ord($name[$char_pos]) - 64);
400 // base26 to base10 conversion : multiply each number
401 // with corresponding value of the position, in this case
402 // $i=0 : 1; $i=1 : 26; $i=2 : 676; ...
403 $column_number +
= $number * pow(26,$i);
405 return $column_number;
412 * Constants definitions
415 /* MySQL type defs */
417 define("VARCHAR", 1);
419 define("DECIMAL", 3);
422 /* Decimal size defs */
427 /* Table array defs */
428 define("TBL_NAME", 0);
429 define("COL_NAMES", 1);
432 /* Analysis array defs */
437 * Obtains the precision (total # of digits) from a size of type decimal
442 * @param string $last_cumulative_size
443 * @return int Precision of the given decimal size notation
445 function PMA_getM($last_cumulative_size) {
446 return (int)substr($last_cumulative_size, 0, strpos($last_cumulative_size, ","));
450 * Obtains the scale (# of digits to the right of the decimal point) from a size of type decimal
455 * @param string $last_cumulative_size
456 * @return int Scale of the given decimal size notation
458 function PMA_getD($last_cumulative_size) {
459 return (int)substr($last_cumulative_size, (strpos($last_cumulative_size, ",") +
1), (strlen($last_cumulative_size) - strpos($last_cumulative_size, ",")));
463 * Obtains the decimal size of a given cell
468 * @param string &$cell
469 * @return array Contains the precision, scale, and full size representation of the given decimal cell
471 function PMA_getDecimalSize(&$cell) {
472 $curr_size = strlen((string)$cell);
473 $decPos = strpos($cell, ".");
474 $decPrecision = ($curr_size - 1) - $decPos;
479 return array($m, $d, ($m . "," . $d));
483 * Obtains the size of the given cell
486 * @todo Handle the error cases more elegantly
490 * @param string $last_cumulative_size Last cumulative column size
491 * @param int $last_cumulative_type Last cumulative column type (NONE or VARCHAR or DECIMAL or INT or BIGINT)
492 * @param int $curr_type Type of the current cell (NONE or VARCHAR or DECIMAL or INT or BIGINT)
493 * @param string &$cell The current cell
494 * @return string Size of the given cell in the type-appropriate format
496 function PMA_detectSize($last_cumulative_size, $last_cumulative_type, $curr_type, &$cell) {
497 $curr_size = strlen((string)$cell);
500 * If the cell is NULL, don't treat it as a varchar
502 if (! strcmp('NULL', $cell)) {
503 return $last_cumulative_size;
506 * What to do if the current cell is of type VARCHAR
508 elseif ($curr_type == VARCHAR
) {
510 * The last cumulative type was VARCHAR
512 if ($last_cumulative_type == VARCHAR
) {
513 if ($curr_size >= $last_cumulative_size) {
516 return $last_cumulative_size;
520 * The last cumulative type was DECIMAL
522 elseif ($last_cumulative_type == DECIMAL
) {
523 $oldM = PMA_getM($last_cumulative_size);
525 if ($curr_size >= $oldM) {
532 * The last cumulative type was BIGINT or INT
534 elseif ($last_cumulative_type == BIGINT ||
$last_cumulative_type == INT) {
535 if ($curr_size >= $last_cumulative_size) {
538 return $last_cumulative_size;
542 * This is the first row to be analyzed
544 elseif (! isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
548 * An error has DEFINITELY occurred
552 * TODO: Handle this MUCH more elegantly
559 * What to do if the current cell is of type DECIMAL
561 elseif ($curr_type == DECIMAL
) {
563 * The last cumulative type was VARCHAR
565 if ($last_cumulative_type == VARCHAR
) {
566 /* Convert $last_cumulative_size from varchar to decimal format */
567 $size = PMA_getDecimalSize($cell);
569 if ($size[M
] >= $last_cumulative_size) {
572 return $last_cumulative_size;
576 * The last cumulative type was DECIMAL
578 elseif ($last_cumulative_type == DECIMAL
) {
579 $size = PMA_getDecimalSize($cell);
581 $oldM = PMA_getM($last_cumulative_size);
582 $oldD = PMA_getD($last_cumulative_size);
584 /* New val if M or D is greater than current largest */
585 if ($size[M
] > $oldM ||
$size[D
] > $oldD) {
586 /* Take the largest of both types */
587 return (string)((($size[M
] > $oldM) ?
$size[M
] : $oldM) . "," . (($size[D
] > $oldD) ?
$size[D
] : $oldD));
589 return $last_cumulative_size;
593 * The last cumulative type was BIGINT or INT
595 elseif ($last_cumulative_type == BIGINT ||
$last_cumulative_type == INT) {
596 /* Convert $last_cumulative_size from int to decimal format */
597 $size = PMA_getDecimalSize($cell);
599 if ($size[M
] >= $last_cumulative_size) {
602 return ($last_cumulative_size.",".$size[D
]);
606 * This is the first row to be analyzed
608 elseif (! isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
609 /* First row of the column */
610 $size = PMA_getDecimalSize($cell);
615 * An error has DEFINITELY occurred
619 * TODO: Handle this MUCH more elegantly
626 * What to do if the current cell is of type BIGINT or INT
628 elseif ($curr_type == BIGINT ||
$curr_type == INT) {
630 * The last cumulative type was VARCHAR
632 if ($last_cumulative_type == VARCHAR
) {
633 if ($curr_size >= $last_cumulative_size) {
636 return $last_cumulative_size;
640 * The last cumulative type was DECIMAL
642 elseif ($last_cumulative_type == DECIMAL
) {
643 $oldM = PMA_getM($last_cumulative_size);
644 $oldD = PMA_getD($last_cumulative_size);
645 $oldInt = $oldM - $oldD;
646 $newInt = strlen((string)$cell);
648 /* See which has the larger integer length */
649 if ($oldInt >= $newInt) {
650 /* Use old decimal size */
651 return $last_cumulative_size;
653 /* Use $newInt + $oldD as new M */
654 return (($newInt +
$oldD) . "," . $oldD);
658 * The last cumulative type was BIGINT or INT
660 elseif ($last_cumulative_type == BIGINT ||
$last_cumulative_type == INT) {
661 if ($curr_size >= $last_cumulative_size) {
664 return $last_cumulative_size;
668 * This is the first row to be analyzed
670 elseif (! isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
674 * An error has DEFINITELY occurred
678 * TODO: Handle this MUCH more elegantly
685 * An error has DEFINITELY occurred
689 * TODO: Handle this MUCH more elegantly
697 * Determines what MySQL type a cell is
702 * @param int $last_cumulative_type Last cumulative column type (VARCHAR or INT or BIGINT or DECIMAL or NONE)
703 * @param string &$cell String representation of the cell for which a best-fit type is to be determined
704 * @return int The MySQL type representation (VARCHAR or INT or BIGINT or DECIMAL or NONE)
706 function PMA_detectType($last_cumulative_type, &$cell) {
708 * If numeric, determine if decimal, int or bigint
709 * Else, we call it varchar for simplicity
712 if (! strcmp('NULL', $cell)) {
713 if ($last_cumulative_type === NULL ||
$last_cumulative_type == NONE
) {
716 return $last_cumulative_type;
718 } elseif (is_numeric($cell)) {
719 if ($cell == (string)(float)$cell && strpos($cell, ".") !== false && substr_count($cell, ".") == 1) {
722 if (abs($cell) > 2147483647) {
734 * Determines if the column types are int, decimal, or string
737 * @link http://wiki.phpmyadmin.net/pma/Import
739 * @todo Handle the error case more elegantly
743 * @param &$table array(string $table_name, array $col_names, array $rows)
744 * @return array array(array $types, array $sizes)
746 function PMA_analyzeTable(&$table) {
747 /* Get number of rows in table */
748 $numRows = count($table[ROWS
]);
749 /* Get number of columns */
750 $numCols = count($table[COL_NAMES
]);
751 /* Current type for each column */
755 /* Initialize $sizes to all 0's */
756 for ($i = 0; $i < $numCols; ++
$i) {
760 /* Initialize $types to NONE */
761 for ($i = 0; $i < $numCols; ++
$i) {
769 /* If the passed array is not of the correct form, do not process it */
770 if (is_array($table) && ! is_array($table[TBL_NAME
]) && is_array($table[COL_NAMES
]) && is_array($table[ROWS
])) {
771 /* Analyze each column */
772 for ($i = 0; $i < $numCols; ++
$i) {
773 /* Analyze the column in each row */
774 for ($j = 0; $j < $numRows; ++
$j) {
775 /* Determine type of the current cell */
776 $curr_type = PMA_detectType($types[$i], $table[ROWS
][$j][$i]);
777 /* Determine size of the current cell */
778 $sizes[$i] = PMA_detectSize($sizes[$i], $types[$i], $curr_type, $table[ROWS
][$j][$i]);
781 * If a type for this column has already been declared,
782 * only alter it if it was a number and a varchar was found
784 if ($curr_type != NONE
) {
785 if ($curr_type == VARCHAR
) {
786 $types[$i] = VARCHAR
;
787 } else if ($curr_type == DECIMAL
) {
788 if ($types[$i] != VARCHAR
) {
789 $types[$i] = DECIMAL
;
791 } else if ($curr_type == BIGINT
) {
792 if ($types[$i] != VARCHAR
&& $types[$i] != DECIMAL
) {
795 } else if ($curr_type == INT) {
796 if ($types[$i] != VARCHAR
&& $types[$i] != DECIMAL
&& $types[$i] != BIGINT
) {
804 /* Check to ensure that all types are valid */
805 $len = count($types);
806 for ($n = 0; $n < $len; ++
$n) {
807 if (! strcmp(NONE
, $types[$n])) {
808 $types[$n] = VARCHAR
;
813 return array($types, $sizes);
818 * TODO: Handle this better
825 /* Needed to quell the beast that is PMA_Message */
826 $import_notice = NULL;
829 * Builds and executes SQL statements to create the database and tables
830 * as necessary, as well as insert all the data.
833 * @link http://wiki.phpmyadmin.net/pma/Import
837 * @param string $db_name Name of the database
838 * @param array &$tables Array of tables for the specified database
839 * @param array &$analyses = NULL Analyses of the tables
840 * @param array &$additional_sql = NULL Additional SQL statements to be executed
841 * @param array $options = NULL Associative array of options
844 function PMA_buildSQL($db_name, &$tables, &$analyses = NULL, &$additional_sql = NULL, $options = NULL) {
845 /* Take care of the options */
846 if (isset($options['db_collation'])&& ! is_null($options['db_collation'])) {
847 $collation = $options['db_collation'];
849 $collation = "utf8_general_ci";
852 if (isset($options['db_charset']) && ! is_null($options['db_charset'])) {
853 $charset = $options['db_charset'];
858 if (isset($options['create_db'])) {
859 $create_db = $options['create_db'];
864 /* Create SQL code to handle the database */
868 $sql[] = "CREATE DATABASE IF NOT EXISTS " . PMA_backquote($db_name) . " DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation;
872 * The calling plug-in should include this statement, if necessary, in the $additional_sql parameter
874 * $sql[] = "USE " . PMA_backquote($db_name);
877 /* Execute the SQL statements create above */
878 $sql_len = count($sql);
879 for ($i = 0; $i < $sql_len; ++
$i) {
880 PMA_importRunQuery($sql[$i], $sql[$i]);
883 /* No longer needed */
886 /* Run the $additional_sql statements supplied by the caller plug-in */
887 if ($additional_sql != NULL) {
888 /* Clean the SQL first */
889 $additional_sql_len = count($additional_sql);
892 * Only match tables for now, because CREATE IF NOT EXISTS
893 * syntax is lacking or nonexisting for views, triggers,
894 * functions, and procedures.
896 * See: http://bugs.mysql.com/bug.php?id=15287
898 * To the best of my knowledge this is still an issue.
900 * $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)';
902 $pattern = '/CREATE .*(TABLE)/';
903 $replacement = 'CREATE \\1 IF NOT EXISTS';
905 /* Change CREATE statements to CREATE IF NOT EXISTS to support inserting into existing structures */
906 for ($i = 0; $i < $additional_sql_len; ++
$i) {
907 $additional_sql[$i] = preg_replace($pattern, $replacement, $additional_sql[$i]);
908 /* Execute the resulting statements */
909 PMA_importRunQuery($additional_sql[$i], $additional_sql[$i]);
913 if ($analyses != NULL) {
914 $type_array = array(NONE
=> "NULL", VARCHAR
=> "varchar", INT => "int", DECIMAL
=> "decimal", BIGINT
=> "bigint");
916 /* TODO: Do more checking here to make sure they really are matched */
917 if (count($tables) != count($analyses)) {
921 /* Create SQL code to create the tables */
923 $num_tables = count($tables);
924 for ($i = 0; $i < $num_tables; ++
$i) {
925 $num_cols = count($tables[$i][COL_NAMES
]);
926 $tempSQLStr = "CREATE TABLE IF NOT EXISTS " . PMA_backquote($db_name) . '.' . PMA_backquote($tables[$i][TBL_NAME
]) . " (";
927 for ($j = 0; $j < $num_cols; ++
$j) {
928 $size = $analyses[$i][SIZES
][$j];
929 if ((int)$size == 0) {
933 $tempSQLStr .= PMA_backquote($tables[$i][COL_NAMES
][$j]) . " " . $type_array[$analyses[$i][TYPES
][$j]] . "(" . $size . ")";
935 if ($j != (count($tables[$i][COL_NAMES
]) - 1)) {
939 $tempSQLStr .= ") ENGINE=MyISAM DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation . ";";
942 * Each SQL statement is executed immediately
943 * after it is formed so that we don't have
944 * to store them in a (possibly large) buffer
946 PMA_importRunQuery($tempSQLStr, $tempSQLStr);
951 * Create the SQL statements to insert all the data
953 * Only one insert query is formed for each table
957 $num_tables = count($tables);
958 for ($i = 0; $i < $num_tables; ++
$i) {
959 $num_cols = count($tables[$i][COL_NAMES
]);
960 $num_rows = count($tables[$i][ROWS
]);
962 $tempSQLStr = "INSERT INTO " . PMA_backquote($db_name) . '.' . PMA_backquote($tables[$i][TBL_NAME
]) . " (";
964 for ($m = 0; $m < $num_cols; ++
$m) {
965 $tempSQLStr .= PMA_backquote($tables[$i][COL_NAMES
][$m]);
967 if ($m != ($num_cols - 1)) {
972 $tempSQLStr .= ") VALUES ";
974 for ($j = 0; $j < $num_rows; ++
$j) {
977 for ($k = 0; $k < $num_cols; ++
$k) {
978 if ($analyses != NULL) {
979 $is_varchar = ($analyses[$i][TYPES
][$col_count] === VARCHAR
);
981 $is_varchar = !is_numeric($tables[$i][ROWS
][$j][$k]);
984 /* Don't put quotes around NULL fields */
985 if (! strcmp($tables[$i][ROWS
][$j][$k], 'NULL')) {
989 $tempSQLStr .= (($is_varchar) ?
"'" : "");
990 $tempSQLStr .= PMA_sqlAddSlashes((string)$tables[$i][ROWS
][$j][$k]);
991 $tempSQLStr .= (($is_varchar) ?
"'" : "");
993 if ($k != ($num_cols - 1)) {
997 if ($col_count == ($num_cols - 1)) {
1003 /* Delete the cell after we are done with it */
1004 unset($tables[$i][ROWS
][$j][$k]);
1009 if ($j != ($num_rows - 1)) {
1010 $tempSQLStr .= ",\n ";
1014 /* Delete the row after we are done with it */
1015 unset($tables[$i][ROWS
][$j]);
1021 * Each SQL statement is executed immediately
1022 * after it is formed so that we don't have
1023 * to store them in a (possibly large) buffer
1025 PMA_importRunQuery($tempSQLStr, $tempSQLStr);
1028 /* No longer needed */
1032 * A work in progress
1035 /* Add the viewable structures from $additional_sql to $tables so they are also displayed */
1037 $view_pattern = '@VIEW `[^`]+`\.`([^`]+)@';
1038 $table_pattern = '@CREATE TABLE IF NOT EXISTS `([^`]+)`@';
1039 /* Check a third pattern to make sure its not a "USE `db_name`;" statement */
1045 $additional_sql_len = count($additional_sql);
1046 for ($i = 0; $i < $additional_sql_len; ++
$i) {
1047 preg_match($view_pattern, $additional_sql[$i], $regs);
1049 if (count($regs) == 0) {
1050 preg_match($table_pattern, $additional_sql[$i], $regs);
1054 for ($n = 0; $n < $num_tables; ++
$n) {
1055 if (!strcmp($regs[1], $tables[$n][TBL_NAME
])) {
1062 $tables[] = array(TBL_NAME
=> $regs[1]);
1066 /* Reset the array */
1071 $params = array('db' => (string)$db_name);
1072 $db_url = 'db_structure.php' . PMA_generate_common_url($params);
1073 $db_ops_url = 'db_operations.php' . PMA_generate_common_url($params);
1075 $message = '<br /><br />';
1076 $message .= '<strong>' . __('The following structures have either been created or altered. Here you can:') . '</strong><br />';
1077 $message .= '<ul><li>' . __('View a structure`s contents by clicking on its name') . '</li>';
1078 $message .= '<li>' . __('Change any of its settings by clicking the corresponding "Options" link') . '</li>';
1079 $message .= '<li>' . __('Edit its structure by following the "Structure" link') . '</li>';
1080 $message .= sprintf('<br /><li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">' . __('Options') . '</a>)</li>',
1082 __('Go to database') . ': ' . PMA_backquote($db_name),
1085 __('Edit') . ' ' . PMA_backquote($db_name) . ' ' . __('settings'));
1091 $num_tables = count($tables);
1092 for ($i = 0; $i < $num_tables; ++
$i)
1094 $params = array('db' => (string)$db_name, 'table' => (string)$tables[$i][TBL_NAME
]);
1095 $tbl_url = 'sql.php' . PMA_generate_common_url($params);
1096 $tbl_struct_url = 'tbl_structure.php' . PMA_generate_common_url($params);
1097 $tbl_ops_url = 'tbl_operations.php' . PMA_generate_common_url($params);
1101 if (! PMA_isView($db_name, $tables[$i][TBL_NAME
])) {
1102 $message .= sprintf('<li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">' . __('Structure') . '</a>) (<a href="%s" title="%s">' . __('Options') . '</a>)</li>',
1104 __('Go to table') . ': ' . PMA_backquote($tables[$i][TBL_NAME
]),
1105 $tables[$i][TBL_NAME
],
1107 PMA_backquote($tables[$i][TBL_NAME
]) . ' ' . __('structure'),
1109 __('Edit') . ' ' . PMA_backquote($tables[$i][TBL_NAME
]) . ' ' . __('settings'));
1111 $message .= sprintf('<li><a href="%s" title="%s">%s</a></li>',
1113 __('Go to view') . ': ' . PMA_backquote($tables[$i][TBL_NAME
]),
1114 $tables[$i][TBL_NAME
]);
1118 $message .= '</ul></ul>';
1120 global $import_notice;
1121 $import_notice = $message;