Skip to content

Commit

Permalink
Update libraries/plugins/export/ExportSql.class.php
Browse files Browse the repository at this point in the history
Rewrited, backquote_compat parameters order changed. Prevented multiple IDENTITY_INSERTs.
  • Loading branch information
hastenax committed Aug 2, 2012
1 parent b062ebd commit da855d3
Showing 1 changed file with 46 additions and 43 deletions.
89 changes: 46 additions & 43 deletions libraries/plugins/export/ExportSql.class.php
Expand Up @@ -698,15 +698,15 @@ public function exportDBCreate($db)
if (! PMA_exportOutputHandler(
'DROP DATABASE '
. (isset($GLOBALS['sql_backquotes'])
? $common_functions->backquote_compat($db, true, $compat) : $db)
? $common_functions->backquote_compat($db, $compat) : $db)
. ';' . $crlf
)) {
return false;
}
}
$create_query = 'CREATE DATABASE '
. (isset($GLOBALS['sql_backquotes'])
? $common_functions->backquote_compat($db, true, $compat) : $db);
. (isset($GLOBALS['sql_backquotes'])
? $common_functions->backquote_compat($db, $compat) : $db);
$collation = PMA_getDbCollation($db);
if (PMA_DRIZZLE) {
$create_query .= ' COLLATE ' . $collation;
Expand All @@ -729,7 +729,7 @@ public function exportDBCreate($db)
|| PMA_DRIZZLE)
) {
$result = PMA_exportOutputHandler(
'USE ' . $common_functions->backquote_compat($db, true, $compat) . ';' . $crlf
'USE ' . $common_functions->backquote_compat($db, $compat) . ';' . $crlf
);
} else {
$result = PMA_exportOutputHandler('USE ' . $db . ';' . $crlf);
Expand All @@ -752,7 +752,7 @@ public function exportDBHeader($db)
. $this->_exportComment(
__('Database') . ': '
. (isset($GLOBALS['sql_backquotes'])
? PMA_CommonFunctions::getInstance()->backquote_compat($db, true, $compat)
? PMA_CommonFunctions::getInstance()->backquote_compat($db, $compat)
: '\'' . $db . '\'')
)
. $this->_exportComment();
Expand Down Expand Up @@ -893,7 +893,7 @@ public function getTableDef(
$schema_create = '';
$auto_increment = '';
$new_crlf = $crlf;

$compat = (isset($GLOBALS['sql_compatibility'])) ? $GLOBALS['sql_compatibility'] : 'NONE';

// need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
Expand Down Expand Up @@ -1051,8 +1051,8 @@ public function getTableDef(
$create_query
);
}
// In MSSQL

// In MSSQL
// 1. DATE field doesn't exist, we will use datetime instead
// 2. UNSIGNED attribute doesn't exist
// 3. No length on int fields
Expand All @@ -1063,8 +1063,8 @@ public function getTableDef(
'/ int\([0-9]*\) /',
' int ',
$create_query
);
}
);
}

// Drizzle (checked on 2011.03.13) returns ROW_FORMAT surrounded
// with quotes, which is not accepted by parser
Expand Down Expand Up @@ -1125,19 +1125,19 @@ public function getTableDef(
. $this->_exportComment(
__('Constraints for table')
. ' '
. $common_functions->backquote_compat($table, true, $compat)
. $common_functions->backquote_compat($table, $compat)
)
. $this->_exportComment();
}

// let's do the work
$sql_constraints_query .= 'ALTER TABLE '
. $common_functions->backquote_compat($table, true, $compat) . $crlf;
. $common_functions->backquote_compat($table, $compat) . $crlf;
$sql_constraints .= 'ALTER TABLE '
. $common_functions->backquote_compat($table, true, $compat) . $crlf;
. $common_functions->backquote_compat($table, $compat) . $crlf;
$sql_drop_foreign_keys .= 'ALTER TABLE '
. $common_functions->backquote_compat($db, true, $compat) . '.'
. $common_functions->backquote_compat($table, true, $compat) . $crlf;
. $common_functions->backquote_compat($db, $compat) . '.'
. $common_functions->backquote_compat($table, $compat) . $crlf;

