diff --git a/ChangeLog b/ChangeLog
index 9c76feceae..eace936295 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,8 @@ phpMyAdmin - ChangeLog
- bug #4007 Analyze option not shown for InnoDB tables
- bug #4015 Forcing a storage engine for configuration storage
- bug Incorrect Drizzle 7 detection
+- bug #4019 Create database if not exists (export): add an option to the
+ interface to enable generating CREATE DATABASE and USE (false by default)
4.0.4.1 (2013-06-30)
- [security] Global variables scope injection vulnerability (see PMASA-2013-7)
diff --git a/export.php b/export.php
index c2373e3051..861aa08c1e 100644
--- a/export.php
+++ b/export.php
@@ -83,6 +83,7 @@ $post_params = array(
'sql_disable_fk',
'sql_compatibility',
'sql_structure_or_data',
+ 'sql_create_database',
'sql_drop_table',
'sql_procedure_function',
'sql_create_table_statements',
diff --git a/libraries/config.default.php b/libraries/config.default.php
index 544e2f4e40..f4aae208d0 100644
--- a/libraries/config.default.php
+++ b/libraries/config.default.php
@@ -1723,6 +1723,13 @@ $cfg['Export']['sql_disable_fk'] = false;
*/
$cfg['Export']['sql_use_transaction'] = false;
+/**
+ *
+ *
+ * @global boolean $cfg['Export']['sql_create_database']
+ */
+$cfg['Export']['sql_create_database'] = false;
+
/**
*
*
diff --git a/libraries/plugins/export/ExportSql.class.php b/libraries/plugins/export/ExportSql.class.php
index 4481013721..fade66e5b3 100644
--- a/libraries/plugins/export/ExportSql.class.php
+++ b/libraries/plugins/export/ExportSql.class.php
@@ -219,6 +219,15 @@ class ExportSql extends ExportPlugin
$leaf->setName('add_statements');
$leaf->setText(__('Add statements:'));
$subgroup->setSubgroupHeader($leaf);
+
+ if ($plugin_param['export_type'] != 'table') {
+ $leaf = new BoolPropertyItem();
+ $leaf->setName('create_database');
+ $create_clause = 'CREATE DATABASE / USE';
+ $leaf->setText(sprintf(__('Add %s statement'), $create_clause));
+ $subgroup->addProperty($leaf);
+ }
+
if ($plugin_param['export_type'] == 'table') {
if (PMA_Table::isView($GLOBALS['db'], $GLOBALS['table'])) {
$drop_clause = 'DROP VIEW';
@@ -706,39 +715,42 @@ class ExportSql extends ExportPlugin
return false;
}
}
- $create_query = 'CREATE DATABASE IF NOT EXISTS '
- . (isset($GLOBALS['sql_backquotes'])
- ? PMA_Util::backquoteCompat($db, $compat) : $db);
- $collation = PMA_getDbCollation($db);
- if (PMA_DRIZZLE) {
- $create_query .= ' COLLATE ' . $collation;
- } else {
- if (strpos($collation, '_')) {
- $create_query .= ' DEFAULT CHARACTER SET '
- . substr($collation, 0, strpos($collation, '_'))
- . ' COLLATE ' . $collation;
+ if (isset($GLOBALS['sql_create_database'])) {
+ $create_query = 'CREATE DATABASE IF NOT EXISTS '
+ . (isset($GLOBALS['sql_backquotes'])
+ ? PMA_Util::backquoteCompat($db, $compat) : $db);
+ $collation = PMA_getDbCollation($db);
+ if (PMA_DRIZZLE) {
+ $create_query .= ' COLLATE ' . $collation;
} else {
- $create_query .= ' DEFAULT CHARACTER SET ' . $collation;
+ if (strpos($collation, '_')) {
+ $create_query .= ' DEFAULT CHARACTER SET '
+ . substr($collation, 0, strpos($collation, '_'))
+ . ' COLLATE ' . $collation;
+ } else {
+ $create_query .= ' DEFAULT CHARACTER SET ' . $collation;
+ }
}
- }
- $create_query .= ';' . $crlf;
- if (! PMA_exportOutputHandler($create_query)) {
- return false;
- }
- if (isset($GLOBALS['sql_backquotes'])
- && ((isset($GLOBALS['sql_compatibility'])
- && $GLOBALS['sql_compatibility'] == 'NONE')
- || PMA_DRIZZLE)
- ) {
- $result = PMA_exportOutputHandler(
- 'USE ' . PMA_Util::backquoteCompat($db, $compat)
- . ';' . $crlf
- );
+ $create_query .= ';' . $crlf;
+ if (! PMA_exportOutputHandler($create_query)) {
+ return false;
+ }
+ if (isset($GLOBALS['sql_backquotes'])
+ && ((isset($GLOBALS['sql_compatibility'])
+ && $GLOBALS['sql_compatibility'] == 'NONE')
+ || PMA_DRIZZLE)
+ ) {
+ $result = PMA_exportOutputHandler(
+ 'USE ' . PMA_Util::backquoteCompat($db, $compat)
+ . ';' . $crlf
+ );
+ } else {
+ $result = PMA_exportOutputHandler('USE ' . $db . ';' . $crlf);
+ }
+ return $result;
} else {
- $result = PMA_exportOutputHandler('USE ' . $db . ';' . $crlf);
+ return true;
}
-
- return $result;
}
/**