QueryBuilder.php 7.86 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\db\sqlite;
Qiang Xue committed
9 10

use yii\db\Exception;
Qiang Xue committed
11
use yii\base\InvalidParamException;
Qiang Xue committed
12
use yii\base\NotSupportedException;
Qiang Xue committed
13 14

/**
Qiang Xue committed
15
 * QueryBuilder is the query builder for SQLite databases.
Qiang Xue committed
16 17 18 19
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
20
class QueryBuilder extends \yii\db\QueryBuilder
Qiang Xue committed
21 22 23 24
{
	/**
	 * @var array mapping from abstract column types (keys) to physical column types (values).
	 */
Qiang Xue committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	public $typeMap = array(
		Schema::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
		Schema::TYPE_STRING => 'varchar(255)',
		Schema::TYPE_TEXT => 'text',
		Schema::TYPE_SMALLINT => 'smallint',
		Schema::TYPE_INTEGER => 'integer',
		Schema::TYPE_BIGINT => 'bigint',
		Schema::TYPE_FLOAT => 'float',
		Schema::TYPE_DECIMAL => 'decimal',
		Schema::TYPE_DATETIME => 'datetime',
		Schema::TYPE_TIMESTAMP => 'timestamp',
		Schema::TYPE_TIME => 'time',
		Schema::TYPE_DATE => 'date',
		Schema::TYPE_BINARY => 'blob',
		Schema::TYPE_BOOLEAN => 'tinyint(1)',
		Schema::TYPE_MONEY => 'decimal(19,4)',
	);
Qiang Xue committed
42 43

	/**
Qiang Xue committed
44
	 * Creates a SQL statement for resetting the sequence value of a table's primary key.
Qiang Xue committed
45 46
	 * The sequence will be reset such that the primary key of the next new row inserted
	 * will have the specified value or 1.
Qiang Xue committed
47
	 * @param string $tableName the name of the table whose primary key sequence will be reset
Qiang Xue committed
48 49
	 * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
	 * the next new row's primary key will have a value 1.
Qiang Xue committed
50
	 * @return string the SQL statement for resetting sequence
Qiang Xue committed
51
	 * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table.
Qiang Xue committed
52
	 */
Qiang Xue committed
53
	public function resetSequence($tableName, $value = null)
Qiang Xue committed
54
	{
Qiang Xue committed
55
		$db = $this->db;
Qiang Xue committed
56 57
		$table = $db->getTableSchema($tableName);
		if ($table !== null && $table->sequenceName !== null) {
Qiang Xue committed
58
			if ($value === null) {
Qiang Xue committed
59 60 61
				$key = reset($table->primaryKey);
				$tableName = $db->quoteTableName($tableName);
				$value = $db->createCommand("SELECT MAX('$key') FROM $tableName")->queryScalar();
Qiang Xue committed
62
			} else {
Qiang Xue committed
63 64 65 66
				$value = (int)$value - 1;
			}
			try {
				// it's possible sqlite_sequence does not exist
Qiang Xue committed
67
				$db->createCommand("UPDATE sqlite_sequence SET seq='$value' WHERE name='{$table->name}'")->execute();
Qiang Xue committed
68 69
			} catch (Exception $e) {
			}
Qiang Xue committed
70
		} elseif ($table === null) {
Qiang Xue committed
71
			throw new InvalidParamException("Table not found: $tableName");
Qiang Xue committed
72
		} else {
Qiang Xue committed
73
			throw new InvalidParamException("There is not sequence associated with table '$tableName'.'");
Qiang Xue committed
74 75 76 77 78 79 80
		}
	}

	/**
	 * Enables or disables integrity check.
	 * @param boolean $check whether to turn on or off the integrity check.
	 * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
Qiang Xue committed
81
	 * @throws NotSupportedException this is not supported by SQLite
Qiang Xue committed
82 83 84
	 */
	public function checkIntegrity($check = true, $schema = '')
	{
Qiang Xue committed
85
		throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
Qiang Xue committed
86 87 88 89 90 91 92 93 94
	}

	/**
	 * Builds a SQL statement for truncating a DB table.
	 * @param string $table the table to be truncated. The name will be properly quoted by the method.
	 * @return string the SQL statement for truncating a DB table.
	 */
	public function truncateTable($table)
	{
Qiang Xue committed
95
		return "DELETE FROM " . $this->db->quoteTableName($table);
Qiang Xue committed
96 97 98 99 100 101 102 103 104 105
	}

	/**
	 * Builds a SQL statement for dropping an index.
	 * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
	 * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
	 * @return string the SQL statement for dropping an index.
	 */
	public function dropIndex($name, $table)
	{
Qiang Xue committed
106
		return 'DROP INDEX ' . $this->db->quoteTableName($name);
Qiang Xue committed
107 108 109 110 111 112 113
	}

	/**
	 * Builds a SQL statement for dropping a DB column.
	 * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method.
	 * @param string $column the name of the column to be dropped. The name will be properly quoted by the method.
	 * @return string the SQL statement for dropping a DB column.
Qiang Xue committed
114
	 * @throws NotSupportedException this is not supported by SQLite
Qiang Xue committed
115 116 117
	 */
	public function dropColumn($table, $column)
	{
Qiang Xue committed
118
		throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
Qiang Xue committed
119 120 121 122 123 124 125 126
	}

	/**
	 * 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.
	 * @param string $oldName the old name of the column. The name will be properly quoted by the method.
	 * @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
127
	 * @throws NotSupportedException this is not supported by SQLite
Qiang Xue committed
128 129 130
	 */
	public function renameColumn($table, $oldName, $newName)
	{
Qiang Xue committed
131
		throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
Qiang Xue committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	}

	/**
	 * Builds a SQL statement for adding a foreign key constraint to an existing table.
	 * The method will properly quote the table and column names.
	 * @param string $name the name of the foreign key constraint.
	 * @param string $table the table that the foreign key constraint will be added to.
	 * @param string|array $columns the name of the column to that the constraint will be added on.
	 * If there are multiple columns, separate them with commas or use an array to represent them.
	 * @param string $refTable the table that the foreign key references to.
	 * @param string|array $refColumns the name of the column that the foreign key references to.
	 * If there are multiple columns, separate them with commas or use an array to represent them.
	 * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
	 * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
	 * @return string the SQL statement for adding a foreign key constraint to an existing table.
Qiang Xue committed
147
	 * @throws NotSupportedException this is not supported by SQLite
Qiang Xue committed
148 149 150
	 */
	public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
	{
Qiang Xue committed
151
		throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
Qiang Xue committed
152 153 154 155 156 157 158
	}

	/**
	 * 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.
Qiang Xue committed
159
	 * @throws NotSupportedException this is not supported by SQLite
Qiang Xue committed
160 161 162
	 */
	public function dropForeignKey($name, $table)
	{
Qiang Xue committed
163
		throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
Qiang Xue committed
164 165 166 167 168 169 170 171 172 173 174
	}

	/**
	 * Builds a SQL statement for changing the definition of a column.
	 * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
	 * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
	 * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract
	 * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept
	 * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null'
	 * will become 'varchar(255) not null'.
	 * @return string the SQL statement for changing the definition of a column.
Qiang Xue committed
175
	 * @throws NotSupportedException this is not supported by SQLite
Qiang Xue committed
176 177 178
	 */
	public function alterColumn($table, $column, $type)
	{
Qiang Xue committed
179
		throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
Qiang Xue committed
180 181
	}
}