Browse Source

- Ported addPrimaryKey and created dropConstraint.

- The dropConstraint method can be used both for dropPrimaryKey and dropForeignKey
tags/2.0.0-beta
gevik 11 years ago
parent
commit
3796db7d00
  1. 30
      framework/yii/db/QueryBuilder.php

30
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.

Loading…
Cancel
Save