From 2c893a7cc7250b36a225fa4456010049f8c45ae2 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Mon, 5 Jan 2009 00:31:53 -0500 Subject: [PATCH] add primaryKey() method for getting an Index object that corresponds to a table's primary key --- .../Db/lib/Horde/Db/Adapter/Abstract/Schema.php | 5 +++++ framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php | 24 ++++++++++++++++++++++ .../Db/lib/Horde/Db/Adapter/Postgresql/Schema.php | 9 ++++++++ .../Db/lib/Horde/Db/Adapter/Sqlite/Schema.php | 24 ++++++++++++++++++++++ framework/Db/test/Horde/Db/Adapter/MysqliTest.php | 8 ++++++++ .../Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php | 8 ++++++++ .../Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php | 8 ++++++++ .../Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php | 8 ++++++++ 8 files changed, 94 insertions(+) diff --git a/framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php index 7ca9a0c1f..6fb15e9f9 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php @@ -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 diff --git a/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php index a2d3c8533..150f98011 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php @@ -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 diff --git a/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php index ff4999f37..5cbb52cd5 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php @@ -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) diff --git a/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php index e280147e5..a15d84fd4 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php @@ -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 diff --git a/framework/Db/test/Horde/Db/Adapter/MysqliTest.php b/framework/Db/test/Horde/Db/Adapter/MysqliTest.php index 006e8e050..85daf32f3 100644 --- a/framework/Db/test/Horde/Db/Adapter/MysqliTest.php +++ b/framework/Db/test/Horde/Db/Adapter/MysqliTest.php @@ -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'); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php index 212b55a4e..78e41d973 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php @@ -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'); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php index 6744842c4..be59ceed2 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php @@ -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'); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php index 04d763c68..00583b651 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php @@ -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'); -- 2.11.0