[auth] Add custom port configuration in signon
[phpmyadmin/madhuracj.git] / libraries / import.lib.php
bloba1445e9a17a7dcb5e4359478f1ecc9562b4cdd9c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Library that provides common import functions that are used by import plugins
6 * @version $Id$
7 * @package phpMyAdmin
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * We need to know something about user
16 require_once './libraries/check_user_privileges.lib.php';
18 /**
19 * We do this check, DROP DATABASE does not need to be confirmed elsewhere
21 define('PMA_CHK_DROP', 1);
23 /**
24 * Check whether timeout is getting close
26 * @return boolean true if timeout is close
27 * @access public
29 function PMA_checkTimeout()
31 global $timestamp, $maximum_time, $timeout_passed;
32 if ($maximum_time == 0) {
33 return FALSE;
34 } elseif ($timeout_passed) {
35 return TRUE;
36 /* 5 in next row might be too much */
37 } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
38 $timeout_passed = TRUE;
39 return TRUE;
40 } else {
41 return FALSE;
45 /**
46 * Detects what compression filse uses
48 * @param string filename to check
49 * @return string MIME type of compression, none for none
50 * @access public
52 function PMA_detectCompression($filepath)
54 $file = @fopen($filepath, 'rb');
55 if (!$file) {
56 return FALSE;
58 $test = fread($file, 4);
59 $len = strlen($test);
60 fclose($file);
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';
70 return 'none';
73 /**
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
81 * @access public
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 $skip_queries, $executed_queries, $max_sql_len, $read_multiply,
88 $cfg, $sql_query_disabled, $db, $run_query, $is_superuser;
89 $read_multiply = 1;
90 if (isset($import_run_buffer)) {
91 // Should we skip something?
92 if ($skip_queries > 0) {
93 $skip_queries--;
94 } else {
95 if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
96 $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
97 if (!$sql_query_disabled) {
98 $sql_query .= $import_run_buffer['full'];
100 if (!$cfg['AllowUserDropDatabase']
101 && !$is_superuser
102 && preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
103 $GLOBALS['message'] = PMA_Message::error('strNoDropDatabases');
104 $error = TRUE;
105 } else {
106 $executed_queries++;
107 if ($run_query && $GLOBALS['finished'] && empty($sql) && !$error && (
108 (!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
109 ($executed_queries == 1)
110 )) {
111 $go_sql = TRUE;
112 if (!$sql_query_disabled) {
113 $complete_query = $sql_query;
114 $display_query = $sql_query;
115 } else {
116 $complete_query = '';
117 $display_query = '';
119 $sql_query = $import_run_buffer['sql'];
120 } elseif ($run_query) {
121 if ($controluser) {
122 $result = PMA_query_as_controluser($import_run_buffer['sql']);
123 } else {
124 $result = PMA_DBI_try_query($import_run_buffer['sql']);
126 $msg = '# ';
127 if ($result === FALSE) { // execution failed
128 if (!isset($my_die)) {
129 $my_die = array();
131 $my_die[] = array('sql' => $import_run_buffer['full'], 'error' => PMA_DBI_getError());
133 if ($cfg['VerboseMultiSubmit']) {
134 $msg .= $GLOBALS['strError'];
137 if (!$cfg['IgnoreMultiSubmitErrors']) {
138 $error = TRUE;
139 return;
141 } elseif ($cfg['VerboseMultiSubmit']) {
142 $a_num_rows = (int)@PMA_DBI_num_rows($result);
143 $a_aff_rows = (int)@PMA_DBI_affected_rows();
144 if ($a_num_rows > 0) {
145 $msg .= $GLOBALS['strRows'] . ': ' . $a_num_rows;
146 } elseif ($a_aff_rows > 0) {
147 $msg .= sprintf($GLOBALS['strRowsAffected'], $a_aff_rows);
148 } else {
149 $msg .= $GLOBALS['strEmptyResultSet'];
152 if (!$sql_query_disabled) {
153 $sql_query .= $msg . "\n";
156 // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
157 if ($result != FALSE && preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $import_run_buffer['sql'], $match)) {
158 $db = trim($match[1]);
159 $db = trim($db,';'); // for example, USE abc;
160 $reload = TRUE;
163 if ($result != FALSE && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
164 $reload = TRUE;
166 } // end run query
167 } // end if not DROP DATABASE
168 } // end non empty query
169 elseif (!empty($import_run_buffer['full'])) {
170 if ($go_sql) {
171 $complete_query .= $import_run_buffer['full'];
172 $display_query .= $import_run_buffer['full'];
173 } else {
174 if (!$sql_query_disabled) {
175 $sql_query .= $import_run_buffer['full'];
179 // check length of query unless we decided to pass it to sql.php
180 // (if $run_query is false, we are just displaying so show
181 // the complete query in the textarea)
182 if (! $go_sql && $run_query) {
183 if ($cfg['VerboseMultiSubmit'] && ! empty($sql_query)) {
184 if (strlen($sql_query) > 50000 || $executed_queries > 50 || $max_sql_len > 1000) {
185 $sql_query = '';
186 $sql_query_disabled = TRUE;
188 } else {
189 if (strlen($sql_query) > 10000 || $executed_queries > 10 || $max_sql_len > 500) {
190 $sql_query = '';
191 $sql_query_disabled = TRUE;
195 } // end do query (no skip)
196 } // end buffer exists
198 // Do we have something to push into buffer?
199 if (!empty($sql) || !empty($full)) {
200 $import_run_buffer = array('sql' => $sql, 'full' => $full);
201 } else {
202 unset($GLOBALS['import_run_buffer']);
208 * Returns next part of imported file/buffer
210 * @uses $GLOBALS['offset'] read and write
211 * @uses $GLOBALS['import_file'] read only
212 * @uses $GLOBALS['import_text'] read and write
213 * @uses $GLOBALS['finished'] read and write
214 * @uses $GLOBALS['read_limit'] read only
215 * @param integer size of buffer to read (this is maximal size
216 * function will return)
217 * @return string part of file/buffer
218 * @access public
220 function PMA_importGetNextChunk($size = 32768)
222 global $compression, $import_handle, $charset_conversion, $charset_of_file,
223 $charset, $read_multiply;
225 // Add some progression while reading large amount of data
226 if ($read_multiply <= 8) {
227 $size *= $read_multiply;
228 } else {
229 $size *= 8;
231 $read_multiply++;
233 // We can not read too much
234 if ($size > $GLOBALS['read_limit']) {
235 $size = $GLOBALS['read_limit'];
238 if (PMA_checkTimeout()) {
239 return FALSE;
241 if ($GLOBALS['finished']) {
242 return TRUE;
245 if ($GLOBALS['import_file'] == 'none') {
246 // Well this is not yet supported and tested, but should return content of textarea
247 if (strlen($GLOBALS['import_text']) < $size) {
248 $GLOBALS['finished'] = TRUE;
249 return $GLOBALS['import_text'];
250 } else {
251 $r = substr($GLOBALS['import_text'], 0, $size);
252 $GLOBALS['offset'] += $size;
253 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
254 return $r;
258 switch ($compression) {
259 case 'application/bzip2':
260 $result = bzread($import_handle, $size);
261 $GLOBALS['finished'] = feof($import_handle);
262 break;
263 case 'application/gzip':
264 $result = gzread($import_handle, $size);
265 $GLOBALS['finished'] = feof($import_handle);
266 break;
267 case 'application/zip':
268 $result = substr($GLOBALS['import_text'], 0, $size);
269 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
270 $GLOBALS['finished'] = empty($GLOBALS['import_text']);
271 break;
272 case 'none':
273 $result = fread($import_handle, $size);
274 $GLOBALS['finished'] = feof($import_handle);
275 break;
277 $GLOBALS['offset'] += $size;
279 if ($charset_conversion) {
280 return PMA_convert_string($charset_of_file, $charset, $result);
281 } else {
283 * Skip possible byte order marks (I do not think we need more
284 * charsets, but feel free to add more, you can use wikipedia for
285 * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
287 * @todo BOM could be used for charset autodetection
289 if ($GLOBALS['offset'] == $size) {
290 // UTF-8
291 if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
292 $result = substr($result, 3);
293 // UTF-16 BE, LE
294 } elseif (strncmp($result, "\xFE\xFF", 2) == 0 || strncmp($result, "\xFF\xFE", 2) == 0) {
295 $result = substr($result, 2);
298 return $result;
303 * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)
304 * This algorithm only works up to ZZ. it fails on AAA (up to 701 columns)
306 * @author Derek Schaefer (derek.schaefer@gmail.com)
308 * @access public
310 * @uses chr()
311 * @param int $num
312 * @return string The column's "Excel" name
314 function PMA_getColumnAlphaName($num)
316 /* ASCII value for capital "A" */
317 $A = 65;
318 $sCol = "";
319 $iRemain = 0;
321 /* This algorithm only works up to ZZ. it fails on AAA */
323 if ($num > 701) {
324 return $num;
325 } else if ($num <= 26) {
326 if ($num == 0) {
327 $sCol = chr(($A + 26) - 1);
328 } else {
329 $sCol = chr(($A + $num) - 1);
331 } else {
332 $iRemain = (($num / 26))-1;
333 if (($num % 26) == 0) {
334 $sCol = PMA_getColumnAlphaName($iRemain) . PMA_getColumnAlphaName($num % 26);
335 } else {
336 $sCol = chr($A + $iRemain) . PMA_getColumnAlphaName($num % 26);
340 return $sCol;
344 * Returns the column number based on the Excel name.
345 * So "A" = 1, "AZ" = 27, etc.
347 * @author Derek Schaefer (derek.schaefer@gmail.com)
349 * @access public
351 * @uses strtoupper()
352 * @uses strlen()
353 * @uses count()
354 * @uses ord()
355 * @param string $name (i.e. "A", or "BC", etc.)
356 * @return int The column number
358 function PMA_getColumnNumberFromName($name) {
359 if (strlen($name) != 0) {
360 $name = strtoupper($name);
361 $num_chars = count($name);
362 $number = 0;
363 for ($i = 0; $i < $num_chars; ++$i) {
364 $number += (ord($name[$i]) - 64);
366 return $number;
367 } else {
368 return 0;
373 * Constants definitions
376 /* MySQL type defs */
377 define("NONE", 0);
378 define("VARCHAR", 1);
379 define("INT", 2);
380 define("DECIMAL", 3);
382 /* Decimal size defs */
383 define("M", 0);
384 define("D", 1);
385 define("FULL", 2);
387 /* Table array defs */
388 define("TBL_NAME", 0);
389 define("COL_NAMES", 1);
390 define("ROWS", 2);
392 /* Analysis array defs */
393 define("TYPES", 0);
394 define("SIZES", 1);
397 * Obtains the precision (total # of digits) from a size of type decimal
399 * @author Derek Schaefer (derek.schaefer@gmail.com)
401 * @access public
403 * @uses substr()
404 * @uses strpos()
405 * @param string $last_cumulative_size
406 * @return int Precision of the given decimal size notation
408 function PMA_getM($last_cumulative_size) {
409 return (int)substr($last_cumulative_size, 0, strpos($last_cumulative_size, ","));
413 * Obtains the scale (# of digits to the right of the decimal point) from a size of type decimal
415 * @author Derek Schaefer (derek.schaefer@gmail.com)
417 * @access public
419 * @uses substr()
420 * @uses strpos()
421 * @uses strlen()
422 * @param string $last_cumulative_size
423 * @return int Scale of the given decimal size notation
425 function PMA_getD($last_cumulative_size) {
426 return (int)substr($last_cumulative_size, (strpos($last_cumulative_size, ",") + 1), (strlen($last_cumulative_size) - strpos($last_cumulative_size, ",")));
430 * Obtains the decimal size of a given cell
432 * @author Derek Schaefer (derek.schaefer@gmail.com)
434 * @access public
436 * @uses strlen()
437 * @uses strpos()
438 * @param string &$cell
439 * @return array Contains the precision, scale, and full size representation of the given decimal cell
441 function PMA_getDecimalSize(&$cell) {
442 $curr_size = strlen((string)$cell);
443 $decPos = strpos($cell, ".");
444 $decPrecision = ($curr_size - 1) - $decPos;
446 $m = $curr_size - 1;
447 $d = $decPrecision;
449 return array($m, $d, ($m.",".$d));
453 * Obtains the size of the given cell
455 * @author Derek Schaefer (derek.schaefer@gmail.com)
457 * @todo Handle the error cases more elegantly
459 * @access public
461 * @uses M
462 * @uses D
463 * @uses FULL
464 * @uses VARCHAR
465 * @uses DECIMAL
466 * @uses INT
467 * @uses NONE
468 * @uses strcmp()
469 * @uses strlen()
470 * @uses PMA_getM()
471 * @uses PMA_getD()
472 * @uses PMA_getDecimalSize()
473 * @param string $last_cumulative_size Last cumulative column size
474 * @param int $last_cumulative_type Last cumulative column type (NONE or VARCHAR or DECIMAL or INT)
475 * @param int $curr_type Type of the current cell (NONE or VARCHAR or DECIMAL or INT)
476 * @param string &$cell The current cell
477 * @return string Size of the given cell in the type-appropriate format
479 function PMA_detectSize($last_cumulative_size, $last_cumulative_type, $curr_type, &$cell) {
480 $curr_size = strlen((string)$cell);
483 * If the cell is NULL, don't treat it as a varchar
485 if (!strcmp('NULL', $cell)) {
486 return $last_cumulative_size;
489 * What to do if the current cell is of type VARCHAR
491 else if ($curr_type == VARCHAR) {
493 * The last cumlative type was VARCHAR
495 if ($last_cumulative_type == VARCHAR) {
496 if ($curr_size >= $last_cumulative_size) {
497 return $curr_size;
498 } else {
499 return $last_cumulative_size;
503 * The last cumlative type was DECIMAL
505 else if ($last_cumulative_type == DECIMAL) {
506 $oldM = PMA_getM($last_cumulative_size);
508 if ($curr_size >= $oldM) {
509 return $curr_size;
510 } else {
511 return $oldM;
515 * The last cumlative type was INT
517 else if ($last_cumulative_type == INT) {
518 if ($curr_size >= $last_cumulative_size) {
519 return $curr_size;
520 } else {
521 return $last_cumulative_size;
525 * This is the first row to be analyzed
527 else if (!isset($last_cumulative_type) || $last_cumulative_type == NONE) {
528 return $curr_size;
531 * An error has DEFINITELY occurred
533 else {
535 * TODO: Handle this MUCH more elegantly
538 return -1;
542 * What to do if the current cell is of type DECIMAL
544 else if ($curr_type == DECIMAL) {
546 * The last cumlative type was VARCHAR
548 if ($last_cumulative_type == VARCHAR) {
549 /* Convert $last_cumulative_size from varchar to decimal format */
550 $size = PMA_getDecimalSize($cell);
552 if ($size[M] >= $last_cumulative_size) {
553 return $size[M];
554 } else {
555 return $last_cumulative_size;
559 * The last cumlative type was DECIMAL
561 else if ($last_cumulative_type == DECIMAL) {
562 $size = PMA_getDecimalSize($cell);
564 $oldM = PMA_getM($last_cumulative_size);
565 $oldD = PMA_getD($last_cumulative_size);
567 /* New val if M or D is greater than current largest */
568 if ($size[M] > $oldM || $size[D] > $oldD) {
569 /* Take the largest of both types */
570 return (string)((($size[M] > $oldM) ? $size[M] : $oldM) . "," . (($size[D] > $oldD) ? $size[D] : $oldD));
571 } else {
572 return $last_cumulative_size;
576 * The last cumlative type was INT
578 else if ($last_cumulative_type == INT) {
579 /* Convert $last_cumulative_size from int to decimal format */
580 $size = PMA_getDecimalSize($cell);
582 if ($size[M] >= $last_cumulative_size) {
583 return $size[FULL];
584 } else {
585 return ($last_cumulative_size.",".$size[D]);
589 * This is the first row to be analyzed
591 else if (!isset($last_cumulative_type) || $last_cumulative_type == NONE) {
592 /* First row of the column */
593 $size = PMA_getDecimalSize($cell);
595 return $size[FULL];
598 * An error has DEFINITELY occurred
600 else {
602 * TODO: Handle this MUCH more elegantly
605 return -1;
609 * What to do if the current cell is of type INT
611 else if ($curr_type == INT) {
613 * The last cumlative type was VARCHAR
615 if ($last_cumulative_type == VARCHAR) {
616 if ($curr_size >= $last_cumulative_size) {
617 return $curr_size;
618 } else {
619 return $last_cumulative_size;
623 * The last cumlative type was DECIMAL
625 else if ($last_cumulative_type == DECIMAL) {
626 $oldM = PMA_getM($last_cumulative_size);
627 $oldD = PMA_getD($last_cumulative_size);
628 $oldInt = $oldM - $oldD;
629 $newInt = strlen((string)$cell);
631 /* See which has the larger integer length */
632 if ($oldInt >= $newInt) {
633 /* Use old decimal size */
634 return $last_cumulative_size;
635 } else {
636 /* Use $newInt + $oldD as new M */
637 return (($newInt + $oldD).",".$oldD);
641 * The last cumlative type was INT
643 else if ($last_cumulative_type == INT) {
644 if ($curr_size >= $last_cumulative_size) {
645 return $curr_size;
646 } else {
647 return $last_cumulative_size;
651 * This is the first row to be analyzed
653 else if (!isset($last_cumulative_type) || $last_cumulative_type == NONE) {
654 return $curr_size;
657 * An error has DEFINITELY occurred
659 else {
661 * TODO: Handle this MUCH more elegantly
664 return -1;
668 * An error has DEFINITELY occurred
670 else {
672 * TODO: Handle this MUCH more elegantly
675 return -1;
680 * Determines what MySQL type a cell is
682 * @author Derek Schaefer (derek.schaefer@gmail.com)
684 * @access public
686 * @uses DECIMAL
687 * @uses INT
688 * @uses VARCHAR
689 * @uses NONE
690 * @uses is_numeric()
691 * @uses strcmp()
692 * @uses strpos()
693 * @uses substr_count()
694 * @param int $last_cumulative_type Last cumulative column type (VARCHAR or INT or DECIMAL or NONE)
695 * @param string &$cell String representation of the cell for which a best-fit type is to be determined
696 * @return int The MySQL type representation (VARCHAR or INT or DECIMAL or NONE)
698 function PMA_detectType($last_cumulative_type, &$cell) {
700 * If numeric, determine if decimal or int
701 * Else, we call it varchar for simplicity
704 if (!strcmp('NULL', $cell)) {
705 if ($last_cumulative_type === NULL || $last_cumulative_type == NONE) {
706 return NONE;
707 } else {
708 return $last_cumulative_type;
710 } else if (is_numeric($cell)) {
711 if ($cell == (string)(float)$cell && strpos($cell, ".") !== false && substr_count($cell, ".") == 1) {
712 return DECIMAL;
713 } else {
714 return INT;
716 } else {
717 return VARCHAR;
722 * Determines if the column types are int, decimal, or string
724 * @author Derek Schaefer (derek.schaefer@gmail.com)
726 * @link http://wiki.phpmyadmin.net/pma/Devel:Import
728 * @todo Handle the error case more elegantly
730 * @access public
732 * @uses TBL_NAME
733 * @uses COL_NAMES
734 * @uses ROWS
735 * @uses VARCHAR
736 * @uses DECIMAL
737 * @uses INT
738 * @uses NONE
739 * @uses count()
740 * @uses is_array()
741 * @uses PMA_detectType()
742 * @uses PMA_detectSize()
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 */
752 $types = array();
753 $sizes = array();
755 /* Initialize $sizes to all 0's */
756 for ($i = 0; $i < $numCols; ++$i) {
757 $sizes[$i] = 0;
760 /* Initialize $types to NONE */
761 for ($i = 0; $i < $numCols; ++$i) {
762 $types[$i] = NONE;
765 /* Temp vars */
766 $curr_type = NONE;
767 $curr_size = 0;
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 alreday been delcared,
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 == INT) {
792 if ($types[$i] != VARCHAR && $types[$i] != DECIMAL) {
793 $types[$i] = INT;
800 /* Check to ensure that all types are valid */
801 $len = count($types);
802 for ($n = 0; $n < $len; ++$n) {
803 if (!strcmp(NONE, $types[$n])) {
804 $types[$n] = VARCHAR;
805 $sizes[$n] = '10';
809 return array($types, $sizes);
811 else
814 * TODO: Handle this better
817 return false;
821 /* Needed to quell the beast that is PMA_Message */
822 $import_notice = NULL;
825 * Builds and executes SQL statements to create the database and tables
826 * as necessary, as well as insert all the data.
828 * @author Derek Schaefer (derek.schaefer@gmail.com)
830 * @link http://wiki.phpmyadmin.net/pma/Devel:Import
832 * @access public
834 * @uses TBL_NAME
835 * @uses COL_NAMES
836 * @uses ROWS
837 * @uses TYPES
838 * @uses SIZES
839 * @uses strcmp()
840 * @uses count()
841 * @uses ereg()
842 * @uses ereg_replace()
843 * @uses PMA_isView()
844 * @uses PMA_backquote()
845 * @uses PMA_importRunQuery()
846 * @uses PMA_generate_common_url()
847 * @uses PMA_Message::notice()
848 * @param string $db_name Name of the database
849 * @param array &$tables Array of tables for the specified database
850 * @param array &$analyses = NULL Analyses of the tables
851 * @param array &$additional_sql = NULL Additional SQL statements to be executed
852 * @param array $options = NULL Associative array of options
853 * @return void
855 function PMA_buildSQL($db_name, &$tables, &$analyses = NULL, &$additional_sql = NULL, $options = NULL) {
856 /* Take care of the options */
857 if (isset($options['db_collation'])) {
858 $collation = $options['db_collation'];
859 } else {
860 $collation = "utf8_general_ci";
863 if (isset($options['db_charset'])) {
864 $charset = $options['db_charset'];
865 } else {
866 $charset = "utf8";
869 if (isset($options['create_db'])) {
870 $create_db = $options['create_db'];
871 } else {
872 $create_db = true;
875 /* Create SQL code to handle the database */
876 $sql = array();
878 if ($create_db) {
879 $sql[] = "CREATE DATABASE IF NOT EXISTS " . PMA_backquote($db_name) . " DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation;
883 * The calling plug-in should include this statement, if necessary, in the $additional_sql parameter
885 * $sql[] = "USE " . PMA_backquote($db_name);
888 /* Execute the SQL statements create above */
889 $sql_len = count($sql);
890 for ($i = 0; $i < $sql_len; ++$i) {
891 PMA_importRunQuery($sql[$i], $sql[$i]);
894 /* No longer needed */
895 unset($sql);
897 /* Run the $additional_sql statements supplied by the caller plug-in */
898 if ($additional_sql != NULL) {
899 /* Clean the SQL first */
900 $additional_sql_len = count($additional_sql);
903 * Only match tables for now, because CREATE IF NOT EXISTS
904 * syntax is lacking or nonexisting for views, triggers,
905 * functions, and procedures.
907 * See: http://bugs.mysql.com/bug.php?id=15287
909 * To the best of my knowledge this is still an issue.
911 * $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)';
913 $pattern = 'CREATE .*(TABLE)';
914 $replacement = 'CREATE \\1 IF NOT EXISTS';
916 /* Change CREATE statements to CREATE IF NOT EXISTS to support inserting into existing structures */
917 for ($i = 0; $i < $additional_sql_len; ++$i) {
918 $additional_sql[$i] = ereg_replace($pattern, $replacement, $additional_sql[$i]);
919 /* Execute the resulting statements */
920 PMA_importRunQuery($additional_sql[$i], $additional_sql[$i]);
924 if ($analyses != NULL) {
925 $type_array = array(NONE => "NULL", VARCHAR => "varchar", INT => "int", DECIMAL => "decimal");
927 /* TODO: Do more checking here to make sure they really are matched */
928 if (count($tables) != count($analyses)) {
929 exit();
932 /* Create SQL code to create the tables */
933 $tempSQLStr = "";
934 $num_tables = count($tables);
935 for ($i = 0; $i < $num_tables; ++$i) {
936 $num_cols = count($tables[$i][COL_NAMES]);
937 $tempSQLStr = "CREATE TABLE IF NOT EXISTS " . PMA_backquote($db_name) . '.' . PMA_backquote($tables[$i][TBL_NAME]) . " (";
938 for ($j = 0; $j < $num_cols; ++$j) {
939 $size = $analyses[$i][SIZES][$j];
940 if ((int)$size == 0) {
941 $size = 10;
944 $tempSQLStr .= PMA_backquote($tables[$i][COL_NAMES][$j]) . " " . $type_array[$analyses[$i][TYPES][$j]] . "(" . $size . ")";
946 if ($j != (count($tables[$i][COL_NAMES]) - 1)) {
947 $tempSQLStr .= ", ";
950 $tempSQLStr .= ") ENGINE=MyISAM;";
953 * Each SQL statement is executed immediately
954 * after it is formed so that we don't have
955 * to store them in a (possibly large) buffer
957 PMA_importRunQuery($tempSQLStr, $tempSQLStr);
962 * Create the SQL statements to insert all the data
964 * Only one insert query is formed for each table
966 $tempSQLStr = "";
967 $col_count = 0;
968 $num_tables = count($tables);
969 for ($i = 0; $i < $num_tables; ++$i) {
970 $num_cols = count($tables[$i][COL_NAMES]);
971 $num_rows = count($tables[$i][ROWS]);
973 $tempSQLStr = "INSERT INTO " . PMA_backquote($db_name) . '.' . PMA_backquote($tables[$i][TBL_NAME]) . " (";
975 for ($m = 0; $m < $num_cols; ++$m) {
976 $tempSQLStr .= PMA_backquote($tables[$i][COL_NAMES][$m]);
978 if ($m != ($num_cols - 1)) {
979 $tempSQLStr .= ", ";
983 $tempSQLStr .= ") VALUES ";
985 for ($j = 0; $j < $num_rows; ++$j) {
986 $tempSQLStr .= "(";
988 for ($k = 0; $k < $num_cols; ++$k) {
989 if ($analyses != NULL) {
990 $is_varchar = ($analyses[$i][TYPES][$col_count] === VARCHAR);
991 } else {
992 $is_varchar = !is_numeric($tables[$i][ROWS][$j][$k]);
995 /* Don't put quotes around NULL fields */
996 if (!strcmp($tables[$i][ROWS][$j][$k], 'NULL')) {
997 $is_varchar = false;
1000 $tempSQLStr .= (($is_varchar) ? "'" : "");
1001 $tempSQLStr .= (str_replace("'", "\'", (string)$tables[$i][ROWS][$j][$k]));
1002 $tempSQLStr .= (($is_varchar) ? "'" : "");
1004 if ($k != ($num_cols - 1)) {
1005 $tempSQLStr .= ", ";
1008 if ($col_count == ($num_cols - 1)) {
1009 $col_count = 0;
1010 } else {
1011 $col_count++;
1014 /* Delete the cell after we are done with it */
1015 unset($tables[$i][ROWS][$j][$k]);
1018 $tempSQLStr .= ")";
1020 if ($j != ($num_rows - 1)) {
1021 $tempSQLStr .= ",\n ";
1024 $col_count = 0;
1025 /* Delete the row after we are done with it */
1026 unset($tables[$i][ROWS][$j]);
1029 $tempSQLStr .= ";";
1032 * Each SQL statement is executed immediately
1033 * after it is formed so that we don't have
1034 * to store them in a (possibly large) buffer
1036 PMA_importRunQuery($tempSQLStr, $tempSQLStr);
1039 /* No longer needed */
1040 unset($tempSQLStr);
1043 * A work in progress
1046 /* Add the viewable structures from $additional_sql to $tables so they are also displayed */
1048 $view_pattern = 'VIEW `[^`]+`\.`([^`]+)';
1049 $table_pattern = 'CREATE TABLE IF NOT EXISTS `([^`]+)`';
1050 /* Check a third pattern to make sure its not a "USE `db_name`;" statement */
1052 $regs = array();
1054 $inTables = false;
1056 $additional_sql_len = count($additional_sql);
1057 for ($i = 0; $i < $additional_sql_len; ++$i) {
1058 ereg($view_pattern, $additional_sql[$i], $regs);
1060 if (count($regs) == 0) {
1061 ereg($table_pattern, $additional_sql[$i], $regs);
1064 if (count($regs)) {
1065 for ($n = 0; $n < $num_tables; ++$n) {
1066 if (!strcmp($regs[1], $tables[$n][TBL_NAME])) {
1067 $inTables = true;
1068 break;
1072 if (!$inTables) {
1073 $tables[] = array(TBL_NAME => $regs[1]);
1077 /* Reset the array */
1078 $regs = array();
1079 $inTables = false;
1082 $params = array('db' => (string)$db_name);
1083 $db_url = 'db_structure.php' . PMA_generate_common_url($params);
1084 $db_ops_url = 'db_operations.php' . PMA_generate_common_url($params);
1086 $message = '<br /><br />';
1087 $message .= '<strong>'.$GLOBALS['strImportNoticePt1'].'</strong><br />';
1088 $message .= '<ul><li>'.$GLOBALS['strImportNoticePt2'].'</li>';
1089 $message .= '<li>'.$GLOBALS['strImportNoticePt3'].'</li>';
1090 $message .= '<li>'.$GLOBALS['strImportNoticePt4'].'</li>';
1091 $message .= sprintf('<br /><li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">'.$GLOBALS['strOptions'].'</a>)</li>',
1092 $db_url,
1093 $GLOBALS['strGoToDatabase'].': '.PMA_backquote($db_name),
1094 $db_name,
1095 $db_ops_url,
1096 $GLOBALS['strEdit'].' '.PMA_backquote($db_name).' '.$GLOBALS['strSettings']);
1098 $message .= '<ul>';
1100 unset($params);
1102 $num_tables = count($tables);
1103 for ($i = 0; $i < $num_tables; ++$i)
1105 $params = array('db' => (string)$db_name, 'table' => (string)$tables[$i][TBL_NAME]);
1106 $tbl_url = 'sql.php' . PMA_generate_common_url($params);
1107 $tbl_struct_url = 'tbl_structure.php' . PMA_generate_common_url($params);
1108 $tbl_ops_url = 'tbl_operations.php' . PMA_generate_common_url($params);
1110 unset($params);
1112 if (!PMA_isView($db_name, $tables[$i][TBL_NAME])) {
1113 $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>',
1114 $tbl_url,
1115 $GLOBALS['strGoToTable'].': '.PMA_backquote($tables[$i][TBL_NAME]),
1116 $tables[$i][TBL_NAME],
1117 $tbl_struct_url,
1118 PMA_backquote($tables[$i][TBL_NAME]).' '.$GLOBALS['strStructureLC'],
1119 $tbl_ops_url,
1120 $GLOBALS['strEdit'].' '.PMA_backquote($tables[$i][TBL_NAME]).' '.$GLOBALS['strSettings']);
1121 } else {
1122 $message .= sprintf('<li><a href="%s" title="%s">%s</a></li>',
1123 $tbl_url,
1124 $GLOBALS['strGoToView'].': '.PMA_backquote($tables[$i][TBL_NAME]),
1125 $tables[$i][TBL_NAME]);
1129 $message .= '</ul></ul>';
1131 global $import_notice;
1132 $import_notice = $message;
1134 unset($tables);