|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* QueryBuilder class file.
|
|
|
|
*
|
|
|
|
* @link http://www.yiiframework.com/
|
|
|
|
* @copyright Copyright © 2008-2012 Yii Software LLC
|
|
|
|
* @license http://www.yiiframework.com/license/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace yii\db\dao\sqlite;
|
|
|
|
|
|
|
|
use yii\db\Exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* QueryBuilder is the query builder for MySQL databases.
|
|
|
|
*
|
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
|
|
|
class QueryBuilder extends \yii\db\dao\QueryBuilder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array mapping from abstract column types (keys) to physical column types (values).
|
|
|
|
*/
|
|
|
|
public $typeMap = array(Driver::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', Driver::TYPE_STRING => 'varchar(255)', Driver::TYPE_TEXT => 'text', Driver::TYPE_SMALLINT => 'smallint', Driver::TYPE_INTEGER => 'integer', Driver::TYPE_BIGINT => 'bigint', 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)',);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the sequence value of a table's primary key.
|
|
|
|
* The sequence will be reset such that the primary key of the next new row inserted
|
|
|
|
* will have the specified value or 1.
|
|
|
|
* @param string $table the table schema whose primary key sequence will be reset
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
public function resetSequence($table, $value = null)
|
|
|
|
{
|
|
|
|
if ($table->sequenceName !== null) {
|
|
|
|
if ($value === null) {
|
|
|
|
$value = $this->connection->createCommand("SELECT MAX(`{$table->primaryKey[0]}`) FROM {$table->quotedName}")->queryScalar();
|
|
|
|
} else {
|
|
|
|
$value = (int)$value - 1;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// it's possible sqlite_sequence does not exist
|
|
|
|
$this->connection->createCommand("UPDATE sqlite_sequence SET seq='$value' WHERE name='{$table->name}'")->execute();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public function checkIntegrity($check = true, $schema = '')
|
|
|
|
{
|
|
|
|
// SQLite doesn't enforce integrity
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
return "DELETE FROM " . $this->quoteTableName($table);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
return 'DROP INDEX ' . $this->quoteTableName($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public function dropColumn($table, $column)
|
|
|
|
{
|
|
|
|
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public function renameColumn($table, $oldName, $newName)
|
|
|
|
{
|
|
|
|
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
|
|
|
|
{
|
|
|
|
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public function alterColumn($table, $column, $type)
|
|
|
|
{
|
|
|
|
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
|
|
|
|
}
|
|
|
|
}
|