Commit 3796db7d by gevik

- Ported addPrimaryKey and created dropConstraint.

- The dropConstraint method can be used both for dropPrimaryKey and dropForeignKey
parent c2b47d81
......@@ -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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment