Browse Source

w

tags/2.0.0-beta
Qiang Xue 14 years ago
parent
commit
7b31e27dd1
  1. 89
      framework/db/dao/ColumnSchema.php
  2. 10
      framework/db/dao/QueryBuilder.php
  3. 103
      framework/db/dao/Schema.php
  4. 12
      framework/db/dao/TableSchema.php
  5. 103
      framework/db/dao/mysql/ColumnSchema.php
  6. 93
      framework/db/dao/mysql/QueryBuilder.php
  7. 276
      framework/db/dao/mysql/Schema.php
  8. 24
      framework/db/dao/mysql/TableSchema.php

89
framework/db/dao/ColumnSchema.php

@ -8,13 +8,15 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\db\dao;
/** /**
* ColumnSchema class describes the column meta data of a database table. * ColumnSchema class describes the column meta data of a database table.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class ColumnSchema extends CComponent class ColumnSchema extends \yii\base\Component
{ {
/** /**
* @var string name of this column (without quotes). * @var string name of this column (without quotes).
@ -30,7 +32,7 @@ class ColumnSchema extends CComponent
public $allowNull; public $allowNull;
/** /**
* @var string logical type of this column. Possible logic types include: * @var string logical type of this column. Possible logic types include:
* string, text, boolean, integer, float, decimal, datetime, timestamp, time, date, binary * string, text, boolean, smallint, integer, bigint, float, decimal, datetime, timestamp, time, date, binary
*/ */
public $type; public $type;
/** /**
@ -47,6 +49,10 @@ class ColumnSchema extends CComponent
*/ */
public $defaultValue; public $defaultValue;
/** /**
* @var array enumerable values
*/
public $enumValues;
/**
* @var integer size of the column. * @var integer size of the column.
*/ */
public $size; public $size;
@ -63,69 +69,37 @@ class ColumnSchema extends CComponent
*/ */
public $isPrimaryKey; public $isPrimaryKey;
/** /**
* @var boolean whether this column is a foreign key
*/
public $isForeignKey;
/**
* @var boolean whether this column is auto-incremental * @var boolean whether this column is auto-incremental
*/ */
public $autoIncrement = false; public $autoIncrement = false;
/** /**
* Initializes the column with its DB type and default value. * @var boolean whether this column is unsigned. This is only meaningful
* This sets up the column's PHP type, size, precision, scale as well as default value. * when [[type]] is `integer` or `bigint`.
* @param string $dbType the column's DB type
* @param mixed $defaultValue the default value
*/ */
public function init($dbType, $defaultValue) public $unsigned;
{
$this->dbType = $dbType;
$this->extractType($dbType);
$this->extractLimit($dbType);
if ($defaultValue !== null)
$this->extractDefault($defaultValue);
}
/** /**
* Extracts the PHP type from DB type. * Extracts the PHP type from DB type.
* @param string $dbType DB type
*/ */
protected function extractType($dbType) protected function getPhpType()
{ {
if (stripos($dbType, 'int') !== false && stripos($dbType, 'unsigned int') === false) static $typeMap = array( // logical type => php type
$this->type = 'integer'; 'smallint' => 'integer',
elseif (stripos($dbType, 'bool') !== false) 'integer' => 'integer',
$this->type = 'boolean'; 'bigint' => 'integer',
elseif (preg_match('/(real|floa|doub)/i', $dbType)) 'boolean' => 'boolean',
$this->type = 'double'; 'float' => 'double',
else );
$this->type = 'string'; if (isset($typeMap[$this->type])) {
if ($this->type === 'bigint') {
return PHP_INT_SIZE == 8 && !$this->unsigned ? 'integer' : 'string';
} }
elseif ($this->type === 'integer') {
/** return PHP_INT_SIZE == 4 && $this->unsigned ? 'string' : 'integer';
* Extracts size, precision and scale information from column's DB type.
* @param string $dbType the column's DB type
*/
protected function extractLimit($dbType)
{
if (strpos($dbType, '(') && preg_match('/\((.*)\)/', $dbType, $matches))
{
$values = explode(',', $matches[1]);
$this->size = $this->precision = (int)$values[0];
if (isset($values[1]))
$this->scale = (int)$values[1];
} }
return $typeMap[$this->type];
} }
return 'string';
/**
* Extracts the default value for the column.
* The value is typecasted to correct PHP type.
* @param mixed $defaultValue the default value obtained from metadata
*/
protected function extractDefault($defaultValue)
{
$this->defaultValue = $this->typecast($defaultValue);
} }
/** /**
@ -135,17 +109,14 @@ class ColumnSchema extends CComponent
*/ */
public function typecast($value) public function typecast($value)
{ {
if (gettype($value) === $this->type || $value === null || $value instanceof CDbExpression) if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
return $value; return $value;
if ($value === '') }
return $this->type === 'string' ? '' : null; switch ($this->phpType) {
switch ($this->type)
{
case 'string': return (string)$value; case 'string': return (string)$value;
case 'integer': return (integer)$value; case 'integer': return (integer)$value;
case 'boolean': return (boolean)$value; case 'boolean': return (boolean)$value;
case 'double':
default: return $value;
} }
return $value;
} }
} }

