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 $filepath 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 $sql query to run
77 * @param string $full query to display, this might be commented
78 * @param bool $controluser 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 buffer to examine
211 * @param string $db current db
212 * @param bool $reload 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 int $size size of buffer to read (this is maximal size function will return)
231 * @return string part of file/buffer
234 function PMA_importGetNextChunk($size = 32768)
236 global $compression, $import_handle, $charset_conversion, $charset_of_file,
239 // Add some progression while reading large amount of data
240 if ($read_multiply <= 8) {
241 $size *= $read_multiply;
247 // We can not read too much
248 if ($size > $GLOBALS['read_limit']) {
249 $size = $GLOBALS['read_limit'];
252 if (PMA_checkTimeout()) {
255 if ($GLOBALS['finished']) {
259 if ($GLOBALS['import_file'] == 'none') {
260 // Well this is not yet supported and tested, but should return content of textarea
261 if (strlen($GLOBALS['import_text']) < $size) {
262 $GLOBALS['finished'] = true;
263 return $GLOBALS['import_text'];
265 $r = substr($GLOBALS['import_text'], 0, $size);
266 $GLOBALS['offset'] +
= $size;
267 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
272 switch ($compression) {
273 case 'application/bzip2':
274 $result = bzread($import_handle, $size);
275 $GLOBALS['finished'] = feof($import_handle);
277 case 'application/gzip':
278 $result = gzread($import_handle, $size);
279 $GLOBALS['finished'] = feof($import_handle);
281 case 'application/zip':
282 $result = substr($GLOBALS['import_text'], 0, $size);
283 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
284 $GLOBALS['finished'] = empty($GLOBALS['import_text']);
287 $result = fread($import_handle, $size);
288 $GLOBALS['finished'] = feof($import_handle);
291 $GLOBALS['offset'] +
= $size;
293 if ($charset_conversion) {
294 return PMA_convert_string($charset_of_file, 'utf-8', $result);
297 * Skip possible byte order marks (I do not think we need more
298 * charsets, but feel free to add more, you can use wikipedia for
299 * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
301 * @todo BOM could be used for charset autodetection
303 if ($GLOBALS['offset'] == $size) {
305 if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
306 $result = substr($result, 3);
308 } elseif (strncmp($result, "\xFE\xFF", 2) == 0 ||
strncmp($result, "\xFF\xFE", 2) == 0) {
309 $result = substr($result, 2);
317 * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)
319 * This functions uses recursion to build the Excel column name.
321 * The column number (1-26) is converted to the responding ASCII character (A-Z) and returned.
323 * If the column number is bigger than 26 (= num of letters in alfabet),
324 * an extra character needs to be added. To find this extra character, the number is divided by 26
325 * and this value is passed to another instance of the same function (hence recursion).
326 * In that new instance the number is evaluated again, and if it is still bigger than 26, it is divided again
327 * and passed to another instance of the same function. This continues until the number is smaller than 26.
328 * Then the last called function returns the corresponding ASCII character to the function that called it.
329 * Each time a called function ends an extra character is added to the column name.
330 * When the first function is reached, the last character is addded and the complete column name is returned.
335 * @return string The column's "Excel" name
337 function PMA_getColumnAlphaName($num)
339 $A = 65; // ASCII value for capital "A"
343 $div = (int)($num / 26);
344 $remain = (int)($num %
26);
346 // subtract 1 of divided value in case the modulus is 0,
347 // this is necessary because A-Z has no 'zero'
352 // recursive function call
353 $col_name = PMA_getColumnAlphaName($div);
354 // use modulus as new column number
359 // use 'Z' if column number is 0,
360 // this is necessary because A-Z has no 'zero'
361 $col_name .= chr(($A +
26) - 1);
363 // convert column number to ASCII character
364 $col_name .= chr(($A +
$num) - 1);
371 * Returns the column number based on the Excel name.
372 * So "A" = 1, "Z" = 26, "AA" = 27, etc.
374 * Basicly this is a base26 (A-Z) to base10 (0-9) conversion.
375 * It iterates through all characters in the column name and
376 * calculates the corresponding value, based on character value
377 * (A = 1, ..., Z = 26) and position in the string.
381 * @param string $name (i.e. "A", or "BC", etc.)
382 * @return int The column number
384 function PMA_getColumnNumberFromName($name) {
386 $name = strtoupper($name);
387 $num_chars = strlen($name);
389 for ($i = 0; $i < $num_chars; ++
$i) {
390 // read string from back to front
391 $char_pos = ($num_chars - 1) - $i;
393 // convert capital character to ASCII value
394 // and subtract 64 to get corresponding decimal value
395 // ASCII value of "A" is 65, "B" is 66, etc.
396 // Decimal equivalent of "A" is 1, "B" is 2, etc.
397 $number = (ord($name[$char_pos]) - 64);
399 // base26 to base10 conversion : multiply each number
400 // with corresponding value of the position, in this case
401 // $i=0 : 1; $i=1 : 26; $i=2 : 676; ...
402 $column_number +
= $number * pow(26,$i);
404 return $column_number;
411 * Constants definitions
414 /* MySQL type defs */
416 define("VARCHAR", 1);
418 define("DECIMAL", 3);
421 /* Decimal size defs */
426 /* Table array defs */
427 define("TBL_NAME", 0);
428 define("COL_NAMES", 1);
431 /* Analysis array defs */
436 * Obtains the precision (total # of digits) from a size of type decimal
440 * @param string $last_cumulative_size
441 * @return int Precision of the given decimal size notation
443 function PMA_getM($last_cumulative_size) {
444 return (int)substr($last_cumulative_size, 0, strpos($last_cumulative_size, ","));
448 * Obtains the scale (# of digits to the right of the decimal point) from a size of type decimal
452 * @param string $last_cumulative_size
453 * @return int Scale of the given decimal size notation
455 function PMA_getD($last_cumulative_size) {
456 return (int)substr($last_cumulative_size, (strpos($last_cumulative_size, ",") +
1), (strlen($last_cumulative_size) - strpos($last_cumulative_size, ",")));
460 * Obtains the decimal size of a given cell
464 * @param string &$cell
465 * @return array Contains the precision, scale, and full size representation of the given decimal cell
467 function PMA_getDecimalSize(&$cell) {
468 $curr_size = strlen((string)$cell);
469 $decPos = strpos($cell, ".");
470 $decPrecision = ($curr_size - 1) - $decPos;
475 return array($m, $d, ($m . "," . $d));
479 * Obtains the size of the given cell
481 * @todo Handle the error cases more elegantly
485 * @param string $last_cumulative_size Last cumulative column size
486 * @param int $last_cumulative_type Last cumulative column type (NONE or VARCHAR or DECIMAL or INT or BIGINT)
487 * @param int $curr_type Type of the current cell (NONE or VARCHAR or DECIMAL or INT or BIGINT)
488 * @param string &$cell The current cell
489 * @return string Size of the given cell in the type-appropriate format
491 function PMA_detectSize($last_cumulative_size, $last_cumulative_type, $curr_type, &$cell) {
492 $curr_size = strlen((string)$cell);
495 * If the cell is NULL, don't treat it as a varchar
497 if (! strcmp('NULL', $cell)) {
498 return $last_cumulative_size;
501 * What to do if the current cell is of type VARCHAR
503 elseif ($curr_type == VARCHAR
) {
505 * The last cumulative type was VARCHAR
507 if ($last_cumulative_type == VARCHAR
) {
508 if ($curr_size >= $last_cumulative_size) {
511 return $last_cumulative_size;
515 * The last cumulative type was DECIMAL
517 elseif ($last_cumulative_type == DECIMAL
) {
518 $oldM = PMA_getM($last_cumulative_size);
520 if ($curr_size >= $oldM) {
527 * The last cumulative type was BIGINT or INT
529 elseif ($last_cumulative_type == BIGINT ||
$last_cumulative_type == INT) {
530 if ($curr_size >= $last_cumulative_size) {
533 return $last_cumulative_size;
537 * This is the first row to be analyzed
539 elseif (! isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
543 * An error has DEFINITELY occurred
547 * TODO: Handle this MUCH more elegantly
554 * What to do if the current cell is of type DECIMAL
556 elseif ($curr_type == DECIMAL
) {
558 * The last cumulative type was VARCHAR
560 if ($last_cumulative_type == VARCHAR
) {
561 /* Convert $last_cumulative_size from varchar to decimal format */
562 $size = PMA_getDecimalSize($cell);
564 if ($size[M
] >= $last_cumulative_size) {
567 return $last_cumulative_size;
571 * The last cumulative type was DECIMAL
573 elseif ($last_cumulative_type == DECIMAL
) {
574 $size = PMA_getDecimalSize($cell);
576 $oldM = PMA_getM($last_cumulative_size);
577 $oldD = PMA_getD($last_cumulative_size);
579 /* New val if M or D is greater than current largest */
580 if ($size[M
] > $oldM ||
$size[D
] > $oldD) {
581 /* Take the largest of both types */
582 return (string)((($size[M
] > $oldM) ?
$size[M
] : $oldM) . "," . (($size[D
] > $oldD) ?
$size[D
] : $oldD));
584 return $last_cumulative_size;
588 * The last cumulative type was BIGINT or INT
590 elseif ($last_cumulative_type == BIGINT ||
$last_cumulative_type == INT) {
591 /* Convert $last_cumulative_size from int to decimal format */
592 $size = PMA_getDecimalSize($cell);
594 if ($size[M
] >= $last_cumulative_size) {
597 return ($last_cumulative_size.",".$size[D
]);
601 * This is the first row to be analyzed
603 elseif (! isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
604 /* First row of the column */
605 $size = PMA_getDecimalSize($cell);
610 * An error has DEFINITELY occurred
614 * TODO: Handle this MUCH more elegantly
621 * What to do if the current cell is of type BIGINT or INT
623 elseif ($curr_type == BIGINT ||
$curr_type == INT) {
625 * The last cumulative type was VARCHAR
627 if ($last_cumulative_type == VARCHAR
) {
628 if ($curr_size >= $last_cumulative_size) {
631 return $last_cumulative_size;
635 * The last cumulative type was DECIMAL
637 elseif ($last_cumulative_type == DECIMAL
) {
638 $oldM = PMA_getM($last_cumulative_size);
639 $oldD = PMA_getD($last_cumulative_size);
640 $oldInt = $oldM - $oldD;
641 $newInt = strlen((string)$cell);
643 /* See which has the larger integer length */
644 if ($oldInt >= $newInt) {
645 /* Use old decimal size */
646 return $last_cumulative_size;
648 /* Use $newInt + $oldD as new M */
649 return (($newInt +
$oldD) . "," . $oldD);
653 * The last cumulative type was BIGINT or INT
655 elseif ($last_cumulative_type == BIGINT ||
$last_cumulative_type == INT) {
656 if ($curr_size >= $last_cumulative_size) {
659 return $last_cumulative_size;
663 * This is the first row to be analyzed
665 elseif (! isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
669 * An error has DEFINITELY occurred
673 * TODO: Handle this MUCH more elegantly
680 * An error has DEFINITELY occurred
684 * TODO: Handle this MUCH more elegantly
692 * Determines what MySQL type a cell is
696 * @param int $last_cumulative_type Last cumulative column type (VARCHAR or INT or BIGINT or DECIMAL or NONE)
697 * @param string &$cell String representation of the cell for which a best-fit type is to be determined
698 * @return int The MySQL type representation (VARCHAR or INT or BIGINT or DECIMAL or NONE)
700 function PMA_detectType($last_cumulative_type, &$cell) {
702 * If numeric, determine if decimal, int or bigint
703 * Else, we call it varchar for simplicity
706 if (! strcmp('NULL', $cell)) {
707 if ($last_cumulative_type === null ||
$last_cumulative_type == NONE
) {
710 return $last_cumulative_type;
712 } elseif (is_numeric($cell)) {
713 if ($cell == (string)(float)$cell && strpos($cell, ".") !== false && substr_count($cell, ".") == 1) {
716 if (abs($cell) > 2147483647) {
728 * Determines if the column types are int, decimal, or string
730 * @link http://wiki.phpmyadmin.net/pma/Import
732 * @todo Handle the error case more elegantly
736 * @param &$table array(string $table_name, array $col_names, array $rows)
737 * @return array array(array $types, array $sizes)
739 function PMA_analyzeTable(&$table) {
740 /* Get number of rows in table */
741 $numRows = count($table[ROWS
]);
742 /* Get number of columns */
743 $numCols = count($table[COL_NAMES
]);
744 /* Current type for each column */
748 /* Initialize $sizes to all 0's */
749 for ($i = 0; $i < $numCols; ++
$i) {
753 /* Initialize $types to NONE */
754 for ($i = 0; $i < $numCols; ++
$i) {
762 /* If the passed array is not of the correct form, do not process it */
763 if (is_array($table) && ! is_array($table[TBL_NAME
]) && is_array($table[COL_NAMES
]) && is_array($table[ROWS
])) {
764 /* Analyze each column */
765 for ($i = 0; $i < $numCols; ++
$i) {
766 /* Analyze the column in each row */
767 for ($j = 0; $j < $numRows; ++
$j) {
768 /* Determine type of the current cell */
769 $curr_type = PMA_detectType($types[$i], $table[ROWS
][$j][$i]);
770 /* Determine size of the current cell */
771 $sizes[$i] = PMA_detectSize($sizes[$i], $types[$i], $curr_type, $table[ROWS
][$j][$i]);
774 * If a type for this column has already been declared,
775 * only alter it if it was a number and a varchar was found
777 if ($curr_type != NONE
) {
778 if ($curr_type == VARCHAR
) {
779 $types[$i] = VARCHAR
;
780 } else if ($curr_type == DECIMAL
) {
781 if ($types[$i] != VARCHAR
) {
782 $types[$i] = DECIMAL
;
784 } else if ($curr_type == BIGINT
) {
785 if ($types[$i] != VARCHAR
&& $types[$i] != DECIMAL
) {
788 } else if ($curr_type == INT) {
789 if ($types[$i] != VARCHAR
&& $types[$i] != DECIMAL
&& $types[$i] != BIGINT
) {
797 /* Check to ensure that all types are valid */
798 $len = count($types);
799 for ($n = 0; $n < $len; ++
$n) {
800 if (! strcmp(NONE
, $types[$n])) {
801 $types[$n] = VARCHAR
;
806 return array($types, $sizes);
811 * TODO: Handle this better
818 /* Needed to quell the beast that is PMA_Message */
819 $import_notice = null;
822 * Builds and executes SQL statements to create the database and tables
823 * as necessary, as well as insert all the data.
825 * @link http://wiki.phpmyadmin.net/pma/Import
829 * @param string $db_name Name of the database
830 * @param array &$tables Array of tables for the specified database
831 * @param array &$analyses Analyses of the tables
832 * @param array &$additional_sql Additional SQL statements to be executed
833 * @param array $options Associative array of options
836 function PMA_buildSQL($db_name, &$tables, &$analyses = null, &$additional_sql = null, $options = null) {
837 /* Take care of the options */
838 if (isset($options['db_collation'])&& ! is_null($options['db_collation'])) {
839 $collation = $options['db_collation'];
841 $collation = "utf8_general_ci";
844 if (isset($options['db_charset']) && ! is_null($options['db_charset'])) {
845 $charset = $options['db_charset'];
850 if (isset($options['create_db'])) {
851 $create_db = $options['create_db'];
856 /* Create SQL code to handle the database */
860 $sql[] = "CREATE DATABASE IF NOT EXISTS " . PMA_backquote($db_name) . " DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation;
864 * The calling plug-in should include this statement, if necessary, in the $additional_sql parameter
866 * $sql[] = "USE " . PMA_backquote($db_name);
869 /* Execute the SQL statements create above */
870 $sql_len = count($sql);
871 for ($i = 0; $i < $sql_len; ++
$i) {
872 PMA_importRunQuery($sql[$i], $sql[$i]);
875 /* No longer needed */
878 /* Run the $additional_sql statements supplied by the caller plug-in */
879 if ($additional_sql != null) {
880 /* Clean the SQL first */
881 $additional_sql_len = count($additional_sql);
884 * Only match tables for now, because CREATE IF NOT EXISTS
885 * syntax is lacking or nonexisting for views, triggers,
886 * functions, and procedures.
888 * See: http://bugs.mysql.com/bug.php?id=15287
890 * To the best of my knowledge this is still an issue.
892 * $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)';
894 $pattern = '/CREATE .*(TABLE)/';
895 $replacement = 'CREATE \\1 IF NOT EXISTS';
897 /* Change CREATE statements to CREATE IF NOT EXISTS to support inserting into existing structures */
898 for ($i = 0; $i < $additional_sql_len; ++
$i) {
899 $additional_sql[$i] = preg_replace($pattern, $replacement, $additional_sql[$i]);
900 /* Execute the resulting statements */
901 PMA_importRunQuery($additional_sql[$i], $additional_sql[$i]);
905 if ($analyses != null) {
906 $type_array = array(NONE
=> "NULL", VARCHAR
=> "varchar", INT => "int", DECIMAL
=> "decimal", BIGINT
=> "bigint");
908 /* TODO: Do more checking here to make sure they really are matched */
909 if (count($tables) != count($analyses)) {
913 /* Create SQL code to create the tables */
915 $num_tables = count($tables);
916 for ($i = 0; $i < $num_tables; ++
$i) {
917 $num_cols = count($tables[$i][COL_NAMES
]);
918 $tempSQLStr = "CREATE TABLE IF NOT EXISTS " . PMA_backquote($db_name) . '.' . PMA_backquote($tables[$i][TBL_NAME
]) . " (";
919 for ($j = 0; $j < $num_cols; ++
$j) {
920 $size = $analyses[$i][SIZES
][$j];
921 if ((int)$size == 0) {
925 $tempSQLStr .= PMA_backquote($tables[$i][COL_NAMES
][$j]) . " " . $type_array[$analyses[$i][TYPES
][$j]] . "(" . $size . ")";
927 if ($j != (count($tables[$i][COL_NAMES
]) - 1)) {
931 $tempSQLStr .= ") DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation . ";";
934 * Each SQL statement is executed immediately
935 * after it is formed so that we don't have
936 * to store them in a (possibly large) buffer
938 PMA_importRunQuery($tempSQLStr, $tempSQLStr);
943 * Create the SQL statements to insert all the data
945 * Only one insert query is formed for each table
949 $num_tables = count($tables);
950 for ($i = 0; $i < $num_tables; ++
$i) {
951 $num_cols = count($tables[$i][COL_NAMES
]);
952 $num_rows = count($tables[$i][ROWS
]);
954 $tempSQLStr = "INSERT INTO " . PMA_backquote($db_name) . '.' . PMA_backquote($tables[$i][TBL_NAME
]) . " (";
956 for ($m = 0; $m < $num_cols; ++
$m) {
957 $tempSQLStr .= PMA_backquote($tables[$i][COL_NAMES
][$m]);
959 if ($m != ($num_cols - 1)) {
964 $tempSQLStr .= ") VALUES ";
966 for ($j = 0; $j < $num_rows; ++
$j) {
969 for ($k = 0; $k < $num_cols; ++
$k) {
970 if ($analyses != null) {
971 $is_varchar = ($analyses[$i][TYPES
][$col_count] === VARCHAR
);
973 $is_varchar = !is_numeric($tables[$i][ROWS
][$j][$k]);
976 /* Don't put quotes around NULL fields */
977 if (! strcmp($tables[$i][ROWS
][$j][$k], 'NULL')) {
981 $tempSQLStr .= (($is_varchar) ?
"'" : "");
982 $tempSQLStr .= PMA_sqlAddSlashes((string)$tables[$i][ROWS
][$j][$k]);
983 $tempSQLStr .= (($is_varchar) ?
"'" : "");
985 if ($k != ($num_cols - 1)) {
989 if ($col_count == ($num_cols - 1)) {
995 /* Delete the cell after we are done with it */
996 unset($tables[$i][ROWS
][$j][$k]);
1001 if ($j != ($num_rows - 1)) {
1002 $tempSQLStr .= ",\n ";
1006 /* Delete the row after we are done with it */
1007 unset($tables[$i][ROWS
][$j]);
1013 * Each SQL statement is executed immediately
1014 * after it is formed so that we don't have
1015 * to store them in a (possibly large) buffer
1017 PMA_importRunQuery($tempSQLStr, $tempSQLStr);
1020 /* No longer needed */
1024 * A work in progress
1027 /* Add the viewable structures from $additional_sql to $tables so they are also displayed */
1029 $view_pattern = '@VIEW `[^`]+`\.`([^`]+)@';
1030 $table_pattern = '@CREATE TABLE IF NOT EXISTS `([^`]+)`@';
1031 /* Check a third pattern to make sure its not a "USE `db_name`;" statement */
1037 $additional_sql_len = count($additional_sql);
1038 for ($i = 0; $i < $additional_sql_len; ++
$i) {
1039 preg_match($view_pattern, $additional_sql[$i], $regs);
1041 if (count($regs) == 0) {
1042 preg_match($table_pattern, $additional_sql[$i], $regs);
1046 for ($n = 0; $n < $num_tables; ++
$n) {
1047 if (!strcmp($regs[1], $tables[$n][TBL_NAME
])) {
1054 $tables[] = array(TBL_NAME
=> $regs[1]);
1058 /* Reset the array */
1063 $params = array('db' => (string)$db_name);
1064 $db_url = 'db_structure.php' . PMA_generate_common_url($params);
1065 $db_ops_url = 'db_operations.php' . PMA_generate_common_url($params);
1067 $message = '<br /><br />';
1068 $message .= '<strong>' . __('The following structures have either been created or altered. Here you can:') . '</strong><br />';
1069 $message .= '<ul><li>' . __("View a structure's contents by clicking on its name") . '</li>';
1070 $message .= '<li>' . htmlspecialchars(__('Change any of its settings by clicking the corresponding "Options" link')) . '</li>';
1071 $message .= '<li>' . htmlspecialchars(__('Edit structure by following the "Structure" link')) . '</li>';
1072 $message .= sprintf('<br /><li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">' . __('Options') . '</a>)</li>',
1074 __('Go to database') . ': ' . htmlspecialchars(PMA_backquote($db_name)),
1075 htmlspecialchars($db_name),
1077 __('Edit') . ' ' . htmlspecialchars(PMA_backquote($db_name)) . ' ' . __('settings'));
1083 $num_tables = count($tables);
1084 for ($i = 0; $i < $num_tables; ++
$i)
1086 $params = array('db' => (string)$db_name, 'table' => (string)$tables[$i][TBL_NAME
]);
1087 $tbl_url = 'sql.php' . PMA_generate_common_url($params);
1088 $tbl_struct_url = 'tbl_structure.php' . PMA_generate_common_url($params);
1089 $tbl_ops_url = 'tbl_operations.php' . PMA_generate_common_url($params);
1093 if (! PMA_isView($db_name, $tables[$i][TBL_NAME
])) {
1094 $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>',
1096 __('Go to table') . ': ' . htmlspecialchars(PMA_backquote($tables[$i][TBL_NAME
])),
1097 htmlspecialchars($tables[$i][TBL_NAME
]),
1099 htmlspecialchars(PMA_backquote($tables[$i][TBL_NAME
])) . ' ' . __('structure'),
1101 __('Edit') . ' ' . htmlspecialchars(PMA_backquote($tables[$i][TBL_NAME
])) . ' ' . __('settings'));
1103 $message .= sprintf('<li><a href="%s" title="%s">%s</a></li>',
1105 __('Go to view') . ': ' . htmlspecialchars(PMA_backquote($tables[$i][TBL_NAME
])),
1106 htmlspecialchars($tables[$i][TBL_NAME
]));
1110 $message .= '</ul></ul>';
1112 global $import_notice;
1113 $import_notice = $message;