$first = true;
for ($j = $i; $j < $sql_count; $j++) {
Expand Down Expand Up @@ -1354,9 +1354,9 @@ public function exportStructure(

$common_functions = PMA_CommonFunctions::getInstance();
$compat = (isset($GLOBALS['sql_compatibility'])) ? $GLOBALS['sql_compatibility'] : 'NONE';

$formatted_table_name = (isset($GLOBALS['sql_backquotes']))
? $common_functions->backquote_compat($table, true, $compat) : '\'' . $table . '\'';
? $common_functions->backquote_compat($table, $compat) : '\'' . $table . '\'';
$dump = $this->_possibleCRLF()
. $this->_exportComment(str_repeat('-', 56))
. $this->_possibleCRLF()
Expand Down Expand Up @@ -1438,12 +1438,12 @@ public function exportStructure(
public function exportData($db, $table, $crlf, $error_url, $sql_query)
{
global $current_row, $sql_backquotes;

$compat = (isset($GLOBALS['sql_compatibility'])) ? $GLOBALS['sql_compatibility'] : 'NONE';

$common_functions = PMA_CommonFunctions::getInstance();
$formatted_table_name = (isset($GLOBALS['sql_backquotes']))
? $common_functions->backquote_compat($table, true, $compat)
? $common_functions->backquote_compat($table, $compat)
: '\'' . $table . '\'';

// Do not export data for a VIEW
Expand Down Expand Up @@ -1492,14 +1492,14 @@ public function exportData($db, $table, $crlf, $error_url, $sql_query)
if (isset($analyzed_sql[0]['select_expr'][$j]['column'])) {
$field_set[$j] = $common_functions->backquote_compat(
$analyzed_sql[0]['select_expr'][$j]['column'],
$sql_backquotes,
$compat
$compat,
$sql_backquotes
);
} else {
$field_set[$j] = $common_functions->backquote_compat(
$fields_meta[$j]->name,
$sql_backquotes,
$compat
$compat,
$sql_backquotes
);
}
}
Expand All @@ -1515,8 +1515,8 @@ public function exportData($db, $table, $crlf, $error_url, $sql_query)
// avoid EOL blank
$schema_insert .= $common_functions->backquote_compat(
$table,
$sql_backquotes,
$compat
$compat,
$sql_backquotes
) . ' SET';
} else {
// insert or replace
Expand Down Expand Up @@ -1550,8 +1550,8 @@ public function exportData($db, $table, $crlf, $error_url, $sql_query)
$truncate = 'TRUNCATE TABLE '
. $common_functions->backquote_compat(
$table,
$sql_backquotes,
$compat
$compat,
$sql_backquotes
) . ";";
$truncatehead = $this->_possibleCRLF()
. $this->_exportComment()
Expand All @@ -1566,25 +1566,19 @@ public function exportData($db, $table, $crlf, $error_url, $sql_query)
} else {
$truncate = '';
}

// We need to SET IDENTITY_INSERT ON for MSSQL
if (isset($GLOBALS['sql_compatibility'])
&& $GLOBALS['sql_compatibility'] == 'MSSQL') {
$sql_command = 'SET IDENTITY_INSERT '. $common_functions->backquote_compat($table, true, $compat). ' ON ;'.$crlf.$sql_command;
}


// scheme for inserting fields
if ($GLOBALS['sql_insert_syntax'] == 'complete'
|| $GLOBALS['sql_insert_syntax'] == 'both'
) {
$fields = implode(', ', $field_set);
$schema_insert = $sql_command . $insert_delayed .' INTO '
. $common_functions->backquote_compat($table, $sql_backquotes, $compat)
. $common_functions->backquote_compat($table, $compat, $sql_backquotes)
// avoid EOL blank
. ' (' . $fields . ') VALUES';
} else {
$schema_insert = $sql_command . $insert_delayed .' INTO '
. $common_functions->backquote_compat($table, $sql_backquotes, $compat)
. $common_functions->backquote_compat($table, $compat, $sql_backquotes)
. ' VALUES';
}
}
Expand Down Expand Up @@ -1618,6 +1612,14 @@ public function exportData($db, $table, $crlf, $error_url, $sql_query)
if (! PMA_exportOutputHandler($head)) {
return false;
}
}
// We need to SET IDENTITY_INSERT ON for MSSQL
if (isset($GLOBALS['sql_compatibility'])
&& $GLOBALS['sql_compatibility'] == 'MSSQL'
&& $current_row == 0) {
if (! PMA_exportOutputHandler('SET IDENTITY_INSERT '. $common_functions->backquote_compat($table, $compat). ' ON ;'.$crlf)) {
return false;
}
}
$current_row++;
for ($j = 0; $j < $fields_cnt; $j++) {
Expand Down Expand Up @@ -1739,22 +1741,23 @@ public function exportData($db, $table, $crlf, $error_url, $sql_query)
}

} // end while

if ($current_row > 0) {
if (! PMA_exportOutputHandler(';' . $crlf)) {
return false;
}
}
// We need to SET IDENTITY_INSERT ON for MSSQL

// We need to SET IDENTITY_INSERT ON for MSSQL
if (isset($GLOBALS['sql_compatibility'])
&& $GLOBALS['sql_compatibility'] == 'MSSQL')
&& $GLOBALS['sql_compatibility'] == 'MSSQL'
&& $current_row > 0)
if (! PMA_exportOutputHandler(
$crlf . 'SET IDENTITY_INSERT ' . $common_functions->backquote_compat($table, true, $compat) . ' OFF;' . $crlf
$crlf . 'SET IDENTITY_INSERT ' . $common_functions->backquote_compat($table, $compat) . ' OFF;' . $crlf
)) {
return false;
}

} // end if ($result != false)
PMA_DBI_free_result($result);

Expand Down

0 comments on commit da855d3

Please sign in to comment.