10
framework/db/dao/QueryBuilder.php

@ -151,7 +151,7 @@ class QueryBuilder extends \yii\base\Component
public function dropColumn($table, $column) public function dropColumn($table, $column)
{ {
return "ALTER TABLE " . $this->quoteTableName($table) return "ALTER TABLE " . $this->quoteTableName($table)
. " DROP COLUMN " . $this->quoteColumnName($column); . " DROP COLUMN " . $this->quoteSimpleColumnName($column);
} }
/** /**
@ -165,8 +165,8 @@ class QueryBuilder extends \yii\base\Component
public function renameColumn($table, $name, $newName) public function renameColumn($table, $name, $newName)
{ {
return "ALTER TABLE " . $this->quoteTableName($table) return "ALTER TABLE " . $this->quoteTableName($table)
. " RENAME COLUMN " . $this->quoteColumnName($name) . " RENAME COLUMN " . $this->quoteSimpleColumnName($name)
. " TO " . $this->quoteColumnName($newName); . " TO " . $this->quoteSimpleColumnName($newName);
} }
/** /**
@ -181,8 +181,8 @@ class QueryBuilder extends \yii\base\Component
public function alterColumn($table, $column, $type) public function alterColumn($table, $column, $type)
{ {
return 'ALTER TABLE ' . $this->quoteTableName($table) . ' CHANGE ' return 'ALTER TABLE ' . $this->quoteTableName($table) . ' CHANGE '
. $this->quoteColumnName($column) . ' ' . $this->quoteSimpleColumnName($column) . ' '
. $this->quoteColumnName($column) . ' ' . $this->quoteSimpleColumnName($column) . ' '
. $this->getColumnType($type); . $this->getColumnType($type);
} }

103
framework/db/dao/Schema.php

@ -18,18 +18,18 @@ namespace yii\db\dao;
*/ */
abstract class Schema extends \yii\base\Component abstract class Schema extends \yii\base\Component
{ {
public $connection;
private $_tableNames = array(); private $_tableNames = array();
private $_tables = array(); private $_tables = array();
private $_connection;
private $_builder; private $_builder;
private $_cacheExclude = array();
/** /**
* 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 CDbTableSchema driver dependent table metadata, null if the table does not exist. * @return TableSchema driver dependent table metadata, null if the table does not exist.
*/ */
abstract protected function loadTable($name); abstract protected function loadTableSchema($name);
/** /**
* Constructor. * Constructor.
@ -37,17 +37,7 @@ abstract class Schema extends \yii\base\Component
*/ */
public function __construct($connection) public function __construct($connection)
{ {
$this->_connection = $connection; $this->connection = $connection;
foreach ($connection->schemaCachingExclude as $name)
$this->_cacheExclude[$name] = true;
}
/**
* @return Connection database connection. The connection is active.
*/
public function getConnection()
{
return $this->_connection;
} }
/** /**
@ -62,13 +52,13 @@ abstract class Schema extends \yii\base\Component
} }
if (strpos($name, '{{') !== false) { if (strpos($name, '{{') !== false) {
$realName = preg_replace('/\{\{(.*?)\}\}/', $this->_connection->tablePrefix . '$1', $name); $realName = preg_replace('/\{\{(.*?)\}\}/', $this->connection->tablePrefix . '$1', $name);
} }
else { else {
$realName = $name; $realName = $name;
} }
$db = $this->_connection; $db = $this->connection;
// temporarily disable query caching // temporarily disable query caching
if ($db->queryCachingDuration >= 0) { if ($db->queryCachingDuration >= 0) {
@ -77,7 +67,7 @@ abstract class Schema extends \yii\base\Component
} }
if (!in_array($name, $db->schemaCachingExclude) && $db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) { if (!in_array($name, $db->schemaCachingExclude) && $db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) {
$key = __CLASS__ . ":{$db->dsn}/{$db->username}/{$name}"; $key = __CLASS__ . "/{$db->dsn}/{$db->username}/{$name}";
if (($table = $cache->get($key)) === false) { if (($table = $cache->get($key)) === false) {
$table = $this->loadTableSchema($realName); $table = $this->loadTableSchema($realName);
if ($table !== null) { if ($table !== null) {
@ -102,15 +92,14 @@ abstract class Schema extends \yii\base\Component
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
* @return array the metadata for all tables in the database. * @return array the metadata for all tables in the database.
* Each array element is an instance of {@link CDbTableSchema} (or its child class). * Each array element is an instance of {@link CDbTableSchema} (or its child class).
* The array keys are table names.
*/ */
public function getTables($schema = '') public function getTableSchemas($schema = '')
{ {
$tables = array(); $tables = array();
foreach ($this->getTableNames($schema) as $name) foreach ($this->getTableNames($schema) as $name) {
{ if (($table = $this->getTableSchema($name)) !== null) {
if (($table = $this->getTable($name)) !== null) $tables[] = $table;
$tables[$name] = $table; }
} }
return $tables; return $tables;
} }
@ -123,20 +112,21 @@ abstract class Schema extends \yii\base\Component
*/ */
public function getTableNames($schema = '') public function getTableNames($schema = '')
{ {
if (!isset($this->_tableNames[$schema])) if (!isset($this->_tableNames[$schema])) {
$this->_tableNames[$schema] = $this->findTableNames($schema); $this->_tableNames[$schema] = $this->findTableNames($schema);
}
return $this->_tableNames[$schema]; return $this->_tableNames[$schema];
} }
/** /**
* @return CDbCommandBuilder the SQL command builder for this connection. * @return QueryBuilder the query builder for this connection.
*/ */
public function getCommandBuilder() public function getQueryBuilder()
{ {
if ($this->_builder !== null) if ($this->_builder === null) {
$this->_builder = $this->createQueryBuilder();
}
return $this->_builder; return $this->_builder;
else
return $this->_builder = $this->createCommandBuilder();
} }
/** /**
@ -146,7 +136,7 @@ abstract class Schema extends \yii\base\Component
*/ */
public function refresh() public function refresh()
{ {
$db = $this->_connection; $db = $this->connection;
if ($db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) { if ($db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) {
foreach ($this->_tables as $name => $table) { foreach ($this->_tables as $name => $table) {
$key = __CLASS__ . ":{$db->dsn}/{$db->username}/{$name}"; $key = __CLASS__ . ":{$db->dsn}/{$db->username}/{$name}";
@ -155,7 +145,6 @@ abstract class Schema extends \yii\base\Component
} }
$this->_tables = array(); $this->_tables = array();
$this->_tableNames = array(); $this->_tableNames = array();
$this->_builder = null;
} }
/** /**
@ -167,11 +156,13 @@ abstract class Schema extends \yii\base\Component
*/ */
public function quoteTableName($name) public function quoteTableName($name)
{ {
if (strpos($name, '.') === false) if (strpos($name, '.') === false) {
return $this->quoteSimpleTableName($name); return $this->quoteSimpleTableName($name);
}
$parts = explode('.', $name); $parts = explode('.', $name);
foreach ($parts as $i => $part) foreach ($parts as $i => $part) {
$parts[$i] = $this->quoteSimpleTableName($part); $parts[$i] = $this->quoteSimpleTableName($part);
}
return implode('.', $parts); return implode('.', $parts);
} }
@ -184,7 +175,7 @@ abstract class Schema extends \yii\base\Component
*/ */
public function quoteSimpleTableName($name) public function quoteSimpleTableName($name)
{ {
return "'" . $name . "'"; return strpos($name, "'") !== false ? $name : "'" . $name . "'";
} }
/** /**
@ -196,14 +187,13 @@ abstract class Schema extends \yii\base\Component
*/ */
public function quoteColumnName($name) public function quoteColumnName($name)
{ {
if (($pos = strrpos($name, '.')) !== false) if (($pos = strrpos($name, '.')) !== false) {
{
$prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.'; $prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.';
$name = substr($name, $pos + 1); $name = substr($name, $pos + 1);
} }
else else
$prefix = ''; $prefix = '';
return $prefix . ($name === '*' ? $name : $this->quoteSimpleColumnName($name)); return $prefix . $this->quoteSimpleColumnName($name);
} }
/** /**
@ -211,45 +201,18 @@ abstract class Schema extends \yii\base\Component
* A simple column name does not contain prefix. * A simple column name does not contain prefix.
* @param string $name column name * @param string $name column name
* @return string the properly quoted column name * @return string the properly quoted column name
* @since 1.1.6
*/ */
public function quoteSimpleColumnName($name) public function quoteSimpleColumnName($name)
{ {
return '"' . $name . '"'; return strpos($name, '"') !== false || $name === '*' ? $name : '"' . $name . '"';
}
/**
* Compares two table names.
* The table names can be either quoted or unquoted. This method
* will consider both cases.
* @param string $name1 table name 1
* @param string $name2 table name 2
* @return boolean whether the two table names refer to the same table.
*/
public function compareTableNames($name1, $name2)
{
$name1 = str_replace(array('"', '`', "'"), '', $name1);
$name2 = str_replace(array('"', '`', "'"), '', $name2);
if (($pos = strrpos($name1, '.')) !== false)
$name1 = substr($name1, $pos + 1);
if (($pos = strrpos($name2, '.')) !== false)
$name2 = substr($name2, $pos + 1);
if ($this->_connection->tablePrefix !== null)
{
if (strpos($name1, '{') !== false)
$name1 = $this->_connection->tablePrefix . str_replace(array('{', '}'), '', $name1);
if (strpos($name2, '{') !== false)
$name2 = $this->_connection->tablePrefix . str_replace(array('{', '}'), '', $name2);
}
return $name1 === $name2;
} }
/** /**
* Creates a command builder for the database. * Creates a query builder for the database.
* This method may be overridden by child classes to create a DBMS-specific command builder. * This method may be overridden by child classes to create a DBMS-specific query builder.
* @return CDbCommandBuilder command builder instance * @return QueryBuilder query builder instance
*/ */
protected function createQueryBuilder() public function createQueryBuilder()
{ {
return new QueryBuilder($this); return new QueryBuilder($this);
} }

12
framework/db/dao/TableSchema.php

@ -39,7 +39,7 @@ class TableSchema extends \yii\base\Component
*/ */
public $name; public $name;
/** /**
* @var string quoted name of this table. This may include [[schemaName]] if it is not empty. * @var string quoted name of this table. This will include [[schemaName]] if it is not empty.
*/ */
public $quotedName; public $quotedName;
/** /**
@ -51,7 +51,15 @@ class TableSchema extends \yii\base\Component
*/ */
public $sequenceName; public $sequenceName;
/** /**
* @var array foreign keys of this table. The array is indexed by column name. Each value is an array of foreign table name and foreign column name. * @var array foreign keys of this table. Each array element is of the following structure:
*
* ~~~
* array(
* 'ForeignTableName',
* 'fk1' => 'pk1', // pk1 is in foreign table
* 'fk2' => 'pk2', // if composite foreign key
* )
* ~~~
*/ */
public $foreignKeys = array(); public $foreignKeys = array();
/** /**

103
framework/db/dao/mysql/ColumnSchema.php

@ -8,6 +8,8 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\db\dao\mysql;
/** /**
* ColumnSchema class describes the column meta data of a MySQL table. * ColumnSchema class describes the column meta data of a MySQL table.
* *
@ -20,18 +22,76 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
* Extracts the PHP type from DB type. * Extracts the PHP type from DB type.
* @param string $dbType DB type * @param string $dbType DB type
*/ */
protected function extractType($dbType) public function initTypes($dbType)
{ {
if (strncmp($dbType, 'enum', 4) === 0) static $typeMap = array( // dbType => type
'tinyint' => 'smallint',
'bit' => 'smallint',
'smallint' => 'smallint',
'mediumint' => 'integer',
'int' => 'integer',
'integer' => 'integer',
'bigint' => 'bigint',
'float' => 'float',
'double' => 'float',
'real' => 'float',
'decimal' => 'decimal',
'numeric' => 'decimal',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'text' => 'text',
'varchar' => 'string',
'string' => 'string',
'char' => 'string',
'datetime' => 'datetime',
'year' => 'date',
'date' => 'date',
'time' => 'time',
'timestamp' => 'timestamp',
'enum' => 'enum',
);
$this->dbType = $dbType;
$this->type = 'string'; $this->type = 'string';
elseif (strpos($dbType, 'float') !== false || strpos($dbType, 'double') !== false) $this->unsigned = strpos($this->dbType, 'unsigned') !== false;
$this->type = 'double';
elseif (strpos($dbType, 'bool') !== false) if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $this->dbType, $matches)) {
$type = $matches[1];
if (isset($typeMap[$type])) {
$this->type = $typeMap[$type];
}
if (!empty($matches[2])) {
if ($this->type === 'enum') {
$values = explode(',', $matches[2]);
foreach ($values as $i => $value) {
$values[$i] = trim($value, "'");
}
$this->enumValues = $values;
}
else {
$values = explode(',', $matches[2]);
$this->size = $this->precision = (int)$values[0];
if (isset($values[1])) {
$this->scale = (int)$values[1];
}
if ($this->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
$this->type = 'boolean'; $this->type = 'boolean';
elseif (strpos($dbType, 'int') === 0 && strpos($dbType, 'unsigned') === false || preg_match('/(bit|tinyint|smallint|mediumint)/', $dbType)) }
elseif ($type === 'bit') {
if ($this->size > 32) {
$this->type = 'bigint';
}
elseif ($this->size === 32) {
$this->type = 'integer'; $this->type = 'integer';
else }
$this->type = 'string'; }
}
}
}
$this->phpType = $this->getPhpType();
} }
/** /**
@ -39,32 +99,13 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
* The value is typecasted to correct PHP type. * The value is typecasted to correct PHP type.
* @param mixed $defaultValue the default value obtained from metadata * @param mixed $defaultValue the default value obtained from metadata
*/ */
protected function extractDefault($defaultValue) public function initDefaultValue($defaultValue)
{ {
if ($this->dbType === 'timestamp' && $defaultValue === 'CURRENT_TIMESTAMP') if ($this->type === 'timestamp' && $defaultValue === 'CURRENT_TIMESTAMP') {
$this->defaultValue = null; $this->defaultValue = null;
else
parent::extractDefault($defaultValue);
}
/**
* Extracts size, precision and scale information from column's DB type.
* @param string $dbType the column's DB type
*/
protected function extractLimit($dbType)
{
if (strncmp($dbType, 'enum', 4) === 0 && preg_match('/\((.*)\)/', $dbType, $matches))
{
$values = explode(',', $matches[1]);
$size = 0;
foreach ($values as $value)
{
if (($n = strlen($value)) > $size)
$size = $n;
} }
$this->size = $this->precision = $size-2; else {
$this->defaultValue = $this->typecast($defaultValue);
} }
else
parent::extractLimit($dbType);
} }
} }

