Commit 3e0cd370 authored by Robert Lyon's avatar Robert Lyon Committed by Cecilia Vela Gurovic

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

Without needing to create the xmldb table / field objects

We do this by trying to query the column and capture the SQLException
if it doesn't exist

behatnotneeded

Change-Id: I0a15dd4d1d4e47e6650576c084ee9e1d36b4ae62
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 1622bd2e
......@@ -1970,3 +1970,17 @@ 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;
}
}
......@@ -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;
}
}
......
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