2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Library that provides common import functions that are used by import plugins
9 if (! defined('PHPMYADMIN')) {
14 * We need to know something about user
16 require_once './libraries/check_user_privileges.lib.php';
19 * We do this check, DROP DATABASE does not need to be confirmed elsewhere
21 define('PMA_CHK_DROP', 1);
24 * Check whether timeout is getting close
26 * @return boolean true if timeout is close
29 function PMA_checkTimeout()
31 global $timestamp, $maximum_time, $timeout_passed;
32 if ($maximum_time == 0) {
34 } elseif ($timeout_passed) {
36 /* 5 in next row might be too much */
37 } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
38 $timeout_passed = TRUE;
46 * Detects what compression filse uses
48 * @param string filename to check
49 * @return string MIME type of compression, none for none
52 function PMA_detectCompression($filepath)
54 $file = @fopen
($filepath, 'rb');
58 $test = fread($file, 4);
61 if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
62 return 'application/gzip';
64 if ($len >= 3 && substr($test, 0, 3) == 'BZh') {
65 return 'application/bzip2';
67 if ($len >= 4 && $test == "PK\003\004") {
68 return 'application/zip';
74 * Runs query inside import buffer. This is needed to allow displaying
75 * of last SELECT, SHOW or HANDLER results and similar nice stuff.
77 * @uses $GLOBALS['finished'] read and write
78 * @param string query to run
79 * @param string query to display, this might be commented
80 * @param bool whether to use control user for queries
83 function PMA_importRunQuery($sql = '', $full = '', $controluser = false)
85 global $import_run_buffer, $go_sql, $complete_query, $display_query,
86 $sql_query, $my_die, $error, $reload,
87 $last_query_with_results,
88 $skip_queries, $executed_queries, $max_sql_len, $read_multiply,
89 $cfg, $sql_query_disabled, $db, $run_query, $is_superuser;
91 if (isset($import_run_buffer)) {
92 // Should we skip something?
93 if ($skip_queries > 0) {
96 if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
97 $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
98 if (!$sql_query_disabled) {
99 $sql_query .= $import_run_buffer['full'];
101 if (!$cfg['AllowUserDropDatabase']
103 && preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
104 $GLOBALS['message'] = PMA_Message
::error('strNoDropDatabases');
108 if ($run_query && $GLOBALS['finished'] && empty($sql) && !$error && (
109 (!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
110 ($executed_queries == 1)
113 if (!$sql_query_disabled) {
114 $complete_query = $sql_query;
115 $display_query = $sql_query;
117 $complete_query = '';
120 $sql_query = $import_run_buffer['sql'];
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']) {
135 $msg .= $GLOBALS['strError'];
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 .= $GLOBALS['strRows'] . ': ' . $a_num_rows;
147 $last_query_with_results = $import_run_buffer['sql'];
148 } elseif ($a_aff_rows > 0) {
149 $msg .= sprintf($GLOBALS['strRowsAffected'], $a_aff_rows);
151 $msg .= $GLOBALS['strEmptyResultSet'];
154 if (!$sql_query_disabled) {
155 $sql_query .= $msg . "\n";
158 // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
159 if ($result != FALSE && preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $import_run_buffer['sql'], $match)) {
160 $db = trim($match[1]);
161 $db = trim($db,';'); // for example, USE abc;
165 if ($result != FALSE && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
169 } // end if not DROP DATABASE
170 } // end non empty query
171 elseif (!empty($import_run_buffer['full'])) {
173 $complete_query .= $import_run_buffer['full'];
174 $display_query .= $import_run_buffer['full'];
176 if (!$sql_query_disabled) {
177 $sql_query .= $import_run_buffer['full'];
181 // check length of query unless we decided to pass it to sql.php
182 // (if $run_query is false, we are just displaying so show
183 // the complete query in the textarea)
184 if (! $go_sql && $run_query) {
185 if ($cfg['VerboseMultiSubmit'] && ! empty($sql_query)) {
186 if (strlen($sql_query) > 50000 ||
$executed_queries > 50 ||
$max_sql_len > 1000) {
188 $sql_query_disabled = TRUE;
191 if (strlen($sql_query) > 10000 ||
$executed_queries > 10 ||
$max_sql_len > 500) {
193 $sql_query_disabled = TRUE;
197 } // end do query (no skip)
198 } // end buffer exists
200 // Do we have something to push into buffer?
201 if (!empty($sql) ||
!empty($full)) {
202 $import_run_buffer = array('sql' => $sql, 'full' => $full);
204 unset($GLOBALS['import_run_buffer']);
210 * Returns next part of imported file/buffer
212 * @uses $GLOBALS['offset'] read and write
213 * @uses $GLOBALS['import_file'] read only
214 * @uses $GLOBALS['import_text'] read and write
215 * @uses $GLOBALS['finished'] read and write
216 * @uses $GLOBALS['read_limit'] read only
217 * @param integer size of buffer to read (this is maximal size
218 * function will return)
219 * @return string part of file/buffer
222 function PMA_importGetNextChunk($size = 32768)
224 global $compression, $import_handle, $charset_conversion, $charset_of_file,
225 $charset, $read_multiply;
227 // Add some progression while reading large amount of data
228 if ($read_multiply <= 8) {
229 $size *= $read_multiply;
235 // We can not read too much
236 if ($size > $GLOBALS['read_limit']) {
237 $size = $GLOBALS['read_limit'];
240 if (PMA_checkTimeout()) {
243 if ($GLOBALS['finished']) {
247 if ($GLOBALS['import_file'] == 'none') {
248 // Well this is not yet supported and tested, but should return content of textarea
249 if (strlen($GLOBALS['import_text']) < $size) {
250 $GLOBALS['finished'] = TRUE;
251 return $GLOBALS['import_text'];
253 $r = substr($GLOBALS['import_text'], 0, $size);
254 $GLOBALS['offset'] +
= $size;
255 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
260 switch ($compression) {
261 case 'application/bzip2':
262 $result = bzread($import_handle, $size);
263 $GLOBALS['finished'] = feof($import_handle);
265 case 'application/gzip':
266 $result = gzread($import_handle, $size);
267 $GLOBALS['finished'] = feof($import_handle);
269 case 'application/zip':
270 $result = substr($GLOBALS['import_text'], 0, $size);
271 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
272 $GLOBALS['finished'] = empty($GLOBALS['import_text']);
275 $result = fread($import_handle, $size);
276 $GLOBALS['finished'] = feof($import_handle);
279 $GLOBALS['offset'] +
= $size;
281 if ($charset_conversion) {
282 return PMA_convert_string($charset_of_file, $charset, $result);
285 * Skip possible byte order marks (I do not think we need more
286 * charsets, but feel free to add more, you can use wikipedia for
287 * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
289 * @todo BOM could be used for charset autodetection
291 if ($GLOBALS['offset'] == $size) {
293 if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
294 $result = substr($result, 3);
296 } elseif (strncmp($result, "\xFE\xFF", 2) == 0 ||
strncmp($result, "\xFF\xFE", 2) == 0) {
297 $result = substr($result, 2);
305 * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)
306 * This algorithm only works up to ZZ. it fails on AAA (up to 701 columns)
308 * @author Derek Schaefer (derek.schaefer@gmail.com)
314 * @return string The column's "Excel" name
316 function PMA_getColumnAlphaName($num)
318 /* ASCII value for capital "A" */
323 /* This algorithm only works up to ZZ. it fails on AAA */
327 } elseif ($num <= 26) {
329 $sCol = chr(($A +
26) - 1);
331 $sCol = chr(($A +
$num) - 1);
334 $iRemain = (($num / 26)) - 1;
335 if (($num %
26) == 0) {
336 $sCol = PMA_getColumnAlphaName($iRemain) . PMA_getColumnAlphaName($num %
26);
338 $sCol = chr($A +
$iRemain) . PMA_getColumnAlphaName($num %
26);
346 * Returns the column number based on the Excel name.
347 * So "A" = 1, "AZ" = 27, etc.
349 * @author Derek Schaefer (derek.schaefer@gmail.com)
357 * @param string $name (i.e. "A", or "BC", etc.)
358 * @return int The column number
360 function PMA_getColumnNumberFromName($name) {
361 if (strlen($name) != 0) {
362 $name = strtoupper($name);
363 $num_chars = count($name);
365 for ($i = 0; $i < $num_chars; ++
$i) {
366 $number +
= (ord($name[$i]) - 64);
375 * Constants definitions
378 /* MySQL type defs */
380 define("VARCHAR", 1);
382 define("DECIMAL", 3);
384 /* Decimal size defs */
389 /* Table array defs */
390 define("TBL_NAME", 0);
391 define("COL_NAMES", 1);
394 /* Analysis array defs */
399 * Obtains the precision (total # of digits) from a size of type decimal
401 * @author Derek Schaefer (derek.schaefer@gmail.com)
407 * @param string $last_cumulative_size
408 * @return int Precision of the given decimal size notation
410 function PMA_getM($last_cumulative_size) {
411 return (int)substr($last_cumulative_size, 0, strpos($last_cumulative_size, ","));
415 * Obtains the scale (# of digits to the right of the decimal point) from a size of type decimal
417 * @author Derek Schaefer (derek.schaefer@gmail.com)
424 * @param string $last_cumulative_size
425 * @return int Scale of the given decimal size notation
427 function PMA_getD($last_cumulative_size) {
428 return (int)substr($last_cumulative_size, (strpos($last_cumulative_size, ",") +
1), (strlen($last_cumulative_size) - strpos($last_cumulative_size, ",")));
432 * Obtains the decimal size of a given cell
434 * @author Derek Schaefer (derek.schaefer@gmail.com)
440 * @param string &$cell
441 * @return array Contains the precision, scale, and full size representation of the given decimal cell
443 function PMA_getDecimalSize(&$cell) {
444 $curr_size = strlen((string)$cell);
445 $decPos = strpos($cell, ".");
446 $decPrecision = ($curr_size - 1) - $decPos;
451 return array($m, $d, ($m . "," . $d));
455 * Obtains the size of the given cell
457 * @author Derek Schaefer (derek.schaefer@gmail.com)
459 * @todo Handle the error cases more elegantly
474 * @uses PMA_getDecimalSize()
475 * @param string $last_cumulative_size Last cumulative column size
476 * @param int $last_cumulative_type Last cumulative column type (NONE or VARCHAR or DECIMAL or INT)
477 * @param int $curr_type Type of the current cell (NONE or VARCHAR or DECIMAL or INT)
478 * @param string &$cell The current cell
479 * @return string Size of the given cell in the type-appropriate format
481 function PMA_detectSize($last_cumulative_size, $last_cumulative_type, $curr_type, &$cell) {
482 $curr_size = strlen((string)$cell);
485 * If the cell is NULL, don't treat it as a varchar
487 if (! strcmp('NULL', $cell)) {
488 return $last_cumulative_size;
491 * What to do if the current cell is of type VARCHAR
493 elseif ($curr_type == VARCHAR
) {
495 * The last cumlative type was VARCHAR
497 if ($last_cumulative_type == VARCHAR
) {
498 if ($curr_size >= $last_cumulative_size) {
501 return $last_cumulative_size;
505 * The last cumlative type was DECIMAL
507 elseif ($last_cumulative_type == DECIMAL
) {
508 $oldM = PMA_getM($last_cumulative_size);
510 if ($curr_size >= $oldM) {
517 * The last cumlative type was INT
519 elseif ($last_cumulative_type == INT) {
520 if ($curr_size >= $last_cumulative_size) {
523 return $last_cumulative_size;
527 * This is the first row to be analyzed
529 elseif (! isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
533 * An error has DEFINITELY occurred
537 * TODO: Handle this MUCH more elegantly
544 * What to do if the current cell is of type DECIMAL
546 elseif ($curr_type == DECIMAL
) {
548 * The last cumlative type was VARCHAR
550 if ($last_cumulative_type == VARCHAR
) {
551 /* Convert $last_cumulative_size from varchar to decimal format */
552 $size = PMA_getDecimalSize($cell);
554 if ($size[M
] >= $last_cumulative_size) {
557 return $last_cumulative_size;
561 * The last cumlative type was DECIMAL
563 elseif ($last_cumulative_type == DECIMAL
) {
564 $size = PMA_getDecimalSize($cell);
566 $oldM = PMA_getM($last_cumulative_size);
567 $oldD = PMA_getD($last_cumulative_size);
569 /* New val if M or D is greater than current largest */
570 if ($size[M
] > $oldM ||
$size[D
] > $oldD) {
571 /* Take the largest of both types */
572 return (string)((($size[M
] > $oldM) ?
$size[M
] : $oldM) . "," . (($size[D
] > $oldD) ?
$size[D
] : $oldD));
574 return $last_cumulative_size;
578 * The last cumlative type was INT
580 elseif ($last_cumulative_type == INT) {
581 /* Convert $last_cumulative_size from int to decimal format */
582 $size = PMA_getDecimalSize($cell);
584 if ($size[M
] >= $last_cumulative_size) {
587 return ($last_cumulative_size.",".$size[D
]);
591 * This is the first row to be analyzed
593 elseif (! isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
594 /* First row of the column */
595 $size = PMA_getDecimalSize($cell);
600 * An error has DEFINITELY occurred
604 * TODO: Handle this MUCH more elegantly
611 * What to do if the current cell is of type INT
613 elseif ($curr_type == INT) {
615 * The last cumlative type was VARCHAR
617 if ($last_cumulative_type == VARCHAR
) {
618 if ($curr_size >= $last_cumulative_size) {
621 return $last_cumulative_size;
625 * The last cumlative type was DECIMAL
627 elseif ($last_cumulative_type == DECIMAL
) {
628 $oldM = PMA_getM($last_cumulative_size);
629 $oldD = PMA_getD($last_cumulative_size);
630 $oldInt = $oldM - $oldD;
631 $newInt = strlen((string)$cell);
633 /* See which has the larger integer length */
634 if ($oldInt >= $newInt) {
635 /* Use old decimal size */
636 return $last_cumulative_size;
638 /* Use $newInt + $oldD as new M */
639 return (($newInt +
$oldD) . "," . $oldD);
643 * The last cumlative type was INT
645 elseif ($last_cumulative_type == INT) {
646 if ($curr_size >= $last_cumulative_size) {
649 return $last_cumulative_size;
653 * This is the first row to be analyzed
655 elseif (!isset($last_cumulative_type) ||
$last_cumulative_type == NONE
) {
659 * An error has DEFINITELY occurred
663 * TODO: Handle this MUCH more elegantly
670 * An error has DEFINITELY occurred
674 * TODO: Handle this MUCH more elegantly
682 * Determines what MySQL type a cell is
684 * @author Derek Schaefer (derek.schaefer@gmail.com)
695 * @uses substr_count()
696 * @param int $last_cumulative_type Last cumulative column type (VARCHAR or INT 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 DECIMAL or NONE)
700 function PMA_detectType($last_cumulative_type, &$cell) {
702 * If numeric, determine if decimal or int
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) {
724 * Determines if the column types are int, decimal, or string
726 * @author Derek Schaefer (derek.schaefer@gmail.com)
728 * @link http://wiki.phpmyadmin.net/pma/Devel:Import
730 * @todo Handle the error case more elegantly
743 * @uses PMA_detectType()
744 * @uses PMA_detectSize()
745 * @param &$table array(string $table_name, array $col_names, array $rows)
746 * @return array array(array $types, array $sizes)
748 function PMA_analyzeTable(&$table) {
749 /* Get number of rows in table */
750 $numRows = count($table[ROWS
]);
751 /* Get number of columns */
752 $numCols = count($table[COL_NAMES
]);
753 /* Current type for each column */
757 /* Initialize $sizes to all 0's */
758 for ($i = 0; $i < $numCols; ++
$i) {
762 /* Initialize $types to NONE */
763 for ($i = 0; $i < $numCols; ++
$i) {
771 /* If the passed array is not of the correct form, do not process it */
772 if (is_array($table) && ! is_array($table[TBL_NAME
]) && is_array($table[COL_NAMES
]) && is_array($table[ROWS
])) {
773 /* Analyze each column */
774 for ($i = 0; $i < $numCols; ++
$i) {
775 /* Analyze the column in each row */
776 for ($j = 0; $j < $numRows; ++
$j) {
777 /* Determine type of the current cell */
778 $curr_type = PMA_detectType($types[$i], $table[ROWS
][$j][$i]);
779 /* Determine size of the current cell */
780 $sizes[$i] = PMA_detectSize($sizes[$i], $types[$i], $curr_type, $table[ROWS
][$j][$i]);
783 * If a type for this column has alreday been delcared,
784 * only alter it if it was a number and a varchar was found
786 if ($curr_type != NONE
) {
787 if ($curr_type == VARCHAR
) {
788 $types[$i] = VARCHAR
;
789 } else if ($curr_type == DECIMAL
) {
790 if ($types[$i] != VARCHAR
) {
791 $types[$i] = DECIMAL
;
793 } else if ($curr_type == INT) {
794 if ($types[$i] != VARCHAR
&& $types[$i] != DECIMAL
) {
802 /* Check to ensure that all types are valid */
803 $len = count($types);
804 for ($n = 0; $n < $len; ++
$n) {
805 if (! strcmp(NONE
, $types[$n])) {
806 $types[$n] = VARCHAR
;
811 return array($types, $sizes);
816 * TODO: Handle this better
823 /* Needed to quell the beast that is PMA_Message */
824 $import_notice = NULL;
827 * Builds and executes SQL statements to create the database and tables
828 * as necessary, as well as insert all the data.
830 * @author Derek Schaefer (derek.schaefer@gmail.com)
832 * @link http://wiki.phpmyadmin.net/pma/Devel:Import
844 * @uses ereg_replace()
846 * @uses PMA_backquote()
847 * @uses PMA_importRunQuery()
848 * @uses PMA_generate_common_url()
849 * @uses PMA_Message::notice()
850 * @param string $db_name Name of the database
851 * @param array &$tables Array of tables for the specified database
852 * @param array &$analyses = NULL Analyses of the tables
853 * @param array &$additional_sql = NULL Additional SQL statements to be executed
854 * @param array $options = NULL Associative array of options
857 function PMA_buildSQL($db_name, &$tables, &$analyses = NULL, &$additional_sql = NULL, $options = NULL) {
858 /* Take care of the options */
859 if (isset($options['db_collation'])) {
860 $collation = $options['db_collation'];
862 $collation = "utf8_general_ci";
865 if (isset($options['db_charset'])) {
866 $charset = $options['db_charset'];
871 if (isset($options['create_db'])) {
872 $create_db = $options['create_db'];
877 /* Create SQL code to handle the database */
881 $sql[] = "CREATE DATABASE IF NOT EXISTS " . PMA_backquote($db_name) . " DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation;
885 * The calling plug-in should include this statement, if necessary, in the $additional_sql parameter
887 * $sql[] = "USE " . PMA_backquote($db_name);
890 /* Execute the SQL statements create above */
891 $sql_len = count($sql);
892 for ($i = 0; $i < $sql_len; ++
$i) {
893 PMA_importRunQuery($sql[$i], $sql[$i]);
896 /* No longer needed */
899 /* Run the $additional_sql statements supplied by the caller plug-in */
900 if ($additional_sql != NULL) {
901 /* Clean the SQL first */
902 $additional_sql_len = count($additional_sql);
905 * Only match tables for now, because CREATE IF NOT EXISTS
906 * syntax is lacking or nonexisting for views, triggers,
907 * functions, and procedures.
909 * See: http://bugs.mysql.com/bug.php?id=15287
911 * To the best of my knowledge this is still an issue.
913 * $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)';
915 $pattern = 'CREATE .*(TABLE)';
916 $replacement = 'CREATE \\1 IF NOT EXISTS';
918 /* Change CREATE statements to CREATE IF NOT EXISTS to support inserting into existing structures */
919 for ($i = 0; $i < $additional_sql_len; ++
$i) {
920 $additional_sql[$i] = ereg_replace($pattern, $replacement, $additional_sql[$i]);
921 /* Execute the resulting statements */
922 PMA_importRunQuery($additional_sql[$i], $additional_sql[$i]);
926 if ($analyses != NULL) {
927 $type_array = array(NONE
=> "NULL", VARCHAR
=> "varchar", INT => "int", DECIMAL
=> "decimal");
929 /* TODO: Do more checking here to make sure they really are matched */
930 if (count($tables) != count($analyses)) {
934 /* Create SQL code to create the tables */
936 $num_tables = count($tables);
937 for ($i = 0; $i < $num_tables; ++
$i) {
938 $num_cols = count($tables[$i][COL_NAMES
]);
939 $tempSQLStr = "CREATE TABLE IF NOT EXISTS " . PMA_backquote($db_name) . '.' . PMA_backquote($tables[$i][TBL_NAME
]) . " (";
940 for ($j = 0; $j < $num_cols; ++
$j) {
941 $size = $analyses[$i][SIZES
][$j];
942 if ((int)$size == 0) {
946 $tempSQLStr .= PMA_backquote($tables[$i][COL_NAMES
][$j]) . " " . $type_array[$analyses[$i][TYPES
][$j]] . "(" . $size . ")";
948 if ($j != (count($tables[$i][COL_NAMES
]) - 1)) {
952 $tempSQLStr .= ") ENGINE=MyISAM DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation . ";";
955 * Each SQL statement is executed immediately
956 * after it is formed so that we don't have
957 * to store them in a (possibly large) buffer
959 PMA_importRunQuery($tempSQLStr, $tempSQLStr);
964 * Create the SQL statements to insert all the data
966 * Only one insert query is formed for each table
970 $num_tables = count($tables);
971 for ($i = 0; $i < $num_tables; ++
$i) {
972 $num_cols = count($tables[$i][COL_NAMES
]);
973 $num_rows = count($tables[$i][ROWS
]);
975 $tempSQLStr = "INSERT INTO " . PMA_backquote($db_name) . '.' . PMA_backquote($tables[$i][TBL_NAME
]) . " (";
977 for ($m = 0; $m < $num_cols; ++
$m) {
978 $tempSQLStr .= PMA_backquote($tables[$i][COL_NAMES
][$m]);
980 if ($m != ($num_cols - 1)) {
985 $tempSQLStr .= ") VALUES ";
987 for ($j = 0; $j < $num_rows; ++
$j) {
990 for ($k = 0; $k < $num_cols; ++
$k) {
991 if ($analyses != NULL) {
992 $is_varchar = ($analyses[$i][TYPES
][$col_count] === VARCHAR
);
994 $is_varchar = !is_numeric($tables[$i][ROWS
][$j][$k]);
997 /* Don't put quotes around NULL fields */
998 if (! strcmp($tables[$i][ROWS
][$j][$k], 'NULL')) {
1002 $tempSQLStr .= (($is_varchar) ?
"'" : "");
1003 $tempSQLStr .= PMA_sqlAddslashes((string)$tables[$i][ROWS
][$j][$k]);
1004 $tempSQLStr .= (($is_varchar) ?
"'" : "");
1006 if ($k != ($num_cols - 1)) {
1007 $tempSQLStr .= ", ";
1010 if ($col_count == ($num_cols - 1)) {
1016 /* Delete the cell after we are done with it */
1017 unset($tables[$i][ROWS
][$j][$k]);
1022 if ($j != ($num_rows - 1)) {
1023 $tempSQLStr .= ",\n ";
1027 /* Delete the row after we are done with it */
1028 unset($tables[$i][ROWS
][$j]);
1034 * Each SQL statement is executed immediately
1035 * after it is formed so that we don't have
1036 * to store them in a (possibly large) buffer
1038 PMA_importRunQuery($tempSQLStr, $tempSQLStr);
1041 /* No longer needed */
1045 * A work in progress
1048 /* Add the viewable structures from $additional_sql to $tables so they are also displayed */
1050 $view_pattern = 'VIEW `[^`]+`\.`([^`]+)';
1051 $table_pattern = 'CREATE TABLE IF NOT EXISTS `([^`]+)`';
1052 /* Check a third pattern to make sure its not a "USE `db_name`;" statement */
1058 $additional_sql_len = count($additional_sql);
1059 for ($i = 0; $i < $additional_sql_len; ++
$i) {
1060 ereg($view_pattern, $additional_sql[$i], $regs);
1062 if (count($regs) == 0) {
1063 ereg($table_pattern, $additional_sql[$i], $regs);
1067 for ($n = 0; $n < $num_tables; ++
$n) {
1068 if (!strcmp($regs[1], $tables[$n][TBL_NAME
])) {
1075 $tables[] = array(TBL_NAME
=> $regs[1]);
1079 /* Reset the array */
1084 $params = array('db' => (string)$db_name);
1085 $db_url = 'db_structure.php' . PMA_generate_common_url($params);
1086 $db_ops_url = 'db_operations.php' . PMA_generate_common_url($params);
1088 $message = '<br /><br />';
1089 $message .= '<strong>' . $GLOBALS['strImportNoticePt1'] . '</strong><br />';
1090 $message .= '<ul><li>' . $GLOBALS['strImportNoticePt2'] . '</li>';
1091 $message .= '<li>' . $GLOBALS['strImportNoticePt3'] . '</li>';
1092 $message .= '<li>' . $GLOBALS['strImportNoticePt4'] . '</li>';
1093 $message .= sprintf('<br /><li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">' . $GLOBALS['strOptions'] . '</a>)</li>',
1095 $GLOBALS['strGoToDatabase'] . ': ' . PMA_backquote($db_name),
1098 $GLOBALS['strEdit'] . ' ' . PMA_backquote($db_name) . ' ' . $GLOBALS['strSettings']);
1104 $num_tables = count($tables);
1105 for ($i = 0; $i < $num_tables; ++
$i)
1107 $params = array('db' => (string)$db_name, 'table' => (string)$tables[$i][TBL_NAME
]);
1108 $tbl_url = 'sql.php' . PMA_generate_common_url($params);
1109 $tbl_struct_url = 'tbl_structure.php' . PMA_generate_common_url($params);
1110 $tbl_ops_url = 'tbl_operations.php' . PMA_generate_common_url($params);
1114 if (! PMA_isView($db_name, $tables[$i][TBL_NAME
])) {
1115 $message .= sprintf('<li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">' . $GLOBALS['strStructure'] . '</a>) (<a href="%s" title="%s">' . $GLOBALS['strOptions'] . '</a>)</li>',
1117 $GLOBALS['strGoToTable'] . ': ' . PMA_backquote($tables[$i][TBL_NAME
]),
1118 $tables[$i][TBL_NAME
],
1120 PMA_backquote($tables[$i][TBL_NAME
]) . ' ' . $GLOBALS['strStructureLC'],
1122 $GLOBALS['strEdit'] . ' ' . PMA_backquote($tables[$i][TBL_NAME
]) . ' ' . $GLOBALS['strSettings']);
1124 $message .= sprintf('<li><a href="%s" title="%s">%s</a></li>',
1126 $GLOBALS['strGoToView'] . ': ' . PMA_backquote($tables[$i][TBL_NAME
]),
1127 $tables[$i][TBL_NAME
]);
1131 $message .= '</ul></ul>';
1133 global $import_notice;
1134 $import_notice = $message;