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.
		
		
		
		
			
				
					184 lines
				
				5.2 KiB
			
		
		
			
		
	
	
					184 lines
				
				5.2 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yii\db\sqlite;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | use yii\db\TableSchema;
 | ||
|  | use yii\db\ColumnSchema;
 | ||
| 
											14 years ago
										 | 
 | ||
|  | /**
 | ||
| 
											13 years ago
										 |  * Schema is the class for retrieving metadata from a SQLite (2/3) database.
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | class Schema extends \yii\db\Schema
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
|  | 	 * @var array mapping from physical column types (keys) to abstract column types (values)
 | ||
|  | 	 */
 | ||
|  | 	public $typeMap = array(
 | ||
|  | 		'tinyint' => self::TYPE_SMALLINT,
 | ||
|  | 		'bit' => self::TYPE_SMALLINT,
 | ||
|  | 		'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_FLOAT,
 | ||
|  | 		'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_STRING,
 | ||
|  | 		'datetime' => self::TYPE_DATETIME,
 | ||
|  | 		'year' => self::TYPE_DATE,
 | ||
|  | 		'date' => self::TYPE_DATE,
 | ||
|  | 		'time' => self::TYPE_TIME,
 | ||
|  | 		'timestamp' => self::TYPE_TIMESTAMP,
 | ||
|  | 		'enum' => self::TYPE_STRING,
 | ||
|  | 	);
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Creates a query builder for the MySQL database.
 | ||
|  | 	 * This method may be overridden by child classes to create a DBMS-specific query builder.
 | ||
|  | 	 * @return QueryBuilder query builder instance
 | ||
|  | 	 */
 | ||
|  | 	public function createQueryBuilder()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return new QueryBuilder($this->db);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns all table names in the database.
 | ||
|  | 	 * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
 | ||
|  | 	 * If not empty, the returned table names will be prefixed with the schema name.
 | ||
|  | 	 * @return array all table names in the database.
 | ||
|  | 	 */
 | ||
|  | 	protected function findTableNames($schema = '')
 | ||
|  | 	{
 | ||
|  | 		$sql = "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'";
 | ||
| 
											13 years ago
										 | 		return $this->db->createCommand($sql)->queryColumn();
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Loads the metadata for the specified table.
 | ||
|  | 	 * @param string $name table name
 | ||
| 
											13 years ago
										 | 	 * @return TableSchema driver dependent table metadata. Null if the table does not exist.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	protected function loadTableSchema($name)
 | ||
|  | 	{
 | ||
|  | 		$table = new TableSchema;
 | ||
|  | 		$table->name = $name;
 | ||
|  | 
 | ||
|  | 		if ($this->findColumns($table)) {
 | ||
|  | 			$this->findConstraints($table);
 | ||
|  | 			return $table;
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
|  | 			return null;
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Collects the table column metadata.
 | ||
| 
											13 years ago
										 | 	 * @param TableSchema $table the table metadata
 | ||
| 
											14 years ago
										 | 	 * @return boolean whether the table exists in the database
 | ||
|  | 	 */
 | ||
|  | 	protected function findColumns($table)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$sql = "PRAGMA table_info(" . $this->quoteSimpleTableName($table->name) . ')';
 | ||
| 
											13 years ago
										 | 		$columns = $this->db->createCommand($sql)->queryAll();
 | ||
| 
											14 years ago
										 | 		if (empty($columns)) {
 | ||
|  | 			return false;
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		foreach ($columns as $info) {
 | ||
|  | 			$column = $this->loadColumnSchema($info);
 | ||
| 
											14 years ago
										 | 			$table->columns[$column->name] = $column;
 | ||
|  | 			if ($column->isPrimaryKey) {
 | ||
| 
											14 years ago
										 | 				$table->primaryKey[] = $column->name;
 | ||
| 
											14 years ago
										 | 			}
 | ||
|  | 		}
 | ||
| 
											14 years ago
										 | 		if (count($table->primaryKey) === 1 && !strncasecmp($table->columns[$table->primaryKey[0]]->dbType, 'int', 3)) {
 | ||
| 
											14 years ago
										 | 			$table->sequenceName = '';
 | ||
| 
											14 years ago
										 | 			$table->columns[$table->primaryKey[0]]->autoIncrement = true;
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 
 | ||
|  | 		return true;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Collects the foreign key column details for the given table.
 | ||
| 
											13 years ago
										 | 	 * @param TableSchema $table the table metadata
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	protected function findConstraints($table)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$sql = "PRAGMA foreign_key_list(" . $this->quoteSimpleTableName($table->name) . ')';
 | ||
| 
											13 years ago
										 | 		$keys = $this->db->createCommand($sql)->queryAll();
 | ||
| 
											14 years ago
										 | 		foreach ($keys as $key) {
 | ||
|  | 			$table->foreignKeys[] = array($key['table'], $key['from'] => $key['to']);
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Loads the column information into a [[ColumnSchema]] object.
 | ||
|  | 	 * @param array $info column information
 | ||
|  | 	 * @return ColumnSchema the column schema object
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	protected function loadColumnSchema($info)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$column = new ColumnSchema;
 | ||
|  | 		$column->name = $info['name'];
 | ||
|  | 		$column->allowNull = !$info['notnull'];
 | ||
|  | 		$column->isPrimaryKey = $info['pk'] != 0;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		$column->dbType = $info['type'];
 | ||
| 
											14 years ago
										 | 		$column->unsigned = strpos($column->dbType, 'unsigned') !== false;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		$column->type = self::TYPE_STRING;
 | ||
| 
											14 years ago
										 | 		if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) {
 | ||
|  | 			$type = $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';
 | ||
|  | 					}
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 		$column->phpType = $this->getColumnPhpType($column);
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		$value = $info['dflt_value'];
 | ||
| 
											14 years ago
										 | 		if ($column->type === 'string') {
 | ||
|  | 			$column->defaultValue = trim($value, "'\"");
 | ||
|  | 		} else {
 | ||
|  | 			$column->defaultValue = $column->typecast(strcasecmp($value, 'null') ? $value : null);
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		return $column;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | }
 |