Commit e99f16f2 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Gerrit Code Review

Merge changes Ic38cee95,I0a15dd4d

* changes:
  Bug 1827445: Check if a table exists in a quicker way
  Bug 1827445: Check if a table column exists in a quicker way
parents d7cd332d 6a26a0c7
......@@ -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,
......
......@@ -625,7 +625,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;
}
......@@ -1795,7 +1795,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)) {
......
......@@ -1970,3 +1970,32 @@ function get_db_version() {
}
return $version;
}
function db_column_exists($table, $field) {
global $DB_IGNORE_SQL_EXCEPTIONS;
// We will check to see if a column exists by seeing if it throws an error or not
try {
$DB_IGNORE_SQL_EXCEPTIONS = true;
get_column_sql("SELECT " . db_quote_identifier($field) . " FROM " . db_table_name($table) . " LIMIT 1");
$DB_IGNORE_SQL_EXCEPTIONS = false;
return true;
}
catch (SQLException $e) {
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;
}
}
......@@ -1883,12 +1883,8 @@ function load_user_institutions($userid) {
}
$userid = (int) $userid;
require_once('ddl.php');
$table = new XMLDBTable('institution');
$field = new XMLDBField('logoxs');
$logoxs = field_exists($table, $field) ? ',i.logoxs' : '';
$field = new XMLDBField('tags');
$tags = field_exists($table, $field) ? ',i.tags' : '';
$logoxs = db_column_exists('institution', 'logoxs') ? ',i.logoxs' : '';
$tags = db_column_exists('institution', 'tags') ? ',i.tags' : '';
if ($userid !== 0 && $institutions = get_records_sql_assoc('
SELECT u.institution, ' . db_format_tsfield('ctime') . ',' . db_format_tsfield('u.expiry', 'membership_expiry') . ',
u.studentid, u.staff, u.admin, i.displayname, i.theme, i.registerallowed, i.showonlineusers,
......
......@@ -1576,10 +1576,7 @@ class View {
if (array_search($accessrecord, $accessdata_added) === false) {
$accessrecord->view = $this->get('id');
require_once('ddl.php');
$table = new XMLDBTable('view_access');
$field = new XMLDBField('id');
if (field_exists($table, $field)) {
if (db_column_exists('view_access', 'id')) {
$vaid = insert_record('view_access', $accessrecord, 'id', true);
handle_event('updateviewaccess', array(
'id' => $vaid,
......
......@@ -1305,10 +1305,7 @@ class Theme {
return get_config('wwwroot') . 'thumb.php?type=logobyid&id=' . $this->headerlogosmall;
}
else {
require_once('ddl.php');
$table = new XMLDBTable('institution');
$field = new XMLDBField('logoxs');
if (field_exists($table, $field) && $sitelogocustomsmallid = get_field('institution', 'logoxs', 'name', 'mahara')) {
if (db_column_exists('institution', 'logoxs') && $sitelogocustomsmallid = get_field('institution', 'logoxs', 'name', 'mahara')) {
return get_config('wwwroot') . 'thumb.php?type=logobyid&id=' . $sitelogocustomsmallid;
}
}
......
......@@ -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