93
framework/db/dao/mysql/QueryBuilder.php

@ -0,0 +1,93 @@
<?php
/**
* QueryBuilder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\dao\mysql;
/**
* QueryBuilder builds a SQL statement based on the specification given as a [[Query]] object.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class QueryBuilder extends \yii\db\dao\QueryBuilder
{
/**
* @var array the abstract column types mapped to physical column types.
*/
public $columnTypes = array(
'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
'string' => 'varchar(255)',
'text' => 'text',
'smallint' => 'smallint',
'integer' => 'int(11)',
'bigint'=> 'bigint',
'boolean' => 'tinyint(1)',
'float' => 'float',
'decimal' => 'decimal',
'money' => 'decimal(19,4)',
'datetime' => 'datetime',
'timestamp' => 'timestamp',
'time' => 'time',
'date' => 'date',
'binary' => 'blob',
);
/**
* Builds a SQL statement for renaming a column.
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
* @param string $name the old name of the column. The name will be properly quoted by the method.
* @param string $newName the new name of the column. The name will be properly quoted by the method.
* @return string the SQL statement for renaming a DB column.
* @since 1.1.6
*/
public function renameColumn($table, $name, $newName)
{
$db = $this->getDbConnection();
$row = $db->createCommand('SHOW CREATE TABLE ' . $db->quoteTableName($table))->queryRow();
if ($row === false)
throw new CDbException(Yii::t('yii', 'Unable to find "{column}" in table "{table}".', array('{column}' => $name, '{table}' => $table)));
if (isset($row['Create Table']))
$sql = $row['Create Table'];
else
{
$row = array_values($row);
$sql = $row[1];
}
if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches))
{
foreach ($matches[1] as $i => $c)
{
if ($c === $name)
{
return "ALTER TABLE " . $db->quoteTableName($table)
. " CHANGE " . $db->quoteColumnName($name)
. ' ' . $db->quoteColumnName($newName) . ' ' . $matches[2][$i];
}
}
}
// try to give back a SQL anyway
return "ALTER TABLE " . $db->quoteTableName($table)
. " CHANGE " . $db->quoteColumnName($name) . ' ' . $newName;
}
/**
* Builds a SQL statement for dropping a foreign key constraint.
* @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
* @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
* @return string the SQL statement for dropping a foreign key constraint.
* @since 1.1.6
*/
public function dropForeignKey($name, $table)
{
return 'ALTER TABLE ' . $this->quoteTableName($table)
. ' DROP FOREIGN KEY ' . $this->quoteColumnName($name);
}
}

