From 8be45558a5cd0d5e4ffb324e3b4259c20b05ec4a Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Tue, 12 Jan 2010 21:53:20 -0500 Subject: [PATCH] Simple implementation of creating tables with composite primary keys. Just lets you specify an array of columns to include in the PRIMARY KEY(...) clause, all of which must be added to the table expliclity. --- .../lib/Horde/Db/Adapter/Base/TableDefinition.php | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php b/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php index bf777c68d..c8b4d42f6 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php +++ b/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php @@ -27,6 +27,7 @@ class Horde_Db_Adapter_Base_TableDefinition implements ArrayAccess, IteratorAggr protected $_base = null; protected $_options = null; protected $_columns = null; + protected $_primaryKey = null; /** * Class Constructor @@ -64,8 +65,12 @@ class Horde_Db_Adapter_Base_TableDefinition implements ArrayAccess, IteratorAggr */ public function primaryKey($name) { - $natives = $this->_native(); - $this->column($name, $natives['primaryKey']); + if (is_scalar($name)) { + $natives = $this->_native(); + $this->column($name, $natives['primaryKey']); + } + + $this->_primaryKey = $name; } /** @@ -142,8 +147,21 @@ class Horde_Db_Adapter_Base_TableDefinition implements ArrayAccess, IteratorAggr { $cols = array(); foreach ($this->_columns as $col) { $cols[] = $col->toSql(); } + $sql = ' ' . implode(", \n ", $cols); + + // Specify composite primary keys as well + if (is_array($this->_primaryKey)) { + $pk = array(); + foreach ($this->_primaryKey as $pkColumn) { $pk[] = $this->_base->quoteColumnName($pkColumn); } + $sql .= ", \n PRIMARY KEY(" . implode(', ', $pk) . ')'; + } - return " ".implode(", \n ", $cols); + return $sql; + } + + public function __toString() + { + return $this->toSql(); } -- 2.11.0