|
|
@ -14,7 +14,7 @@ use yii\db\dao\TableSchema; |
|
|
|
use yii\db\dao\ColumnSchema; |
|
|
|
use yii\db\dao\ColumnSchema; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Driver is the class for retrieving meta data from a MySQL database (version 4.1.x and 5.x). |
|
|
|
* Driver is the class for retrieving metadata from a MySQL database (version 4.1.x and 5.x). |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @since 2.0 |
|
|
|
* @since 2.0 |
|
|
@ -24,7 +24,7 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var array mapping from physical column types (keys) to abstract column types (values) |
|
|
|
* @var array mapping from physical column types (keys) to abstract column types (values) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $typeMap = array( // dbType => type |
|
|
|
public $typeMap = array( |
|
|
|
'tinyint' => self::TYPE_SMALLINT, |
|
|
|
'tinyint' => self::TYPE_SMALLINT, |
|
|
|
'bit' => self::TYPE_SMALLINT, |
|
|
|
'bit' => self::TYPE_SMALLINT, |
|
|
|
'smallint' => self::TYPE_SMALLINT, |
|
|
|
'smallint' => self::TYPE_SMALLINT, |
|
|
@ -75,6 +75,16 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* Creates a query builder for the database. |
|
|
|
|
|
|
|
* This method may be overridden by child classes to create a DBMS-specific query builder. |
|
|
|
|
|
|
|
* @return QueryBuilder query builder instance |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function createQueryBuilder() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new QueryBuilder($this->connection); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Loads the metadata for the specified table. |
|
|
|
* Loads the metadata for the specified table. |
|
|
|
* @param string $name table name |
|
|
|
* @param string $name table name |
|
|
|
* @return \yii\db\dao\TableSchema driver dependent table metadata. Null if the table does not exist. |
|
|
|
* @return \yii\db\dao\TableSchema driver dependent table metadata. Null if the table does not exist. |
|
|
@ -91,9 +101,9 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Generates various kinds of table names. |
|
|
|
* Resolves the table name and schema name (if any). |
|
|
|
* @param \yii\db\dao\TableSchema $table the table instance |
|
|
|
* @param \yii\db\dao\TableSchema $table the table metadata object |
|
|
|
* @param string $name the unquoted table name |
|
|
|
* @param string $name the table name |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function resolveTableNames($table, $name) |
|
|
|
protected function resolveTableNames($table, $name) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -127,16 +137,17 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
$this->resolveColumnType($c); |
|
|
|
$this->resolveColumnType($c); |
|
|
|
$c->resolvePhpType(); |
|
|
|
$c->resolvePhpType(); |
|
|
|
|
|
|
|
|
|
|
|
$this->resolveDefaultValue($c, $column['Default']); |
|
|
|
$this->resolveColumnDefault($c, $column['Default']); |
|
|
|
|
|
|
|
|
|
|
|
return $c; |
|
|
|
return $c; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param \yii\db\dao\ColumnSchema $column |
|
|
|
* Resolves the default value for the column. |
|
|
|
* @param string $value |
|
|
|
* @param \yii\db\dao\ColumnSchema $column the column metadata object |
|
|
|
|
|
|
|
* @param string $value the default value fetched from database |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function resolveDefaultValue($column, $value) |
|
|
|
protected function resolveColumnDefault($column, $value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($column->type !== 'timestamp' || $value !== 'CURRENT_TIMESTAMP') { |
|
|
|
if ($column->type !== 'timestamp' || $value !== 'CURRENT_TIMESTAMP') { |
|
|
|
$column->defaultValue = $column->typecast($value); |
|
|
|
$column->defaultValue = $column->typecast($value); |
|
|
@ -144,8 +155,8 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Extracts the PHP type from DB type. |
|
|
|
* Resolves the abstract data type for the column. |
|
|
|
* @param \yii\db\dao\ColumnSchema $column the column |
|
|
|
* @param \yii\db\dao\ColumnSchema $column the column metadata object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function resolveColumnType($column) |
|
|
|
public function resolveColumnType($column) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -186,7 +197,7 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Collects the table column metadata. |
|
|
|
* Collects the metadata of table columns. |
|
|
|
* @param \yii\db\dao\TableSchema $table the table metadata |
|
|
|
* @param \yii\db\dao\TableSchema $table the table metadata |
|
|
|
* @return boolean whether the table exists in the database |
|
|
|
* @return boolean whether the table exists in the database |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -195,21 +206,21 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
$sql = 'SHOW COLUMNS FROM ' . $table->quotedName; |
|
|
|
$sql = 'SHOW COLUMNS FROM ' . $table->quotedName; |
|
|
|
try { |
|
|
|
try { |
|
|
|
$columns = $this->connection->createCommand($sql)->queryAll(); |
|
|
|
$columns = $this->connection->createCommand($sql)->queryAll(); |
|
|
|
} |
|
|
|
} catch (\Exception $e) { |
|
|
|
catch (\Exception $e) { |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
foreach ($columns as $column) { |
|
|
|
foreach ($columns as $column) { |
|
|
|
$table->columns[$c->name] = $c = $this->createColumn($column); |
|
|
|
$column = $this->createColumn($column); |
|
|
|
if ($c->isPrimaryKey) { |
|
|
|
$table->columns[$column->name] = $column; |
|
|
|
|
|
|
|
if ($column->isPrimaryKey) { |
|
|
|
if ($table->primaryKey === null) { |
|
|
|
if ($table->primaryKey === null) { |
|
|
|
$table->primaryKey = $c->name; |
|
|
|
$table->primaryKey = $column->name; |
|
|
|
} elseif (is_string($table->primaryKey)) { |
|
|
|
} elseif (is_string($table->primaryKey)) { |
|
|
|
$table->primaryKey = array($table->primaryKey, $c->name); |
|
|
|
$table->primaryKey = array($table->primaryKey, $column->name); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$table->primaryKey[] = $c->name; |
|
|
|
$table->primaryKey[] = $column->name; |
|
|
|
} |
|
|
|
} |
|
|
|
if ($c->autoIncrement) { |
|
|
|
if ($column->autoIncrement) { |
|
|
|
$table->sequenceName = ''; |
|
|
|
$table->sequenceName = ''; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -224,9 +235,14 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
protected function findConstraints($table) |
|
|
|
protected function findConstraints($table) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$row = $this->connection->createCommand('SHOW CREATE TABLE ' . $table->quotedName)->queryRow(); |
|
|
|
$row = $this->connection->createCommand('SHOW CREATE TABLE ' . $table->quotedName)->queryRow(); |
|
|
|
$matches = array(); |
|
|
|
if (isset($row['Create Table'])) { |
|
|
|
|
|
|
|
$sql = $row['Create Table']; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$row = array_values($row); |
|
|
|
|
|
|
|
$sql = $row[1]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$regexp = '/FOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)/mi'; |
|
|
|
$regexp = '/FOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)/mi'; |
|
|
|
foreach ($row as $sql) { |
|
|
|
|
|
|
|
if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) { |
|
|
|
if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) { |
|
|
|
foreach ($matches as $match) { |
|
|
|
foreach ($matches as $match) { |
|
|
|
$fks = array_map('trim', explode(',', str_replace('`', '', $match[1]))); |
|
|
|
$fks = array_map('trim', explode(',', str_replace('`', '', $match[1]))); |
|
|
@ -237,8 +253,6 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
} |
|
|
|
} |
|
|
|
$table->foreignKeys[] = $constraint; |
|
|
|
$table->foreignKeys[] = $constraint; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -255,19 +269,9 @@ class Driver extends \yii\db\dao\Driver |
|
|
|
} |
|
|
|
} |
|
|
|
$sql = 'SHOW TABLES FROM ' . $this->quoteSimpleTableName($schema); |
|
|
|
$sql = 'SHOW TABLES FROM ' . $this->quoteSimpleTableName($schema); |
|
|
|
$names = $this->connection->createCommand($sql)->queryColumn(); |
|
|
|
$names = $this->connection->createCommand($sql)->queryColumn(); |
|
|
|
foreach ($names as &$name) { |
|
|
|
foreach ($names as $i => $name) { |
|
|
|
$name = $schema . '.' . $name; |
|
|
|
$names[$i] = $schema . '.' . $name; |
|
|
|
} |
|
|
|
} |
|
|
|
return $names; |
|
|
|
return $names; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Creates a query builder for the database. |
|
|
|
|
|
|
|
* This method may be overridden by child classes to create a DBMS-specific query builder. |
|
|
|
|
|
|
|
* @return QueryBuilder query builder instance |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function createQueryBuilder() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new QueryBuilder($this->connection); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|