diff --git a/libraries/plugins/export/ExportSql.class.php b/libraries/plugins/export/ExportSql.class.php index 035ca44a67..dcf6414c69 100644 --- a/libraries/plugins/export/ExportSql.class.php +++ b/libraries/plugins/export/ExportSql.class.php @@ -801,6 +801,18 @@ class ExportSql extends ExportPlugin global $crlf; $result = true; + + //add indexes to the sql dump file + if (isset($GLOBALS['sql_indexes'])) { + $result = PMA_exportOutputHandler($GLOBALS['sql_indexes']); + unset($GLOBALS['sql_indexes']); + } + //add auto increments to the sql dump file + if (isset($GLOBALS['sql_auto_increments'])) { + $result = PMA_exportOutputHandler($GLOBALS['sql_auto_increments']); + unset($GLOBALS['sql_auto_increments']); + } + //add constraints to the sql dump file if (isset($GLOBALS['sql_constraints'])) { $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']); unset($GLOBALS['sql_constraints']); @@ -984,7 +996,7 @@ class ExportSql extends ExportPlugin $view = false ) { global $sql_drop_table, $sql_backquotes, $sql_constraints, - $sql_constraints_query, $sql_drop_foreign_keys; + $sql_constraints_query,$sql_indexes,$sql_indexes_query,$sql_auto_increments,$sql_drop_foreign_keys; $schema_create = ''; $auto_increment = ''; @@ -1170,74 +1182,170 @@ class ExportSql extends ExportPlugin ); } - // are there any constraints to cut out? - if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $create_query)) { + //are there any constraints to cut out? + if (preg_match('@CONSTRAINT|KEY@', $create_query)) { + $has_constraints=0; + $has_indexes=0; + + //if there are constraints + if (preg_match( + '@CONSTRAINT@', + $create_query + )) { + $has_constraints=1; + // comments -> constraints for dumped tables + if (! isset($sql_constraints)) { + if (isset($GLOBALS['no_constraints_comments'])) { + $sql_constraints = ''; + } else { + $sql_constraints = $crlf + . $this->_exportComment() + . $this->_exportComment( + __('Constraints for dumped tables') + ) + . $this->_exportComment(); + } + } + // comments for current table + if (! isset($GLOBALS['no_constraints_comments'])) { + $sql_constraints .= $crlf + . $this->_exportComment() + . $this->_exportComment( + __('Constraints for table') + . ' ' + . PMA_Util::backquoteCompat($table, $compat) + ) + . $this->_exportComment(); + } + $sql_constraints_query .= 'ALTER TABLE ' + . PMA_Util::backquoteCompat($table, $compat) + . $crlf; + $sql_constraints .= 'ALTER TABLE ' + . PMA_Util::backquoteCompat($table, $compat) + . $crlf; + $sql_drop_foreign_keys .= 'ALTER TABLE ' + . PMA_Util::backquoteCompat($db, $compat) . '.' + . PMA_Util::backquoteCompat($table, $compat) + . $crlf; + } + //if there are indexes + if (preg_match( + '@KEY@', + $create_query + )) { + $has_indexes=1; + + // comments -> indexes for dumped tables + if (! isset($sql_indexes)) { + if (isset($GLOBALS['no_constraints_comments'])) { + $sql_indexes = ''; + } else { + $sql_indexes = $crlf + . $this->_exportComment() + . $this->_exportComment( + __('Indexes for dumped tables') + ) + . $this->_exportComment(); + } + } + // comments for current table + if (! isset($GLOBALS['no_constraints_comments'])) { + $sql_indexes .= $crlf + . $this->_exportComment() + . $this->_exportComment( + __('Indexes for table') + . ' ' + . PMA_Util::backquoteCompat($table, $compat) + ) + . $this->_exportComment(); + } + $sql_indexes_query .= 'ALTER TABLE ' + . PMA_Util::backquoteCompat($table, $compat) + . $crlf; + + $sql_indexes .= 'ALTER TABLE ' + . PMA_Util::backquoteCompat($table, $compat) + . $crlf; + } + if (preg_match( + '@AUTO_INCREMENT@', + $create_query + )) { + // comments -> auto increments for dumped tables + if (! isset($sql_auto_increments)) { + if (isset($GLOBALS['no_constraints_comments'])) { + $sql_auto_increments = ''; + } else { + $sql_auto_increments = $crlf + . $this->_exportComment() + . $this->_exportComment( + __('Auto Increments for dumped tables') + ) + . $this->_exportComment(); + } + } + // comments for current table + if (! isset($GLOBALS['no_constraints_comments'])) { + $sql_auto_increments .= $crlf + . $this->_exportComment() + . $this->_exportComment( + __('Auto Increments for table') + . ' ' + . PMA_Util::backquoteCompat($table, $compat) + ) + . $this->_exportComment(); + } + $sql_auto_increments .= 'ALTER TABLE ' + . PMA_Util::backquoteCompat($table, $compat) + . $crlf; + + } // Split the query into lines, so we can easily handle it. // We know lines are separated by $crlf (done few lines above). $sql_lines = explode($crlf, $create_query); $sql_count = count($sql_lines); - + // lets find first line with constraints - for ($i = 0; $i < $sql_count; $i++) { + $first_occur=-1; + for ($i = 0; $i < $sql_count; $i++) { if (preg_match( - '@^[\s]*(CONSTRAINT|FOREIGN[\s]+KEY)@', + '@[\s]+(CONSTRAINT|KEY)@', $sql_lines[$i] - )) { - break; + )&&$first_occur==-1) { + $first_occur=$i; + } + } + + for ($k = 0; $k < $sql_count; $k++) { + if (preg_match( + '@[\s]+(AUTO_INCREMENT,)@', + $sql_lines[$k] + )){ + //removes extra space at the beginning, if there is + $sql_lines[$k]=ltrim($sql_lines[$k], ' '); + //backup auto increment code before remove + $sql_auto_increments .= "MODIFY ".$sql_lines[$k]; + //removes auto increments from table creation + $sql_lines[$k]=str_replace(" AUTO_INCREMENT","",$sql_lines[$k]); } } + $sql_auto_increments = substr($sql_auto_increments, 0, -1).';'; // If we really found a constraint - if ($i != $sql_count) { - - // remove, from the end of create statement - $sql_lines[$i - 1] = preg_replace( + if ($first_occur != $sql_count) { + // lets find first line + $sql_lines[$first_occur - 1] = preg_replace( '@,$@', '', - $sql_lines[$i - 1] - ); - - // prepare variable for constraints - if (! isset($sql_constraints)) { - if (isset($GLOBALS['no_constraints_comments'])) { - $sql_constraints = ''; - } else { - $sql_constraints = $crlf - . $this->_exportComment() - . $this->_exportComment( - __('Constraints for dumped tables') - ) - . $this->_exportComment(); - } - } - - // comments for current table - if (! isset($GLOBALS['no_constraints_comments'])) { - $sql_constraints .= $crlf - . $this->_exportComment() - . $this->_exportComment( - __('Constraints for table') - . ' ' - . PMA_Util::backquoteCompat($table, $compat) - ) - . $this->_exportComment(); - } - - // let's do the work - $sql_constraints_query .= 'ALTER TABLE ' - . PMA_Util::backquoteCompat($table, $compat) - . $crlf; - $sql_constraints .= 'ALTER TABLE ' - . PMA_Util::backquoteCompat($table, $compat) - . $crlf; - $sql_drop_foreign_keys .= 'ALTER TABLE ' - . PMA_Util::backquoteCompat($db, $compat) . '.' - . PMA_Util::backquoteCompat($table, $compat) - . $crlf; - + $sql_lines[$first_occur - 1] + ); + $first = true; - for ($j = $i; $j < $sql_count; $j++) { + for ($j = $first_occur; $j < $sql_count; $j++) { + //removes extra space at the beginning, if there is + $sql_lines[$j]=ltrim($sql_lines[$j], ' '); + //if it's a constraint if (preg_match( '@CONSTRAINT|FOREIGN[\s]+KEY@', $sql_lines[$j] @@ -1251,14 +1359,17 @@ class ExportSql extends ExportPlugin 'ADD \1', $sql_lines[$j] ); + $sql_constraints_query .= $tmp_str; $sql_constraints .= $tmp_str; + } else { $tmp_str = preg_replace( '/(CONSTRAINT)/', 'ADD \1', $sql_lines[$j] ); + $sql_constraints_query .= $tmp_str; $sql_constraints .= $tmp_str; preg_match( @@ -1273,16 +1384,36 @@ class ExportSql extends ExportPlugin . $matches[3]; } $first = false; - } else { + } + //if it's a index + else if (preg_match( + '@KEY@', + $sql_lines[$j] + )) { + $tmp_str=" ADD ".$sql_lines[$j]; + $sql_indexes_query .= $tmp_str; + $sql_indexes .= $tmp_str; + } + else { break; } } + //removes attional comma at the end + $sql_indexes=rtrim($sql_indexes, ','); + $sql_indexes_query=rtrim($sql_indexes_query, ','); + //removes attional semicolon at the end + if($has_constraints==1){ $sql_constraints .= ';' . $crlf; $sql_constraints_query .= ';'; - + } + if($has_indexes==1){ + $sql_indexes .= ';' . $crlf; + $sql_indexes_query .= ';'; + } + //remove indexes and constraints from the $create_query $create_query = implode( $crlf, - array_slice($sql_lines, 0, $i) + array_slice($sql_lines, 0, $first_occur) ) . $crlf . implode(