diff --git a/doc/config.rst b/doc/config.rst index c367f2f673..b203703c12 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -1275,6 +1275,16 @@ Generic settings middle-clicking for pasting the clipboard contents in some Linux distributions (such as Ubuntu) is not supported by all browsers. +.. config:option:: $cfg['DefaultForeignKeyChecks'] + + :type: string + :default: ``'default'`` + + Default values for checkbox for foreign key checks, to disable/enable + foreign key checks for certain queries. Value can be either of ``'default'``, + ``'enable'`` or ``'disable'``. If set to ``'default'``, the value of + MySQL variable ``FOREIGN_KEY_CHECKS`` is used. + .. config:option:: $cfg['AllowUserDropDatabase'] :type: boolean @@ -2019,8 +2029,8 @@ Languages :default: ``'utf8_general_ci'`` Defines the default connection collation to use, if not user-defined. - See the `MySQL documentation for charsets - `_ + See the `MySQL documentation for charsets + `_ for list of possible values. This setting is ignored when connected to Drizzle server. @@ -2817,4 +2827,3 @@ Developer Enable to let server present itself as demo server. This is used for . - diff --git a/import.php b/import.php index 1f3f0ef9e1..20e3fbe6ce 100644 --- a/import.php +++ b/import.php @@ -616,28 +616,13 @@ if (! $error) { PMA_stopImport($message); } else { // Do the real import - if (isset($_REQUEST['disable_foreign_keys'])) { - - $default_fk_check_value = $GLOBALS['dbi']->fetchValue( - "SHOW VARIABLES LIKE 'foreign_key_checks';", 0, 1 - ) == 'ON'; - - try { - if ($default_fk_check_value) { - $GLOBALS['dbi']->tryQuery("SET FOREIGN_KEY_CHECKS = 0;"); - } - $import_plugin->doImport($sql_data); - if ($default_fk_check_value) { - $GLOBALS['dbi']->tryQuery('SET FOREIGN_KEY_CHECKS = 1;'); - } - } catch (Exception $e) { - if ($default_fk_check_value) { - $GLOBALS['dbi']->tryQuery('SET FOREIGN_KEY_CHECKS = 1;'); - } - throw $e; - } - } else { + try { + $default_fk_check = PMA_Util::handleDisableFKCheckInit(); $import_plugin->doImport($sql_data); + PMA_Util::handleDisableFKCheckCleanup($default_fk_check); + } catch (Exception $e) { + PMA_Util::handleDisableFKCheckCleanup($default_fk_check); + throw $e; } } } diff --git a/js/functions.js b/js/functions.js index a89b04ec70..6ebcbee4db 100644 --- a/js/functions.js +++ b/js/functions.js @@ -1718,8 +1718,15 @@ AJAX.registerOnload('functions.js', function () { var sql_query = $form.find("input[name='sql_query']").val().trim(); var $inner_sql = $(this).parent().prev().find('code.sql'); var old_text = $inner_sql.html(); + var default_fk_check_value = $form.find("input[name='default_fk_check_value']").val() == 'true'; var new_content = "\n"; + new_content += "
"; + new_content += ""; + new_content += ""; + new_content += ""; + new_content += "
"; new_content += "\n"; new_content += "\n"; var $editor_area = $('div#inline_editor'); @@ -1742,15 +1749,17 @@ AJAX.registerOnload('functions.js', function () { codemirror_inline_editor.save(); sql_query = codemirror_inline_editor.getValue(); } else { - sql_query = $(this).prev().val(); + sql_query = $(this).parent().find('#sql_query_edit').val(); } + var fk_check = $(this).parent().find('#fk_checks').is(':checked'); var $form = $("a.inline_edit_sql").prev('form'); var $fake_form = $('
', {action: 'import.php', method: 'post'}) .append($form.find("input[name=server], input[name=db], input[name=table], input[name=token]").clone()) .append($('', {type: 'hidden', name: 'show_query', value: 1})) .append($('', {type: 'hidden', name: 'is_js_confirmed', value: 0})) - .append($('', {type: 'hidden', name: 'sql_query', value: sql_query})); + .append($('', {type: 'hidden', name: 'sql_query', value: sql_query})) + .append($('', {type: 'hidden', name: 'fk_checks', value: fk_check ? 1 : 0})); if (! checkSqlQuery($fake_form[0])) { return false; } @@ -4290,13 +4299,6 @@ AJAX.registerOnload('functions.js', function () { syntaxHighlighter = PMA_getSQLEditor($('textarea[name="view[as]"]')); - $(document).on('change', '#fkc_checkbox', function () { - if ($(this).prop("checked")) { - $("#fkc_status").html(PMA_messages.strForeignKeyCheckEnabled); - } else { - $("#fkc_status").html(PMA_messages.strForeignKeyCheckDisabled); - } - }); // End of event handler for 'Foreign Key Check' }); function PMA_createViewDialog($this) diff --git a/js/messages.php b/js/messages.php index 81a58304d5..c26cc47146 100644 --- a/js/messages.php +++ b/js/messages.php @@ -300,10 +300,10 @@ $js_messages['strCopyingDatabase'] = __('Copying Database'); $js_messages['strChangingCharset'] = __('Changing Charset'); $js_messages['strNo'] = __('No'); +/* For Foreign key checks */ +$js_messages['strForeignKeyCheck'] = __('Enable foreign key checks'); + /* For db_stucture.js */ -$js_messages['strForeignKeyCheck'] = __('Foreign key check:'); -$js_messages['strForeignKeyCheckEnabled'] = __('(Enabled)'); -$js_messages['strForeignKeyCheckDisabled'] = __('(Disabled)'); $js_messages['strErrorRealRowCount'] = __('Failed to get real row count.'); /* For db_search.js */ diff --git a/libraries/DatabaseInterface.class.php b/libraries/DatabaseInterface.class.php index eac7dc175b..8a66556f8b 100644 --- a/libraries/DatabaseInterface.class.php +++ b/libraries/DatabaseInterface.class.php @@ -1650,6 +1650,20 @@ class PMA_DatabaseInterface ); } + /** + * Set foreign key check variable in session + * + * @param bool $value Turn it on or off + * + * @return void + */ + public function setForeignKeyCheck($value) { + $current_fk_check_value = $GLOBALS['dbi']->getVariable('FOREIGN_KEY_CHECKS') == 'ON'; + if ($current_fk_check_value == $value) return; + + $this->query('SET FOREIGN_KEY_CHECKS = ' . ($value ? 1 : 0) . ';'); + } + /** * Function called just after a connection to the MySQL database server has * been established. It sets the connection collation, and determines the diff --git a/libraries/Util.class.php b/libraries/Util.class.php index 45a0859114..870d8c42cf 100644 --- a/libraries/Util.class.php +++ b/libraries/Util.class.php @@ -1275,6 +1275,11 @@ class PMA_Util 'profiling', __('Profiling'), isset($_SESSION['profiling']), true ); } + + // Pass default foreign_key_checks + $default_fk_check_value = $GLOBALS['dbi']->getVariable('FOREIGN_KEY_CHECKS') == 'ON'; + $retval .= ''; $retval .= '
'; /** @@ -3185,6 +3190,60 @@ class PMA_Util } } + /** + * Get HTML for Foreign key check checkbox + * + * @return string HTML for checkbox + */ + public static function getFKCheckbox() + { + if ($GLOBALS['cfg']['DefaultForeignKeyChecks'] === 'enable') { + $checked = true; + } else if ($GLOBALS['cfg']['DefaultForeignKeyChecks'] === 'disable') { + $checked = false; + } else { + $checked = $GLOBALS['dbi']->getVariable('FOREIGN_KEY_CHECKS') == 'ON'; + } + $html = ''; + $html .= ''; + $html .= ''; + return $html; + } + + /** + * Handle foreign key check request + * + * @return bool Default foreign key checks value + */ + public static function handleDisableFKCheckInit() + { + $default_fk_check_value = $GLOBALS['dbi']->getVariable('FOREIGN_KEY_CHECKS') == 'ON'; + if (isset($_REQUEST['fk_checks'])) { + if (empty($_REQUEST['fk_checks'])) { + // Disable foreign key checks + $GLOBALS['dbi']->setForeignKeyCheck(false); + } else { + // Enable foreign key checks + $GLOBALS['dbi']->setForeignKeyCheck(true); + } + } // else do nothing, go with default + return $default_fk_check_value; + } + + /** + * Cleanup changes done for foreign key check + * + * @param bool $default_fk_check_value + * + * @return void + */ + public static function handleDisableFKCheckCleanup($default_fk_check_value) + { + $GLOBALS['dbi']->setForeignKeyCheck($default_fk_check_value); + } + /** * Replaces some characters by a displayable equivalent * diff --git a/libraries/config.default.php b/libraries/config.default.php index f13c4333ce..648a4b7132 100644 --- a/libraries/config.default.php +++ b/libraries/config.default.php @@ -1216,6 +1216,12 @@ $cfg['ForeignKeyDropdownOrder'] = array('content-id', 'id-content'); */ $cfg['ForeignKeyMaxLimit'] = 100; +/** + * Whether to disable foreign key checks while importing + * + * @global boolean $cfg['DefaultForeignKeyChecks'] + */ +$cfg['DefaultForeignKeyChecks'] = 'default'; /******************************************************************************* * For the export features... @@ -2137,13 +2143,6 @@ $cfg['Import']['allow_interrupt'] = true; */ $cfg['Import']['skip_queries'] = 0; -/** - * Whether to disable foreign key checks while importing - * - * @global boolean $cfg['Import']['disable_foreign_keys'] - */ -$cfg['Import']['disable_foreign_keys'] = false; - /** * * diff --git a/libraries/config.values.php b/libraries/config.values.php index cec70cd212..ca811814b0 100644 --- a/libraries/config.values.php +++ b/libraries/config.values.php @@ -128,6 +128,11 @@ $cfg_db['SendErrorReports'] = array( 'always' => __('Always send error reports'), 'never' => __('Never send error reports') ); +$cfg_db['DefaultForeignKeyChecks'] = array( + 'default' => __('Server default'), + 'enable' => __('Enable'), + 'disable' => __('Disable') +); $cfg_db['Import']['format'] = array( 'csv', // CSV 'docsql', // DocSQL diff --git a/libraries/config/messages.inc.php b/libraries/config/messages.inc.php index 5120c0487f..b28c877a45 100644 --- a/libraries/config/messages.inc.php +++ b/libraries/config/messages.inc.php @@ -212,6 +212,10 @@ $strConfigForeignKeyDropdownOrder_name = __('Foreign key dropdown order'); $strConfigForeignKeyMaxLimit_desc = __('A dropdown will be used if fewer items are present.'); $strConfigForeignKeyMaxLimit_name = __('Foreign key limit'); +$strConfigDefaultForeignKeyChecks_desc = __( + 'Default value for foreign key checks checkbox for some queries.' +); +$strConfigDefaultForeignKeyChecks_name = __('Foreign key checks'); $strConfigForm_Browse = __('Browse mode'); $strConfigForm_Browse_desc = __('Customize browse mode.'); $strConfigForm_CodeGen = 'CodeGen'; @@ -334,10 +338,6 @@ $strConfigImport_allow_interrupt_desc = __( . 'transactions.' ); $strConfigImport_allow_interrupt_name = __('Partial import: allow interrupt'); -$strConfigImport_disable_foreign_keys_desc = __( - 'Temporarily disable foreign key checks while importing' -); -$strConfigImport_disable_foreign_keys_name = __('Disable foreign key checks'); $strConfigImport_charset_name = __('Character set of the file'); $strConfigImport_csv_col_names_name = __('Lines terminated with'); $strConfigImport_csv_enclosed_name = __('Columns enclosed with'); diff --git a/libraries/config/setup.forms.php b/libraries/config/setup.forms.php index 8c851cbb71..095e745194 100644 --- a/libraries/config/setup.forms.php +++ b/libraries/config/setup.forms.php @@ -160,7 +160,8 @@ $forms['Sql_queries']['Sql_queries'] = array( 'MaxCharactersInDisplayedSQL', 'RetainQueryBox', 'CodemirrorEnable', - 'EnableAutocompleteForTablesAndColumns'); + 'EnableAutocompleteForTablesAndColumns', + 'DefaultForeignKeyChecks'); $forms['Sql_queries']['Sql_box'] = array('SQLQuery' => array( 'Edit', 'Explain', @@ -246,8 +247,7 @@ $forms['Import']['Import_defaults'] = array('Import' => array( 'format', 'charset', 'allow_interrupt', - 'skip_queries', - 'disable_foreign_keys')); + 'skip_queries')); $forms['Import']['Sql'] = array('Import' => array( 'sql_compatibility', 'sql_no_auto_value_on_zero')); diff --git a/libraries/config/user_preferences.forms.php b/libraries/config/user_preferences.forms.php index 0b17bbc2e3..6dac345429 100644 --- a/libraries/config/user_preferences.forms.php +++ b/libraries/config/user_preferences.forms.php @@ -70,7 +70,8 @@ $forms['Sql_queries']['Sql_queries'] = array( 'MaxCharactersInDisplayedSQL', 'RetainQueryBox', 'CodemirrorEnable', - 'EnableAutocompleteForTablesAndColumns'); + 'EnableAutocompleteForTablesAndColumns', + 'DefaultForeignKeyChecks'); $forms['Sql_queries']['Sql_box'] = array( 'SQLQuery/Edit', 'SQLQuery/Explain', @@ -146,8 +147,7 @@ $forms['Import']['Import_defaults'] = array( 'Import/format', 'Import/charset', 'Import/allow_interrupt', - 'Import/skip_queries', - 'Import/disable_foreign_keys' + 'Import/skip_queries' ); $forms['Import']['Sql'] = array( 'Import/sql_compatibility', diff --git a/libraries/display_import.lib.php b/libraries/display_import.lib.php index b15341ab44..a0a8efb57c 100644 --- a/libraries/display_import.lib.php +++ b/libraries/display_import.lib.php @@ -337,12 +337,7 @@ function PMA_getHtmlForImportOptionsOther() $html = '
'; $html .= '

' . __('Other Options:') . '

'; $html .= '
'; - $html .= ' '; - $html .= ' '; + $html .= PMA_Util::getFKCheckbox(); $html .= '
'; $html .= '
'; diff --git a/libraries/import.lib.php b/libraries/import.lib.php index 5c977c6f8b..4bd0c6d243 100644 --- a/libraries/import.lib.php +++ b/libraries/import.lib.php @@ -136,7 +136,7 @@ function PMA_importRunQuery($sql = '', $full = '', $controluser = false, && ((! empty($import_run_buffer['sql']) && preg_match($pattern, $import_run_buffer['sql'])) || ($executed_queries == 1 - && ! isset($_REQUEST['disable_foreign_keys']))) + && ! isset($_REQUEST['fk_checks']))) ) { $go_sql = true; if (! $sql_query_disabled) { diff --git a/libraries/mult_submits.inc.php b/libraries/mult_submits.inc.php index 9a8db27bd6..1fcad48230 100644 --- a/libraries/mult_submits.inc.php +++ b/libraries/mult_submits.inc.php @@ -217,17 +217,11 @@ if (!empty($submit_mult) && !empty($what)) { $GLOBALS['dbi']->freeResult($result); } - if (! isset($_REQUEST['fk_check']) - && ($query_type == 'drop_tbl' + if ($query_type == 'drop_tbl' || $query_type == 'empty_tbl' - || $query_type == 'row_delete') + || $query_type == 'row_delete' ) { - $default_fk_check_value = $GLOBALS['dbi']->fetchValue( - 'SHOW VARIABLES LIKE \'foreign_key_checks\';', 0, 1 - ) == 'ON'; - - // for disabling foreign key checks while dropping tables - $GLOBALS['dbi']->query('SET FOREIGN_KEY_CHECKS = 0;'); + $default_fk_check_value = PMA_Util::handleDisableFKCheckInit(); } list( @@ -277,13 +271,11 @@ if (!empty($submit_mult) && !empty($what)) { $message = PMA_Message::error($GLOBALS['dbi']->getError()); } } - if (! isset($_REQUEST['fk_check']) - && ($query_type == 'drop_tbl' + if ($query_type == 'drop_tbl' || $query_type == 'empty_tbl' - || $query_type == 'row_delete') - && $default_fk_check_value + || $query_type == 'row_delete' ) { - $GLOBALS['dbi']->query('SET FOREIGN_KEY_CHECKS = 1;'); + PMA_Util::handleDisableFKCheckCleanup($default_fk_check_value); } if ($rebuild_database_list) { // avoid a problem with the database list navigator diff --git a/libraries/mult_submits.lib.php b/libraries/mult_submits.lib.php index 3c0856149a..4e86ca9363 100644 --- a/libraries/mult_submits.lib.php +++ b/libraries/mult_submits.lib.php @@ -434,22 +434,7 @@ function PMA_getHtmlForOtherActions($what, $action, $_url_params, $full_query) // Display option to disable foreign key checks while dropping tables if ($what === 'drop_tbl' || $what === 'empty_tbl' || $what === 'row_delete') { $html .= '
'; - $html .= ''; - $html .= ''; - $html .= 'fetchValue( - 'SHOW VARIABLES LIKE \'foreign_key_checks\';', 0, 1 - ) == 'ON'; - if ($default_fk_check_value) { - $html .= ' checked="checked"'; - } - $html .= '/>'; - $html .= ''; + $html .= PMA_Util::getFKCheckbox(); $html .= '
'; } $html .= ''; + // Disable/Enable foreign key checks + $html .= '
'; + $html .= PMA_Util::getFKCheckbox(); + $html .= '
'; + // parameter binding $html .= '
'; $html .= '';