Allow specifying a different name for the default primary key.
authorChuck Hagenbuch <chuck@horde.org>
Wed, 13 Jan 2010 02:00:17 +0000 (21:00 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Wed, 13 Jan 2010 02:03:18 +0000 (21:03 -0500)
framework/Db/lib/Horde/Db/Adapter/Base/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 89f57b4..aa5d9e9 100644 (file)
@@ -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;
     }
 
index 0b604f1..fdd5a3f 100644 (file)
@@ -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');
index d6a82aa..b2da708 100644 (file)
@@ -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');
index ee9105f..75e2c51 100644 (file)
@@ -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');
index 2911534..0edc58e 100644 (file)
@@ -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');