add primaryKey() method for getting an Index object that corresponds to a table's...
authorChuck Hagenbuch <chuck@horde.org>
Mon, 5 Jan 2009 05:31:53 +0000 (00:31 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 5 Jan 2009 05:31:53 +0000 (00:31 -0500)
framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.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
framework/Db/test/Horde/Db/Adapter/MysqliTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php

index 7ca9a0c..6fb15e9 100644 (file)
@@ -245,6 +245,11 @@ abstract class Horde_Db_Adapter_Abstract_Schema
     abstract public function tables($name = null);
 
     /**
+     * Return a table's primary key
+     */
+    abstract public function primaryKey($tableName, $name = null);
+
+    /**
      * Returns an array of indexes for the given table.
      *
      * @param   string  $tableName
index a2d3c85..150f980 100644 (file)
@@ -195,6 +195,30 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Abstract_Schema
     }
 
     /**
+     * Return a table's primary key
+     */
+    public function primaryKey($tableName, $name = null)
+    {
+        // Share the column cache with the columns() method
+        $rows = @unserialize($this->_cache->get("tables/columns/$tableName"));
+
+        if (!$rows) {
+            $rows = $this->selectAll('SHOW FIELDS FROM ' . $this->quoteTableName($tableName), $name);
+
+            $this->_cache->set("tables/columns/$tableName", serialize($rows));
+        }
+
+        $pk = $this->componentFactory('Index', array($tableName, 'PRIMARY', true, true, array()));
+        foreach ($rows as $row) {
+            if ($row['Key'] == 'PRI') {
+                $pk->columns[] = $row['Field'];
+            }
+        }
+
+        return $pk;
+    }
+
+    /**
      * List of indexes for the given table
      *
      * @param   string  $tableName
index ff4999f..5cbb52c 100644 (file)
@@ -199,6 +199,15 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Abstract_Schem
     }
 
     /**
+     * Return a table's primary key
+     */
+    public function primaryKey($tableName, $name = null)
+    {
+        list($defaultPk, ) = $this->pkAndSequenceFor($tableName);
+        return $this->componentFactory('Index', array($tableName, 'PRIMARY', true, true, array($defaultPk)));
+    }
+
+    /**
      * Returns the list of all indexes for a table.
      */
     public function indexes($tableName, $name = null)
index e280147..a15d84f 100644 (file)
@@ -130,6 +130,30 @@ class Horde_Db_Adapter_Sqlite_Schema extends Horde_Db_Adapter_Abstract_Schema
     }
 
     /**
+     * Return a table's primary key
+     */
+    public function primaryKey($tableName, $name = null)
+    {
+        // Share the columns cache with the columns() method
+        $rows = @unserialize($this->_cache->get("tables/columns/$tableName"));
+
+        if (!$rows) {
+            $rows = $this->selectAll('PRAGMA table_info(' . $this->quoteTableName($tableName) . ')', $name);
+
+            $this->_cache->set("tables/columns/$tableName", serialize($rows));
+        }
+
+        $pk = $this->componentFactory('Index', array($tableName, 'PRIMARY', true, true, array()));
+        foreach ($rows as $row) {
+            if ($row['pk'] == 1) {
+                $pk->columns[] = $row['name'];
+            }
+        }
+
+        return $pk;
+    }
+
+    /**
      * List of indexes for the given table
      *
      * @param   string  $tableName
index 006e8e0..85daf32 100644 (file)
@@ -337,6 +337,14 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $this->assertContains('unit_tests', $tables);
     }
 
+    public function testPrimaryKey()
+    {
+        $pk = $this->_conn->primaryKey('unit_tests');
+        $this->assertEquals('id', (string)$pk);
+        $this->assertEquals(1, count($pk->columns));
+        $this->assertEquals('id', $pk->columns[0]);
+    }
+
     public function testIndexes()
     {
         $indexes = $this->_conn->indexes('unit_tests');
index 212b55a..78e41d9 100644 (file)
@@ -337,6 +337,14 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $this->assertContains('unit_tests', $tables);
     }
 
+    public function testPrimaryKey()
+    {
+        $pk = $this->_conn->primaryKey('unit_tests');
+        $this->assertEquals('id', (string)$pk);
+        $this->assertEquals(1, count($pk->columns));
+        $this->assertEquals('id', $pk->columns[0]);
+    }
+
     public function testIndexes()
     {
         $indexes = $this->_conn->indexes('unit_tests');
index 6744842..be59cee 100644 (file)
@@ -338,6 +338,14 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
         $this->assertContains('unit_tests', $tables);
     }
 
+    public function testPrimaryKey()
+    {
+        $pk = $this->_conn->primaryKey('unit_tests');
+        $this->assertEquals('id', (string)$pk);
+        $this->assertEquals(1, count($pk->columns));
+        $this->assertEquals('id', $pk->columns[0]);
+    }
+
     public function testIndexes()
     {
         $indexes = $this->_conn->indexes('unit_tests');
index 04d763c..00583b6 100644 (file)
@@ -331,6 +331,14 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
         $this->assertContains('unit_tests', $tables);
     }
 
+    public function testPrimaryKey()
+    {
+        $pk = $this->_conn->primaryKey('unit_tests');
+        $this->assertEquals('id', (string)$pk);
+        $this->assertEquals(1, count($pk->columns));
+        $this->assertEquals('id', $pk->columns[0]);
+    }
+
     public function testIndexes()
     {
         $indexes = $this->_conn->indexes('unit_tests');