Commit 6a26a0c7 authored by Robert Lyon's avatar Robert Lyon Committed by Cecilia Vela Gurovic

Bug 1827445: Check if a table exists in a quicker way

Without needing to include the lib/ddl.php file, and create
the xmldb table objects

We do this by trying to query the table to return one value
and capture the SQLException if it doesn't exist

The places changed are where we don't manipulate the table - so have
left upgrade places the same as we will need to do things the ddl.php
way anyway

behatnotneeded

Change-Id: Ic38cee95a221a22e644370b308356cf687a1e831
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 3e0cd370
......@@ -1617,7 +1617,7 @@ function artefact_check_plugin_sanity($pluginname) {
foreach ($types as $type) {
$pluginclassname = generate_class_name('blocktype', $type);
if (get_config('installed')) {
if (table_exists(new XMLDBTable('blocktype_installed')) && $taken = get_record_select('blocktype_installed',
if (db_table_exists('blocktype_installed') && $taken = get_record_select('blocktype_installed',
'name = ? AND artefactplugin != ? ',
array($type, $pluginname))) {
throw new InstallationException(get_string('blocktypenametaken', 'error', $type,
......
......@@ -622,7 +622,7 @@ class User {
// We need to check if table exists otherwise we get error message about usr_agreement table
// not existing.
require_once('ddl.php');
if (!table_exists(new XMLDBTable("usr_agreement"))) {
if (!db_table_exists("usr_agreement")) {
return true;
}
......@@ -1792,7 +1792,7 @@ class LiveUser extends User {
$this->SESSION->set('remoteavatar', null);
$this->SESSION->set('nocheckrequiredfields', null);
if (get_config('installed') && !defined('INSTALLER') && $this->get('sessionid')
&& table_exists(new XMLDBTable('usr_session'))) {
&& db_table_exists('usr_session')) {
delete_records('usr_session', 'session', $this->get('sessionid'));
}
......
......@@ -477,8 +477,7 @@ if (!$mobile_detection_done) {
// Run modules bootstrap code.
if (!defined('INSTALLER')) {
// make sure the table exists if upgrading from older version
require_once('ddl.php');
if (table_exists(new XMLDBTable('module_installed'))) {
if (db_table_exists('module_installed')) {
if ($plugins = plugins_installed('module')) {
foreach ($plugins as &$plugin) {
if (safe_require_plugin('module', $plugin->name)) {
......
......@@ -1984,3 +1984,18 @@ function db_column_exists($table, $field) {
return false;
}
}
function db_table_exists($table) {
global $DB_IGNORE_SQL_EXCEPTIONS;
// We will check to see if a table exists by seeing if it throws an error or not
// This is meant to be a faster way than using the table_exists() function in lib/ddl.php
try {
$DB_IGNORE_SQL_EXCEPTIONS = true;
get_column_sql("SELECT 1 FROM " . db_table_name($table) . " LIMIT 1");
$DB_IGNORE_SQL_EXCEPTIONS = false;
return true;
}
catch (SQLException $e) {
return false;
}
}
......@@ -1074,7 +1074,7 @@ class Framework {
function get_config($value) {
$record = false;
if (table_exists(new XMLDBTable('framework_config')) &&
if (db_table_exists('framework_config') &&
$record = get_record('framework_config', 'framework', $this->id, 'field', $value)) {
return $record->value;
}
......@@ -1110,7 +1110,7 @@ class Framework {
public function get_framework_config_options() {
// check if the plugin has been upgraded
// if not, all status settings should be greyed out
$disabled = !table_exists(new XMLDBTable('framework_config'));
$disabled = !db_table_exists('framework_config');
$warning = array();
if ($disabled) {
$warning['plugin_warning'] = array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment