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.

174 lines
5.0 KiB

14 years ago
<?php
/**
13 years ago
* Driver class file.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
14 years ago
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\db\dao\mysql;
13 years ago
use yii\db\dao\TableSchema;
14 years ago
/**
13 years ago
* Driver is the class for retrieving meta data 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 Driver extends \yii\db\dao\Driver
14 years ago
{
/**
* Quotes a table name for use in a query.
13 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.
13 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
}
/**
* Loads the metadata for the specified table.
* @param string $name table name
13 years ago
* @return \yii\db\dao\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;
}
}
/**
* Generates various kinds of table names.
13 years ago
* @param \yii\db\dao\TableSchema $table the table instance
14 years ago
* @param string $name the unquoted table name
*/
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
$table->quotedName = $this->quoteSimpleTableName($table->schemaName) . '.' . $this->quoteSimpleTableName($table->name);
13 years ago
} else {
14 years ago
$table->name = $parts[0];
14 years ago
$table->quotedName = $this->quoteSimpleTableName($table->name);
14 years ago
}
}
/**
* Creates a table column.
* @param array $column column metadata
* @return CDbColumnSchema normalized column metadata
*/
protected function createColumn($column)
{
14 years ago
$c = new ColumnSchema;
14 years ago
$c->name = $column['Field'];
14 years ago
$c->quotedName = $this->quoteSimpleColumnName($c->name);
14 years ago
$c->allowNull = $column['Null'] === 'YES';
$c->isPrimaryKey = strpos($column['Key'], 'PRI') !== false;
14 years ago
$c->autoIncrement = stripos($column['Extra'], 'auto_increment') !== false;
$c->initTypes($column['Type']);
$c->initDefaultValue($column['Default']);
14 years ago
return $c;
}
/**
14 years ago
* Collects the table column metadata.
13 years ago
* @param \yii\db\dao\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
{
14 years ago
$sql = 'SHOW COLUMNS FROM ' . $table->quotedName;
try {
$columns = $this->connection->createCommand($sql)->queryAll();
}
catch(\Exception $e) {
return false;
}
foreach ($columns as $column) {
$table->columns[$c->name] = $c = $this->createColumn($column);
if ($c->isPrimaryKey) {
if ($table->primaryKey === null) {
$table->primaryKey = $c->name;
13 years ago
} elseif (is_string($table->primaryKey)) {
14 years ago
$table->primaryKey = array($table->primaryKey, $c->name);
13 years ago
} else {
14 years ago
$table->primaryKey[] = $c->name;
}
if ($c->autoIncrement) {
$table->sequenceName = '';
}
}
}
return true;
14 years ago
}
/**
* Collects the foreign key column details for the given table.
13 years ago
* @param \yii\db\dao\TableSchema $table the table metadata
14 years ago
*/
protected function findConstraints($table)
{
14 years ago
$row = $this->connection->createCommand('SHOW CREATE TABLE ' . $table->quotedName)->queryRow();
14 years ago
$matches = array();
$regexp = '/FOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)/mi';
14 years ago
foreach ($row as $sql) {
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]));
14 years ago
foreach ($fks as $k => $name) {
$constraint[$name] = $pks[$k];
}
$table->foreignKeys[] = $constraint;
}
14 years ago
break;
}
}
}
/**
* 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 = '')
{
14 years ago
if ($schema === '') {
return $this->connection->createCommand('SHOW TABLES')->queryColumn();
14 years ago
}
14 years ago
$names = $this->connection->createCommand('SHOW TABLES FROM ' . $this->quoteSimpleTableName($schema))->queryColumn();
foreach ($names as &$name) {
$name = $schema . '.' . $name;
14 years ago
}
14 years ago
return $names;
14 years ago
}
}