From: Chuck Hagenbuch Date: Wed, 13 Jan 2010 02:00:17 +0000 (-0500) Subject: Allow specifying a different name for the default primary key. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=ae0576c6fbb2bd837d77b9432c5e8cd08120f0fc;p=horde.git Allow specifying a different name for the default primary key. --- diff --git a/framework/Db/lib/Horde/Db/Adapter/Base/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Base/Schema.php index 89f57b426..aa5d9e90b 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Base/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Base/Schema.php @@ -358,12 +358,39 @@ abstract class Horde_Db_Adapter_Base_Schema */ public function createTable($name, $options=array()) { - $pk = isset($options['primaryKey']) && $options['primaryKey'] === false ? false : 'id'; $tableDefinition = $this->componentFactory('TableDefinition', array($name, $this, $options)); + + if (isset($options['primaryKey'])) { + if ($options['primaryKey'] === false) { + $pk = false; + } else { + switch ($options['primaryKey']) { + case 'true': + case 't': + case 1: + case '1': + $pk = 'id'; + break; + + case 'false': + case 'f': + case 0: + case '0': + $pk = false; + + default: + $pk = $options['primaryKey']; + } + } + } else { + $pk = 'id'; + } + if ($pk != false) { $tableDefinition->primaryKey($pk); } + return $tableDefinition; } diff --git a/framework/Db/test/Horde/Db/Adapter/MysqliTest.php b/framework/Db/test/Horde/Db/Adapter/MysqliTest.php index 0b604f19e..fdd5a3f31 100644 --- a/framework/Db/test/Horde/Db/Adapter/MysqliTest.php +++ b/framework/Db/test/Horde/Db/Adapter/MysqliTest.php @@ -490,7 +490,23 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase $this->fail("Expected exception for no pk"); } - public function testCreateTableWithExplicitPk() + public function testCreateTableWithNamedPk() + { + $this->_createTestTable('sports', array('primaryKey' => 'sports_id')); + + $sql = "SELECT sports_id FROM sports WHERE sports_id = 1"; + $this->assertEquals(1, $this->_conn->selectValue($sql)); + + try { + $sql = "SELECT id FROM sports WHERE id = 1"; + $this->assertNull($this->_conn->selectValue($sql)); + } catch (Exception $e) { + return; + } + $this->fail("Expected exception for wrong pk name"); + } + + public function testCreateTableWithSeparatePk() { $table = $this->_conn->createTable('testings'); $table->column('foo', 'primaryKey'); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php index d6a82aa53..b2da70823 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php @@ -510,7 +510,23 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase $this->fail("Expected exception for no pk"); } - public function testCreateTableWithExplicitPk() + public function testCreateTableWithNamedPk() + { + $this->_createTestTable('sports', array('primaryKey' => 'sports_id')); + + $sql = "SELECT sports_id FROM sports WHERE sports_id = 1"; + $this->assertEquals(1, $this->_conn->selectValue($sql)); + + try { + $sql = "SELECT id FROM sports WHERE id = 1"; + $this->assertNull($this->_conn->selectValue($sql)); + } catch (Exception $e) { + return; + } + $this->fail("Expected exception for wrong pk name"); + } + + public function testCreateTableWithSeparatePk() { $table = $this->_conn->createTable('testings'); $table->column('foo', 'primaryKey'); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php index ee9105fd3..75e2c5127 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php @@ -410,7 +410,23 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase $this->fail("Expected exception for no pk"); } - public function testCreateTableWithExplicitPk() + public function testCreateTableWithNamedPk() + { + $this->_createTestTable('sports', array('primaryKey' => 'sports_id')); + + $sql = "SELECT sports_id FROM sports WHERE sports_id = 1"; + $this->assertEquals(1, $this->_conn->selectValue($sql)); + + try { + $sql = "SELECT id FROM sports WHERE id = 1"; + $this->assertNull($this->_conn->selectValue($sql)); + } catch (Exception $e) { + return; + } + $this->fail("Expected exception for wrong pk name"); + } + + public function testCreateTableWithSeparatePk() { $table = $this->_conn->createTable('testings'); $table->column('foo', 'primaryKey'); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php index 291153428..0edc58e33 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php @@ -406,7 +406,23 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase $this->fail("Expected exception for no pk"); } - public function testCreateTableWithExplicitPk() + public function testCreateTableWithNamedPk() + { + $this->_createTestTable('sports', array('primaryKey' => 'sports_id')); + + $sql = "SELECT sports_id FROM sports WHERE sports_id = 1"; + $this->assertEquals(1, $this->_conn->selectValue($sql)); + + try { + $sql = "SELECT id FROM sports WHERE id = 1"; + $this->assertNull($this->_conn->selectValue($sql)); + } catch (Exception $e) { + return; + } + $this->fail("Expected exception for wrong pk name"); + } + + public function testCreateTableWithSeparatePk() { $table = $this->_conn->createTable('testings'); $table->column('foo', 'primaryKey');