From c7a3fcecd25acfe64a9885beccbc0ccd841adf18 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Mon, 5 Jan 2009 00:09:32 -0500 Subject: [PATCH] add an Index object, and use it in the scheme objects. use long names where possible as well --- .../Db/lib/Horde/Db/Adapter/Abstract/Index.php | 70 ++++++++++++++++++++++ framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php | 18 +++--- .../Db/lib/Horde/Db/Adapter/Postgresql/Schema.php | 18 +++--- .../Db/lib/Horde/Db/Adapter/Sqlite/Schema.php | 8 +-- framework/Db/package.xml | 2 + 5 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 framework/Db/lib/Horde/Db/Adapter/Abstract/Index.php diff --git a/framework/Db/lib/Horde/Db/Adapter/Abstract/Index.php b/framework/Db/lib/Horde/Db/Adapter/Abstract/Index.php new file mode 100644 index 000000000..f5d1ccff2 --- /dev/null +++ b/framework/Db/lib/Horde/Db/Adapter/Abstract/Index.php @@ -0,0 +1,70 @@ + + * @author Derek DeVries + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_Db + * @subpackage Adapter + */ + +/** + * @author Mike Naberezny + * @author Derek DeVries + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_Db + * @subpackage Adapter + */ +class Horde_Db_Adapter_Abstract_Index +{ + public $table; + public $name; + public $unique; + public $primary; + public $columns; + + + /*########################################################################## + # Construct/Destruct + ##########################################################################*/ + + /** + * Construct + * + * @param string $table The table the index is on + * @param string $name The index's name + * @param boolean $primary Is this a primary key? + * @param boolean $unique Is this a unique index? + * @param array $columns The columns this index covers + */ + public function __construct($table, $name, $primary, $unique, $columns) + { + $this->table = $table; + $this->name = $name; + $this->primary = $primary; + $this->unique = $unique; + $this->columns = $columns; + } + + + /*########################################################################## + # Casting + ##########################################################################*/ + + /** + * Comma-separated list of the columns in the primary key + * + * @return string + */ + public function __toString() + { + return implode(',', $this->columns); + } + +} diff --git a/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php index 3f5608942..eedf1d16f 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php @@ -208,15 +208,15 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Abstract_Schema $indexes = array(); $currentIndex = null; foreach ($this->select('SHOW KEYS FROM ' . $this->quoteTableName($tableName)) as $row) { - if ($currentIndex != $row[2]) { - if ($row[2] == 'PRIMARY') continue; - $currentIndex = $row[2]; - $indexes[] = (object)array('table' => $row[0], - 'name' => $row[2], - 'unique' => $row[1] == '0', - 'columns' => array()); + if ($currentIndex != $row['Key_name']) { + if ($row['Key_name'] == 'PRIMARY') { + continue; + } + $currentIndex = $row['Key_name']; + $indexes[] = $this->componentFactory('Index', array( + $tableName, $row['Key_name'], false, $row['Non_unique'] == '0', array())); } - $indexes[count($indexes) - 1]->columns[] = $row[4]; + $indexes[count($indexes) - 1]->columns[] = $row['Column_name']; } $this->_cache->set("tables/indexes/$tableName", serialize($indexes)); @@ -243,7 +243,7 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Abstract_Schema $columns = array(); foreach ($rows as $row) { $columns[] = $this->componentFactory('Column', array( - $row[0], $row[4], $row[1], $row[2] == 'YES')); + $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 b84c3b163..ff4999f37 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php @@ -221,11 +221,11 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Abstract_Schem AND t.relname = " . $this->quote($tableName) . " AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (" . implode(',', $schemas) . ") ) AND a.attrelid = t.oid - AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum - OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum - OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum - OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum - OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum ) + AND (d.indkey[0] = a.attnum OR d.indkey[1] = a.attnum + OR d.indkey[2] = a.attnum OR d.indkey[3] = a.attnum + OR d.indkey[4] = a.attnum OR d.indkey[5] = a.attnum + OR d.indkey[6] = a.attnum OR d.indkey[7] = a.attnum + OR d.indkey[8] = a.attnum OR d.indkey[9] = a.attnum) ORDER BY i.relname"; $result = $this->select($sql, $name); @@ -235,13 +235,11 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Abstract_Schem foreach ($result as $row) { if ($currentIndex != $row[0]) { - $indexes[] = (object)array('table' => $tableName, - 'name' => $row[0], - 'unique' => $row[1] == 't', - 'columns' => array()); $currentIndex = $row[0]; + $indexes[] = $this->componentFactory('Index', array( + $tableName, $row[0], false, $row[1] == 't', array())); } - $indexes[sizeof($indexes)-1]->columns[] = $row[2]; + $indexes[count($indexes) - 1]->columns[] = $row[2]; } $this->_cache->set("tables/indexes/$tableName", serialize($indexes)); diff --git a/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php index 66dad57df..e280147e5 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php @@ -142,12 +142,10 @@ class Horde_Db_Adapter_Sqlite_Schema extends Horde_Db_Adapter_Abstract_Schema if (!$indexes) { $indexes = array(); foreach ($this->select('PRAGMA index_list(' . $this->quoteTableName($tableName) . ')') as $row) { - $index = (object)array('table' => $tableName, - 'name' => $row[1], - 'unique' => (bool)$row[2], - 'columns' => array()); + $index = $this->componentFactory('Index', array( + $tableName, $row['name'], false, (bool)$row['unique'], array())); foreach ($this->select('PRAGMA index_info(' . $this->quoteColumnName($index->name) . ')') as $field) { - $index->columns[] = $field[2]; + $index->columns[] = $field['name']; } $indexes[] = $index; diff --git a/framework/Db/package.xml b/framework/Db/package.xml index dafafcd18..c3963aaf4 100644 --- a/framework/Db/package.xml +++ b/framework/Db/package.xml @@ -41,6 +41,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -98,6 +99,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0