Convert export to plugin architecture, so that plugins are independant piece of code (RFE #1325937).

This commit is contained in:
Michal Čihař
2006-04-26 16:33:59 +00:00
parent 29977afa0e
commit 6e17c995fb
12 changed files with 419 additions and 795 deletions

View File

@@ -5,6 +5,116 @@
* Set of functions used to build SQL dumps of tables
*/
if (isset($plugin_list)) {
$hide_sql = false;
$hide_structure = false;
if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
$hide_structure = true;
$hide_sql = true;
}
if (!$hide_sql) {
$plugin_list['sql'] = array(
'text' => 'strSQL',
'extension' => 'sql',
'options' => array(
array('type' => 'text', 'name' => 'header_comment', 'text' => 'strAddHeaderComment'),
array('type' => 'bool', 'name' => 'use_transaction', 'text' => 'strEncloseInTransaction'),
array('type' => 'bool', 'name' => 'disable_fk', 'text' => 'strDisableForeignChecks'),
),
'options_text' => 'strSQLOptions',
);
$compats = PMA_DBI_getCompatibilities();
if (count($compats) > 0) {
$values = array();
foreach($compats as $val) {
$values[$val] = $val;
}
$plugin_list['sql']['options'][] =
array('type' => 'select', 'name' => 'compatibility', 'text' => 'strSQLCompatibility', 'values' => $values, 'doc' => array('manual_MySQL_Database_Administration', 'Server_SQL_mode'));
unset($values);
}
/* Server export options */
if ($plugin_param['export_type'] == 'server') {
$plugin_list['sql']['options'][] =
array('type' => 'bgroup', 'text' => 'strDatabaseExportOptions');
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'drop_database', 'text' => sprintf($GLOBALS['strAddClause'], 'DROP DATABASE'));
$plugin_list['sql']['options'][] =
array('type' => 'egroup');
}
/* Structure options */
if (!$hide_structure) {
$plugin_list['sql']['options'][] =
array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
if ($plugin_param['export_type'] == 'table') {
if (PMA_Table::_isView($GLOBALS['db'], $GLOBALS['table'])) {
$drop_clause = 'DROP VIEW';
} else {
$drop_clause = 'DROP TABLE';
}
} elseif (PMA_MYSQL_INT_VERSION >= 50000) {
$drop_clause = 'DROP TABLE / DROP VIEW';
} else {
$drop_clause = 'DROP TABLE';
}
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'drop', 'text' => sprintf($GLOBALS['strAddClause'], $drop_clause));
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'if_not_exists', 'text' => sprintf($GLOBALS['strAddClause'], 'IF NOT EXISTS'));
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'auto_increment', 'text' => 'strAddAutoIncrement');
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'use_backquotes', 'text' => 'strUseBackquotes');
/* MIME stuff etc. */
$plugin_list['sql']['options'][] =
array('type' => 'bgroup', 'text' => 'strAddIntoComments');
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'dates', 'text' => 'strCreationDates');
if (!empty($GLOBALS['cfgRelation']['relation'])) {
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
}
if (!empty($GLOBALS['cfgRelation']['commwork']) && PMA_MYSQL_INT_VERSION < 40100) {
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
}
if (!empty($GLOBALS['cfgRelation']['mimework'])) {
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
}
$plugin_list['sql']['options'][] =
array('type' => 'egroup');
$plugin_list['sql']['options'][] =
array('type' => 'egroup');
}
/* Data */
$plugin_list['sql']['options'][] =
array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'columns', 'text' => 'strCompleteInserts');
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'extended', 'text' => 'strExtendedInserts');
$plugin_list['sql']['options'][] =
array('type' => 'text', 'name' => 'max_query_size', 'text' => 'strMaximalQueryLength');
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'delayed', 'text' => 'strDelayedInserts');
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreInserts');
$plugin_list['sql']['options'][] =
array('type' => 'bool', 'name' => 'hex_for_binary', 'text' => 'strHexForBinary');
$plugin_list['sql']['options'][] =
array('type' => 'select', 'name' => 'type', 'text' => 'strSQLExportType', 'values' => array('INSERT', 'UPDATE', 'REPLACE'));
$plugin_list['sql']['options'][] =
array('type' => 'egroup');
/* FIXME: force either data or structure */
}
} else {
/**
* Marker for comments, -- is needed for ANSI SQL.
*/
@@ -13,8 +123,8 @@ $GLOBALS['comment_marker'] = '-- ';
/**
* Avoids undefined variables, use NULL so isset() returns false
*/
if ( ! isset( $use_backquotes ) ) {
$use_backquotes = null;
if ( ! isset( $sql_use_backquotes ) ) {
$sql_use_backquotes = null;
}
/**
@@ -42,11 +152,11 @@ function PMA_exportFooter()
$foot = '';
if (isset($GLOBALS['disable_fk'])) {
if (isset($GLOBALS['sql_disable_fk'])) {
$foot .= $crlf . 'SET FOREIGN_KEY_CHECKS=1;' . $crlf;
}
if (isset($GLOBALS['use_transaction'])) {
if (isset($GLOBALS['sql_use_transaction'])) {
$foot .= $crlf . 'COMMIT;' . $crlf;
}
@@ -82,18 +192,18 @@ function PMA_exportHeader()
. $GLOBALS['comment_marker'] . $GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
. $GLOBALS['comment_marker'] . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
if (isset($GLOBALS['header_comment']) && !empty($GLOBALS['header_comment'])) {
$lines = explode('\n', $GLOBALS['header_comment']);
if (isset($GLOBALS['sql_header_comment']) && !empty($GLOBALS['sql_header_comment'])) {
$lines = explode('\n', $GLOBALS['sql_header_comment']);
$head .= $GLOBALS['comment_marker'] . $crlf
. $GLOBALS['comment_marker'] . implode($crlf . $GLOBALS['comment_marker'], $lines) . $crlf
. $GLOBALS['comment_marker'] . $crlf;
}
if (isset($GLOBALS['disable_fk'])) {
if (isset($GLOBALS['sql_disable_fk'])) {
$head .= $crlf . 'SET FOREIGN_KEY_CHECKS=0;' . $crlf;
}
if (isset($GLOBALS['use_transaction'])) {
if (isset($GLOBALS['sql_use_transaction'])) {
$head .= $crlf .'SET AUTOCOMMIT=0;' . $crlf
. 'START TRANSACTION;' . $crlf . $crlf;
}
@@ -113,12 +223,12 @@ function PMA_exportHeader()
function PMA_exportDBCreate($db)
{
global $crlf;
if (isset($GLOBALS['drop_database'])) {
if (!PMA_exportOutputHandler('DROP DATABASE ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : $db) . ';' . $crlf)) {
if (isset($GLOBALS['sql_drop_database'])) {
if (!PMA_exportOutputHandler('DROP DATABASE ' . (isset($GLOBALS['sql_use_backquotes']) ? PMA_backquote($db) : $db) . ';' . $crlf)) {
return FALSE;
}
}
$create_query = 'CREATE DATABASE ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : $db);
$create_query = 'CREATE DATABASE ' . (isset($GLOBALS['sql_use_backquotes']) ? PMA_backquote($db) : $db);
if (PMA_MYSQL_INT_VERSION >= 40101) {
$collation = PMA_getDbCollation($db);
if (strpos($collation, '_')) {
@@ -131,7 +241,7 @@ function PMA_exportDBCreate($db)
if (!PMA_exportOutputHandler($create_query)) {
return FALSE;
}
if (isset($GLOBALS['use_backquotes']) && PMA_MYSQL_INT_VERSION >= 40100 && isset($GLOBALS['sql_compat']) && $GLOBALS['sql_compat'] == 'NONE') {
if (isset($GLOBALS['sql_use_backquotes']) && PMA_MYSQL_INT_VERSION >= 40100 && isset($GLOBALS['sql_compat']) && $GLOBALS['sql_compat'] == 'NONE') {
return PMA_exportOutputHandler('USE ' . PMA_backquote($db) . ';' . $crlf);
}
return PMA_exportOutputHandler('USE ' . $db . ';' . $crlf);
@@ -150,7 +260,7 @@ function PMA_exportDBHeader($db)
{
global $crlf;
$head = $GLOBALS['comment_marker'] . $crlf
. $GLOBALS['comment_marker'] . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
. $GLOBALS['comment_marker'] . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['sql_use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
. $GLOBALS['comment_marker'] . $crlf;
return PMA_exportOutputHandler($head);
}
@@ -193,8 +303,8 @@ function PMA_exportDBFooter($db)
*/
function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false)
{
global $drop;
global $use_backquotes;
global $sql_drop;
global $sql_use_backquotes;
global $cfgRelation;
global $sql_constraints;
@@ -231,19 +341,19 @@ function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false)
$schema_create .= $new_crlf;
if (!empty($drop)) {
if (!empty($sql_drop)) {
if (PMA_Table::_isView($db,$table)) {
$drop_clause = 'DROP VIEW';
} else {
$drop_clause = 'DROP TABLE';
}
$schema_create .= $drop_clause . ' IF EXISTS ' . PMA_backquote($table, $use_backquotes) . ';' . $crlf;
$schema_create .= $drop_clause . ' IF EXISTS ' . PMA_backquote($table, $sql_use_backquotes) . ';' . $crlf;
unset($drop_clause);
}
// Steve Alberty's patch for complete table dump,
// Whether to quote table and fields names or not
if ($use_backquotes) {
if ($sql_use_backquotes) {
PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 1');
} else {
PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 0');
@@ -264,7 +374,7 @@ function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false)
}
// Should we use IF NOT EXISTS?
if (isset($GLOBALS['if_not_exists'])) {
if (isset($GLOBALS['sql_if_not_exists'])) {
$create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);
}
@@ -357,7 +467,7 @@ function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false)
function PMA_getTableComments($db, $table, $crlf, $do_relation = false, $do_comments = false, $do_mime = false)
{
global $cfgRelation;
global $use_backquotes;
global $sql_use_backquotes;
global $sql_constraints;
$schema_create = '';
@@ -392,32 +502,32 @@ function PMA_getTableComments($db, $table, $crlf, $do_relation = false, $do_comm
if (isset($comments_map) && count($comments_map) > 0) {
$schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf
. $GLOBALS['comment_marker'] . $GLOBALS['strCommentsForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
. $GLOBALS['comment_marker'] . $GLOBALS['strCommentsForTable']. ' ' . PMA_backquote($table, $sql_use_backquotes) . ':' . $crlf;
foreach ($comments_map AS $comment_field => $comment) {
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($comment_field, $use_backquotes) . $crlf
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($comment, $use_backquotes) . $crlf;
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($comment_field, $sql_use_backquotes) . $crlf
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($comment, $sql_use_backquotes) . $crlf;
}
$schema_create .= $GLOBALS['comment_marker'] . $crlf;
}
if (isset($mime_map) && count($mime_map) > 0) {
$schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf
. $GLOBALS['comment_marker'] . $GLOBALS['strMIMETypesForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
. $GLOBALS['comment_marker'] . $GLOBALS['strMIMETypesForTable']. ' ' . PMA_backquote($table, $sql_use_backquotes) . ':' . $crlf;
@reset($mime_map);
foreach ($mime_map AS $mime_field => $mime) {
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($mime_field, $use_backquotes) . $crlf
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($mime['mimetype'], $use_backquotes) . $crlf;
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($mime_field, $sql_use_backquotes) . $crlf
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($mime['mimetype'], $sql_use_backquotes) . $crlf;
}
$schema_create .= $GLOBALS['comment_marker'] . $crlf;
}
if ($have_rel) {
$schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf
. $GLOBALS['comment_marker'] . $GLOBALS['strRelationsForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
. $GLOBALS['comment_marker'] . $GLOBALS['strRelationsForTable']. ' ' . PMA_backquote($table, $sql_use_backquotes) . ':' . $crlf;
foreach ($res_rel AS $rel_field => $rel) {
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($rel_field, $use_backquotes) . $crlf
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($rel['foreign_table'], $use_backquotes)
. ' -> ' . PMA_backquote($rel['foreign_field'], $use_backquotes) . $crlf;
$schema_create .= $GLOBALS['comment_marker'] . ' ' . PMA_backquote($rel_field, $sql_use_backquotes) . $crlf
. $GLOBALS['comment_marker'] . ' ' . PMA_backquote($rel['foreign_table'], $sql_use_backquotes)
. ' -> ' . PMA_backquote($rel['foreign_field'], $sql_use_backquotes) . $crlf;
}
$schema_create .= $GLOBALS['comment_marker'] . $crlf;
}
@@ -443,7 +553,7 @@ function PMA_getTableComments($db, $table, $crlf, $do_relation = false, $do_comm
*/
function PMA_exportStructure($db, $table, $crlf, $error_url, $relation = FALSE, $comments = FALSE, $mime = FALSE, $dates = FALSE)
{
$formatted_table_name = (isset($GLOBALS['use_backquotes']))
$formatted_table_name = (isset($GLOBALS['sql_use_backquotes']))
? PMA_backquote($table)
: '\'' . $table . '\'';
$dump = $crlf
@@ -483,11 +593,11 @@ function PMA_exportStructure($db, $table, $crlf, $error_url, $relation = FALSE,
*/
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
{
global $use_backquotes;
global $sql_use_backquotes;
global $rows_cnt;
global $current_row;
$formatted_table_name = (isset($GLOBALS['use_backquotes']))
$formatted_table_name = (isset($GLOBALS['sql_use_backquotes']))
? PMA_backquote($table)
: '\'' . $table . '\'';
$head = $crlf
@@ -519,9 +629,9 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
for ($j = 0; $j < $fields_cnt; $j++) {
if (isset($analyzed_sql[0]['select_expr'][$j]['column'])) {
$field_set[$j] = PMA_backquote($analyzed_sql[0]['select_expr'][$j]['column'], $use_backquotes);
$field_set[$j] = PMA_backquote($analyzed_sql[0]['select_expr'][$j]['column'], $sql_use_backquotes);
} else {
$field_set[$j] = PMA_backquote($fields_meta[$j]->name, $use_backquotes);
$field_set[$j] = PMA_backquote($fields_meta[$j]->name, $sql_use_backquotes);
}
}
@@ -531,7 +641,7 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
if (isset($GLOBALS['sql_ignore'])) {
$schema_insert .= 'IGNORE ';
}
$schema_insert .= PMA_backquote($table, $use_backquotes) . ' SET ';
$schema_insert .= PMA_backquote($table, $sql_use_backquotes) . ' SET ';
} else {
// insert or replace
if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'replace') {
@@ -541,7 +651,7 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
}
// delayed inserts?
if (isset($GLOBALS['delayed'])) {
if (isset($GLOBALS['sql_delayed'])) {
$insert_delayed = ' DELAYED';
} else {
$insert_delayed = '';
@@ -553,12 +663,12 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
}
// scheme for inserting fields
if (isset($GLOBALS['showcolumns'])) {
if (isset($GLOBALS['sql_columns'])) {
$fields = implode(', ', $field_set);
$schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $use_backquotes)
$schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $sql_use_backquotes)
. ' (' . $fields . ') VALUES ';
} else {
$schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $use_backquotes)
$schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $sql_use_backquotes)
. ' VALUES ';
}
}
@@ -567,7 +677,7 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
$replace = array('\0', '\n', '\r', '\Z');
$current_row = 0;
$query_size = 0;
if (isset($GLOBALS['extended_ins'])) {
if (isset($GLOBALS['sql_extended'])) {
$separator = ',';
$schema_insert .= $crlf;
} else {
@@ -589,7 +699,7 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
// Note: with mysqli, under MySQL 4.1.3, we get the flag
// "binary" for those field types (I don't know why)
} elseif (stristr($field_flags[$j], 'BINARY')
&& isset($GLOBALS['hexforbinary'])
&& isset($GLOBALS['sql_hex_for_binary'])
&& $fields_meta[$j]->type != 'datetime'
&& $fields_meta[$j]->type != 'date'
&& $fields_meta[$j]->type != 'time'
@@ -623,12 +733,12 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
} else {
// Extended inserts case
if (isset($GLOBALS['extended_ins'])) {
if (isset($GLOBALS['sql_extended'])) {
if ($current_row == 1) {
$insert_line = $schema_insert . '(' . implode(', ', $values) . ')';
} else {
$insert_line = '(' . implode(', ', $values) . ')';
if (isset($GLOBALS['max_query_size']) && $GLOBALS['max_query_size'] > 0 && $query_size + strlen($insert_line) > $GLOBALS['max_query_size']) {
if (isset($GLOBALS['sql_max_query_size']) && $GLOBALS['sql_max_query_size'] > 0 && $query_size + strlen($insert_line) > $GLOBALS['sql_max_query_size']) {
if (!PMA_exportOutputHandler(';' . $crlf)) {
return FALSE;
}
@@ -661,4 +771,5 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
return TRUE;
} // end of the 'PMA_exportData()' function
}
?>