add an Index object, and use it in the scheme objects. use long names where possible...
authorChuck Hagenbuch <chuck@horde.org>
Mon, 5 Jan 2009 05:09:32 +0000 (00:09 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 5 Jan 2009 05:09:32 +0000 (00:09 -0500)
framework/Db/lib/Horde/Db/Adapter/Abstract/Index.php [new file with mode: 0644]
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
framework/Db/package.xml

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 (file)
index 0000000..f5d1ccf
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Copyright 2007 Maintainable Software, LLC
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * @author     Mike Naberezny <mike@maintainable.com>
+ * @author     Derek DeVries <derek@maintainable.com>
+ * @author     Chuck Hagenbuch <chuck@horde.org>
+ * @license    http://opensource.org/licenses/bsd-license.php
+ * @category   Horde
+ * @package    Horde_Db
+ * @subpackage Adapter
+ */
+
+/**
+ * @author     Mike Naberezny <mike@maintainable.com>
+ * @author     Derek DeVries <derek@maintainable.com>
+ * @author     Chuck Hagenbuch <chuck@horde.org>
+ * @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);
+    }
+
+}
index 3f56089..eedf1d1 100644 (file)
@@ -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;
index b84c3b1..ff4999f 100644 (file)
@@ -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));
index 66dad57..e280147 100644 (file)
@@ -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;
index dafafcd..c3963aa 100644 (file)
@@ -41,6 +41,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <dir name="Abstract">
         <file name="Column.php" role="php" />
         <file name="ColumnDefinition.php" role="php" />
+        <file name="Index.php" role="php" />
         <file name="Schema.php" role="php" />
         <file name="TableDefinition.php" role="php" />
        </dir> <!-- /lib/Horde/Db/Adapter/Abstract -->
@@ -98,6 +99,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <filelist>
    <install name="lib/Horde/Db/Adapter/Abstract/Column.php" as="Horde/Db/Adapter/Abstract/Column.php" />
    <install name="lib/Horde/Db/Adapter/Abstract/ColumnDefinition.php" as="Horde/Db/Adapter/Abstract/ColumnDefinition.php" />
+   <install name="lib/Horde/Db/Adapter/Abstract/Index.php" as="Horde/Db/Adapter/Abstract/Index.php" />
    <install name="lib/Horde/Db/Adapter/Abstract/Schema.php" as="Horde/Db/Adapter/Abstract/Schema.php" />
    <install name="lib/Horde/Db/Adapter/Abstract/TableDefinition.php" as="Horde/Db/Adapter/Abstract/TableDefinition.php" />
    <install name="lib/Horde/Db/Adapter/Abstract.php" as="Horde/Db/Adapter/Abstract.php" />