From b25fa6fb35df02b0ba66db79928790f4da1b4444 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Sun, 30 May 2010 15:54:31 -0400 Subject: [PATCH] Replace componentFactory() with individual factories that can be overridden. 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 | 21 -------- framework/Db/lib/Horde/Db/Adapter/Base/Schema.php | 56 ++++++++++++++++++++-- .../lib/Horde/Db/Adapter/Base/TableDefinition.php | 3 +- framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php | 23 +++++++-- .../Db/lib/Horde/Db/Adapter/Postgresql/Schema.php | 23 +++++++-- .../Db/lib/Horde/Db/Adapter/Sqlite/Schema.php | 23 +++++++-- 6 files changed, 106 insertions(+), 43 deletions(-) diff --git a/framework/Db/lib/Horde/Db/Adapter/Base.php b/framework/Db/lib/Horde/Db/Adapter/Base.php index 120083a37..562d1b7d0 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Base.php +++ b/framework/Db/lib/Horde/Db/Adapter/Base.php @@ -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 ##########################################################################*/ diff --git a/framework/Db/lib/Horde/Db/Adapter/Base/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Base/Schema.php index db3066770..482e78d43 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Base/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Base/Schema.php @@ -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) { diff --git a/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php b/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php index 11d7f758f..b3a8e6b6a 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php +++ b/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php @@ -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); diff --git a/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php index 24b3263ca..46df902ba 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php @@ -24,6 +24,19 @@ 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; diff --git a/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php index 4a585cdef..a1f349252 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php @@ -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; } diff --git a/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php index 76a1d797e..89ea35b90 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php @@ -24,6 +24,19 @@ 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; -- 2.11.0