You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
3.0 KiB
91 lines
3.0 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
|
* QueryBuilder class file.
|
||
|
*
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright © 2008-2012 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\db\mysql;
|
||
14 years ago
|
|
||
13 years ago
|
use yii\db\Exception;
|
||
|
|
||
14 years ago
|
/**
|
||
13 years ago
|
* QueryBuilder is the query builder for MySQL databases.
|
||
14 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class QueryBuilder extends \yii\db\QueryBuilder
|
||
14 years ago
|
{
|
||
|
/**
|
||
13 years ago
|
* @var array mapping from abstract column types (keys) to physical column types (values).
|
||
14 years ago
|
*/
|
||
13 years ago
|
public $typeMap = array(
|
||
13 years ago
|
Driver::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
|
||
13 years ago
|
Driver::TYPE_STRING => 'varchar(255)',
|
||
|
Driver::TYPE_TEXT => 'text',
|
||
|
Driver::TYPE_SMALLINT => 'smallint(6)',
|
||
|
Driver::TYPE_INTEGER => 'int(11)',
|
||
|
Driver::TYPE_BIGINT => 'bigint(20)',
|
||
|
Driver::TYPE_FLOAT => 'float',
|
||
|
Driver::TYPE_DECIMAL => 'decimal',
|
||
|
Driver::TYPE_DATETIME => 'datetime',
|
||
|
Driver::TYPE_TIMESTAMP => 'timestamp',
|
||
|
Driver::TYPE_TIME => 'time',
|
||
|
Driver::TYPE_DATE => 'date',
|
||
|
Driver::TYPE_BINARY => 'blob',
|
||
|
Driver::TYPE_BOOLEAN => 'tinyint(1)',
|
||
|
Driver::TYPE_MONEY => 'decimal(19,4)',
|
||
13 years ago
|
);
|
||
14 years ago
|
|
||
|
/**
|
||
|
* Builds a SQL statement for renaming a column.
|
||
|
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
|
||
13 years ago
|
* @param string $oldName the old name of the column. The name will be properly quoted by the method.
|
||
14 years ago
|
* @param string $newName the new name of the column. The name will be properly quoted by the method.
|
||
|
* @return string the SQL statement for renaming a DB column.
|
||
|
*/
|
||
13 years ago
|
public function renameColumn($table, $oldName, $newName)
|
||
14 years ago
|
{
|
||
13 years ago
|
$quotedTable = $this->quoteTableName($table);
|
||
14 years ago
|
$row = $this->connection->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryRow();
|
||
13 years ago
|
if ($row === false) {
|
||
|
throw new Exception("Unable to find '$oldName' in table '$table'.");
|
||
|
}
|
||
14 years ago
|
if (isset($row['Create Table'])) {
|
||
14 years ago
|
$sql = $row['Create Table'];
|
||
13 years ago
|
} else {
|
||
14 years ago
|
$row = array_values($row);
|
||
|
$sql = $row[1];
|
||
|
}
|
||
14 years ago
|
if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) {
|
||
|
foreach ($matches[1] as $i => $c) {
|
||
13 years ago
|
if ($c === $oldName) {
|
||
13 years ago
|
return "ALTER TABLE $quotedTable CHANGE "
|
||
|
. $this->quoteColumnName($oldName, true) . ' '
|
||
|
. $this->quoteColumnName($newName, true) . ' '
|
||
|
. $matches[2][$i];
|
||
14 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
// try to give back a SQL anyway
|
||
13 years ago
|
return "ALTER TABLE $quotedTable CHANGE "
|
||
|
. $this->quoteColumnName($oldName, true) . ' '
|
||
|
. $this->quoteColumnName($newName, true);
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Builds a SQL statement for dropping a foreign key constraint.
|
||
|
* @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
|
||
|
* @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
|
||
|
* @return string the SQL statement for dropping a foreign key constraint.
|
||
|
*/
|
||
|
public function dropForeignKey($name, $table)
|
||
|
{
|
||
13 years ago
|
return 'ALTER TABLE ' . $this->quoteTableName($table)
|
||
|
. ' DROP FOREIGN KEY ' . $this->quoteColumnName($name);
|
||
14 years ago
|
}
|
||
|
}
|