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

Bug 1827445: db_table_exists alternative without changing SQL exceptions

and the same for db_column_exists

behatnotneeded

Change-Id: Id69e285b39aff1dca7edbbf16847c3b7a436baa5
parent 24e838cf
......@@ -1978,52 +1978,43 @@ function get_db_version() {
* inconsistent state before db_commit() is called.
*/
function db_column_exists($table, $field) {
global $DB_IGNORE_SQL_EXCEPTIONS;
if (defined('INSTALLER')) {
// We do the check in the old way
require_once('ddl.php');
$table = new XMLDBTable($table);
$field = new XMLDBField($field);
return field_exists($table, $field);
}
else {
// 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;
}
}
$dbname = db_quote(get_config('dbname'));
// not using db_table_name here because it uses double quotes that break the query
$table = db_quote(get_config('dbprefix') . $table);
$field = db_quote($field);
return get_column_sql("
SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (
TABLE_SCHEMA = $dbname
OR
TABLE_CATALOG = $dbname
)
AND TABLE_NAME = $table
AND COLUMN_NAME = $field
");
}
/**
* Check to see if table exists by querying it and catching error if not
* This is a faster way than using the table_exists() function in lib/ddl.php
* This is a faster way than using the table_exists() function in lib/ddl.php db_quote_identifier(get_config('dbprefix') . $name);
* NOTE: When doing an upgrade we fallback to check the table in the old way to avoid
* inconsistent state before db_commit() is called.
*/
function db_table_exists($table) {
global $DB_IGNORE_SQL_EXCEPTIONS;
if (defined('INSTALLER')) {
// We do the check in the old way
require_once('ddl.php');
return table_exists(new XMLDBTable($table));
}
else {
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;
}
}
$dbname = db_quote(get_config('dbname'));
// not using db_table_name here because it uses double quotes that break the query
$table = db_quote(get_config('dbprefix') . $table);
return get_column_sql("
SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE (
TABLE_SCHEMA = $dbname
OR
TABLE_CATALOG = $dbname
)
AND TABLE_NAME = $table
");
}
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