From 3796db7d0077d11973e1ce0a170e9fb1dc552f09 Mon Sep 17 00:00:00 2001 From: gevik Date: Sun, 9 Jun 2013 11:00:15 +0200 Subject: [PATCH] - Ported addPrimaryKey and created dropConstraint. - The dropConstraint method can be used both for dropPrimaryKey and dropForeignKey --- framework/yii/db/QueryBuilder.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/framework/yii/db/QueryBuilder.php b/framework/yii/db/QueryBuilder.php index 04f1969..f65ee25 100644 --- a/framework/yii/db/QueryBuilder.php +++ b/framework/yii/db/QueryBuilder.php @@ -268,6 +268,36 @@ class QueryBuilder extends \yii\base\Object { return "DROP TABLE " . $this->db->quoteTableName($table); } + + /** + * Builds a SQL statement for adding a primary key constraint to an existing table. + * @param string $name the name of the primary key constraint. + * @param string $table the table that the primary key constraint will be added to. + * @param string|array $columns comma separated string or array of columns that the primary key will consist of. + * @return string the SQL statement for adding a primary key constraint to an existing table. + */ + public function addPrimaryKey($name,$table,$columns) + { + if(is_string($columns)) + $columns=preg_split('/\s*,\s*/',$columns,-1,PREG_SPLIT_NO_EMPTY); + foreach($columns as $i=>$col) + $columns[$i]=$this->quoteColumnName($col); + return 'ALTER TABLE ' . $this->quoteTableName($table) . ' ADD CONSTRAINT ' + . $this->quoteColumnName($name) . ' PRIMARY KEY (' + . implode(', ', $columns). ' )'; + } + + /** + * Builds a SQL statement for removing a constraint to an existing table. + * @param string $name the name of the constraint to be removed. + * @param string $table the table that constraint will be removed from. + * @return string the SQL statement for removing constraint from an existing table. + */ + public function dropConstraint($name,$table) + { + return 'ALTER TABLE ' . $this->quoteTableName($table) . ' DROP CONSTRAINT ' + . $this->quoteColumnName($name); + } /** * Builds a SQL statement for truncating a DB table.