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.
		
		
		
		
			
				
					247 lines
				
				7.0 KiB
			
		
		
			
		
	
	
					247 lines
				
				7.0 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\mysql;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | use yii\db\TableSchema;
 | ||
|  | use yii\db\ColumnSchema;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											13 years ago
										 |  * Schema is the class for retrieving metadata from a MySQL database (version 4.1.x and 5.x).
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
| 
											14 years ago
										 |  * @since 2.0
 | ||
| 
											14 years ago
										 |  */
 | ||
| 
											13 years ago
										 | class Schema extends \yii\db\Schema
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * @var array mapping from physical column types (keys) to abstract column types (values)
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	public $typeMap = array(
 | ||
| 
											14 years ago
										 | 		'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,
 | ||
|  | 	);
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Quotes a table name for use in a query.
 | ||
| 
											14 years ago
										 | 	 * A simple table name has no schema prefix.
 | ||
| 
											14 years ago
										 | 	 * @param string $name table name
 | ||
|  | 	 * @return string the properly quoted table name
 | ||
|  | 	 */
 | ||
|  | 	public function quoteSimpleTableName($name)
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		return strpos($name, "`") !== false ? $name : "`" . $name . "`";
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Quotes a column name for use in a query.
 | ||
| 
											14 years ago
										 | 	 * A simple column name has no prefix.
 | ||
| 
											14 years ago
										 | 	 * @param string $name column name
 | ||
|  | 	 * @return string the properly quoted column name
 | ||
|  | 	 */
 | ||
|  | 	public function quoteSimpleColumnName($name)
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		return strpos($name, '`') !== false || $name === '*' ? $name : '`' . $name . '`';
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Creates a query builder for the MySQL database.
 | ||
| 
											14 years ago
										 | 	 * @return QueryBuilder query builder instance
 | ||
|  | 	 */
 | ||
|  | 	public function createQueryBuilder()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return new QueryBuilder($this->db);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											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
										 | 	 */
 | ||
| 
											14 years ago
										 | 	protected function loadTableSchema($name)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											14 years ago
										 | 		$table = new TableSchema;
 | ||
| 
											14 years ago
										 | 		$this->resolveTableNames($table, $name);
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 		if ($this->findColumns($table)) {
 | ||
| 
											14 years ago
										 | 			$this->findConstraints($table);
 | ||
|  | 			return $table;
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
|  | 			return null;
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Resolves the table name and schema name (if any).
 | ||
| 
											13 years ago
										 | 	 * @param TableSchema $table the table metadata object
 | ||
| 
											14 years ago
										 | 	 * @param string $name the table name
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	protected function resolveTableNames($table, $name)
 | ||
|  | 	{
 | ||
|  | 		$parts = explode('.', str_replace('`', '', $name));
 | ||
| 
											14 years ago
										 | 		if (isset($parts[1])) {
 | ||
| 
											14 years ago
										 | 			$table->schemaName = $parts[0];
 | ||
|  | 			$table->name = $parts[1];
 | ||
| 
											14 years ago
										 | 		} else {
 | ||
| 
											14 years ago
										 | 			$table->name = $parts[0];
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											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;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		$column->name = $info['Field'];
 | ||
|  | 		$column->allowNull = $info['Null'] === 'YES';
 | ||
|  | 		$column->isPrimaryKey = strpos($info['Key'], 'PRI') !== false;
 | ||
|  | 		$column->autoIncrement = stripos($info['Extra'], 'auto_increment') !== false;
 | ||
| 
											13 years ago
										 | 		$column->comment = $info['Comment'];
 | ||
|  | 
 | ||
| 
											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])) {
 | ||
|  | 				if ($type === 'enum') {
 | ||
|  | 					$values = explode(',', $matches[2]);
 | ||
|  | 					foreach ($values as $i => $value) {
 | ||
|  | 						$values[$i] = trim($value, "'");
 | ||
|  | 					}
 | ||
|  | 					$column->enumValues = $values;
 | ||
|  | 				} else {
 | ||
|  | 					$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
										 | 
 | ||
| 
											13 years ago
										 | 		$column->phpType = $this->getColumnPhpType($column);
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		if ($column->type !== 'timestamp' || $info['Default'] !== 'CURRENT_TIMESTAMP') {
 | ||
|  | 			$column->defaultValue = $column->typecast($info['Default']);
 | ||
|  | 		}
 | ||
|  | 
 | ||
|  | 		return $column;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Collects the metadata of table columns.
 | ||
| 
											13 years ago
										 | 	 * @param TableSchema $table the table metadata
 | ||
| 
											14 years ago
										 | 	 * @return boolean whether the table exists in the database
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	protected function findColumns($table)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
 | ||
| 
											14 years ago
										 | 		try {
 | ||
| 
											13 years ago
										 | 			$columns = $this->db->createCommand($sql)->queryAll();
 | ||
| 
											14 years ago
										 | 		} catch (\Exception $e) {
 | ||
| 
											14 years ago
										 | 			return false;
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 		foreach ($columns as $info) {
 | ||
| 
											13 years ago
										 | 			$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
										 | 				if ($column->autoIncrement) {
 | ||
| 
											14 years ago
										 | 					$table->sequenceName = '';
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return true;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * 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
										 | 		$row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->quoteSimpleTableName($table->name))->queryRow();
 | ||
| 
											14 years ago
										 | 		if (isset($row['Create Table'])) {
 | ||
|  | 			$sql = $row['Create Table'];
 | ||
|  | 		} else {
 | ||
|  | 			$row = array_values($row);
 | ||
|  | 			$sql = $row[1];
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 		$regexp = '/FOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)/mi';
 | ||
| 
											14 years ago
										 | 		if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
 | ||
|  | 			foreach ($matches as $match) {
 | ||
|  | 				$fks = array_map('trim', explode(',', str_replace('`', '', $match[1])));
 | ||
|  | 				$pks = array_map('trim', explode(',', str_replace('`', '', $match[3])));
 | ||
|  | 				$constraint = array(str_replace('`', '', $match[2]));
 | ||
|  | 				foreach ($fks as $k => $name) {
 | ||
|  | 					$constraint[$name] = $pks[$k];
 | ||
| 
											14 years ago
										 | 				}
 | ||
| 
											14 years ago
										 | 				$table->foreignKeys[] = $constraint;
 | ||
| 
											14 years ago
										 | 			}
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns all table names in the database.
 | ||
| 
											13 years ago
										 | 	 * This method should be overridden by child classes in order to support this feature
 | ||
|  | 	 * because the default implementation simply throws an exception.
 | ||
| 
											14 years ago
										 | 	 * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
 | ||
| 
											13 years ago
										 | 	 * @return array all table names in the database. The names have NO the schema name prefix.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	protected function findTableNames($schema = '')
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$sql = 'SHOW TABLES';
 | ||
|  | 		if ($schema !== '') {
 | ||
|  | 			$sql .= ' FROM ' . $this->quoteSimpleTableName($schema);
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		return $this->db->createCommand($sql)->queryColumn();
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | }
 |