From 67ebe72ade638f89e940bd1e5d8e31f81d7b7e32 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Fri, 2 Jul 2010 10:01:09 +0000 Subject: [PATCH] * Replaced the mess of every database class implementing filedExists() is its own way with one simple function in base class. Verified to work on MySQL, Postgres and SQLite. * Fixed fieldInfo() on Postgres not using tableName() and thus failing for table user, for example. * Made fieldInfo() on MySQL return false instead of throwing a query error if table does not exist. This is consistent with other databases' behaviour. --- includes/db/Database.php | 23 ++++++----------------- includes/db/DatabaseIbm_db2.php | 27 +-------------------------- includes/db/DatabaseMysql.php | 5 ++++- includes/db/DatabaseOracle.php | 4 ---- includes/db/DatabasePostgres.php | 20 ++------------------ includes/db/DatabaseSqlite.php | 8 -------- 6 files changed, 13 insertions(+), 74 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index bed6724561..d38b6c3dc7 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -1017,25 +1017,14 @@ abstract class DatabaseBase { /** * Determines whether a field exists in a table - * Usually aborts on failure - * If errors are explicitly ignored, returns NULL on failure + * @param $table: table name + * @param $filed: filed to check on that table + * @param $fname: calling function name (optional) + * @return bool: whether $table has filed $field */ function fieldExists( $table, $field, $fname = 'Database::fieldExists' ) { - $table = $this->tableName( $table ); - $res = $this->query( 'DESCRIBE '.$table, $fname ); - if ( !$res ) { - return null; - } - - $found = false; - - while ( $row = $this->fetchObject( $res ) ) { - if ( $row->Field == $field ) { - $found = true; - break; - } - } - return $found; + $info = $this->fieldInfo( $table, $field ); + return (bool)$info; } /** diff --git a/includes/db/DatabaseIbm_db2.php b/includes/db/DatabaseIbm_db2.php index 824267c7ca..4a10bd6fc4 100644 --- a/includes/db/DatabaseIbm_db2.php +++ b/includes/db/DatabaseIbm_db2.php @@ -291,8 +291,7 @@ class DatabaseIbm_db2 extends DatabaseBase { * setFakeSlaveLag [Done] * setFakeMaster [Done] * - * Reflection: 6 / 6 - * fieldExists [Done] + * Reflection: 5 / 5 * indexInfo [Done] * fieldInfo [Done] * fieldType [Done] @@ -1534,30 +1533,6 @@ EOF; # Reflection ###################################### - /** - * Query whether a given column exists in the mediawiki schema - * @param $table String: name of the table - * @param $field String: name of the column - * @param $fname String: function name for logging and profiling - */ - public function fieldExists( $table, $field, $fname = 'DatabaseIbm_db2::fieldExists' ) { - $table = $this->tableName( $table ); - $schema = $this->mSchema; - $etable = preg_replace("/'/", "''", $table); - $eschema = preg_replace("/'/", "''", $schema); - $ecol = preg_replace("/'/", "''", $field); - $sql = <<query( $sql, $fname ); - $count = $res ? $this->numRows($res) : 0; - if ($res) - $this->freeResult( $res ); - return $count; - } - /** * Returns information about an index * If errors are explicitly ignored, returns NULL on failure diff --git a/includes/db/DatabaseMysql.php b/includes/db/DatabaseMysql.php index 139c0b5e91..58809f1f97 100644 --- a/includes/db/DatabaseMysql.php +++ b/includes/db/DatabaseMysql.php @@ -262,7 +262,10 @@ class DatabaseMysql extends DatabaseBase { function fieldInfo( $table, $field ) { $table = $this->tableName( $table ); - $res = $this->query( "SELECT * FROM $table LIMIT 1" ); + $res = $this->query( "SELECT * FROM $table LIMIT 1", __METHOD__, true ); + if ( !$res ) { + return false; + } $n = mysql_num_fields( $res->result ); for( $i = 0; $i < $n; $i++ ) { $meta = mysql_fetch_field( $res->result, $i ); diff --git a/includes/db/DatabaseOracle.php b/includes/db/DatabaseOracle.php index bb967fc4b0..9bb947f36a 100644 --- a/includes/db/DatabaseOracle.php +++ b/includes/db/DatabaseOracle.php @@ -905,10 +905,6 @@ class DatabaseOracle extends DatabaseBase { return $this->fieldInfoMulti ($table, $field); } - function fieldExists( $table, $field, $fname = 'DatabaseOracle::fieldExists' ) { - return (bool)$this->fieldInfo( $table, $field, $fname ); - } - function begin( $fname = '' ) { $this->mTrxLevel = 1; } diff --git a/includes/db/DatabasePostgres.php b/includes/db/DatabasePostgres.php index 230f238deb..784845d2fb 100644 --- a/includes/db/DatabasePostgres.php +++ b/includes/db/DatabasePostgres.php @@ -31,6 +31,8 @@ AND nspname=%s AND relname=%s AND attname=%s; SQL; + + $table = $db->tableName( $table ); $res = $db->query(sprintf($q, $db->addQuotes($wgDBmwschema), $db->addQuotes($table), @@ -1264,24 +1266,6 @@ SQL; return $owner; } - /** - * Query whether a given column exists in the mediawiki schema - */ - function fieldExists( $table, $field, $fname = 'DatabasePostgres::fieldExists' ) { - global $wgDBmwschema; - $etable = preg_replace("/'/", "''", $table); - $eschema = preg_replace("/'/", "''", $wgDBmwschema); - $ecol = preg_replace("/'/", "''", $field); - $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n, pg_catalog.pg_attribute a " - . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema' " - . "AND a.attrelid = c.oid AND a.attname = '$ecol'"; - $res = $this->query( $SQL, $fname ); - $count = $res ? $res->numRows() : 0; - if ($res) - $this->freeResult( $res ); - return $count; - } - function fieldInfo( $table, $field ) { return PostgresField::fromText($this, $table, $field); } diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index 7e698382da..418dcd0663 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -435,14 +435,6 @@ class DatabaseSqlite extends DatabaseBase { return $ver; } - /** - * Query whether a given column exists in the mediawiki schema - */ - function fieldExists( $table, $field, $fname = '' ) { - $info = $this->fieldInfo( $table, $field ); - return (bool)$info; - } - /** * Get information about a given field * Returns false if the field does not exist. -- 2.20.1