mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-18 23:43:19 +08:00
- Ported addPrimaryKey and created dropConstraint.
- The dropConstraint method can be used both for dropPrimaryKey and dropForeignKey
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user