Replace componentFactory() with individual factories that can be overridden.
authorChuck Hagenbuch <chuck@horde.org>
Sun, 30 May 2010 19:54:31 +0000 (15:54 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 31 May 2010 01:30:32 +0000 (21:30 -0400)
This removes the use of reflection, which gets rid of some overhead, and also
means that Horde_Db isn't reliant on a class-loading mechanism that's tolerant
of classes that aren't there.

framework/Db/lib/Horde/Db/Adapter/Base.php
framework/Db/lib/Horde/Db/Adapter/Base/Schema.php
framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php
framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php
framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php
framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php

index 120083a..562d1b7 100644 (file)
@@ -188,27 +188,6 @@ abstract class Horde_Db_Adapter_Base implements Horde_Db_Adapter
 
 
     /*##########################################################################
-    # Object factory
-    ##########################################################################*/
-
-    /**
-     * Delegate calls to the schema object.
-     *
-     * @param  string  $method
-     * @param  array   $args
-     *
-     * @return TODO
-     */
-    public function componentFactory($component, $args)
-    {
-        $class = str_replace('_Schema', '', $this->_schemaClass) . '_' . $component;
-        $class = new ReflectionClass(class_exists($class) ? $class : __CLASS__ . '_' . $component);
-
-        return $class->newInstanceArgs($args);
-    }
-
-
-    /*##########################################################################
     # Object composition
     ##########################################################################*/
 
index db30667..482e78d 100644 (file)
@@ -63,6 +63,53 @@ abstract class Horde_Db_Adapter_Base_Schema
 
 
     /*##########################################################################
+    # Object factories
+    ##########################################################################*/
+
+    /**
+     * Factory for Column objects
+     */
+    public function makeColumn($name, $default, $sqlType = null, $null = true)
+    {
+        return new Horde_Db_Adapter_Base_Column($name, $default, $sqlType, $null);
+    }
+
+    /**
+     * Factory for ColumnDefinition objects
+     */
+    public function makeColumnDefinition($base, $name, $type, $limit = null,
+        $precision = null, $scale = null, $unsigned = null,
+        $default = null, $null = null, $autoincrement = null)
+    {
+        return new Horde_Db_Adapter_Base_ColumnDefinition($base, $name, $type, $limit, $precision, $scale, $unsigned, $default, $null, $autoincrement);
+    }
+
+    /**
+     * Factory for Index objects
+     */
+    public function makeIndex($table, $name, $primary, $unique, $columns)
+    {
+        return new Horde_Db_Adapter_Base_Index($table, $name, $primary, $unique, $columns);
+    }
+
+    /**
+     * Factory for Table objects
+     */
+    public function makeTable($name, $primaryKey, $columns, $indexes)
+    {
+        return new Horde_Db_Adapter_Base_Table($name, $primaryKey, $columns, $indexes);
+    }
+
+    /**
+     * Factory for TableDefinition objects
+     */
+    public function makeTableDefinition($name, $base, $options = array())
+    {
+        return new Horde_Db_Adapter_Base_TableDefinition($name, $base, $options);
+    }
+
+
+    /*##########################################################################
     # Object composition
     ##########################################################################*/
 
@@ -258,12 +305,12 @@ abstract class Horde_Db_Adapter_Base_Schema
      */
     public function table($tableName, $name = null)
     {
-        return $this->componentFactory('Table', array(
+        return $this->makeTable(
             $tableName,
             $this->primaryKey($tableName),
             $this->columns($tableName, $name),
-            $this->indexes($tableName, $name),
-        ));
+            $this->indexes($tableName, $name)
+        );
     }
 
     /**
@@ -358,8 +405,7 @@ abstract class Horde_Db_Adapter_Base_Schema
      */
     public function createTable($name, $options=array())
     {
-        $tableDefinition =
-            $this->componentFactory('TableDefinition', array($name, $this, $options));
+        $tableDefinition = $this->makeTableDefinition($name, $this, $options);
 
         if (isset($options['primaryKey'])) {
             if ($options['primaryKey'] === false) {
index 11d7f75..b3a8e6b 100644 (file)
@@ -121,8 +121,7 @@ class Horde_Db_Adapter_Base_TableDefinition implements ArrayAccess, IteratorAggr
         if ($this[$name]) {
             $column = $this[$name];
         } else {
-            $column = $this->_base->componentFactory('ColumnDefinition', array(
-                $this->_base, $name, $type));
+            $column = $this->_base->makeColumnDefinition($this->_base, $name, $type);
         }
 
         $column->setLimit(isset($options['limit'])         ? $options['limit']     : null);
index 24b3263..46df902 100644 (file)
 class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Base_Schema
 {
     /*##########################################################################
+    # Object factories
+    ##########################################################################*/
+
+    /**
+     * Factory for Column objects
+     */
+    public function makeColumn($name, $default, $sqlType = null, $null = true)
+    {
+        return new Horde_Db_Adapter_Mysql_Column($name, $default, $sqlType, $null);
+    }
+
+
+    /*##########################################################################
     # Quoting
     ##########################################################################*/
 
@@ -192,7 +205,7 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Base_Schema
             $this->_cache->set("tables/columns/$tableName", serialize($rows));
         }
 
-        $pk = $this->componentFactory('Index', array($tableName, 'PRIMARY', true, true, array()));
+        $pk = $this->makeIndex($tableName, 'PRIMARY', true, true, array());
         foreach ($rows as $row) {
             if ($row['Key'] == 'PRI') {
                 $pk->columns[] = $row['Field'];
@@ -221,8 +234,8 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Base_Schema
                         continue;
                     }
                     $currentIndex = $row['Key_name'];
-                    $indexes[] = $this->componentFactory('Index', array(
-                        $tableName, $row['Key_name'], false, $row['Non_unique'] == '0', array()));
+                    $indexes[] = $this->makeIndex(
+                        $tableName, $row['Key_name'], false, $row['Non_unique'] == '0', array());
                 }
                 $indexes[count($indexes) - 1]->columns[] = $row['Column_name'];
             }
@@ -250,8 +263,8 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Base_Schema
         // create columns from rows
         $columns = array();
         foreach ($rows as $row) {
-            $columns[$row['Field']] = $this->componentFactory('Column', array(
-                $row['Field'], $row['Default'], $row['Type'], $row['Null'] == 'YES'));
+            $columns[$row['Field']] = $this->makeColumn(
+                $row['Field'], $row['Default'], $row['Type'], $row['Null'] == 'YES');
         }
 
         return $columns;
index 4a585cd..a1f3492 100644 (file)
@@ -30,6 +30,19 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Base_Schema
 
 
     /*##########################################################################
+    # Object factories
+    ##########################################################################*/
+
+    /**
+     * Factory for Column objects
+     */
+    public function makeColumn($name, $default, $sqlType = null, $null = true)
+    {
+        return new Horde_Db_Adapter_Postgresql_Column($name, $default, $sqlType, $null);
+    }
+
+
+    /*##########################################################################
     # Quoting
     ##########################################################################*/
 
@@ -208,7 +221,7 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Base_Schema
                   ' AND constraint_name = ' . $this->quoteString($tableName . '_pkey');
         $pk = $this->selectValues($sql, $name);
 
-        return $this->componentFactory('Index', array($tableName, 'PRIMARY', true, true, $pk));
+        return $this->makeIndex($tableName, 'PRIMARY', true, true, $pk);
     }
 
     /**
@@ -249,8 +262,8 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Base_Schema
             foreach ($result as $row) {
                 if ($currentIndex != $row[0]) {
                     $currentIndex = $row[0];
-                    $indexes[] = $this->componentFactory('Index', array(
-                        $tableName, $row[0], false, $row[1] == 't', array()));
+                    $indexes[] = $this->makeIndex(
+                        $tableName, $row[0], false, $row[1] == 't', array());
                 }
                 $indexes[count($indexes) - 1]->columns[] = $row[2];
             }
@@ -277,8 +290,8 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Base_Schema
         // create columns from rows
         $columns = array();
         foreach ($rows as $row) {
-            $columns[$row[0]] = $this->componentFactory('Column', array(
-                $row[0], $row[2], $row[1], !(boolean)$row[3]));
+            $columns[$row[0]] = $this->makeColumn(
+                $row[0], $row[2], $row[1], !(boolean)$row[3]);
         }
         return $columns;
     }
index 76a1d79..89ea35b 100644 (file)
 class Horde_Db_Adapter_Sqlite_Schema extends Horde_Db_Adapter_Base_Schema
 {
     /*##########################################################################
+    # Object factories
+    ##########################################################################*/
+
+    /**
+     * Factory for Column objects
+     */
+    public function makeColumn($name, $default, $sqlType = null, $null = true)
+    {
+        return new Horde_Db_Adapter_Sqlite_Column($name, $default, $sqlType, $null);
+    }
+
+
+    /*##########################################################################
     # Quoting
     ##########################################################################*/
 
@@ -123,7 +136,7 @@ class Horde_Db_Adapter_Sqlite_Schema extends Horde_Db_Adapter_Base_Schema
             $this->_cache->set("tables/columns/$tableName", serialize($rows));
         }
 
-        $pk = $this->componentFactory('Index', array($tableName, 'PRIMARY', true, true, array()));
+        $pk = $this->makeIndex($tableName, 'PRIMARY', true, true, array());
         foreach ($rows as $row) {
             if ($row['pk'] == 1) {
                 $pk->columns[] = $row['name'];
@@ -146,8 +159,8 @@ class Horde_Db_Adapter_Sqlite_Schema extends Horde_Db_Adapter_Base_Schema
         if (!$indexes) {
             $indexes = array();
             foreach ($this->select('PRAGMA index_list(' . $this->quoteTableName($tableName) . ')') as $row) {
-                $index = $this->componentFactory('Index', array(
-                    $tableName, $row['name'], false, (bool)$row['unique'], array()));
+                $index = $this->makeIndex(
+                    $tableName, $row['name'], false, (bool)$row['unique'], array());
                 foreach ($this->select('PRAGMA index_info(' . $this->quoteColumnName($index->name) . ')') as $field) {
                     $index->columns[] = $field['name'];
                 }
@@ -178,8 +191,8 @@ class Horde_Db_Adapter_Sqlite_Schema extends Horde_Db_Adapter_Base_Schema
         // create columns from rows
         $columns = array();
         foreach ($rows as $row) {
-            $columns[$row[1]] = $this->componentFactory('Column', array(
-                $row[1], $row[4], $row[2], !(bool)$row[3]));
+            $columns[$row[1]] = $this->makeColumn(
+                $row[1], $row[4], $row[2], !(bool)$row[3]);
         }
 
         return $columns;