UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / plugins / export / ExportSql.class.php
blob91227abcd8d537fc2c2f41f12854bb928f9b0ad7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build SQL dumps of tables
6 * @package PhpMyAdmin-Export
7 * @subpackage SQL
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 /**
17 * Handles the export for the SQL class
19 * @package PhpMyAdmin-Export
20 * @subpackage SQL
22 class ExportSql extends ExportPlugin
24 /**
25 * Constructor
27 public function __construct()
29 $this->setProperties();
31 // Avoids undefined variables, use NULL so isset() returns false
32 if (! isset($GLOBALS['sql_backquotes'])) {
33 $GLOBALS['sql_backquotes'] = null;
37 /**
38 * Sets the export SQL properties
40 * @return void
42 protected function setProperties()
44 global $plugin_param;
46 $hide_sql = false;
47 $hide_structure = false;
48 if ($plugin_param['export_type'] == 'table'
49 && ! $plugin_param['single_table']
50 ) {
51 $hide_structure = true;
52 $hide_sql = true;
55 if (! $hide_sql) {
56 $props = 'libraries/properties/';
57 include_once "$props/plugins/ExportPluginProperties.class.php";
58 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
59 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
60 include_once "$props/options/groups/OptionsPropertySubgroup.class.php";
61 include_once "$props/options/items/BoolPropertyItem.class.php";
62 include_once "$props/options/items/MessageOnlyPropertyItem.class.php";
63 include_once "$props/options/items/RadioPropertyItem.class.php";
64 include_once "$props/options/items/SelectPropertyItem.class.php";
65 include_once "$props/options/items/TextPropertyItem.class.php";
66 include_once "$props/options/items/NumberPropertyItem.class.php";
68 $exportPluginProperties = new ExportPluginProperties();
69 $exportPluginProperties->setText('SQL');
70 $exportPluginProperties->setExtension('sql');
71 $exportPluginProperties->setMimeType('text/x-sql');
72 $exportPluginProperties->setOptionsText(__('Options'));
74 // create the root group that will be the options field for
75 // $exportPluginProperties
76 // this will be shown as "Format specific options"
77 $exportSpecificOptions = new OptionsPropertyRootGroup();
78 $exportSpecificOptions->setName("Format Specific Options");
80 // general options main group
81 $generalOptions = new OptionsPropertyMainGroup();
82 $generalOptions->setName("general_opts");
84 // comments
85 $subgroup = new OptionsPropertySubgroup();
86 $subgroup->setName("include_comments");
87 $leaf = new BoolPropertyItem();
88 $leaf->setName('include_comments');
89 $leaf->setText(
90 __(
91 'Display comments <i>(includes info such as export'
92 . ' timestamp, PHP version, and server version)</i>'
95 $subgroup->setSubgroupHeader($leaf);
97 $leaf = new TextPropertyItem();
98 $leaf->setName('header_comment');
99 $leaf->setText(
100 __('Additional custom header comment (\n splits lines):')
102 $subgroup->addProperty($leaf);
103 $leaf = new BoolPropertyItem();
104 $leaf->setName('dates');
105 $leaf->setText(
107 'Include a timestamp of when databases were created, last'
108 . ' updated, and last checked'
111 $subgroup->addProperty($leaf);
112 if (! empty($GLOBALS['cfgRelation']['relation'])) {
113 $leaf = new BoolPropertyItem();
114 $leaf->setName('relation');
115 $leaf->setText(__('Display foreign key relationships'));
116 $subgroup->addProperty($leaf);
118 if (! empty($GLOBALS['cfgRelation']['mimework'])) {
119 $leaf = new BoolPropertyItem();
120 $leaf->setName('mime');
121 $leaf->setText(__('Display MIME types'));
122 $subgroup->addProperty($leaf);
124 $generalOptions->addProperty($subgroup);
126 // enclose in a transaction
127 $leaf = new BoolPropertyItem();
128 $leaf->setName("use_transaction");
129 $leaf->setText(__('Enclose export in a transaction'));
130 $leaf->setDoc(
131 array(
132 'programs',
133 'mysqldump',
134 'option_mysqldump_single-transaction'
137 $generalOptions->addProperty($leaf);
139 // disable foreign key checks
140 $leaf = new BoolPropertyItem();
141 $leaf->setName("disable_fk");
142 $leaf->setText(__('Disable foreign key checks'));
143 $leaf->setDoc(
144 array(
145 'manual_MySQL_Database_Administration',
146 'server-system-variables',
147 'sysvar_foreign_key_checks'
150 $generalOptions->addProperty($leaf);
152 // export views as tables
153 $leaf = new BoolPropertyItem();
154 $leaf->setName("views_as_tables");
155 $leaf->setText(__('Export views as tables'));
156 $generalOptions->addProperty($leaf);
158 // compatibility maximization
159 $compats = $GLOBALS['dbi']->getCompatibilities();
160 if (count($compats) > 0) {
161 $values = array();
162 foreach ($compats as $val) {
163 $values[$val] = $val;
166 $leaf = new SelectPropertyItem();
167 $leaf->setName("compatibility");
168 $leaf->setText(
170 'Database system or older MySQL server to maximize output'
171 . ' compatibility with:'
174 $leaf->setValues($values);
175 $leaf->setDoc(
176 array(
177 'manual_MySQL_Database_Administration',
178 'Server_SQL_mode'
181 $generalOptions->addProperty($leaf);
183 unset($values);
186 // server export options
187 if ($plugin_param['export_type'] == 'server') {
188 $leaf = new BoolPropertyItem();
189 $leaf->setName("drop_database");
190 $leaf->setText(
191 sprintf(__('Add %s statement'), '<code>DROP DATABASE</code>')
193 $generalOptions->addProperty($leaf);
196 // what to dump (structure/data/both)
197 $subgroup = new OptionsPropertySubgroup();
198 $subgroup->setName("dump_table");
199 $subgroup->setText("Dump table");
200 $leaf = new RadioPropertyItem();
201 $leaf->setName('structure_or_data');
202 $leaf->setValues(
203 array(
204 'structure' => __('structure'),
205 'data' => __('data'),
206 'structure_and_data' => __('structure and data')
209 $subgroup->setSubgroupHeader($leaf);
210 $generalOptions->addProperty($subgroup);
212 // add the main group to the root group
213 $exportSpecificOptions->addProperty($generalOptions);
215 // structure options main group
216 if (! $hide_structure) {
217 $structureOptions = new OptionsPropertyMainGroup();
218 $structureOptions->setName("structure");
219 $structureOptions->setText(__('Object creation options'));
220 $structureOptions->setForce('data');
222 // begin SQL Statements
223 $subgroup = new OptionsPropertySubgroup();
224 $leaf = new MessageOnlyPropertyItem();
225 $leaf->setName('add_statements');
226 $leaf->setText(__('Add statements:'));
227 $subgroup->setSubgroupHeader($leaf);
229 if ($plugin_param['export_type'] != 'table') {
230 $leaf = new BoolPropertyItem();
231 $leaf->setName('create_database');
232 $create_clause = '<code>CREATE DATABASE / USE</code>';
233 $leaf->setText(sprintf(__('Add %s statement'), $create_clause));
234 $subgroup->addProperty($leaf);
237 if ($plugin_param['export_type'] == 'table') {
238 if (PMA_Table::isView($GLOBALS['db'], $GLOBALS['table'])) {
239 $drop_clause = '<code>DROP VIEW</code>';
240 } else {
241 $drop_clause = '<code>DROP TABLE</code>';
243 } else {
244 if (PMA_DRIZZLE) {
245 $drop_clause = '<code>DROP TABLE</code>';
246 } else {
247 $drop_clause = '<code>DROP TABLE / VIEW / PROCEDURE'
248 . ' / FUNCTION / EVENT</code>';
252 $drop_clause .= '<code> / TRIGGER</code>';
254 $leaf = new BoolPropertyItem();
255 $leaf->setName('drop_table');
256 $leaf->setText(sprintf(__('Add %s statement'), $drop_clause));
257 $subgroup->addProperty($leaf);
259 // Add table structure option
260 $leaf = new BoolPropertyItem();
261 $leaf->setName('create_table');
262 $leaf->setText(
263 sprintf(__('Add %s statement'), '<code>CREATE TABLE</code>')
265 $subgroup->addProperty($leaf);
267 // Add view option
268 $leaf = new BoolPropertyItem();
269 $leaf->setName('create_view');
270 $leaf->setText(
271 sprintf(__('Add %s statement'), '<code>CREATE VIEW</code>')
273 $subgroup->addProperty($leaf);
275 // Drizzle doesn't support procedures and functions
276 if (! PMA_DRIZZLE) {
277 $leaf = new BoolPropertyItem();
278 $leaf->setName('procedure_function');
279 $leaf->setText(
280 sprintf(
281 __('Add %s statement'),
282 '<code>CREATE PROCEDURE / FUNCTION / EVENT</code>'
285 $subgroup->addProperty($leaf);
288 // Add triggers option
289 $leaf = new BoolPropertyItem();
290 $leaf->setName('create_trigger');
291 $leaf->setText(
292 sprintf(__('Add %s statement'), '<code>CREATE TRIGGER</code>')
294 $subgroup->addProperty($leaf);
296 // begin CREATE TABLE statements
297 $subgroup_create_table = new OptionsPropertySubgroup();
298 $leaf = new BoolPropertyItem();
299 $leaf->setName('create_table_statements');
300 $leaf->setText(__('<code>CREATE TABLE</code> options:'));
301 $subgroup_create_table->setSubgroupHeader($leaf);
302 $leaf = new BoolPropertyItem();
303 $leaf->setName('if_not_exists');
304 $leaf->setText('<code>IF NOT EXISTS</code>');
305 $subgroup_create_table->addProperty($leaf);
306 $leaf = new BoolPropertyItem();
307 $leaf->setName('auto_increment');
308 $leaf->setText('<code>AUTO_INCREMENT</code>');
309 $subgroup_create_table->addProperty($leaf);
310 $subgroup->addProperty($subgroup_create_table);
311 $structureOptions->addProperty($subgroup);
313 $leaf = new BoolPropertyItem();
314 $leaf->setName("backquotes");
315 $leaf->setText(
317 'Enclose table and column names with backquotes '
318 . '<i>(Protects column and table names formed with'
319 . ' special characters or keywords)</i>'
323 $structureOptions->addProperty($leaf);
325 // add the main group to the root group
326 $exportSpecificOptions->addProperty($structureOptions);
329 // begin Data options
330 $dataOptions = new OptionsPropertyMainGroup();
331 $dataOptions->setName("data");
332 $dataOptions->setText(__('Data creation options'));
333 $dataOptions->setForce('structure');
334 $leaf = new BoolPropertyItem();
335 $leaf->setName("truncate");
336 $leaf->setText(__('Truncate table before insert'));
337 $dataOptions->addProperty($leaf);
339 // begin SQL Statements
340 $subgroup = new OptionsPropertySubgroup();
341 $leaf = new MessageOnlyPropertyItem();
342 $leaf->setText(__('Instead of <code>INSERT</code> statements, use:'));
343 $subgroup->setSubgroupHeader($leaf);
344 // Not supported in Drizzle
345 if (! PMA_DRIZZLE) {
346 $leaf = new BoolPropertyItem();
347 $leaf->setName("delayed");
348 $leaf->setText(__('<code>INSERT DELAYED</code> statements'));
349 $leaf->setDoc(
350 array(
351 'manual_MySQL_Database_Administration',
352 'insert_delayed'
355 $subgroup->addProperty($leaf);
357 $leaf = new BoolPropertyItem();
358 $leaf->setName("ignore");
359 $leaf->setText(__('<code>INSERT IGNORE</code> statements'));
360 $leaf->setDoc(
361 array(
362 'manual_MySQL_Database_Administration',
363 'insert'
366 $subgroup->addProperty($leaf);
367 $dataOptions->addProperty($subgroup);
369 // Function to use when dumping dat
370 $leaf = new SelectPropertyItem();
371 $leaf->setName("type");
372 $leaf->setText(__('Function to use when dumping data:'));
373 $leaf->setValues(
374 array(
375 'INSERT' => 'INSERT',
376 'UPDATE' => 'UPDATE',
377 'REPLACE' => 'REPLACE'
380 $dataOptions->addProperty($leaf);
382 /* Syntax to use when inserting data */
383 $subgroup = new OptionsPropertySubgroup();
384 $leaf = new MessageOnlyPropertyItem();
385 $leaf->setText(__('Syntax to use when inserting data:'));
386 $subgroup->setSubgroupHeader($leaf);
387 $leaf = new RadioPropertyItem();
388 $leaf->setName("insert_syntax");
389 $leaf->setText(__('<code>INSERT IGNORE</code> statements'));
390 $leaf->setValues(
391 array(
392 'complete' => __(
393 'include column names in every <code>INSERT</code> statement'
394 . ' <br /> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO'
395 . ' tbl_name (col_A,col_B,col_C) VALUES (1,2,3)</code>'
397 'extended' => __(
398 'insert multiple rows in every <code>INSERT</code> statement'
399 . '<br /> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO'
400 . ' tbl_name VALUES (1,2,3), (4,5,6), (7,8,9)</code>'
402 'both' => __(
403 'both of the above<br /> &nbsp; &nbsp; &nbsp; Example:'
404 . ' <code>INSERT INTO tbl_name (col_A,col_B,col_C) VALUES'
405 . ' (1,2,3), (4,5,6), (7,8,9)</code>'
407 'none' => __(
408 'neither of the above<br /> &nbsp; &nbsp; &nbsp; Example:'
409 . ' <code>INSERT INTO tbl_name VALUES (1,2,3)</code>'
413 $subgroup->addProperty($leaf);
414 $dataOptions->addProperty($subgroup);
416 // Max length of query
417 $leaf = new NumberPropertyItem();
418 $leaf->setName("max_query_size");
419 $leaf->setText(__('Maximal length of created query'));
420 $dataOptions->addProperty($leaf);
422 // Dump binary columns in hexadecimal
423 $leaf = new BoolPropertyItem();
424 $leaf->setName("hex_for_binary");
425 $leaf->setText(
427 'Dump binary columns in hexadecimal notation'
428 . ' <i>(for example, "abc" becomes 0x616263)</i>'
431 $dataOptions->addProperty($leaf);
433 // Drizzle works only with UTC timezone
434 if (! PMA_DRIZZLE) {
435 // Dump time in UTC
436 $leaf = new BoolPropertyItem();
437 $leaf->setName("utc_time");
438 $leaf->setText(
440 'Dump TIMESTAMP columns in UTC <i>(enables TIMESTAMP columns'
441 . ' to be dumped and reloaded between servers in different'
442 . ' time zones)</i>'
445 $dataOptions->addProperty($leaf);
448 // add the main group to the root group
449 $exportSpecificOptions->addProperty($dataOptions);
451 // set the options for the export plugin property item
452 $exportPluginProperties->setOptions($exportSpecificOptions);
453 $this->properties = $exportPluginProperties;
458 * Exports routines (procedures and functions)
460 * @param string $db Database
461 * @param array $aliases Aliases of db/table/columns
463 * @return bool Whether it succeeded
465 public function exportRoutines($db, $aliases = array())
467 global $crlf;
469 $db_alias = $db;
470 $this->initAlias($aliases, $db_alias);
472 $text = '';
473 $delimiter = '$$';
475 $procedure_names = $GLOBALS['dbi']
476 ->getProceduresOrFunctions($db, 'PROCEDURE');
477 $function_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'FUNCTION');
479 if ($procedure_names || $function_names) {
480 $text .= $crlf
481 . 'DELIMITER ' . $delimiter . $crlf;
484 if ($procedure_names) {
485 $text .=
486 $this->_exportComment()
487 . $this->_exportComment(__('Procedures'))
488 . $this->_exportComment();
489 $used_alias = false;
490 $proc_query = '';
491 foreach ($procedure_names as $procedure_name) {
492 if (! empty($GLOBALS['sql_drop_table'])) {
493 $proc_query .= 'DROP PROCEDURE IF EXISTS '
494 . PMA_Util::backquote($procedure_name)
495 . $delimiter . $crlf;
497 $create_query = $GLOBALS['dbi']
498 ->getDefinition($db, 'PROCEDURE', $procedure_name);
499 $create_query = $this->replaceWithAliases(
500 $create_query, $aliases, $db, '', $flag
502 // One warning per database
503 if ($flag) {
504 $used_alias = true;
506 $proc_query .= $create_query . $delimiter . $crlf . $crlf;
508 if ($used_alias) {
509 $text .= $this->_exportComment(
511 'It appears your database uses procedures;'
514 . $this->_exportComment(
515 __('alias export may not work reliably in all cases.')
517 . $this->_exportComment();
519 $text .= $proc_query;
522 if ($function_names) {
523 $text .=
524 $this->_exportComment()
525 . $this->_exportComment(__('Functions'))
526 . $this->_exportComment();
527 $used_alias = false;
528 $function_query = '';
529 foreach ($function_names as $function_name) {
530 if (! empty($GLOBALS['sql_drop_table'])) {
531 $function_query .= 'DROP FUNCTION IF EXISTS '
532 . PMA_Util::backquote($function_name)
533 . $delimiter . $crlf;
535 $create_query = $GLOBALS['dbi']
536 ->getDefinition($db, 'FUNCTION', $function_name);
537 $create_query = $this->replaceWithAliases(
538 $create_query, $aliases, $db, '', $flag
540 // One warning per database
541 if ($flag) {
542 $used_alias = true;
544 $function_query .= $create_query . $delimiter . $crlf . $crlf;
546 if ($used_alias) {
547 $text .= $this->_exportComment(
548 __('It appears your database uses functions;')
550 . $this->_exportComment(
551 __('alias export may not work reliably in all cases.')
553 . $this->_exportComment();
555 $text .= $function_query;
558 if ($procedure_names || $function_names) {
559 $text .= 'DELIMITER ;' . $crlf;
562 if (! empty($text)) {
563 return PMA_exportOutputHandler($text);
564 } else {
565 return false;
570 * Possibly outputs comment
572 * @param string $text Text of comment
574 * @return string The formatted comment
576 private function _exportComment($text = '')
578 if (isset($GLOBALS['sql_include_comments'])
579 && $GLOBALS['sql_include_comments']
581 // see http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-comments.html
582 return '--' . (empty($text) ? '' : ' ') . $text . $GLOBALS['crlf'];
583 } else {
584 return '';
589 * Possibly outputs CRLF
591 * @return string $crlf or nothing
593 private function _possibleCRLF()
595 if (isset($GLOBALS['sql_include_comments'])
596 && $GLOBALS['sql_include_comments']
598 return $GLOBALS['crlf'];
599 } else {
600 return '';
605 * Outputs export footer
607 * @return bool Whether it succeeded
609 public function exportFooter()
611 global $crlf, $mysql_charset_map;
613 $foot = '';
615 if (isset($GLOBALS['sql_disable_fk'])) {
616 $foot .= 'SET FOREIGN_KEY_CHECKS=1;' . $crlf;
619 if (isset($GLOBALS['sql_use_transaction'])) {
620 $foot .= 'COMMIT;' . $crlf;
623 // restore connection settings
624 $charset_of_file = isset($GLOBALS['charset_of_file'])
625 ? $GLOBALS['charset_of_file'] : '';
626 if (! empty($GLOBALS['asfile'])
627 && isset($mysql_charset_map[$charset_of_file])
628 && ! PMA_DRIZZLE
630 $foot .= $crlf
631 . '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;'
632 . $crlf
633 . '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;'
634 . $crlf
635 . '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;'
636 . $crlf;
639 /* Restore timezone */
640 if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) {
641 $GLOBALS['dbi']->query('SET time_zone = "' . $GLOBALS['old_tz'] . '"');
644 return PMA_exportOutputHandler($foot);
648 * Outputs export header. It is the first method to be called, so all
649 * the required variables are initialized here.
651 * @return bool Whether it succeeded
653 public function exportHeader()
655 global $crlf, $cfg;
656 global $mysql_charset_map;
658 if (isset($GLOBALS['sql_compatibility'])) {
659 $tmp_compat = $GLOBALS['sql_compatibility'];
660 if ($tmp_compat == 'NONE') {
661 $tmp_compat = '';
663 $GLOBALS['dbi']->tryQuery('SET SQL_MODE="' . $tmp_compat . '"');
664 unset($tmp_compat);
666 $head = $this->_exportComment('phpMyAdmin SQL Dump')
667 . $this->_exportComment('version ' . PMA_VERSION)
668 . $this->_exportComment('http://www.phpmyadmin.net')
669 . $this->_exportComment();
670 $host_string = __('Host:') . ' ' . $cfg['Server']['host'];
671 if (! empty($cfg['Server']['port'])) {
672 $host_string .= ':' . $cfg['Server']['port'];
674 $head .= $this->_exportComment($host_string);
675 $head .=
676 $this->_exportComment(
677 __('Generation Time:') . ' '
678 . PMA_Util::localisedDate()
680 . $this->_exportComment(
681 __('Server version:') . ' ' . PMA_MYSQL_STR_VERSION
683 . $this->_exportComment(__('PHP Version:') . ' ' . phpversion())
684 . $this->_possibleCRLF();
686 if (isset($GLOBALS['sql_header_comment'])
687 && ! empty($GLOBALS['sql_header_comment'])
689 // '\n' is not a newline (like "\n" would be), it's the characters
690 // backslash and n, as explained on the export interface
691 $lines = explode('\n', $GLOBALS['sql_header_comment']);
692 $head .= $this->_exportComment();
693 foreach ($lines as $one_line) {
694 $head .= $this->_exportComment($one_line);
696 $head .= $this->_exportComment();
699 if (isset($GLOBALS['sql_disable_fk'])) {
700 $head .= 'SET FOREIGN_KEY_CHECKS=0;' . $crlf;
703 // We want exported AUTO_INCREMENT columns to have still same value,
704 // do this only for recent MySQL exports
705 if ((! isset($GLOBALS['sql_compatibility'])
706 || $GLOBALS['sql_compatibility'] == 'NONE')
707 && ! PMA_DRIZZLE
709 $head .= 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";' . $crlf;
712 if (isset($GLOBALS['sql_use_transaction'])) {
713 $head .= 'SET AUTOCOMMIT = 0;' . $crlf
714 . 'START TRANSACTION;' . $crlf;
717 /* Change timezone if we should export timestamps in UTC */
718 if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) {
719 $head .= 'SET time_zone = "+00:00";' . $crlf;
720 $GLOBALS['old_tz'] = $GLOBALS['dbi']
721 ->fetchValue('SELECT @@session.time_zone');
722 $GLOBALS['dbi']->query('SET time_zone = "+00:00"');
725 $head .= $this->_possibleCRLF();
727 if (! empty($GLOBALS['asfile']) && ! PMA_DRIZZLE) {
728 // we are saving as file, therefore we provide charset information
729 // so that a utility like the mysql client can interpret
730 // the file correctly
731 if (isset($GLOBALS['charset_of_file'])
732 && isset($mysql_charset_map[$GLOBALS['charset_of_file']])
734 // we got a charset from the export dialog
735 $set_names = $mysql_charset_map[$GLOBALS['charset_of_file']];
736 } else {
737 // by default we use the connection charset
738 $set_names = $mysql_charset_map['utf-8'];
740 $head .= $crlf
741 . '/*!40101 SET @OLD_CHARACTER_SET_CLIENT='
742 . '@@CHARACTER_SET_CLIENT */;' . $crlf
743 . '/*!40101 SET @OLD_CHARACTER_SET_RESULTS='
744 . '@@CHARACTER_SET_RESULTS */;' . $crlf
745 . '/*!40101 SET @OLD_COLLATION_CONNECTION='
746 . '@@COLLATION_CONNECTION */;' . $crlf
747 . '/*!40101 SET NAMES ' . $set_names . ' */;' . $crlf . $crlf;
750 return PMA_exportOutputHandler($head);
754 * Outputs CREATE DATABASE statement
756 * @param string $db Database name
757 * @param string $db_alias Aliases of db
759 * @return bool Whether it succeeded
761 public function exportDBCreate($db, $db_alias = '')
763 global $crlf;
765 if (empty($db_alias)) {
766 $db_alias = $db;
768 if (isset($GLOBALS['sql_compatibility'])) {
769 $compat = $GLOBALS['sql_compatibility'];
770 } else {
771 $compat = 'NONE';
773 if (isset($GLOBALS['sql_drop_database'])) {
774 if (! PMA_exportOutputHandler(
775 'DROP DATABASE '
776 . PMA_Util::backquoteCompat(
777 $db_alias, $compat, isset($GLOBALS['sql_backquotes'])
779 . ';' . $crlf
780 )) {
781 return false;
784 if (!isset($GLOBALS['sql_create_database'])) {
785 return true;
788 $create_query = 'CREATE DATABASE IF NOT EXISTS '
789 . PMA_Util::backquoteCompat(
790 $db_alias, $compat, isset($GLOBALS['sql_backquotes'])
792 $collation = PMA_getDbCollation($db);
793 if (PMA_DRIZZLE) {
794 $create_query .= ' COLLATE ' . $collation;
795 } else {
796 if (/*overload*/mb_strpos($collation, '_')) {
797 $create_query .= ' DEFAULT CHARACTER SET '
798 . /*overload*/mb_substr(
799 $collation,
801 /*overload*/mb_strpos($collation, '_')
803 . ' COLLATE ' . $collation;
804 } else {
805 $create_query .= ' DEFAULT CHARACTER SET ' . $collation;
808 $create_query .= ';' . $crlf;
809 if (! PMA_exportOutputHandler($create_query)) {
810 return false;
812 if ((isset($GLOBALS['sql_compatibility'])
813 && $GLOBALS['sql_compatibility'] == 'NONE')
814 || PMA_DRIZZLE
816 $result = PMA_exportOutputHandler(
817 'USE '
818 . PMA_Util::backquoteCompat(
819 $db_alias, $compat, isset($GLOBALS['sql_backquotes'])
821 . ';' . $crlf
823 } else {
824 $result = PMA_exportOutputHandler('USE ' . $db_alias . ';' . $crlf);
826 return $result;
830 * Outputs database header
832 * @param string $db Database name
833 * @param string $db_alias Alias of db
835 * @return bool Whether it succeeded
837 public function exportDBHeader($db, $db_alias = '')
839 if (empty($db_alias)) {
840 $db_alias = $db;
842 if (isset($GLOBALS['sql_compatibility'])) {
843 $compat = $GLOBALS['sql_compatibility'];
844 } else {
845 $compat = 'NONE';
847 $head = $this->_exportComment()
848 . $this->_exportComment(
849 __('Database:') . ' '
850 . PMA_Util::backquoteCompat(
851 $db_alias, $compat, isset($GLOBALS['sql_backquotes'])
854 . $this->_exportComment();
855 return PMA_exportOutputHandler($head);
859 * Outputs database footer
861 * @param string $db Database name
863 * @return bool Whether it succeeded
865 public function exportDBFooter($db)
867 global $crlf;
869 $result = true;
871 //add indexes to the sql dump file
872 if (isset($GLOBALS['sql_indexes'])) {
873 $result = PMA_exportOutputHandler($GLOBALS['sql_indexes']);
874 unset($GLOBALS['sql_indexes']);
876 //add auto increments to the sql dump file
877 if (isset($GLOBALS['sql_auto_increments'])) {
878 $result = PMA_exportOutputHandler($GLOBALS['sql_auto_increments']);
879 unset($GLOBALS['sql_auto_increments']);
881 //add constraints to the sql dump file
882 if (isset($GLOBALS['sql_constraints'])) {
883 $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']);
884 unset($GLOBALS['sql_constraints']);
887 if (($GLOBALS['sql_structure_or_data'] == 'structure'
888 || $GLOBALS['sql_structure_or_data'] == 'structure_and_data')
889 && isset($GLOBALS['sql_procedure_function'])
891 $text = '';
892 $delimiter = '$$';
894 $event_names = $GLOBALS['dbi']->fetchResult(
895 'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE'
896 . ' EVENT_SCHEMA= \''
897 . PMA_Util::sqlAddSlashes($db, true)
898 . '\';'
901 if ($event_names) {
902 $text .= $crlf
903 . 'DELIMITER ' . $delimiter . $crlf;
905 $text .=
906 $this->_exportComment()
907 . $this->_exportComment(__('Events'))
908 . $this->_exportComment();
910 foreach ($event_names as $event_name) {
911 if (! empty($GLOBALS['sql_drop_table'])) {
912 $text .= 'DROP EVENT '
913 . PMA_Util::backquote($event_name)
914 . $delimiter . $crlf;
916 $text .= $GLOBALS['dbi']
917 ->getDefinition($db, 'EVENT', $event_name)
918 . $delimiter . $crlf . $crlf;
921 $text .= 'DELIMITER ;' . $crlf;
924 if (! empty($text)) {
925 $result = PMA_exportOutputHandler($text);
928 return $result;
932 * Returns a stand-in CREATE definition to resolve view dependencies
934 * @param string $db the database name
935 * @param string $view the view name
936 * @param string $crlf the end of line sequence
937 * @param array $aliases Aliases of db/table/columns
939 * @return string resulting definition
941 public function getTableDefStandIn($db, $view, $crlf, $aliases = array())
943 $db_alias = $db;
944 $view_alias = $view;
945 $this->initAlias($aliases, $db_alias, $view_alias);
946 $create_query = '';
947 if (! empty($GLOBALS['sql_drop_table'])) {
948 $create_query .= 'DROP VIEW IF EXISTS '
949 . PMA_Util::backquote($view_alias)
950 . ';' . $crlf;
953 $create_query .= 'CREATE TABLE ';
955 if (isset($GLOBALS['sql_if_not_exists'])
956 && $GLOBALS['sql_if_not_exists']
958 $create_query .= 'IF NOT EXISTS ';
960 $create_query .= PMA_Util::backquote($view_alias) . ' (' . $crlf;
961 $tmp = array();
962 $columns = $GLOBALS['dbi']->getColumnsFull($db, $view);
963 foreach ($columns as $column_name => $definition) {
964 $col_alias = $column_name;
965 if (!empty($aliases[$db]['tables'][$view]['columns'][$col_alias])) {
966 $col_alias = $aliases[$db]['tables'][$view]['columns'][$col_alias];
968 $tmp[] = PMA_Util::backquote($col_alias) . ' ' .
969 $definition['Type'] . $crlf;
971 $create_query .= implode(',', $tmp) . ');' . $crlf;
972 return($create_query);
976 * Returns CREATE definition that matches $view's structure
978 * @param string $db the database name
979 * @param string $view the view name
980 * @param string $crlf the end of line sequence
981 * @param bool $add_semicolon whether to add semicolon and end-of-line at
982 * the end
983 * @param array $aliases Aliases of db/table/columns
985 * @return string resulting schema
987 private function _getTableDefForView(
988 $db,
989 $view,
990 $crlf,
991 $add_semicolon = true,
992 $aliases = array()
994 $db_alias = $db;
995 $view_alias = $view;
996 $this->initAlias($aliases, $db_alias, $view_alias);
997 $create_query = "CREATE TABLE";
998 if (isset($GLOBALS['sql_if_not_exists'])) {
999 $create_query .= " IF NOT EXISTS ";
1001 $create_query .= PMA_Util::backquote($view_alias) . "(" . $crlf;
1003 $columns = $GLOBALS['dbi']->getColumns($db, $view, null, true);
1005 $firstCol = true;
1006 foreach ($columns as $column) {
1007 $col_alias = $column['Field'];
1008 if (!empty($aliases[$db]['tables'][$view]['columns'][$col_alias])) {
1009 $col_alias = $aliases[$db]['tables'][$view]['columns'][$col_alias];
1011 $extracted_columnspec = PMA_Util::extractColumnSpec($column['Type']);
1013 if (! $firstCol) {
1014 $create_query .= "," . $crlf;
1016 $create_query .= " " . PMA_Util::backquote($col_alias);
1017 $create_query .= " " . $column['Type'];
1018 if ($extracted_columnspec['can_contain_collation']
1019 && ! empty($column['Collation'])
1021 $create_query .= " COLLATE " . $column['Collation'];
1023 if ($column['Null'] == 'NO') {
1024 $create_query .= " NOT NULL";
1026 if (isset($column['Default'])) {
1027 $create_query .= " DEFAULT '"
1028 . PMA_Util::sqlAddSlashes($column['Default']) . "'";
1029 } else if ($column['Null'] == 'YES') {
1030 $create_query .= " DEFAULT NULL";
1032 if (! empty($column['Comment'])) {
1033 $create_query .= " COMMENT '"
1034 . PMA_Util::sqlAddSlashes($column['Comment']) . "'";
1036 $firstCol = false;
1038 $create_query .= $crlf . ")" . ($add_semicolon ? ';' : '') . $crlf;
1040 if (isset($GLOBALS['sql_compatibility'])) {
1041 $compat = $GLOBALS['sql_compatibility'];
1042 } else {
1043 $compat = 'NONE';
1045 if ($compat == 'MSSQL') {
1046 $create_query = $this->_makeCreateTableMSSQLCompatible(
1047 $create_query
1050 return $create_query;
1054 * Returns $table's CREATE definition
1056 * @param string $db the database name
1057 * @param string $table the table name
1058 * @param string $crlf the end of line sequence
1059 * @param string $error_url the url to go back in case
1060 * of error
1061 * @param bool $show_dates whether to include creation/
1062 * update/check dates
1063 * @param bool $add_semicolon whether to add semicolon and
1064 * end-of-line at the end
1065 * @param bool $view whether we're handling a view
1066 * @param bool $update_indexes_increments whether we need to update
1067 * two global variables
1068 * @param array $aliases Aliases of db/table/columns
1070 * @return string resulting schema
1072 public function getTableDef(
1073 $db,
1074 $table,
1075 $crlf,
1076 $error_url,
1077 $show_dates = false,
1078 $add_semicolon = true,
1079 $view = false,
1080 $update_indexes_increments = true,
1081 $aliases = array()
1083 global $sql_drop_table, $sql_backquotes, $sql_constraints,
1084 $sql_constraints_query, $sql_indexes, $sql_indexes_query,
1085 $sql_auto_increments,$sql_drop_foreign_keys;
1087 $db_alias = $db;
1088 $table_alias = $table;
1089 $this->initAlias($aliases, $db_alias, $table_alias);
1091 $schema_create = '';
1092 $auto_increment = '';
1093 $new_crlf = $crlf;
1095 if (isset($GLOBALS['sql_compatibility'])) {
1096 $compat = $GLOBALS['sql_compatibility'];
1097 } else {
1098 $compat = 'NONE';
1101 // need to use PMA_DatabaseInterface::QUERY_STORE
1102 // with $GLOBALS['dbi']->numRows() in mysqli
1103 $result = $GLOBALS['dbi']->query(
1104 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($db)
1105 . ' WHERE Name = \'' . PMA_Util::sqlAddSlashes($table) . '\'',
1106 null,
1107 PMA_DatabaseInterface::QUERY_STORE
1109 if ($result != false) {
1110 if ($GLOBALS['dbi']->numRows($result) > 0) {
1111 $tmpres = $GLOBALS['dbi']->fetchAssoc($result);
1112 if (PMA_DRIZZLE && $show_dates) {
1113 // Drizzle doesn't give Create_time and Update_time in
1114 // SHOW TABLE STATUS, add it
1115 $sql ="SELECT
1116 TABLE_CREATION_TIME AS Create_time,
1117 TABLE_UPDATE_TIME AS Update_time
1118 FROM data_dictionary.TABLES
1119 WHERE TABLE_SCHEMA = '"
1120 . PMA_Util::sqlAddSlashes($db) . "'
1121 AND TABLE_NAME = '"
1122 . PMA_Util::sqlAddSlashes($table) . "'";
1123 $tmpres = array_merge(
1124 $GLOBALS['dbi']->fetchSingleRow($sql), $tmpres
1127 // Here we optionally add the AUTO_INCREMENT next value,
1128 // but starting with MySQL 5.0.24, the clause is already included
1129 // in SHOW CREATE TABLE so we'll remove it below
1130 // It's required for Drizzle because SHOW CREATE TABLE uses
1131 // the value from table's creation time
1132 if (isset($GLOBALS['sql_auto_increment'])
1133 && ! empty($tmpres['Auto_increment'])
1135 $auto_increment .= ' AUTO_INCREMENT='
1136 . $tmpres['Auto_increment'] . ' ';
1139 if ($show_dates
1140 && isset($tmpres['Create_time'])
1141 && ! empty($tmpres['Create_time'])
1143 $schema_create .= $this->_exportComment(
1144 __('Creation:') . ' '
1145 . PMA_Util::localisedDate(
1146 strtotime($tmpres['Create_time'])
1149 $new_crlf = $this->_exportComment() . $crlf;
1152 if ($show_dates
1153 && isset($tmpres['Update_time'])
1154 && ! empty($tmpres['Update_time'])
1156 $schema_create .= $this->_exportComment(
1157 __('Last update:') . ' '
1158 . PMA_Util::localisedDate(
1159 strtotime($tmpres['Update_time'])
1162 $new_crlf = $this->_exportComment() . $crlf;
1165 if ($show_dates
1166 && isset($tmpres['Check_time'])
1167 && ! empty($tmpres['Check_time'])
1169 $schema_create .= $this->_exportComment(
1170 __('Last check:') . ' '
1171 . PMA_Util::localisedDate(
1172 strtotime($tmpres['Check_time'])
1175 $new_crlf = $this->_exportComment() . $crlf;
1178 $GLOBALS['dbi']->freeResult($result);
1181 $schema_create .= $new_crlf;
1183 // no need to generate a DROP VIEW here, it was done earlier
1184 if (! empty($sql_drop_table) && ! PMA_Table::isView($db, $table)) {
1185 $schema_create .= 'DROP TABLE IF EXISTS '
1186 . PMA_Util::backquote($table_alias, $sql_backquotes) . ';'
1187 . $crlf;
1190 // Complete table dump,
1191 // Whether to quote table and column names or not
1192 // Drizzle always quotes names
1193 if (! PMA_DRIZZLE) {
1194 if ($sql_backquotes) {
1195 $GLOBALS['dbi']->query('SET SQL_QUOTE_SHOW_CREATE = 1');
1196 } else {
1197 $GLOBALS['dbi']->query('SET SQL_QUOTE_SHOW_CREATE = 0');
1201 // I don't see the reason why this unbuffered query could cause problems,
1202 // because SHOW CREATE TABLE returns only one row, and we free the
1203 // results below. Nonetheless, we got 2 user reports about this
1204 // (see bug 1562533) so I removed the unbuffered mode.
1205 // $result = $GLOBALS['dbi']->query('SHOW CREATE TABLE ' . backquote($db)
1206 // . '.' . backquote($table), null, PMA_DatabaseInterface::QUERY_UNBUFFERED);
1208 // Note: SHOW CREATE TABLE, at least in MySQL 5.1.23, does not
1209 // produce a displayable result for the default value of a BIT
1210 // column, nor does the mysqldump command. See MySQL bug 35796
1211 $result = $GLOBALS['dbi']->tryQuery(
1212 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.'
1213 . PMA_Util::backquote($table)
1215 // an error can happen, for example the table is crashed
1216 $tmp_error = $GLOBALS['dbi']->getError();
1217 if ($tmp_error) {
1218 return $this->_exportComment(__('in use') . '(' . $tmp_error . ')');
1221 $warning = '';
1222 if ($result != false && ($row = $GLOBALS['dbi']->fetchRow($result))) {
1223 $create_query = $row[1];
1224 unset($row);
1225 // Convert end of line chars to one that we want (note that MySQL
1226 // doesn't return query it will accept in all cases)
1227 if (/*overload*/mb_strpos($create_query, "(\r\n ")) {
1228 $create_query = str_replace("\r\n", $crlf, $create_query);
1229 } elseif (/*overload*/mb_strpos($create_query, "(\n ")) {
1230 $create_query = str_replace("\n", $crlf, $create_query);
1231 } elseif (/*overload*/mb_strpos($create_query, "(\r ")) {
1232 $create_query = str_replace("\r", $crlf, $create_query);
1236 * Drop database name from VIEW creation.
1238 * This is a bit tricky, but we need to issue SHOW CREATE TABLE with
1239 * database name, but we don't want name to show up in CREATE VIEW
1240 * statement.
1242 if ($view) {
1243 $create_query = preg_replace(
1244 '/' . preg_quote(PMA_Util::backquote($db)) . '\./',
1246 $create_query
1249 // substitute aliases in create query
1250 $create_query = $this->replaceWithAliases(
1251 $create_query, $aliases, $db, $table, $flag
1253 // One warning per view
1254 if ($flag && $view) {
1255 $warning = $this->_exportComment()
1256 . $this->_exportComment(
1257 __('It appears your database uses views;')
1259 . $this->_exportComment(
1260 __('alias export may not work reliably in all cases.')
1262 . $this->_exportComment();
1264 // Should we use IF NOT EXISTS?
1265 if (isset($GLOBALS['sql_if_not_exists'])) {
1266 $create_query = preg_replace(
1267 '/^CREATE TABLE/',
1268 'CREATE TABLE IF NOT EXISTS',
1269 $create_query
1273 if ($compat == 'MSSQL') {
1274 $create_query = $this->_makeCreateTableMSSQLCompatible(
1275 $create_query
1279 // Drizzle (checked on 2011.03.13) returns ROW_FORMAT surrounded
1280 // with quotes, which is not accepted by parser
1281 if (PMA_DRIZZLE) {
1282 $create_query = preg_replace(
1283 '/ROW_FORMAT=\'(\S+)\'/',
1284 'ROW_FORMAT=$1',
1285 $create_query
1289 //are there any constraints to cut out?
1290 if (preg_match('@CONSTRAINT|KEY@', $create_query)) {
1291 $has_constraints = 0;
1292 $has_indexes = 0;
1294 //if there are constraints
1295 if (preg_match(
1296 '@CONSTRAINT@',
1297 $create_query
1298 )) {
1299 $has_constraints = 1;
1300 // comments -> constraints for dumped tables
1301 $sql_constraints = $this->generateComment(
1302 $crlf, $sql_constraints, __('Constraints for dumped tables'),
1303 __('Constraints for table'), $table_alias, $compat
1306 $sql_constraints_query .= 'ALTER TABLE '
1307 . PMA_Util::backquoteCompat(
1308 $table_alias, $compat, $sql_backquotes
1310 . $crlf;
1311 $sql_constraints .= 'ALTER TABLE '
1312 . PMA_Util::backquoteCompat(
1313 $table_alias, $compat, $sql_backquotes
1315 . $crlf;
1316 $sql_drop_foreign_keys .= 'ALTER TABLE '
1317 . PMA_Util::backquoteCompat(
1318 $db_alias, $compat, $sql_backquotes
1320 . '.'
1321 . PMA_Util::backquoteCompat(
1322 $table_alias, $compat, $sql_backquotes
1324 . $crlf;
1326 //if there are indexes
1327 // (look for KEY followed by whitespace to avoid matching
1328 // keywords like PACK_KEYS)
1329 if ($update_indexes_increments && preg_match(
1330 '@KEY[\s]+@',
1331 $create_query
1332 )) {
1333 $has_indexes = 1;
1335 // comments -> indexes for dumped tables
1336 $sql_indexes = $this->generateComment(
1337 $crlf, $sql_indexes, __('Indexes for dumped tables'),
1338 __('Indexes for table'), $table_alias, $compat
1340 $sql_indexes_query_start = 'ALTER TABLE '
1341 . PMA_Util::backquoteCompat(
1342 $table_alias, $compat, $sql_backquotes
1344 $sql_indexes_query .= $sql_indexes_query_start;
1346 $sql_indexes_start = 'ALTER TABLE '
1347 . PMA_Util::backquoteCompat(
1348 $table_alias, $compat, $sql_backquotes
1350 $sql_indexes .= $sql_indexes_start;
1352 if ($update_indexes_increments && preg_match(
1353 '@AUTO_INCREMENT@',
1354 $create_query
1355 )) {
1356 // comments -> auto increments for dumped tables
1357 $sql_auto_increments = $this->generateComment(
1358 $crlf, $sql_auto_increments,
1359 __('AUTO_INCREMENT for dumped tables'),
1360 __('AUTO_INCREMENT for table'), $table_alias, $compat
1362 $sql_auto_increments .= 'ALTER TABLE '
1363 . PMA_Util::backquoteCompat(
1364 $table_alias, $compat, $sql_backquotes
1366 . $crlf;
1369 // Split the query into lines, so we can easily handle it.
1370 // We know lines are separated by $crlf (done few lines above).
1371 $sql_lines = explode($crlf, $create_query);
1372 $sql_count = count($sql_lines);
1374 // lets find first line with constraints
1375 $first_occur = -1;
1376 for ($i = 0; $i < $sql_count; $i++) {
1377 $sql_line = current(explode(' COMMENT ', $sql_lines[$i], 2));
1378 if (preg_match(
1379 '@[\s]+(CONSTRAINT|KEY)@',
1380 $sql_line
1381 ) && $first_occur == -1) {
1382 $first_occur = $i;
1386 for ($k = 0; $k < $sql_count; $k++) {
1387 if ($update_indexes_increments && preg_match(
1388 '( AUTO_INCREMENT | AUTO_INCREMENT,| AUTO_INCREMENT$)',
1389 $sql_lines[$k]
1390 )) {
1391 //creates auto increment code
1392 $sql_auto_increments .= " MODIFY " . ltrim($sql_lines[$k]);
1393 //removes auto increment code from table definition
1394 $sql_lines[$k] = str_replace(
1395 " AUTO_INCREMENT", "", $sql_lines[$k]
1398 if (isset($GLOBALS['sql_auto_increment'])
1399 && $update_indexes_increments && preg_match(
1400 '@[\s]+(AUTO_INCREMENT=)@',
1401 $sql_lines[$k]
1402 )) {
1403 //adds auto increment value
1404 $increment_value = /*overload*/mb_substr(
1405 $sql_lines[$k],
1406 /*overload*/mb_strpos($sql_lines[$k], "AUTO_INCREMENT")
1408 $increment_value_array = explode(' ', $increment_value);
1409 $sql_auto_increments .= $increment_value_array[0] . ";";
1414 if ($sql_auto_increments != '') {
1415 $sql_auto_increments = /*overload*/mb_substr(
1416 $sql_auto_increments, 0, -1
1417 ) . ';';
1419 // If we really found a constraint
1420 if ($first_occur != $sql_count) {
1421 // lets find first line
1422 $sql_lines[$first_occur - 1] = preg_replace(
1423 '@,$@',
1425 $sql_lines[$first_occur - 1]
1428 $first = true;
1429 $sql_index_ended = false;
1430 for ($j = $first_occur; $j < $sql_count; $j++) {
1431 //removes extra space at the beginning, if there is
1432 $sql_lines[$j]=ltrim($sql_lines[$j], ' ');
1434 //if it's a constraint
1435 if (preg_match(
1436 '@CONSTRAINT|FOREIGN[\s]+KEY@',
1437 $sql_lines[$j]
1438 )) {
1439 if (! $first) {
1440 $sql_constraints .= $crlf;
1442 $posConstraint = /*overload*/mb_strpos(
1443 $sql_lines[$j],
1444 'CONSTRAINT'
1446 if ($posConstraint === false) {
1447 $tmp_str = preg_replace(
1448 '/(FOREIGN[\s]+KEY)/',
1449 ' ADD \1',
1450 $sql_lines[$j]
1453 $sql_constraints_query .= $tmp_str;
1454 $sql_constraints .= $tmp_str;
1456 } else {
1457 $tmp_str = preg_replace(
1458 '/(CONSTRAINT)/',
1459 ' ADD \1',
1460 $sql_lines[$j]
1463 $sql_constraints_query .= $tmp_str;
1464 $sql_constraints .= $tmp_str;
1465 preg_match(
1466 '/(CONSTRAINT)([\s])([\S]*)([\s])/',
1467 $sql_lines[$j],
1468 $matches
1470 if (! $first) {
1471 $sql_drop_foreign_keys .= ', ';
1473 $sql_drop_foreign_keys .= 'DROP FOREIGN KEY '
1474 . $matches[3];
1476 $first = false;
1477 } else if ($update_indexes_increments && preg_match(
1478 '@KEY[\s]+@',
1479 $sql_lines[$j]
1480 )) {
1481 //if it's a index
1483 // if index query was terminated earlier
1484 if ($sql_index_ended) {
1485 // start a new query with ALTER TABLE
1486 $sql_indexes .= $sql_indexes_start;
1487 $sql_indexes_query .= $sql_indexes_query_start;
1489 $sql_index_ended = false;
1492 $tmp_str = $crlf . " ADD " . $sql_lines[$j];
1493 $sql_indexes_query .= $tmp_str;
1494 $sql_indexes .= $tmp_str;
1496 // InnoDB supports one FULLTEXT index creation at a time
1497 // So end the query and start over
1498 if ($update_indexes_increments && preg_match(
1499 '@FULLTEXT KEY[\s]+@',
1500 $sql_lines[$j]
1501 )) {
1502 //removes superfluous comma at the end
1503 $sql_indexes = rtrim($sql_indexes, ',');
1504 $sql_indexes_query = rtrim($sql_indexes_query, ',');
1506 // add ending semicolon
1507 $sql_indexes .= ';' . $crlf;
1508 $sql_indexes_query .= ';' . $crlf;
1510 $sql_index_ended = true;
1512 } else {
1513 break;
1516 //removes superfluous comma at the end
1517 $sql_indexes = rtrim($sql_indexes, ',');
1518 $sql_indexes_query = rtrim($sql_indexes_query, ',');
1519 //removes superfluous semicolon at the end
1520 if ($has_constraints == 1) {
1521 $sql_constraints .= ';' . $crlf;
1522 $sql_constraints_query .= ';';
1524 if ($has_indexes == 1 && ! $sql_index_ended) {
1525 $sql_indexes .= ';' . $crlf;
1526 $sql_indexes_query .= ';';
1528 //remove indexes and constraints from the $create_query
1529 $create_query = implode(
1530 $crlf,
1531 array_slice($sql_lines, 0, $first_occur)
1533 . $crlf
1534 . implode(
1535 $crlf,
1536 array_slice($sql_lines, $j, $sql_count - 1)
1538 unset($sql_lines);
1541 $schema_create .= $create_query;
1544 // remove a possible "AUTO_INCREMENT = value" clause
1545 // that could be there starting with MySQL 5.0.24
1546 // in Drizzle it's useless as it contains the value given at table
1547 // creation time
1548 if (preg_match('/AUTO_INCREMENT\s*=\s*([0-9])+/', $schema_create)) {
1549 if ($compat == 'MSSQL' || $auto_increment == '') {
1550 $auto_increment = ' ';
1552 $schema_create = preg_replace(
1553 '/\sAUTO_INCREMENT\s*=\s*([0-9])+\s/',
1554 $auto_increment,
1555 $schema_create
1559 $GLOBALS['dbi']->freeResult($result);
1560 return $warning . $schema_create . ($add_semicolon ? ';' . $crlf : '');
1561 } // end of the 'getTableDef()' function
1564 * Returns $table's comments, relations etc.
1566 * @param string $db database name
1567 * @param string $table table name
1568 * @param string $crlf end of line sequence
1569 * @param bool $do_relation whether to include relation comments
1570 * @param bool $do_mime whether to include mime comments
1571 * @param array $aliases Aliases of db/table/columns
1573 * @return string resulting comments
1575 private function _getTableComments(
1576 $db,
1577 $table,
1578 $crlf,
1579 $do_relation = false,
1580 $do_mime = false,
1581 $aliases = array()
1583 global $cfgRelation, $sql_backquotes;
1585 $db_alias = $db;
1586 $table_alias = $table;
1587 $this->initAlias($aliases, $db_alias, $table_alias);
1589 $schema_create = '';
1591 // Check if we can use Relations
1592 list($res_rel, $have_rel) = PMA_getRelationsAndStatus(
1593 $do_relation && ! empty($cfgRelation['relation']),
1594 $db,
1595 $table
1598 if ($do_mime && $cfgRelation['mimework']) {
1599 if (! ($mime_map = PMA_getMIME($db, $table, true))) {
1600 unset($mime_map);
1604 if (isset($mime_map) && count($mime_map) > 0) {
1605 $schema_create .= $this->_possibleCRLF()
1606 . $this->_exportComment()
1607 . $this->_exportComment(
1608 __('MIME TYPES FOR TABLE') . ' '
1609 . PMA_Util::backquote($table, $sql_backquotes) . ':'
1611 @reset($mime_map);
1612 foreach ($mime_map as $mime_field => $mime) {
1613 $schema_create .=
1614 $this->_exportComment(
1616 . PMA_Util::backquote($mime_field, $sql_backquotes)
1618 . $this->_exportComment(
1620 . PMA_Util::backquote(
1621 $mime['mimetype'],
1622 $sql_backquotes
1626 $schema_create .= $this->_exportComment();
1629 if ($have_rel) {
1630 $schema_create .= $this->_possibleCRLF()
1631 . $this->_exportComment()
1632 . $this->_exportComment(
1633 __('RELATIONS FOR TABLE') . ' '
1634 . PMA_Util::backquote($table_alias, $sql_backquotes)
1635 . ':'
1638 foreach ($res_rel as $rel_field => $rel) {
1639 if ($rel_field != 'foreign_keys_data') {
1640 $rel_field_alias = !empty(
1641 $aliases[$db]['tables'][$table]['columns'][$rel_field]
1642 ) ? $aliases[$db]['tables'][$table]['columns'][$rel_field]
1643 : $rel_field;
1644 $schema_create .=
1645 $this->_exportComment(
1647 . PMA_Util::backquote($rel_field_alias, $sql_backquotes)
1649 . $this->_exportComment(
1651 . PMA_Util::backquote(
1652 $rel['foreign_table'],
1653 $sql_backquotes
1655 . ' -> '
1656 . PMA_Util::backquote(
1657 $rel['foreign_field'],
1658 $sql_backquotes
1661 } else {
1662 foreach ($rel as $one_key) {
1663 foreach ($one_key['index_list'] as $index => $field) {
1664 $rel_field_alias = !empty(
1665 $aliases[$db]['tables'][$table]['columns'][$field]
1666 ) ? $aliases[$db]['tables'][$table]['columns'][$field]
1667 : $field;
1668 $schema_create .=
1669 $this->_exportComment(
1671 . PMA_Util::backquote(
1672 $rel_field_alias,
1673 $sql_backquotes
1676 . $this->_exportComment(
1678 . PMA_Util::backquote(
1679 $one_key['ref_table_name'],
1680 $sql_backquotes
1682 . ' -> '
1683 . PMA_Util::backquote(
1684 $one_key['ref_index_list'][$index],
1685 $sql_backquotes
1692 $schema_create .= $this->_exportComment();
1695 return $schema_create;
1697 } // end of the '_getTableComments()' function
1700 * Outputs table's structure
1702 * @param string $db database name
1703 * @param string $table table name
1704 * @param string $crlf the end of line sequence
1705 * @param string $error_url the url to go back in case of error
1706 * @param string $export_mode 'create_table','triggers','create_view',
1707 * 'stand_in'
1708 * @param string $export_type 'server', 'database', 'table'
1709 * @param bool $relation whether to include relation comments
1710 * @param bool $comments whether to include the pmadb-style column
1711 * comments as comments in the structure; this is
1712 * deprecated but the parameter is left here
1713 * because export.php calls exportStructure()
1714 * also for other export types which use this
1715 * parameter
1716 * @param bool $mime whether to include mime comments
1717 * @param bool $dates whether to include creation/update/check dates
1718 * @param array $aliases Aliases of db/table/columns
1720 * @return bool Whether it succeeded
1722 public function exportStructure(
1723 $db,
1724 $table,
1725 $crlf,
1726 $error_url,
1727 $export_mode,
1728 $export_type,
1729 $relation = false,
1730 $comments = false,
1731 $mime = false,
1732 $dates = false,
1733 $aliases = array()
1735 $db_alias = $db;
1736 $table_alias = $table;
1737 $this->initAlias($aliases, $db_alias, $table_alias);
1738 if (isset($GLOBALS['sql_compatibility'])) {
1739 $compat = $GLOBALS['sql_compatibility'];
1740 } else {
1741 $compat = 'NONE';
1744 $formatted_table_name = PMA_Util::backquoteCompat(
1745 $table_alias, $compat, isset($GLOBALS['sql_backquotes'])
1747 $dump = $this->_possibleCRLF()
1748 . $this->_exportComment(str_repeat('-', 56))
1749 . $this->_possibleCRLF()
1750 . $this->_exportComment();
1752 switch($export_mode) {
1753 case 'create_table':
1754 $dump .= $this->_exportComment(
1755 __('Table structure for table') . ' ' . $formatted_table_name
1757 $dump .= $this->_exportComment();
1758 $dump .= $this->getTableDef(
1759 $db, $table, $crlf, $error_url, $dates,
1760 true, false, true, $aliases
1762 $dump .= $this->_getTableComments(
1763 $db, $table, $crlf, $relation, $mime, $aliases
1765 break;
1766 case 'triggers':
1767 $dump = '';
1768 $delimiter = '$$';
1769 $triggers = $GLOBALS['dbi']->getTriggers($db, $table, $delimiter);
1770 if ($triggers) {
1771 $dump .= $this->_possibleCRLF()
1772 . $this->_exportComment()
1773 . $this->_exportComment(
1774 __('Triggers') . ' ' . $formatted_table_name
1776 . $this->_exportComment();
1777 $used_alias = false;
1778 $trigger_query = '';
1779 foreach ($triggers as $trigger) {
1780 if (! empty($GLOBALS['sql_drop_table'])) {
1781 $trigger_query .= $trigger['drop'] . ';' . $crlf;
1784 $trigger_query .= 'DELIMITER ' . $delimiter . $crlf;
1785 $trigger_query .= $this->replaceWithAliases(
1786 $trigger['create'], $aliases, $db, $table, $flag
1788 if ($flag) {
1789 $used_alias = true;
1791 $trigger_query .= 'DELIMITER ;' . $crlf;
1793 // One warning per table.
1794 if ($used_alias) {
1795 $dump .= $this->_exportComment(
1796 __('It appears your table uses triggers;')
1798 . $this->_exportComment(
1799 __('alias export may not work reliably in all cases.')
1801 . $this->_exportComment();
1803 $dump .= $trigger_query;
1805 break;
1806 case 'create_view':
1807 if (empty($GLOBALS['sql_views_as_tables'])) {
1808 $dump .=
1809 $this->_exportComment(
1810 __('Structure for view')
1811 . ' '
1812 . $formatted_table_name
1814 . $this->_exportComment();
1815 // delete the stand-in table previously created (if any)
1816 if ($export_type != 'table') {
1817 $dump .= 'DROP TABLE IF EXISTS '
1818 . PMA_Util::backquote($table_alias) . ';' . $crlf;
1820 $dump .= $this->getTableDef(
1821 $db, $table, $crlf, $error_url, $dates,
1822 true, true, true, $aliases
1824 } else {
1825 $dump .=
1826 $this->_exportComment(
1827 sprintf(
1828 __('Structure for view %s exported as a table'),
1829 $formatted_table_name
1832 . $this->_exportComment();
1833 // delete the stand-in table previously created (if any)
1834 if ($export_type != 'table') {
1835 $dump .= 'DROP TABLE IF EXISTS '
1836 . PMA_Util::backquote($table_alias) . ';' . $crlf;
1838 $dump .= $this->_getTableDefForView(
1839 $db, $table, $crlf, true, $aliases
1842 break;
1843 case 'stand_in':
1844 $dump .=
1845 $this->_exportComment(
1846 __('Stand-in structure for view') . ' ' . $formatted_table_name
1848 . $this->_exportComment();
1849 // export a stand-in definition to resolve view dependencies
1850 $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases);
1851 } // end switch
1853 // this one is built by getTableDef() to use in table copy/move
1854 // but not in the case of export
1855 unset($GLOBALS['sql_constraints_query']);
1857 return PMA_exportOutputHandler($dump);
1861 * Outputs the content of a table in SQL format
1863 * @param string $db database name
1864 * @param string $table table name
1865 * @param string $crlf the end of line sequence
1866 * @param string $error_url the url to go back in case of error
1867 * @param string $sql_query SQL query for obtaining data
1868 * @param array $aliases Aliases of db/table/columns
1870 * @return bool Whether it succeeded
1872 public function exportData(
1873 $db, $table, $crlf, $error_url, $sql_query, $aliases = array()
1875 global $current_row, $sql_backquotes;
1877 $db_alias = $db;
1878 $table_alias = $table;
1879 $this->initAlias($aliases, $db_alias, $table_alias);
1881 if (isset($GLOBALS['sql_compatibility'])) {
1882 $compat = $GLOBALS['sql_compatibility'];
1883 } else {
1884 $compat = 'NONE';
1887 $formatted_table_name = PMA_Util::backquoteCompat(
1888 $table_alias, $compat, $sql_backquotes
1891 // Do not export data for a VIEW, unless asked to export the view as a table
1892 // (For a VIEW, this is called only when exporting a single VIEW)
1893 if (PMA_Table::isView($db, $table)
1894 && empty($GLOBALS['sql_views_as_tables'])
1896 $head = $this->_possibleCRLF()
1897 . $this->_exportComment()
1898 . $this->_exportComment('VIEW ' . ' ' . $formatted_table_name)
1899 . $this->_exportComment(__('Data:') . ' ' . __('None'))
1900 . $this->_exportComment()
1901 . $this->_possibleCRLF();
1903 if (! PMA_exportOutputHandler($head)) {
1904 return false;
1906 return true;
1909 $result = $GLOBALS['dbi']->tryQuery(
1910 $sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED
1912 // a possible error: the table has crashed
1913 $tmp_error = $GLOBALS['dbi']->getError();
1914 if ($tmp_error) {
1915 return PMA_exportOutputHandler(
1916 $this->_exportComment(
1917 __('Error reading data:') . ' (' . $tmp_error . ')'
1922 if ($result == false) {
1923 $GLOBALS['dbi']->freeResult($result);
1924 return true;
1927 $fields_cnt = $GLOBALS['dbi']->numFields($result);
1929 // Get field information
1930 $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
1931 $field_flags = array();
1932 for ($j = 0; $j < $fields_cnt; $j++) {
1933 $field_flags[$j] = $GLOBALS['dbi']->fieldFlags($result, $j);
1936 $field_set = array();
1937 for ($j = 0; $j < $fields_cnt; $j++) {
1938 $col_as = $fields_meta[$j]->name;
1939 if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
1940 $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
1942 $field_set[$j] = PMA_Util::backquoteCompat(
1943 $col_as,
1944 $compat,
1945 $sql_backquotes
1949 if (isset($GLOBALS['sql_type'])
1950 && $GLOBALS['sql_type'] == 'UPDATE'
1952 // update
1953 $schema_insert = 'UPDATE ';
1954 if (isset($GLOBALS['sql_ignore'])) {
1955 $schema_insert .= 'IGNORE ';
1957 // avoid EOL blank
1958 $schema_insert .= PMA_Util::backquoteCompat(
1959 $table_alias,
1960 $compat,
1961 $sql_backquotes
1962 ) . ' SET';
1963 } else {
1964 // insert or replace
1965 if (isset($GLOBALS['sql_type'])
1966 && $GLOBALS['sql_type'] == 'REPLACE'
1968 $sql_command = 'REPLACE';
1969 } else {
1970 $sql_command = 'INSERT';
1973 // delayed inserts?
1974 if (isset($GLOBALS['sql_delayed'])) {
1975 $insert_delayed = ' DELAYED';
1976 } else {
1977 $insert_delayed = '';
1980 // insert ignore?
1981 if (isset($GLOBALS['sql_type'])
1982 && $GLOBALS['sql_type'] == 'INSERT'
1983 && isset($GLOBALS['sql_ignore'])
1985 $insert_delayed .= ' IGNORE';
1987 //truncate table before insert
1988 if (isset($GLOBALS['sql_truncate'])
1989 && $GLOBALS['sql_truncate']
1990 && $sql_command == 'INSERT'
1992 $truncate = 'TRUNCATE TABLE '
1993 . PMA_Util::backquoteCompat(
1994 $table_alias,
1995 $compat,
1996 $sql_backquotes
1997 ) . ";";
1998 $truncatehead = $this->_possibleCRLF()
1999 . $this->_exportComment()
2000 . $this->_exportComment(
2001 __('Truncate table before insert') . ' '
2002 . $formatted_table_name
2004 . $this->_exportComment()
2005 . $crlf;
2006 PMA_exportOutputHandler($truncatehead);
2007 PMA_exportOutputHandler($truncate);
2010 // scheme for inserting fields
2011 if ($GLOBALS['sql_insert_syntax'] == 'complete'
2012 || $GLOBALS['sql_insert_syntax'] == 'both'
2014 $fields = implode(', ', $field_set);
2015 $schema_insert = $sql_command . $insert_delayed . ' INTO '
2016 . PMA_Util::backquoteCompat(
2017 $table_alias,
2018 $compat,
2019 $sql_backquotes
2021 // avoid EOL blank
2022 . ' (' . $fields . ') VALUES';
2023 } else {
2024 $schema_insert = $sql_command . $insert_delayed . ' INTO '
2025 . PMA_Util::backquoteCompat(
2026 $table_alias,
2027 $compat,
2028 $sql_backquotes
2030 . ' VALUES';
2034 //\x08\\x09, not required
2035 $search = array("\x00", "\x0a", "\x0d", "\x1a");
2036 $replace = array('\0', '\n', '\r', '\Z');
2037 $current_row = 0;
2038 $query_size = 0;
2039 if (($GLOBALS['sql_insert_syntax'] == 'extended'
2040 || $GLOBALS['sql_insert_syntax'] == 'both')
2041 && (! isset($GLOBALS['sql_type'])
2042 || $GLOBALS['sql_type'] != 'UPDATE')
2044 $separator = ',';
2045 $schema_insert .= $crlf;
2046 } else {
2047 $separator = ';';
2050 while ($row = $GLOBALS['dbi']->fetchRow($result)) {
2051 if ($current_row == 0) {
2052 $head = $this->_possibleCRLF()
2053 . $this->_exportComment()
2054 . $this->_exportComment(
2055 __('Dumping data for table') . ' '
2056 . $formatted_table_name
2058 . $this->_exportComment()
2059 . $crlf;
2060 if (! PMA_exportOutputHandler($head)) {
2061 return false;
2064 // We need to SET IDENTITY_INSERT ON for MSSQL
2065 if (isset($GLOBALS['sql_compatibility'])
2066 && $GLOBALS['sql_compatibility'] == 'MSSQL'
2067 && $current_row == 0
2069 if (! PMA_exportOutputHandler(
2070 'SET IDENTITY_INSERT '
2071 . PMA_Util::backquoteCompat(
2072 $table_alias,
2073 $compat,
2074 $sql_backquotes
2076 . ' ON ;' . $crlf
2077 )) {
2078 return false;
2081 $current_row++;
2082 $values = array();
2083 for ($j = 0; $j < $fields_cnt; $j++) {
2084 // NULL
2085 if (! isset($row[$j]) || is_null($row[$j])) {
2086 $values[] = 'NULL';
2087 } elseif ($fields_meta[$j]->numeric
2088 && $fields_meta[$j]->type != 'timestamp'
2089 && ! $fields_meta[$j]->blob
2091 // a number
2092 // timestamp is numeric on some MySQL 4.1, BLOBs are
2093 // sometimes numeric
2094 $values[] = $row[$j];
2095 } elseif (stristr($field_flags[$j], 'BINARY') !== false
2096 && isset($GLOBALS['sql_hex_for_binary'])
2098 // a true BLOB
2099 // - mysqldump only generates hex data when the --hex-blob
2100 // option is used, for fields having the binary attribute
2101 // no hex is generated
2102 // - a TEXT field returns type blob but a real blob
2103 // returns also the 'binary' flag
2105 // empty blobs need to be different, but '0' is also empty
2106 // :-(
2107 if (empty($row[$j]) && $row[$j] != '0') {
2108 $values[] = '\'\'';
2109 } else {
2110 $values[] = '0x' . bin2hex($row[$j]);
2112 } elseif ($fields_meta[$j]->type == 'bit') {
2113 // detection of 'bit' works only on mysqli extension
2114 $values[] = "b'" . PMA_Util::sqlAddSlashes(
2115 PMA_Util::printableBitValue(
2116 $row[$j], $fields_meta[$j]->length
2119 . "'";
2120 } else {
2121 // something else -> treat as a string
2122 $values[] = '\''
2123 . str_replace(
2124 $search, $replace,
2125 PMA_Util::sqlAddSlashes($row[$j])
2127 . '\'';
2128 } // end if
2129 } // end for
2131 // should we make update?
2132 if (isset($GLOBALS['sql_type'])
2133 && $GLOBALS['sql_type'] == 'UPDATE'
2136 $insert_line = $schema_insert;
2137 for ($i = 0; $i < $fields_cnt; $i++) {
2138 if (0 == $i) {
2139 $insert_line .= ' ';
2141 if ($i > 0) {
2142 // avoid EOL blank
2143 $insert_line .= ',';
2145 $insert_line .= $field_set[$i] . ' = ' . $values[$i];
2148 list($tmp_unique_condition, $tmp_clause_is_unique)
2149 = PMA_Util::getUniqueCondition(
2150 $result,
2151 $fields_cnt,
2152 $fields_meta,
2153 $row
2155 $insert_line .= ' WHERE ' . $tmp_unique_condition;
2156 unset($tmp_unique_condition, $tmp_clause_is_unique);
2158 } else {
2160 // Extended inserts case
2161 if ($GLOBALS['sql_insert_syntax'] == 'extended'
2162 || $GLOBALS['sql_insert_syntax'] == 'both'
2164 if ($current_row == 1) {
2165 $insert_line = $schema_insert . '('
2166 . implode(', ', $values) . ')';
2167 } else {
2168 $insert_line = '(' . implode(', ', $values) . ')';
2169 $insertLineSize = /*overload*/mb_strlen($insert_line);
2170 $sql_max_size = $GLOBALS['sql_max_query_size'];
2171 if (isset($sql_max_size)
2172 && $sql_max_size > 0
2173 && $query_size + $insertLineSize > $sql_max_size
2175 if (! PMA_exportOutputHandler(';' . $crlf)) {
2176 return false;
2178 $query_size = 0;
2179 $current_row = 1;
2180 $insert_line = $schema_insert . $insert_line;
2183 $query_size += /*overload*/mb_strlen($insert_line);
2184 // Other inserts case
2185 } else {
2186 $insert_line = $schema_insert
2187 . '(' . implode(', ', $values) . ')';
2190 unset($values);
2192 if (! PMA_exportOutputHandler(
2193 ($current_row == 1 ? '' : $separator . $crlf)
2194 . $insert_line
2195 )) {
2196 return false;
2199 } // end while
2201 if ($current_row > 0) {
2202 if (! PMA_exportOutputHandler(';' . $crlf)) {
2203 return false;
2207 // We need to SET IDENTITY_INSERT OFF for MSSQL
2208 if (isset($GLOBALS['sql_compatibility'])
2209 && $GLOBALS['sql_compatibility'] == 'MSSQL'
2210 && $current_row > 0
2212 $outputSucceeded = PMA_exportOutputHandler(
2213 $crlf . 'SET IDENTITY_INSERT '
2214 . PMA_Util::backquoteCompat(
2215 $table_alias, $compat, $sql_backquotes
2217 . ' OFF;' . $crlf
2219 if (! $outputSucceeded) {
2220 return false;
2224 $GLOBALS['dbi']->freeResult($result);
2225 return true;
2226 } // end of the 'exportData()' function
2229 * Make a create table statement compatible with MSSQL
2231 * @param string $create_query MySQL create table statement
2233 * @return string MSSQL compatible create table statement
2235 private function _makeCreateTableMSSQLCompatible($create_query)
2237 // In MSSQL
2238 // 1. No 'IF NOT EXISTS' in CREATE TABLE
2239 // 2. DATE field doesn't exists, we will use DATETIME instead
2240 // 3. UNSIGNED attribute doesn't exist
2241 // 4. No length on INT, TINYINT, SMALLINT, BIGINT and no precision on
2242 // FLOAT fields
2243 // 5. No KEY and INDEX inside CREATE TABLE
2244 // 6. DOUBLE field doesn't exists, we will use FLOAT instead
2246 $create_query = preg_replace(
2247 "/^CREATE TABLE IF NOT EXISTS/",
2248 'CREATE TABLE',
2249 $create_query
2251 // first we need to replace all lines ended with '" DATE ...,\n'
2252 // last preg_replace preserve us from situation with date text
2253 // inside DEFAULT field value
2254 $create_query = preg_replace(
2255 "/\" date DEFAULT NULL(,)?\n/",
2256 '" datetime DEFAULT NULL$1' . "\n",
2257 $create_query
2259 $create_query = preg_replace(
2260 "/\" date NOT NULL(,)?\n/",
2261 '" datetime NOT NULL$1' . "\n",
2262 $create_query
2264 $create_query = preg_replace(
2265 '/" date NOT NULL DEFAULT \'([^\'])/',
2266 '" datetime NOT NULL DEFAULT \'$1',
2267 $create_query
2270 // next we need to replace all lines ended with ') UNSIGNED ...,'
2271 // last preg_replace preserve us from situation with unsigned text
2272 // inside DEFAULT field value
2273 $create_query = preg_replace(
2274 "/\) unsigned NOT NULL(,)?\n/",
2275 ') NOT NULL$1' . "\n",
2276 $create_query
2278 $create_query = preg_replace(
2279 "/\) unsigned DEFAULT NULL(,)?\n/",
2280 ') DEFAULT NULL$1' . "\n",
2281 $create_query
2283 $create_query = preg_replace(
2284 '/\) unsigned NOT NULL DEFAULT \'([^\'])/',
2285 ') NOT NULL DEFAULT \'$1',
2286 $create_query
2289 // we need to replace all lines ended with
2290 // '" INT|TINYINT([0-9]{1,}) ...,' last preg_replace preserve us
2291 // from situation with int([0-9]{1,}) text inside DEFAULT field
2292 // value
2293 $create_query = preg_replace(
2294 '/" (int|tinyint|smallint|bigint)\([0-9]+\) DEFAULT NULL(,)?\n/',
2295 '" $1 DEFAULT NULL$2' . "\n",
2296 $create_query
2298 $create_query = preg_replace(
2299 '/" (int|tinyint|smallint|bigint)\([0-9]+\) NOT NULL(,)?\n/',
2300 '" $1 NOT NULL$2' . "\n",
2301 $create_query
2303 $create_query = preg_replace(
2304 '/" (int|tinyint|smallint|bigint)\([0-9]+\) NOT NULL DEFAULT \'([^\'])/',
2305 '" $1 NOT NULL DEFAULT \'$2',
2306 $create_query
2309 // we need to replace all lines ended with
2310 // '" FLOAT|DOUBLE([0-9,]{1,}) ...,'
2311 // last preg_replace preserve us from situation with
2312 // float([0-9,]{1,}) text inside DEFAULT field value
2313 $create_query = preg_replace(
2314 '/" (float|double)(\([0-9]+,[0-9,]+\))? DEFAULT NULL(,)?\n/',
2315 '" float DEFAULT NULL$3' . "\n",
2316 $create_query
2318 $create_query = preg_replace(
2319 '/" (float|double)(\([0-9,]+,[0-9,]+\))? NOT NULL(,)?\n/',
2320 '" float NOT NULL$3' . "\n",
2321 $create_query
2323 $create_query = preg_replace(
2324 '/" (float|double)(\([0-9,]+,[0-9,]+\))? NOT NULL DEFAULT \'([^\'])/',
2325 '" float NOT NULL DEFAULT \'$3',
2326 $create_query
2329 // @todo remove indexes from CREATE TABLE
2331 return $create_query;
2335 * replaces db/table/column names with their aliases
2337 * @param string $sql_query SQL query in which aliases are to be substituted
2338 * @param array $aliases Alias information for db/table/column
2339 * @param string $db the database name
2340 * @param string $table the tablename
2341 * @param string &$flag the flag denoting whether any replacement was done
2343 * @return string query replaced with aliases
2345 public function replaceWithAliases(
2346 $sql_query, $aliases, $db, $table = '', &$flag = null
2348 $flag = false;
2349 // Return original sql query if no aliases are provided.
2350 if (!is_array($aliases) || empty($aliases) || empty($sql_query)) {
2351 return $sql_query;
2353 $supported_query_types = array(
2354 'CREATE' => true,
2356 $supported_query_ons = array(
2357 'TABLE' => true,
2358 'VIEW' => true,
2359 'TRIGGER' => true,
2360 'FUNCTION' => true,
2361 'PROCEDURE' => true
2363 $identifier_types = array(
2364 'alpha_identifier',
2365 'quote_backtick'
2367 $query_type = '';
2368 $query_on = '';
2369 // Adjustment value for each pos value
2370 // of token after replacement
2371 $offset = 0;
2372 $open_braces = 0;
2373 $in_create_table_fields = false;
2374 // flag to force end query parsing
2375 $query_end = false;
2376 // Convert all line feeds to Unix style
2377 $sql_query = str_replace("\r\n", "\n", $sql_query);
2378 $sql_query = str_replace("\r", "\n", $sql_query);
2379 $tokens = PMA_SQP_parse($sql_query);
2380 $ref_seen = false;
2381 $ref_table_seen = false;
2382 $old_table = $table;
2383 $on_seen = false;
2384 $size = $tokens['len'];
2386 for ($i = 0; $i < $size && !$query_end; $i++) {
2387 $type = $tokens[$i]['type'];
2388 $data = $tokens[$i]['data'];
2389 $data_next = isset($tokens[$i+1]['data'])
2390 ? $tokens[$i+1]['data'] : '';
2391 $data_prev = ($i > 0) ? $tokens[$i-1]['data'] : '';
2392 $d_unq = PMA_Util::unQuote($data);
2393 $d_unq_next = PMA_Util::unQuote($data_next);
2394 $d_unq_prev = PMA_Util::unQuote($data_prev);
2395 $d_upper = /*overload*/mb_strtoupper($d_unq);
2396 $d_upper_next = /*overload*/mb_strtoupper($d_unq_next);
2397 $d_upper_prev = /*overload*/mb_strtoupper($d_unq_prev);
2398 $pos = $tokens[$i]['pos'] + $offset;
2399 if ($type === 'alpha_reservedWord') {
2400 if ($query_type === ''
2401 && !empty($supported_query_types[$d_upper])
2403 $query_type = $d_upper;
2404 } elseif ($query_on === ''
2405 && !empty($supported_query_ons[$d_upper])
2407 $query_on = $d_upper;
2410 // CREATE TABLE - Alias replacement
2411 if ($query_type === 'CREATE' && $query_on === 'TABLE') {
2412 // replace create table name
2413 if (!$in_create_table_fields
2414 && in_array($type, $identifier_types)
2415 && !empty($aliases[$db]['tables'][$table]['alias'])
2417 $sql_query = $this->substituteAlias(
2418 $sql_query, $data,
2419 $aliases[$db]['tables'][$table]['alias'],
2420 $pos, $offset
2422 $flag = true;
2423 } elseif ($type === 'punct_bracket_open_round') {
2424 // CREATE TABLE fields started
2425 if (!$in_create_table_fields) {
2426 $in_create_table_fields = true;
2428 $open_braces++;
2429 } elseif ($type === 'punct_bracket_close_round') {
2430 // end our parsing after last )
2431 // no columns appear after that
2432 if ($in_create_table_fields && $open_braces === 0) {
2433 $query_end = true;
2435 // End of Foreign key reference
2436 if ($ref_seen) {
2437 $ref_seen = $ref_table_seen = false;
2438 $table = $old_table;
2440 $open_braces--;
2441 // handles Foreign key references
2442 } elseif ($type === 'alpha_reservedWord'
2443 && $d_upper === 'REFERENCES'
2445 $ref_seen = true;
2446 } elseif (in_array($type, $identifier_types)
2447 && $ref_seen === true && !$ref_table_seen
2449 $table = $d_unq;
2450 $ref_table_seen = true;
2451 if (!empty($aliases[$db]['tables'][$table]['alias'])) {
2452 $sql_query = $this->substituteAlias(
2453 $sql_query, $data,
2454 $aliases[$db]['tables'][$table]['alias'],
2455 $pos, $offset
2457 $flag = true;
2459 // Replace column names
2460 } elseif (in_array($type, $identifier_types)
2461 && !empty($aliases[$db]['tables'][$table]['columns'][$d_unq])
2463 $sql_query = $this->substituteAlias(
2464 $sql_query, $data,
2465 $aliases[$db]['tables'][$table]['columns'][$d_unq],
2466 $pos, $offset
2468 $flag = true;
2470 // CREATE TRIGGER - Alias replacement
2471 } elseif ($query_type === 'CREATE' && $query_on === 'TRIGGER') {
2472 // Skip till 'ON' in encountered
2473 if (!$on_seen && $type === 'alpha_reservedWord'
2474 && $d_upper === 'ON'
2476 $on_seen = true;
2477 } elseif ($on_seen && in_array($type, $identifier_types)) {
2478 if (!$ref_table_seen
2479 && !empty($aliases[$db]['tables'][$d_unq]['alias'])
2481 $ref_table_seen = true;
2482 $sql_query = $this->substituteAlias(
2483 $sql_query, $data,
2484 $aliases[$db]['tables'][$d_unq]['alias'],
2485 $pos, $offset
2487 $flag = true;
2488 } else {
2489 // search for identifier alias
2490 $alias = $this->getAlias($aliases, $d_unq);
2491 if (!empty($alias)) {
2492 $sql_query = $this->substituteAlias(
2493 $sql_query, $data, $alias, $pos, $offset
2495 $flag = true;
2499 // CREATE PROCEDURE|FUNCTION|VIEW - Alias replacement
2500 } elseif ($query_type === 'CREATE'
2501 && ($query_on === 'FUNCTION'
2502 || $query_on === 'PROCEDURE'
2503 || $query_on === 'VIEW')
2505 // LANGUAGE SQL | (READS|MODIFIES) SQL DATA
2506 // characteristics are skipped
2507 if ($type === 'alpha_identifier'
2508 && (($d_upper === 'LANGUAGE' && $d_upper_next === 'SQL')
2509 || ($d_upper === 'DATA' && $d_upper_prev === 'SQL'))
2511 continue;
2512 // No need to process further in case of VIEW
2513 // when 'WITH' keyword has been detected
2514 } elseif ($query_on === 'VIEW'
2515 && $type === 'alpha_reservedWord' && $d_upper === 'WITH'
2517 $query_end = true;
2518 } elseif (in_array($type, $identifier_types)) {
2519 // search for identifier alias
2520 $alias = $this->getAlias($aliases, $d_unq);
2521 if (!empty($alias)) {
2522 $sql_query = $this->substituteAlias(
2523 $sql_query, $data, $alias, $pos, $offset
2525 $flag = true;
2530 return $sql_query;
2534 * substitutes alias in query at given position
2535 * Note: pos is the value from PMA_SQP_parse() + offset
2537 * @param string $sql_query the SQL query
2538 * @param string $data the data to be replaced
2539 * @param string $alias the replacement
2540 * @param string $pos the position of alias
2541 * @param string &$offset the change in pos occurred after substitution
2543 * @return string replaced query with alias
2545 public function substituteAlias($sql_query, $data, $alias, $pos, &$offset = null)
2547 if (!empty($GLOBALS['sql_backquotes'])) {
2548 $alias = PMA_Util::backquote($alias);
2550 $alias_len = /*overload*/mb_strlen($alias);
2551 $data_len = /*overload*/mb_strlen($data);
2552 if (isset($offset)) {
2553 $offset += ($alias_len - $data_len);
2555 $sql_query = substr_replace(
2556 $sql_query, $alias, $pos - $data_len, $data_len
2558 return $sql_query;
2562 * Generate comment
2564 * @param string $crlf Carriage return character
2565 * @param string $sql_statement SQL statement
2566 * @param string $comment1 Comment for dumped table
2567 * @param string $comment2 Comment for current table
2568 * @param string $table_alias Table alias
2569 * @param string $compat Compatibility mode
2571 * @return string
2573 protected function generateComment(
2574 $crlf, $sql_statement, $comment1, $comment2, $table_alias, $compat
2576 if (!isset($sql_statement)) {
2577 if (isset($GLOBALS['no_constraints_comments'])) {
2578 $sql_statement = '';
2579 } else {
2580 $sql_statement = $crlf
2581 . $this->_exportComment()
2582 . $this->_exportComment($comment1)
2583 . $this->_exportComment();
2587 // comments for current table
2588 if (!isset($GLOBALS['no_constraints_comments'])) {
2589 $sql_statement .= $crlf
2590 . $this->_exportComment()
2591 . $this->_exportComment(
2592 $comment2 . ' ' . PMA_Util::backquoteCompat(
2593 $table_alias, $compat, isset($GLOBALS['sql_backquotes'])
2596 . $this->_exportComment();
2599 return $sql_statement;