* Replaced the mess of every database class implementing filedExists() is its own...
authorMax Semenik <maxsem@users.mediawiki.org>
Fri, 2 Jul 2010 10:01:09 +0000 (10:01 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Fri, 2 Jul 2010 10:01:09 +0000 (10:01 +0000)
* 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
includes/db/DatabaseIbm_db2.php
includes/db/DatabaseMysql.php
includes/db/DatabaseOracle.php
includes/db/DatabasePostgres.php
includes/db/DatabaseSqlite.php

index bed6724..d38b6c3 100644 (file)
@@ -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;
        }
 
        /**
index 824267c..4a10bd6 100644 (file)
@@ -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 = <<<SQL
-SELECT 1 as fieldexists
-FROM sysibm.syscolumns sc
-WHERE sc.name='$ecol' AND sc.tbname='$etable' AND sc.tbcreator='$eschema'
-SQL;
-               $res = $this->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
index 139c0b5..58809f1 100644 (file)
@@ -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 );
index bb967fc..9bb947f 100644 (file)
@@ -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;
        }
index 230f238..784845d 100644 (file)
@@ -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);
        }
index 7e69838..418dcd0 100644 (file)
@@ -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.