QueryBuilder.php 3.01 KB
Newer Older
w  
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * QueryBuilder class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\db\dao\mysql;

Qiang Xue committed
12 13
use yii\db\Exception;

w  
Qiang Xue committed
14
/**
Qiang Xue committed
15
 * QueryBuilder is the query builder for MySQL databases.
w  
Qiang Xue committed
16 17 18 19 20 21 22
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class QueryBuilder extends \yii\db\dao\QueryBuilder
{
	/**
Qiang Xue committed
23
	 * @var array mapping from abstract column types (keys) to physical column types (values).
w  
Qiang Xue committed
24
	 */
Qiang Xue committed
25
	public $typeMap = array(
Qiang Xue committed
26
		Driver::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
Qiang Xue committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40
		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)',
Qiang Xue committed
41
	);
w  
Qiang Xue committed
42 43 44 45

	/**
	 * 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.
Qiang Xue committed
46
	 * @param string $oldName the old name of the column. The name will be properly quoted by the method.
w  
Qiang Xue committed
47 48 49
	 * @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.
	 */
Qiang Xue committed
50
	public function renameColumn($table, $oldName, $newName)
w  
Qiang Xue committed
51
	{
Qiang Xue committed
52
		$quotedTable = $this->quoteTableName($table);
w  
Qiang Xue committed
53
		$row = $this->connection->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryRow();
Qiang Xue committed
54 55 56
		if ($row === false) {
			throw new Exception("Unable to find '$oldName' in table '$table'.");
		}
w  
Qiang Xue committed
57
		if (isset($row['Create Table'])) {
w  
Qiang Xue committed
58
			$sql = $row['Create Table'];
Qiang Xue committed
59
		} else {
w  
Qiang Xue committed
60 61 62
			$row = array_values($row);
			$sql = $row[1];
		}
w  
Qiang Xue committed
63 64
		if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) {
			foreach ($matches[1] as $i => $c) {
Qiang Xue committed
65
				if ($c === $oldName) {
Qiang Xue committed
66 67 68 69
					return "ALTER TABLE $quotedTable CHANGE "
						. $this->quoteColumnName($oldName, true) . ' '
						. $this->quoteColumnName($newName, true) . ' '
						. $matches[2][$i];
w  
Qiang Xue committed
70 71 72 73
				}
			}
		}
		// try to give back a SQL anyway
Qiang Xue committed
74 75 76
		return "ALTER TABLE $quotedTable CHANGE "
			. $this->quoteColumnName($oldName, true) . ' '
			. $this->quoteColumnName($newName, true);
w  
Qiang Xue committed
77 78 79 80 81 82 83 84 85 86
	}

	/**
	 * 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)
	{
Qiang Xue committed
87 88
		return 'ALTER TABLE ' . $this->quoteTableName($table)
			. ' DROP FOREIGN KEY ' . $this->quoteColumnName($name);
w  
Qiang Xue committed
89 90
	}
}