2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build SQL dumps of tables
12 if (isset($plugin_list)) {
14 $hide_structure = false;
15 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
16 $hide_structure = true;
20 $plugin_list['sql'] = array(
23 'mime_type' => 'text/x-sql',
25 array('type' => 'text', 'name' => 'header_comment', 'text' => 'strAddHeaderComment'),
26 array('type' => 'bool', 'name' => 'use_transaction', 'text' => 'strEncloseInTransaction'),
27 array('type' => 'bool', 'name' => 'disable_fk', 'text' => 'strDisableForeignChecks'),
29 'options_text' => 'strOptions',
31 $compats = PMA_DBI_getCompatibilities();
32 if (count($compats) > 0) {
34 foreach($compats as $val) {
37 $plugin_list['sql']['options'][] =
38 array('type' => 'select', 'name' => 'compatibility', 'text' => 'strSQLCompatibility', 'values' => $values, 'doc' => array('manual_MySQL_Database_Administration', 'Server_SQL_mode'));
42 /* Server export options */
43 if ($plugin_param['export_type'] == 'server') {
44 $plugin_list['sql']['options'][] =
45 array('type' => 'bgroup', 'text' => 'strDatabaseExportOptions');
46 $plugin_list['sql']['options'][] =
47 array('type' => 'bool', 'name' => 'drop_database', 'text' => sprintf($GLOBALS['strAddClause'], 'DROP DATABASE'));
48 $plugin_list['sql']['options'][] =
49 array('type' => 'egroup');
52 /* Structure options */
53 if (!$hide_structure) {
54 $plugin_list['sql']['options'][] =
55 array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
56 if ($plugin_param['export_type'] == 'table') {
57 if (PMA_Table
::_isView($GLOBALS['db'], $GLOBALS['table'])) {
58 $drop_clause = 'DROP VIEW';
60 $drop_clause = 'DROP TABLE';
62 } elseif (PMA_MYSQL_INT_VERSION
>= 50000) {
63 $drop_clause = 'DROP TABLE / DROP VIEW';
65 $drop_clause = 'DROP TABLE';
67 $plugin_list['sql']['options'][] =
68 array('type' => 'bool', 'name' => 'drop_table', 'text' => sprintf($GLOBALS['strAddClause'], $drop_clause));
69 $plugin_list['sql']['options'][] =
70 array('type' => 'bool', 'name' => 'if_not_exists', 'text' => sprintf($GLOBALS['strAddClause'], 'IF NOT EXISTS'));
71 $plugin_list['sql']['options'][] =
72 array('type' => 'bool', 'name' => 'auto_increment', 'text' => 'strAddAutoIncrement');
73 $plugin_list['sql']['options'][] =
74 array('type' => 'bool', 'name' => 'backquotes', 'text' => 'strUseBackquotes');
75 $plugin_list['sql']['options'][] =
76 array('type' => 'bool', 'name' => 'procedure_function', 'text' => sprintf($GLOBALS['strAddClause'], 'CREATE PROCEDURE / FUNCTION'));
79 $plugin_list['sql']['options'][] =
80 array('type' => 'bgroup', 'text' => 'strAddIntoComments');
81 $plugin_list['sql']['options'][] =
82 array('type' => 'bool', 'name' => 'dates', 'text' => 'strCreationDates');
83 if (!empty($GLOBALS['cfgRelation']['relation'])) {
84 $plugin_list['sql']['options'][] =
85 array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
87 if (!empty($GLOBALS['cfgRelation']['commwork']) && PMA_MYSQL_INT_VERSION
< 40100) {
88 $plugin_list['sql']['options'][] =
89 array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
91 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
92 $plugin_list['sql']['options'][] =
93 array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
95 $plugin_list['sql']['options'][] =
96 array('type' => 'egroup');
98 $plugin_list['sql']['options'][] =
99 array('type' => 'egroup');
103 $plugin_list['sql']['options'][] =
104 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
105 $plugin_list['sql']['options'][] =
106 array('type' => 'bool', 'name' => 'columns', 'text' => 'strCompleteInserts');
107 $plugin_list['sql']['options'][] =
108 array('type' => 'bool', 'name' => 'extended', 'text' => 'strExtendedInserts');
109 $plugin_list['sql']['options'][] =
110 array('type' => 'text', 'name' => 'max_query_size', 'text' => 'strMaximalQueryLength');
111 $plugin_list['sql']['options'][] =
112 array('type' => 'bool', 'name' => 'delayed', 'text' => 'strDelayedInserts');
113 $plugin_list['sql']['options'][] =
114 array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreInserts');
115 $plugin_list['sql']['options'][] =
116 array('type' => 'bool', 'name' => 'hex_for_blob', 'text' => 'strHexForBLOB');
117 $plugin_list['sql']['options'][] =
118 array('type' => 'select', 'name' => 'type', 'text' => 'strSQLExportType', 'values' => array('INSERT', 'UPDATE', 'REPLACE'));
119 $plugin_list['sql']['options'][] =
120 array('type' => 'egroup');
125 * Avoids undefined variables, use NULL so isset() returns false
127 if (! isset($sql_backquotes)) {
128 $sql_backquotes = null;
134 * @param string Text of comment
136 * @return string The formatted comment
138 function PMA_exportComment($text = '')
140 // see http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-comments.html
141 return '--' . (empty($text) ?
'' : ' ') . $text . $GLOBALS['crlf'];
145 * Outputs export footer
147 * @return bool Whether it suceeded
151 function PMA_exportFooter()
154 global $mysql_charset_map;
158 if (isset($GLOBALS['sql_disable_fk'])) {
159 $foot .= $crlf . 'SET FOREIGN_KEY_CHECKS=1;' . $crlf;
162 if (isset($GLOBALS['sql_use_transaction'])) {
163 $foot .= $crlf . 'COMMIT;' . $crlf;
166 // restore connection settings
167 // (not set if $cfg['AllowAnywhereRecoding'] is false)
168 $charset_of_file = isset($GLOBALS['charset_of_file']) ?
$GLOBALS['charset_of_file'] : '';
169 if (!empty($GLOBALS['asfile']) && isset($mysql_charset_map[$charset_of_file])) {
171 . '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;' . $crlf
172 . '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;' . $crlf
173 . '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;' . $crlf;
176 return PMA_exportOutputHandler($foot);
180 * Outputs export header
182 * @return bool Whether it suceeded
186 function PMA_exportHeader()
190 global $mysql_charset_map;
192 if (PMA_MYSQL_INT_VERSION
>= 40100 && isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] != 'NONE') {
193 PMA_DBI_try_query('SET SQL_MODE="' . $GLOBALS['sql_compatibility'] . '"');
195 $head = PMA_exportComment('phpMyAdmin SQL Dump')
196 . PMA_exportComment('version ' . PMA_VERSION
)
197 . PMA_exportComment('http://www.phpmyadmin.net')
198 . PMA_exportComment();
199 $head .= empty($cfg['Server']['port']) ?
PMA_exportComment($GLOBALS['strHost'] . ': ' . $cfg['Server']['host']) : PMA_exportComment($GLOBALS['strHost'] . ': ' . $cfg['Server']['host'] . ':' . $cfg['Server']['port']);
200 $head .= PMA_exportComment($GLOBALS['strGenTime']
201 . ': ' . PMA_localisedDate())
202 . PMA_exportComment($GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION
, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION
, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION
, 3))
203 . PMA_exportComment($GLOBALS['strPHPVersion'] . ': ' . phpversion());
205 if (isset($GLOBALS['sql_header_comment']) && !empty($GLOBALS['sql_header_comment'])) {
206 // '\n' is not a newline (like "\n" would be), it's the characters
207 // backslash and n, as explained on the export interface
208 $lines = explode('\n', $GLOBALS['sql_header_comment']);
209 $head .= PMA_exportComment();
210 foreach($lines as $one_line) {
211 $head .= PMA_exportComment($one_line);
213 $head .= PMA_exportComment();
216 if (isset($GLOBALS['sql_disable_fk'])) {
217 $head .= $crlf . 'SET FOREIGN_KEY_CHECKS=0;' . $crlf;
220 /* We want exported AUTO_INCREMENT fields to have still same value, do this only for recent MySQL exports */
221 if (!isset($GLOBALS['sql_compatibility']) ||
$GLOBALS['sql_compatibility'] == 'NONE') {
222 $head .= $crlf . (PMA_MYSQL_INT_VERSION
>= 40101 ?
'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";' . $crlf : '');
225 if (isset($GLOBALS['sql_use_transaction'])) {
226 $head .= $crlf .'SET AUTOCOMMIT=0;' . $crlf
227 . 'START TRANSACTION;' . $crlf;
232 $charset_of_file = isset($GLOBALS['charset_of_file']) ?
$GLOBALS['charset_of_file'] : '';
233 if (!empty($GLOBALS['asfile']) && isset($mysql_charset_map[$charset_of_file])) {
235 . '/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;' . $crlf
236 . '/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;' . $crlf
237 . '/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;' . $crlf
238 . '/*!40101 SET NAMES ' . $mysql_charset_map[$charset_of_file] . ' */;' . $crlf . $crlf;
241 return PMA_exportOutputHandler($head);
245 * Outputs CREATE DATABASE database
247 * @param string Database name
249 * @return bool Whether it suceeded
253 function PMA_exportDBCreate($db)
256 if (isset($GLOBALS['sql_drop_database'])) {
257 if (!PMA_exportOutputHandler('DROP DATABASE ' . (isset($GLOBALS['sql_backquotes']) ?
PMA_backquote($db) : $db) . ';' . $crlf)) {
261 $create_query = 'CREATE DATABASE ' . (isset($GLOBALS['sql_backquotes']) ?
PMA_backquote($db) : $db);
262 if (PMA_MYSQL_INT_VERSION
>= 40101) {
263 $collation = PMA_getDbCollation($db);
264 if (strpos($collation, '_')) {
265 $create_query .= ' DEFAULT CHARACTER SET ' . substr($collation, 0, strpos($collation, '_')) . ' COLLATE ' . $collation;
267 $create_query .= ' DEFAULT CHARACTER SET ' . $collation;
270 $create_query .= ';' . $crlf;
271 if (!PMA_exportOutputHandler($create_query)) {
274 if (isset($GLOBALS['sql_backquotes']) && PMA_MYSQL_INT_VERSION
>= 40100 && isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] == 'NONE') {
275 return PMA_exportOutputHandler('USE ' . PMA_backquote($db) . ';' . $crlf);
277 return PMA_exportOutputHandler('USE ' . $db . ';' . $crlf);
281 * Outputs database header
283 * @param string Database name
285 * @return bool Whether it suceeded
289 function PMA_exportDBHeader($db)
291 $head = PMA_exportComment()
292 . PMA_exportComment($GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['sql_backquotes']) ?
PMA_backquote($db) : '\'' . $db . '\''))
293 . PMA_exportComment();
294 return PMA_exportOutputHandler($head);
298 * Outputs database footer
300 * @param string Database name
302 * @return bool Whether it suceeded
306 function PMA_exportDBFooter($db)
311 if (isset($GLOBALS['sql_constraints'])) {
312 $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']);
313 unset($GLOBALS['sql_constraints']);
316 if (PMA_MYSQL_INT_VERSION
>= 50000 && isset($GLOBALS['sql_structure']) && isset($GLOBALS['sql_procedure_function'])) {
319 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
320 if ($procedure_names) {
323 . PMA_exportComment()
324 . PMA_exportComment($GLOBALS['strProcedures'])
325 . PMA_exportComment()
326 . 'DELIMITER ' . $delimiter . $crlf
327 . PMA_exportComment();
329 foreach($procedure_names as $procedure_name) {
330 $procs_funcs .= PMA_DBI_get_procedure_or_function_def($db, 'PROCEDURE', $procedure_name) . $delimiter . $crlf . $crlf;
333 $procs_funcs .= PMA_exportComment()
334 . 'DELIMITER ;' . $crlf
335 . PMA_exportComment();
338 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
340 if ($function_names) {
341 $procs_funcs .= PMA_exportComment($GLOBALS['strFunctions'])
342 . PMA_exportComment() . $crlf;
344 foreach($function_names as $function_name) {
345 $procs_funcs .= PMA_DBI_get_procedure_or_function_def($db, 'FUNCTION', $function_name) . $crlf . $crlf;
348 if (!empty($procs_funcs)) {
349 $result = PMA_exportOutputHandler($procs_funcs);
357 * Returns a stand-in CREATE definition to resolve view dependencies
359 * @param string the database name
360 * @param string the vew name
361 * @param string the end of line sequence
363 * @return string resulting definition
367 function PMA_getTableDefStandIn($db, $view, $crlf) {
368 $create_query = 'CREATE TABLE ';
369 if (isset($GLOBALS['sql_if_not_exists']) && $GLOBALS['sql_if_not_exists']) {
370 $create_query .= 'IF NOT EXISTS ';
372 $create_query .= PMA_backquote($view) . ' (' . $crlf;
374 $columns = PMA_DBI_get_columns_full($db, $view);
375 foreach($columns as $column_name => $definition) {
376 $tmp[] = PMA_backquote($column_name) . ' ' . $definition['Type'] . $crlf;
378 $create_query .= implode(',', $tmp) . ');';
379 return($create_query);
383 * Returns $table's CREATE definition
385 * @param string the database name
386 * @param string the table name
387 * @param string the end of line sequence
388 * @param string the url to go back in case of error
389 * @param boolean whether to include creation/update/check dates
391 * @return string resulting schema
393 * @global boolean whether to add 'drop' statements or not
394 * @global boolean whether to use backquotes to allow the use of special
395 * characters in database, table and fields names or not
399 function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false)
401 global $sql_drop_table;
402 global $sql_backquotes;
404 global $sql_constraints;
405 global $sql_constraints_query; // just the text of the query
408 $auto_increment = '';
411 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
412 $result = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table) . '\'', null, PMA_DBI_QUERY_STORE
);
413 if ($result != FALSE) {
414 if (PMA_DBI_num_rows($result) > 0) {
415 $tmpres = PMA_DBI_fetch_assoc($result);
416 // Here we optionally add the AUTO_INCREMENT next value,
417 // but starting with MySQL 5.0.24, the clause is already included
418 // in SHOW CREATE TABLE so we'll remove it below
419 if (isset($GLOBALS['sql_auto_increment']) && !empty($tmpres['Auto_increment'])) {
420 $auto_increment .= ' AUTO_INCREMENT=' . $tmpres['Auto_increment'] . ' ';
423 if ($show_dates && isset($tmpres['Create_time']) && !empty($tmpres['Create_time'])) {
424 $schema_create .= PMA_exportComment($GLOBALS['strStatCreateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Create_time'])));
425 $new_crlf = PMA_exportComment() . $crlf;
428 if ($show_dates && isset($tmpres['Update_time']) && !empty($tmpres['Update_time'])) {
429 $schema_create .= PMA_exportComment($GLOBALS['strStatUpdateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Update_time'])));
430 $new_crlf = PMA_exportComment() . $crlf;
433 if ($show_dates && isset($tmpres['Check_time']) && !empty($tmpres['Check_time'])) {
434 $schema_create .= PMA_exportComment($GLOBALS['strStatCheckTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Check_time'])));
435 $new_crlf = PMA_exportComment() . $crlf;
438 PMA_DBI_free_result($result);
441 $schema_create .= $new_crlf;
443 if (!empty($sql_drop_table)) {
444 if (PMA_Table
::_isView($db,$table)) {
445 $drop_clause = 'DROP VIEW';
447 $drop_clause = 'DROP TABLE';
449 $schema_create .= $drop_clause . ' IF EXISTS ' . PMA_backquote($table, $sql_backquotes) . ';' . $crlf;
453 // Steve Alberty's patch for complete table dump,
454 // Whether to quote table and fields names or not
455 if ($sql_backquotes) {
456 PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 1');
458 PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 0');
461 // I don't see the reason why this unbuffered query could cause problems,
462 // because SHOW CREATE TABLE returns only one row, and we free the
463 // results below. Nonetheless, we got 2 user reports about this
464 // (see bug 1562533) so I remove the unbuffered mode.
465 //$result = PMA_DBI_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), null, PMA_DBI_QUERY_UNBUFFERED);
466 $result = PMA_DBI_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table));
467 if ($result != FALSE && ($row = PMA_DBI_fetch_row($result))) {
468 $create_query = $row[1];
471 // Convert end of line chars to one that we want (note that MySQL doesn't return query it will accept in all cases)
472 if (strpos($create_query, "(\r\n ")) {
473 $create_query = str_replace("\r\n", $crlf, $create_query);
474 } elseif (strpos($create_query, "(\n ")) {
475 $create_query = str_replace("\n", $crlf, $create_query);
476 } elseif (strpos($create_query, "(\r ")) {
477 $create_query = str_replace("\r", $crlf, $create_query);
480 // Should we use IF NOT EXISTS?
481 if (isset($GLOBALS['sql_if_not_exists'])) {
482 $create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);
485 // are there any constraints to cut out?
486 if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $create_query)) {
488 // Split the query into lines, so we can easily handle it. We know lines are separated by $crlf (done few lines above).
489 $sql_lines = explode($crlf, $create_query);
490 $sql_count = count($sql_lines);
492 // lets find first line with constraints
493 for ($i = 0; $i < $sql_count; $i++
) {
494 if (preg_match('@^[\s]*(CONSTRAINT|FOREIGN[\s]+KEY)@', $sql_lines[$i])) {
499 // If we really found a constraint
500 if ($i != $sql_count) {
502 // remove , from the end of create statement
503 $sql_lines[$i - 1] = preg_replace('@,$@', '', $sql_lines[$i - 1]);
505 // prepare variable for constraints
506 if (!isset($sql_constraints)) {
507 if (isset($GLOBALS['no_constraints_comments'])) {
508 $sql_constraints = '';
510 $sql_constraints = $crlf
511 . PMA_exportComment()
512 . PMA_exportComment($GLOBALS['strConstraintsForDumped'])
513 . PMA_exportComment();
517 // comments for current table
518 if (!isset($GLOBALS['no_constraints_comments'])) {
519 $sql_constraints .= $crlf
520 . PMA_exportComment()
521 . PMA_exportComment($GLOBALS['strConstraintsForTable'] . ' ' . PMA_backquote($table))
522 . PMA_exportComment();
526 $sql_constraints_query .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf;
527 $sql_constraints .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf;
530 for ($j = $i; $j < $sql_count; $j++
) {
531 if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $sql_lines[$j])) {
533 $sql_constraints .= $crlf;
535 if (strpos($sql_lines[$j], 'CONSTRAINT') === FALSE) {
536 $str_tmp = preg_replace('/(FOREIGN[\s]+KEY)/', 'ADD \1', $sql_lines[$j]);
537 $sql_constraints_query .= $str_tmp;
538 $sql_constraints .= $str_tmp;
540 $str_tmp = preg_replace('/(CONSTRAINT)/', 'ADD \1', $sql_lines[$j]);
541 $sql_constraints_query .= $str_tmp;
542 $sql_constraints .= $str_tmp;
549 $sql_constraints .= ';' . $crlf;
550 $sql_constraints_query .= ';';
552 $create_query = implode($crlf, array_slice($sql_lines, 0, $i)) . $crlf . implode($crlf, array_slice($sql_lines, $j, $sql_count - 1));
556 $schema_create .= $create_query;
559 // remove a possible "AUTO_INCREMENT = value" clause
560 // that could be there starting with MySQL 5.0.24
561 $schema_create = preg_replace('/AUTO_INCREMENT\s*=\s*([0-9])+/', '', $schema_create);
563 $schema_create .= $auto_increment;
565 PMA_DBI_free_result($result);
566 return $schema_create;
567 } // end of the 'PMA_getTableDef()' function
571 * Returns $table's comments, relations etc.
573 * @param string the database name
574 * @param string the table name
575 * @param string the end of line sequence
576 * @param boolean whether to include relation comments
577 * @param boolean whether to include column comments
578 * @param boolean whether to include mime comments
580 * @return string resulting comments
584 function PMA_getTableComments($db, $table, $crlf, $do_relation = false, $do_comments = false, $do_mime = false)
587 global $sql_backquotes;
588 global $sql_constraints;
592 // triggered only for MySQL < 4.1.x (pmadb-style comments)
593 if ($do_comments && $cfgRelation['commwork']) {
594 if (!($comments_map = PMA_getComments($db, $table))) {
595 unset($comments_map);
599 // Check if we can use Relations (Mike Beck)
600 if ($do_relation && !empty($cfgRelation['relation'])) {
601 // Find which tables are related with the current one and write it in
603 $res_rel = PMA_getForeigners($db, $table);
605 if ($res_rel && count($res_rel) > 0) {
614 if ($do_mime && $cfgRelation['mimework']) {
615 if (!($mime_map = PMA_getMIME($db, $table, true))) {
620 if (isset($comments_map) && count($comments_map) > 0) {
621 $schema_create .= $crlf
622 . PMA_exportComment()
623 . PMA_exportComment($GLOBALS['strCommentsForTable']. ' ' . PMA_backquote($table, $sql_backquotes) . ':');
624 foreach ($comments_map AS $comment_field => $comment) {
625 $schema_create .= PMA_exportComment(' ' . PMA_backquote($comment_field, $sql_backquotes))
626 . PMA_exportComment(' ' . PMA_backquote($comment, $sql_backquotes));
628 $schema_create .= PMA_exportComment();
631 if (isset($mime_map) && count($mime_map) > 0) {
632 $schema_create .= $crlf
633 . PMA_exportComment()
634 . PMA_exportComment($GLOBALS['strMIMETypesForTable']. ' ' . PMA_backquote($table, $sql_backquotes) . ':');
636 foreach ($mime_map AS $mime_field => $mime) {
637 $schema_create .= PMA_exportComment(' ' . PMA_backquote($mime_field, $sql_backquotes))
638 . PMA_exportComment(' ' . PMA_backquote($mime['mimetype'], $sql_backquotes));
640 $schema_create .= PMA_exportComment();
644 $schema_create .= $crlf
645 . PMA_exportComment()
646 . PMA_exportComment($GLOBALS['strRelationsForTable']. ' ' . PMA_backquote($table, $sql_backquotes) . ':');
647 foreach ($res_rel AS $rel_field => $rel) {
648 $schema_create .= PMA_exportComment(' ' . PMA_backquote($rel_field, $sql_backquotes))
649 . PMA_exportComment(' ' . PMA_backquote($rel['foreign_table'], $sql_backquotes)
650 . ' -> ' . PMA_backquote($rel['foreign_field'], $sql_backquotes));
652 $schema_create .= PMA_exportComment();
655 return $schema_create;
657 } // end of the 'PMA_getTableComments()' function
660 * Outputs table's structure
662 * @param string the database name
663 * @param string the table name
664 * @param string the end of line sequence
665 * @param string the url to go back in case of error
666 * @param boolean whether to include relation comments
667 * @param boolean whether to include column comments
668 * @param boolean whether to include mime comments
669 * @param string 'stand_in', 'create_table', 'create_view'
670 * @param string 'server', 'database', 'table'
672 * @return bool Whether it suceeded
676 function PMA_exportStructure($db, $table, $crlf, $error_url, $relation = FALSE, $comments = FALSE, $mime = FALSE, $dates = FALSE, $export_mode, $export_type)
678 $formatted_table_name = (isset($GLOBALS['sql_backquotes']))
679 ?
PMA_backquote($table)
680 : '\'' . $table . '\'';
682 . PMA_exportComment(str_repeat('-', 56))
684 . PMA_exportComment();
686 switch($export_mode) {
688 $dump .= PMA_exportComment($GLOBALS['strTableStructure'] . ' ' . $formatted_table_name)
689 . PMA_exportComment();
690 $dump .= PMA_getTableDef($db, $table, $crlf, $error_url, $dates) . ';' . $crlf;
691 $triggers = PMA_DBI_get_triggers($db, $table);
694 . PMA_exportComment()
695 . PMA_exportComment($GLOBALS['strTriggers'] . ' ' . $formatted_table_name)
696 . PMA_exportComment();
698 foreach ($triggers as $trigger) {
699 $dump .= $trigger['drop'] . ';' . $crlf;
700 $dump .= 'DELIMITER ' . $delimiter . $crlf;
701 $dump .= $trigger['create'];
702 $dump .= 'DELIMITER ;' . $crlf;
707 $dump .= PMA_exportComment($GLOBALS['strStructureForView'] . ' ' . $formatted_table_name)
708 . PMA_exportComment();
709 // delete the stand-in table previously created (if any)
710 if ($export_type != 'table') {
711 $dump .= 'DROP TABLE IF EXISTS ' . PMA_backquote($table) . ';' . $crlf;
713 $dump .= PMA_getTableDef($db, $table, $crlf, $error_url, $dates) . ';' . $crlf;
716 $dump .= PMA_exportComment($GLOBALS['strStandInStructureForView'] . ' ' . $formatted_table_name)
717 . PMA_exportComment();
718 // export a stand-in definition to resolve view dependencies
719 $dump .= PMA_getTableDefStandIn($db, $table, $crlf);
722 $dump .= PMA_getTableComments($db, $table, $crlf, $relation, $comments, $mime);
723 // this one is built by PMA_getTableDef() to use in table copy/move
724 // but not in the case of export
725 unset($GLOBALS['sql_constraints_query']);
727 return PMA_exportOutputHandler($dump);
731 * Dispatches between the versions of 'getTableContent' to use depending
734 * @param string the database name
735 * @param string the table name
736 * @param string the end of line sequence
737 * @param string the url to go back in case of error
738 * @param string SQL query for obtaining data
740 * @return bool Whether it suceeded
742 * @global boolean whether to use backquotes to allow the use of special
743 * characters in database, table and fields names or not
744 * @global integer the number of records
745 * @global integer the current record position
749 * @see PMA_getTableContentFast(), PMA_getTableContentOld()
753 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
755 global $sql_backquotes;
759 $formatted_table_name = (isset($GLOBALS['sql_backquotes']))
760 ?
PMA_backquote($table)
761 : '\'' . $table . '\'';
763 // Do not export data for a VIEW
764 // (For a VIEW, this is called only when exporting a single VIEW)
765 if (PMA_Table
::_isView($db, $table)) {
767 . PMA_exportComment()
768 . PMA_exportComment('VIEW ' . ' ' . $formatted_table_name)
769 . PMA_exportComment($GLOBALS['strData'] . ': ' . $GLOBALS['strNone'])
770 . PMA_exportComment()
773 if (! PMA_exportOutputHandler($head)) {
781 . PMA_exportComment()
782 . PMA_exportComment($GLOBALS['strDumpingData'] . ' ' . $formatted_table_name)
783 . PMA_exportComment()
786 if (! PMA_exportOutputHandler($head)) {
792 // analyze the query to get the true column names, not the aliases
793 // (this fixes an undefined index, also if Complete inserts
794 // are used, we did not get the true column name in case of aliases)
795 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($sql_query));
797 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
798 if ($result != FALSE) {
799 $fields_cnt = PMA_DBI_num_fields($result);
801 // Get field information
802 $fields_meta = PMA_DBI_get_fields_meta($result);
803 $field_flags = array();
804 for ($j = 0; $j < $fields_cnt; $j++
) {
805 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
808 for ($j = 0; $j < $fields_cnt; $j++
) {
809 if (isset($analyzed_sql[0]['select_expr'][$j]['column'])) {
810 $field_set[$j] = PMA_backquote($analyzed_sql[0]['select_expr'][$j]['column'], $sql_backquotes);
812 $field_set[$j] = PMA_backquote($fields_meta[$j]->name
, $sql_backquotes);
816 if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'UPDATE') {
818 $schema_insert = 'UPDATE ';
819 if (isset($GLOBALS['sql_ignore'])) {
820 $schema_insert .= 'IGNORE ';
823 $schema_insert .= PMA_backquote($table, $sql_backquotes) . ' SET';
826 if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'REPLACE') {
827 $sql_command = 'REPLACE';
829 $sql_command = 'INSERT';
833 if (isset($GLOBALS['sql_delayed'])) {
834 $insert_delayed = ' DELAYED';
836 $insert_delayed = '';
840 if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'INSERT' && isset($GLOBALS['sql_ignore'])) {
841 $insert_delayed .= ' IGNORE';
844 // scheme for inserting fields
845 if (isset($GLOBALS['sql_columns'])) {
846 $fields = implode(', ', $field_set);
847 $schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $sql_backquotes)
849 . ' (' . $fields . ') VALUES';
851 $schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $sql_backquotes)
856 $search = array("\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
857 $replace = array('\0', '\n', '\r', '\Z');
860 if (isset($GLOBALS['sql_extended']) && (!isset($GLOBALS['sql_type']) ||
$GLOBALS['sql_type'] != 'UPDATE')) {
862 $schema_insert .= $crlf;
867 while ($row = PMA_DBI_fetch_row($result)) {
869 for ($j = 0; $j < $fields_cnt; $j++
) {
871 if (!isset($row[$j]) ||
is_null($row[$j])) {
874 // timestamp is numeric on some MySQL 4.1, BLOBs are sometimes numeric
875 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type
!= 'timestamp'
876 && ! $fields_meta[$j]->blob
) {
877 $values[] = $row[$j];
879 // - mysqldump only generates hex data when the --hex-blob
880 // option is used, for fields having the binary attribute
881 // no hex is generated
882 // - a TEXT field returns type blob but a real blob
883 // returns also the 'binary' flag
884 } elseif (stristr($field_flags[$j], 'BINARY')
885 && $fields_meta[$j]->blob
886 && isset($GLOBALS['sql_hex_for_blob'])) {
887 // empty blobs need to be different, but '0' is also empty :-(
888 if (empty($row[$j]) && $row[$j] != '0') {
891 $values[] = '0x' . bin2hex($row[$j]);
893 // something else -> treat as a string
895 $values[] = '\'' . str_replace($search, $replace, PMA_sqlAddslashes($row[$j])) . '\'';
899 // should we make update?
900 if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'UPDATE') {
902 $insert_line = $schema_insert;
903 for ($i = 0; $i < $fields_cnt; $i++
) {
911 $insert_line .= $field_set[$i] . ' = ' . $values[$i];
914 $insert_line .= ' WHERE ' . PMA_getUniqueCondition($result, $fields_cnt, $fields_meta, $row);
918 // Extended inserts case
919 if (isset($GLOBALS['sql_extended'])) {
920 if ($current_row == 1) {
921 $insert_line = $schema_insert . '(' . implode(', ', $values) . ')';
923 $insert_line = '(' . implode(', ', $values) . ')';
924 if (isset($GLOBALS['sql_max_query_size']) && $GLOBALS['sql_max_query_size'] > 0 && $query_size +
strlen($insert_line) > $GLOBALS['sql_max_query_size']) {
925 if (!PMA_exportOutputHandler(';' . $crlf)) {
930 $insert_line = $schema_insert . $insert_line;
933 $query_size +
= strlen($insert_line);
935 // Other inserts case
937 $insert_line = $schema_insert . '(' . implode(', ', $values) . ')';
942 if (!PMA_exportOutputHandler(($current_row == 1 ?
'' : $separator . $crlf) . $insert_line)) {
947 if ($current_row > 0) {
948 if (!PMA_exportOutputHandler(';' . $crlf)) {
952 } // end if ($result != FALSE)
953 PMA_DBI_free_result($result);
956 } // end of the 'PMA_exportData()' function