276
framework/db/dao/mysql/Schema.php

@ -1,53 +1,32 @@
<?php <?php
/** /**
* CMysqlSchema class file. * Schema class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\db\dao\mysql;
/** /**
* CMysqlSchema is the class for retrieving metadata information from a MySQL database (version 4.1.x and 5.x). * Schema is the class for retrieving metadata information 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>
* @version $Id: CMysqlSchema.php 3304 2011-06-23 14:53:50Z qiang.xue $ * @since 2.0
* @package system.db.schema.mysql
* @since 1.0
*/ */
class CMysqlSchema extends CDbSchema class Schema extends \yii\db\dao\Schema
{ {
/** /**
* @var array the abstract column types mapped to physical column types.
* @since 1.1.6
*/
public $columnTypes = array(
'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
'string' => 'varchar(255)',
'text' => 'text',
'integer' => 'int(11)',
'float' => 'float',
'decimal' => 'decimal',
'datetime' => 'datetime',
'timestamp' => 'timestamp',
'time' => 'time',
'date' => 'date',
'binary' => 'blob',
'boolean' => 'tinyint(1)',
'money' => 'decimal(19,4)',
);
/**
* Quotes a table name for use in a query. * Quotes a table name for use in a query.
* A simple table name does not schema prefix. * A simple table name does not schema prefix.
* @param string $name table name * @param string $name table name
* @return string the properly quoted table name * @return string the properly quoted table name
* @since 1.1.6
*/ */
public function quoteSimpleTableName($name) public function quoteSimpleTableName($name)
{ {
return '`' . $name . '`'; return strpos($name, "`") !== false ? $name : "`" . $name . "`";
} }
/** /**
@ -55,75 +34,26 @@ class CMysqlSchema extends CDbSchema
* A simple column name does not contain prefix. * A simple column name does not contain prefix.
* @param string $name column name * @param string $name column name
* @return string the properly quoted column name * @return string the properly quoted column name
* @since 1.1.6
*/ */
public function quoteSimpleColumnName($name) public function quoteSimpleColumnName($name)
{ {
return '`' . $name . '`'; return strpos($name, '`') !== false || $name === '*' ? $name : '`' . $name . '`';
}
/**
* Compares two table names.
* The table names can be either quoted or unquoted. This method
* will consider both cases.
* @param string $name1 table name 1
* @param string $name2 table name 2
* @return boolean whether the two table names refer to the same table.
*/
public function compareTableNames($name1, $name2)
{
return parent::compareTableNames(strtolower($name1), strtolower($name2));
}
/**
* Resets the sequence value of a table's primary key.
* The sequence will be reset such that the primary key of the next new row inserted
* will have the specified value or 1.
* @param CDbTableSchema $table the table schema whose primary key sequence will be reset
* @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
* the next new row's primary key will have a value 1.
* @since 1.1
*/
public function resetSequence($table, $value = null)
{
if ($table->sequenceName !== null)
{
if ($value === null)
$value = $this->getDbConnection()->createCommand("SELECT MAX(` {$table->primaryKey}`) FROM {$table->rawName}")->queryScalar() + 1;
else
$value = (int)$value;
$this->getDbConnection()->createCommand("ALTER TABLE {$table->rawName} AUTO_INCREMENT=$value")->execute();
}
}
/**
* Enables or disables integrity check.
* @param boolean $check whether to turn on or off the integrity check.
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
* @since 1.1
*/
public function checkIntegrity($check = true, $schema = '')
{
$this->getDbConnection()->createCommand('SET FOREIGN_KEY_CHECKS=' . ($check ? 1 : 0))->execute();
} }
/** /**
* 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 CMysqlTableSchema driver dependent table metadata. Null if the table does not exist. * @return TableSchema driver dependent table metadata. Null if the table does not exist.
*/ */
protected function loadTable($name) protected function loadTableSchema($name)
{ {
$table = new CMysqlTableSchema; $table = new TableSchema;
$this->resolveTableNames($table, $name); $this->resolveTableNames($table, $name);
if ($this->findColumns($table)) if ($this->findColumns($table)) {
{
$this->findConstraints($table); $this->findConstraints($table);
return $table; return $table;
} }
else
return null;
} }
/** /**
@ -134,52 +64,15 @@ class CMysqlSchema extends CDbSchema
protected function resolveTableNames($table, $name) protected function resolveTableNames($table, $name)
{ {
$parts = explode('.', str_replace('`', '', $name)); $parts = explode('.', str_replace('`', '', $name));
if (isset($parts[1])) if (isset($parts[1])) {
{
$table->schemaName = $parts[0]; $table->schemaName = $parts[0];
$table->name = $parts[1]; $table->name = $parts[1];
$table->rawName = $this->quoteTableName($table->schemaName) . '.' . $this->quoteTableName($table->name); $table->quotedName = $this->quoteSimpleTableName($table->schemaName) . '.' . $this->quoteSimpleTableName($table->name);
} }
else else {
{
$table->name = $parts[0]; $table->name = $parts[0];
$table->rawName = $this->quoteTableName($table->name); $table->quotedName = $this->quoteSimpleTableName($table->name);
}
}
/**
* Collects the table column metadata.
* @param CMysqlTableSchema $table the table metadata
* @return boolean whether the table exists in the database
*/
protected function findColumns($table)
{
$sql = 'SHOW COLUMNS FROM ' . $table->rawName;
try
{
$columns = $this->getDbConnection()->createCommand($sql)->queryAll();
}
catch(Exception $e)
{
return false;
}
foreach ($columns as $column)
{
$c = $this->createColumn($column);
$table->columns[$c->name] = $c;
if ($c->isPrimaryKey)
{
if ($table->primaryKey === null)
$table->primaryKey = $c->name;
elseif (is_string($table->primaryKey))
$table->primaryKey = array($table->primaryKey, $c->name);
else
$table->primaryKey[] = $c->name;
if ($c->autoIncrement)
$table->sequenceName = '';
}
} }
return true;
} }
/** /**
@ -189,27 +82,51 @@ class CMysqlSchema extends CDbSchema
*/ */
protected function createColumn($column) protected function createColumn($column)
{ {
$c = new CMysqlColumnSchema; $c = new ColumnSchema;
$c->name = $column['Field']; $c->name = $column['Field'];
$c->rawName = $this->quoteColumnName($c->name); $c->quotedName = $this->quoteSimpleColumnName($c->name);
$c->allowNull = $column['Null'] === 'YES'; $c->allowNull = $column['Null'] === 'YES';
$c->isPrimaryKey = strpos($column['Key'], 'PRI') !== false; $c->isPrimaryKey = strpos($column['Key'], 'PRI') !== false;
$c->isForeignKey = false; $c->autoIncrement = stripos($column['Extra'], 'auto_increment') !== false;
$c->init($column['Type'], $column['Default']); $c->initTypes($column['Type']);
$c->autoIncrement = strpos(strtolower($column['Extra']), 'auto_increment') !== false; $c->initDefaultValue($column['Default']);
return $c; return $c;
} }
/** /**
* @return float server version. * Collects the table column metadata.
* @param CMysqlTableSchema $table the table metadata
* @return boolean whether the table exists in the database
*/ */
protected function getServerVersion() protected function findColumns($table)
{ {
$version = $this->getDbConnection()->getAttribute(PDO::ATTR_SERVER_VERSION); $sql = 'SHOW COLUMNS FROM ' . $table->quotedName;
$digits = array(); try {
preg_match('/(\d+)\.(\d+)\.(\d+)/', $version, $digits); $columns = $this->connection->createCommand($sql)->queryAll();
return floatval($digits[1] . '.' . $digits[2] . $digits[3]); }
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;
}
elseif (is_string($table->primaryKey)) {
$table->primaryKey = array($table->primaryKey, $c->name);
}
else {
$table->primaryKey[] = $c->name;
}
if ($c->autoIncrement) {
$table->sequenceName = '';
}
}
}
return true;
} }
/** /**
@ -218,23 +135,21 @@ class CMysqlSchema extends CDbSchema
*/ */
protected function findConstraints($table) protected function findConstraints($table)
{ {
$row = $this->getDbConnection()->createCommand('SHOW CREATE TABLE ' . $table->rawName)->queryRow(); $row = $this->connection->createCommand('SHOW CREATE TABLE ' . $table->quotedName)->queryRow();
$matches = array(); $matches = array();
$regexp = '/FOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)/mi'; $regexp = '/FOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)/mi';
foreach ($row as $sql) 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) {
break; $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];
} }
foreach ($matches as $match) $table->foreignKeys[] = $constraint;
{ }
$keys = array_map('trim', explode(',', str_replace('`', '', $match[1]))); break;
$fks = array_map('trim', explode(',', str_replace('`', '', $match[3])));
foreach ($keys as $k => $name)
{
$table->foreignKeys[$name] = array(str_replace('`', '', $match[2]), $fks[$k]);
if (isset($table->columns[$name]))
$table->columns[$name]->isForeignKey = true;
} }
} }
} }
@ -244,67 +159,16 @@ class CMysqlSchema extends CDbSchema
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. * @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. * If not empty, the returned table names will be prefixed with the schema name.
* @return array all table names in the database. * @return array all table names in the database.
* @since 1.0.2
*/ */
protected function findTableNames($schema = '') protected function findTableNames($schema = '')
{ {
if ($schema === '') if ($schema === '') {
return $this->getDbConnection()->createCommand('SHOW TABLES')->queryColumn(); return $this->connection->createCommand('SHOW TABLES')->queryColumn();
$names = $this->getDbConnection()->createCommand('SHOW TABLES FROM ' . $this->quoteTableName($schema))->queryColumn();
foreach ($names as &$name)
$name = $schema . '.' . $name;
return $names;
}
/**
* Builds a SQL statement for renaming a column.
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
* @param string $name the old name of the column. The name will be properly quoted by the method.
* @param string $newName the new name of the column. The name will be properly quoted by the method.
* @return string the SQL statement for renaming a DB column.
* @since 1.1.6
*/
public function renameColumn($table, $name, $newName)
{
$db = $this->getDbConnection();
$row = $db->createCommand('SHOW CREATE TABLE ' . $db->quoteTableName($table))->queryRow();
if ($row === false)
throw new CDbException(Yii::t('yii', 'Unable to find "{column}" in table "{table}".', array('{column}' => $name, '{table}' => $table)));
if (isset($row['Create Table']))
$sql = $row['Create Table'];
else
{
$row = array_values($row);
$sql = $row[1];
} }
if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) $names = $this->connection->createCommand('SHOW TABLES FROM ' . $this->quoteSimpleTableName($schema))->queryColumn();
{ foreach ($names as &$name) {
foreach ($matches[1] as $i => $c) $name = $schema . '.' . $name;
{
if ($c === $name)
{
return "ALTER TABLE " . $db->quoteTableName($table)
. " CHANGE " . $db->quoteColumnName($name)
. ' ' . $db->quoteColumnName($newName) . ' ' . $matches[2][$i];
}
}
}
// try to give back a SQL anyway
return "ALTER TABLE " . $db->quoteTableName($table)
. " CHANGE " . $db->quoteColumnName($name) . ' ' . $newName;
} }
return $names;
/**
* Builds a SQL statement for dropping a foreign key constraint.
* @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
* @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
* @return string the SQL statement for dropping a foreign key constraint.
* @since 1.1.6
*/
public function dropForeignKey($name, $table)
{
return 'ALTER TABLE ' . $this->quoteTableName($table)
. ' DROP FOREIGN KEY ' . $this->quoteColumnName($name);
} }
} }

24
framework/db/dao/mysql/TableSchema.php

@ -1,24 +0,0 @@
<?php
/**
* TableSchema class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* TableSchema represents the metadata for a MySQL table.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class TableSchema extends CDbTableSchema
{
/**
* @var string name of the schema (database) that this table belongs to.
* Defaults to null, meaning no schema (or the current database).
*/
public $schemaName;
}
Loading…
Cancel
Save