Refactoring of *Field classes:
authorMax Semenik <maxsem@users.mediawiki.org>
Sun, 21 Nov 2010 19:56:51 +0000 (19:56 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Sun, 21 Nov 2010 19:56:51 +0000 (19:56 +0000)
* Made them all implement one common interface (might add more functions to it later)
* Moved MySQLField to DatabaseMysql.php
* Renamed nullable() to isNullable()
* Removed maxLength() from:
** SQLiteField: makes no sense
** MySQLField: doesn't do what people may think, useless for this class' purpose of assisting querying the DB schema

includes/AutoLoader.php
includes/db/Database.php
includes/db/DatabaseIbm_db2.php
includes/db/DatabaseMssql.php
includes/db/DatabaseMysql.php
includes/db/DatabaseOracle.php
includes/db/DatabasePostgres.php
includes/db/DatabaseSqlite.php
includes/installer/MysqlUpdater.php
includes/installer/PostgresUpdater.php

index 79148d9..bcaa038 100644 (file)
@@ -381,6 +381,7 @@ $wgAutoloadLocalClasses = array(
        'DBQueryError' => 'includes/db/Database.php',
        'DBUnexpectedError' => 'includes/db/Database.php',
        'FakeResultWrapper' => 'includes/db/Database.php',
+       'Field' => 'includes/db/Database.php',
        'IBM_DB2Blob' => 'includes/db/DatabaseIbm_db2.php',
        'LBFactory' => 'includes/db/LBFactory.php',
        'LBFactory_Multi' => 'includes/db/LBFactory_Multi.php',
@@ -389,7 +390,7 @@ $wgAutoloadLocalClasses = array(
        'LoadBalancer' => 'includes/db/LoadBalancer.php',
        'LoadMonitor' => 'includes/db/LoadMonitor.php',
        'LoadMonitor_MySQL' => 'includes/db/LoadMonitor.php',
-       'MySQLField' => 'includes/db/Database.php',
+       'MySQLField' => 'includes/db/DatabaseMysql.php',
        'MySQLMasterPos' => 'includes/db/DatabaseMysql.php',
        'ORABlob' => 'includes/db/DatabaseOracle.php',
        'ORAField' => 'includes/db/DatabaseOracle.php',
index 9b80334..cf6d9df 100644 (file)
@@ -2646,57 +2646,33 @@ class Blob {
 }
 
 /**
- * Utility class.
+ * Base for all database-specific classes representing information about database fields
  * @ingroup Database
  */
-class MySQLField {
-       private $name, $tablename, $default, $max_length, $nullable,
-               $is_pk, $is_unique, $is_multiple, $is_key, $type;
-
-       function __construct ( $info ) {
-               $this->name = $info->name;
-               $this->tablename = $info->table;
-               $this->default = $info->def;
-               $this->max_length = $info->max_length;
-               $this->nullable = !$info->not_null;
-               $this->is_pk = $info->primary_key;
-               $this->is_unique = $info->unique_key;
-               $this->is_multiple = $info->multiple_key;
-               $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple );
-               $this->type = $info->type;
-       }
-
-       function name() {
-               return $this->name;
-       }
-
-       function tableName() {
-               return $this->tableName;
-       }
-
-       function defaultValue() {
-               return $this->default;
-       }
-
-       function maxLength() {
-               return $this->max_length;
-       }
-
-       function nullable() {
-               return $this->nullable;
-       }
+interface Field {
+       /**
+        * Field name
+        * @return string
+        */
+       function name();
 
-       function isKey() {
-               return $this->is_key;
-       }
+       /**
+        * Name of table this field belongs to
+        * @return string
+        */
+       function tableName();
 
-       function isMultipleKey() {
-               return $this->is_multiple;
-       }
+       /**
+        * Database type
+        * @return string
+        */
+       function type();
 
-       function type() {
-               return $this->type;
-       }
+       /**
+        * Whether this field can store NULL values
+        * @return bool
+        */
+       function isNullable();
 }
 
 /******************************************************************************
index 579581c..0105a23 100644 (file)
@@ -13,7 +13,7 @@
  * This represents a column in a DB2 database
  * @ingroup Database
  */
-class IBM_DB2Field {
+class IBM_DB2Field implements Field {
        private $name = '';
        private $tablename = '';
        private $type = '';
@@ -75,7 +75,7 @@ SQL;
         * Can column be null?
         * @return bool true or false
         */
-       function nullable() { return $this->nullable; }
+       function isNullable() { return $this->nullable; }
        /**
         * How much can you fit in the column per row?
         * @return int length
index 943c070..4846563 100644 (file)
@@ -1051,7 +1051,7 @@ class DatabaseMssql extends DatabaseBase {
  *
  * @ingroup Database
  */
-class MssqlField {
+class MssqlField implements Field {
        private $name, $tablename, $default, $max_length, $nullable, $type;
        function __construct ( $info ) {
                $this->name = $info['COLUMN_NAME'];
@@ -1077,7 +1077,7 @@ class MssqlField {
                return $this->max_length;
        }
 
-       function nullable() {
+       function isNullable() {
                return $this->nullable;
        }
 
index 07a4042..39e8037 100644 (file)
@@ -503,6 +503,56 @@ class DatabaseMysql extends DatabaseBase {
  */
 class Database extends DatabaseMysql {}
 
+/**
+ * Utility class.
+ * @ingroup Database
+ */
+class MySQLField implements Field {
+       private $name, $tablename, $default, $max_length, $nullable,
+               $is_pk, $is_unique, $is_multiple, $is_key, $type;
+
+       function __construct ( $info ) {
+               $this->name = $info->name;
+               $this->tablename = $info->table;
+               $this->default = $info->def;
+               $this->max_length = $info->max_length;
+               $this->nullable = !$info->not_null;
+               $this->is_pk = $info->primary_key;
+               $this->is_unique = $info->unique_key;
+               $this->is_multiple = $info->multiple_key;
+               $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple );
+               $this->type = $info->type;
+       }
+
+       function name() {
+               return $this->name;
+       }
+
+       function tableName() {
+               return $this->tableName;
+       }
+
+       function type() {
+               return $this->type;
+       }
+
+       function isNullable() {
+               return $this->nullable;
+       }
+
+       function defaultValue() {
+               return $this->default;
+       }
+
+       function isKey() {
+               return $this->is_key;
+       }
+
+       function isMultipleKey() {
+               return $this->is_multiple;
+       }
+}
+
 class MySQLMasterPos {
        var $file, $pos;
 
index b1f34f0..b258ca5 100644 (file)
@@ -124,7 +124,7 @@ class ORAResult {
  * Utility class.
  * @ingroup Database
  */
-class ORAField {
+class ORAField implements Field {
        private $name, $tablename, $default, $max_length, $nullable,
                $is_pk, $is_unique, $is_multiple, $is_key, $type;
 
@@ -157,7 +157,7 @@ class ORAField {
                return $this->max_length;
        }
 
-       function nullable() {
+       function isNullable() {
                return $this->nullable;
        }
 
index ec0e663..b619557 100644 (file)
@@ -6,7 +6,7 @@
  * @ingroup Database
  */
 
-class PostgresField {
+class PostgresField implements Field {
        private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname;
 
        static function fromText($db, $table, $field) {
@@ -69,7 +69,7 @@ SQL;
                return $this->type;
        }
 
-       function nullable() {
+       function isNullable() {
                return $this->nullable;
        }
 
index 1ebf1fd..4635d33 100644 (file)
@@ -626,7 +626,7 @@ class DatabaseSqliteStandalone extends DatabaseSqlite {
 /**
  * @ingroup Database
  */
-class SQLiteField {
+class SQLiteField implements Field {
        private $info, $tableName;
        function __construct( $info, $tableName ) {
                $this->info = $info;
@@ -651,17 +651,10 @@ class SQLiteField {
                return $this->info->dflt_value;
        }
 
-       function maxLength() {
-               return -1;
-       }
-
-       function nullable() {
+       function isNullable() {
                return !$this->info->notnull;
        }
 
-       # isKey(),  isMultipleKey() not implemented, MySQL-specific concept.
-       # Suggest removal from base class [TS]
-
        function type() {
                return $this->info->type;
        }
index b8414de..50376a5 100644 (file)
@@ -601,7 +601,7 @@ class MysqlUpdater extends DatabaseUpdater {
         */
        protected function doWatchlistNull() {
                $info = $this->db->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
-               if ( $info->nullable() ) {
+               if ( $info->isNullable() ) {
                        $this->output( "...wl_notificationtimestamp is already nullable.\n" );
                        return;
                }
index ed57098..44f5a24 100644 (file)
@@ -442,7 +442,7 @@ END;
                        $this->output( "... error: expected column $table.$field to exist\n" );
                        exit( 1 );
                }
-               if ( $fi->nullable() ) {
+               if ( $fi->isNullable() ) {
                        # # It's NULL - does it need to be NOT NULL?
                        if ( 'NOT NULL' === $null ) {
                                $this->output( "Changing \"$table.$field\" to not allow NULLs\n" );
@@ -616,7 +616,7 @@ END;
 
        protected function checkRcCurIdNullable(){
                $fi = $this->db->fieldInfo( 'recentchanges', 'rc_cur_id' );
-               if ( !$fi->nullable() ) {
+               if ( !$fi->isNullable() ) {
                        $this->output( "Removing NOT NULL constraint from \"recentchanges.rc_cur_id\"\n" );
                        $this->applyPatch( 'patch-rc_cur_id-not-null.sql' );
                } else {