|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php
-
-
- namespace yii\db\sqlite;
-
- use yii\base\NotSupportedException;
- use yii\db\Expression;
- use yii\db\TableSchema;
- use yii\db\ColumnSchema;
- use yii\db\Transaction;
-
-
- class Schema extends \yii\db\Schema
- {
-
-
- public $typeMap = [
- 'tinyint' => self::TYPE_SMALLINT,
- 'bit' => self::TYPE_SMALLINT,
- 'boolean' => self::TYPE_BOOLEAN,
- 'bool' => self::TYPE_BOOLEAN,
- 'smallint' => self::TYPE_SMALLINT,
- 'mediumint' => self::TYPE_INTEGER,
- 'int' => self::TYPE_INTEGER,
- 'integer' => self::TYPE_INTEGER,
- 'bigint' => self::TYPE_BIGINT,
- 'float' => self::TYPE_FLOAT,
- 'double' => self::TYPE_DOUBLE,
- 'real' => self::TYPE_FLOAT,
- 'decimal' => self::TYPE_DECIMAL,
- 'numeric' => self::TYPE_DECIMAL,
- 'tinytext' => self::TYPE_TEXT,
- 'mediumtext' => self::TYPE_TEXT,
- 'longtext' => self::TYPE_TEXT,
- 'text' => self::TYPE_TEXT,
- 'varchar' => self::TYPE_STRING,
- 'string' => self::TYPE_STRING,
- 'char' => self::TYPE_CHAR,
- 'blob' => self::TYPE_BINARY,
- 'datetime' => self::TYPE_DATETIME,
- 'year' => self::TYPE_DATE,
- 'date' => self::TYPE_DATE,
- 'time' => self::TYPE_TIME,
- 'timestamp' => self::TYPE_TIMESTAMP,
- 'enum' => self::TYPE_STRING,
- ];
-
-
-
-
- public function quoteSimpleTableName($name)
- {
- return strpos($name, '`') !== false ? $name : "`$name`";
- }
-
-
-
- public function quoteSimpleColumnName($name)
- {
- return strpos($name, '`') !== false || $name === '*' ? $name : "`$name`";
- }
-
-
-
- public function createQueryBuilder()
- {
- return new QueryBuilder($this->db);
- }
-
-
-
- public function createColumnSchemaBuilder($type, $length = null)
- {
- return new ColumnSchemaBuilder($type, $length);
- }
-
-
-
- protected function findTableNames($schema = '')
- {
- $sql = "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence' ORDER BY tbl_name";
-
- return $this->db->createCommand($sql)->queryColumn();
- }
-
-
-
- protected function loadTableSchema($name)
- {
- $table = new TableSchema;
- $table->name = $name;
- $table->fullName = $name;
-
- if ($this->findColumns($table)) {
- $this->findConstraints($table);
-
- return $table;
- } else {
- return null;
- }
- }
-
-
-
- protected function findColumns($table)
- {
- $sql = 'PRAGMA table_info(' . $this->quoteSimpleTableName($table->name) . ')';
- $columns = $this->db->createCommand($sql)->queryAll();
- if (empty($columns)) {
- return false;
- }
-
- foreach ($columns as $info) {
- $column = $this->loadColumnSchema($info);
- $table->columns[$column->name] = $column;
- if ($column->isPrimaryKey) {
- $table->primaryKey[] = $column->name;
- }
- }
- if (count($table->primaryKey) === 1 && !strncasecmp($table->columns[$table->primaryKey[0]]->dbType, 'int', 3)) {
- $table->sequenceName = '';
- $table->columns[$table->primaryKey[0]]->autoIncrement = true;
- }
-
- return true;
- }
-
-
-
- protected function findConstraints($table)
- {
- $sql = 'PRAGMA foreign_key_list(' . $this->quoteSimpleTableName($table->name) . ')';
- $keys = $this->db->createCommand($sql)->queryAll();
- foreach ($keys as $key) {
- $id = (int) $key['id'];
- if (!isset($table->foreignKeys[$id])) {
- $table->foreignKeys[$id] = [$key['table'], $key['from'] => $key['to']];
- } else {
-
- $table->foreignKeys[$id][$key['from']] = $key['to'];
- }
- }
- }
-
-
-
- public function findUniqueIndexes($table)
- {
- $sql = 'PRAGMA index_list(' . $this->quoteSimpleTableName($table->name) . ')';
- $indexes = $this->db->createCommand($sql)->queryAll();
- $uniqueIndexes = [];
-
- foreach ($indexes as $index) {
- $indexName = $index['name'];
- $indexInfo = $this->db->createCommand('PRAGMA index_info(' . $this->quoteValue($index['name']) . ')')->queryAll();
-
- if ($index['unique']) {
- $uniqueIndexes[$indexName] = [];
- foreach ($indexInfo as $row) {
- $uniqueIndexes[$indexName][] = $row['name'];
- }
- }
- }
-
- return $uniqueIndexes;
- }
-
-
-
- protected function loadColumnSchema($info)
- {
- $column = $this->createColumnSchema();
- $column->name = $info['name'];
- $column->allowNull = !$info['notnull'];
- $column->isPrimaryKey = $info['pk'] != 0;
-
- $column->dbType = strtolower($info['type']);
- $column->unsigned = strpos($column->dbType, 'unsigned') !== false;
-
- $column->type = self::TYPE_STRING;
- if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) {
- $type = strtolower($matches[1]);
- if (isset($this->typeMap[$type])) {
- $column->type = $this->typeMap[$type];
- }
-
- if (!empty($matches[2])) {
- $values = explode(',', $matches[2]);
- $column->size = $column->precision = (int) $values[0];
- if (isset($values[1])) {
- $column->scale = (int) $values[1];
- }
- if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
- $column->type = 'boolean';
- } elseif ($type === 'bit') {
- if ($column->size > 32) {
- $column->type = 'bigint';
- } elseif ($column->size === 32) {
- $column->type = 'integer';
- }
- }
- }
- }
- $column->phpType = $this->getColumnPhpType($column);
-
- if (!$column->isPrimaryKey) {
- if ($info['dflt_value'] === 'null' || $info['dflt_value'] === '' || $info['dflt_value'] === null) {
- $column->defaultValue = null;
- } elseif ($column->type === 'timestamp' && $info['dflt_value'] === 'CURRENT_TIMESTAMP') {
- $column->defaultValue = new Expression('CURRENT_TIMESTAMP');
- } else {
- $value = trim($info['dflt_value'], "'\"");
- $column->defaultValue = $column->phpTypecast($value);
- }
- }
-
- return $column;
- }
-
-
-
- public function setTransactionIsolationLevel($level)
- {
- switch ($level) {
- case Transaction::SERIALIZABLE:
- $this->db->createCommand('PRAGMA read_uncommitted = False;')->execute();
- break;
- case Transaction::READ_UNCOMMITTED:
- $this->db->createCommand('PRAGMA read_uncommitted = True;')->execute();
- break;
- default:
- throw new NotSupportedException(get_class($this) . ' only supports transaction isolation levels READ UNCOMMITTED and SERIALIZABLE.');
- }
- }
- }
|