mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-01 11:39:41 +08:00
...
This commit is contained in:
@ -18,24 +18,6 @@ namespace yii\db\dao;
|
||||
*/
|
||||
class ColumnSchema extends \yii\base\Component
|
||||
{
|
||||
/**
|
||||
* The followings are the supported abstract column data types.
|
||||
*/
|
||||
const TYPE_STRING = 'string';
|
||||
const TYPE_TEXT = 'text';
|
||||
const TYPE_SMALLINT = 'smallint';
|
||||
const TYPE_INTEGER = 'integer';
|
||||
const TYPE_BIGINT = 'bigint';
|
||||
const TYPE_FLOAT = 'float';
|
||||
const TYPE_DECIMAL = 'decimal';
|
||||
const TYPE_DATETIME = 'datetime';
|
||||
const TYPE_TIMESTAMP = 'timestamp';
|
||||
const TYPE_TIME = 'time';
|
||||
const TYPE_DATE = 'date';
|
||||
const TYPE_BINARY = 'binary';
|
||||
const TYPE_BOOLEAN = 'boolean';
|
||||
const TYPE_MONEY = 'money';
|
||||
|
||||
/**
|
||||
* @var string name of this column (without quotes).
|
||||
*/
|
||||
@ -99,9 +81,8 @@ class ColumnSchema extends \yii\base\Component
|
||||
|
||||
/**
|
||||
* Extracts the PHP type from DB type.
|
||||
* @return string PHP type name.
|
||||
*/
|
||||
protected function extractPhpType()
|
||||
public function resolvePhpType()
|
||||
{
|
||||
static $typeMap = array( // logical type => php type
|
||||
'smallint' => 'integer',
|
||||
@ -112,13 +93,15 @@ class ColumnSchema extends \yii\base\Component
|
||||
);
|
||||
if (isset($typeMap[$this->type])) {
|
||||
if ($this->type === 'bigint') {
|
||||
return PHP_INT_SIZE == 8 && !$this->unsigned ? 'integer' : 'string';
|
||||
$this->phpType = PHP_INT_SIZE == 8 && !$this->unsigned ? 'integer' : 'string';
|
||||
} elseif ($this->type === 'integer') {
|
||||
return PHP_INT_SIZE == 4 && $this->unsigned ? 'string' : 'integer';
|
||||
$this->phpType = PHP_INT_SIZE == 4 && $this->unsigned ? 'string' : 'integer';
|
||||
} else {
|
||||
$this->phpType = $typeMap[$this->type];
|
||||
}
|
||||
return $typeMap[$this->type];
|
||||
} else {
|
||||
$this->phpType = 'string';
|
||||
}
|
||||
return 'string';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -26,6 +26,24 @@ use yii\db\Exception;
|
||||
*/
|
||||
abstract class Driver extends \yii\base\Object
|
||||
{
|
||||
/**
|
||||
* The followings are the supported abstract column data types.
|
||||
*/
|
||||
const TYPE_STRING = 'string';
|
||||
const TYPE_TEXT = 'text';
|
||||
const TYPE_SMALLINT = 'smallint';
|
||||
const TYPE_INTEGER = 'integer';
|
||||
const TYPE_BIGINT = 'bigint';
|
||||
const TYPE_FLOAT = 'float';
|
||||
const TYPE_DECIMAL = 'decimal';
|
||||
const TYPE_DATETIME = 'datetime';
|
||||
const TYPE_TIMESTAMP = 'timestamp';
|
||||
const TYPE_TIME = 'time';
|
||||
const TYPE_DATE = 'date';
|
||||
const TYPE_BINARY = 'binary';
|
||||
const TYPE_BOOLEAN = 'boolean';
|
||||
const TYPE_MONEY = 'money';
|
||||
|
||||
/**
|
||||
* @var Connection the database connection
|
||||
*/
|
||||
@ -260,5 +278,4 @@ abstract class Driver extends \yii\base\Object
|
||||
{
|
||||
throw new Exception(get_class($this) . 'does not support fetching all table names.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -11,9 +11,7 @@
|
||||
namespace yii\db\dao;
|
||||
|
||||
/**
|
||||
* TableSchema is the base class for representing the metadata of a database table.
|
||||
*
|
||||
* It may be extended by different DBMS driver to represent DBMS-specific table metadata.
|
||||
* TableSchema represents the meta data of a database table.
|
||||
*
|
||||
* @property array $columnNames list of column names
|
||||
*
|
||||
@ -22,6 +20,12 @@ namespace yii\db\dao;
|
||||
*/
|
||||
class TableSchema extends \yii\base\Object
|
||||
{
|
||||
/**
|
||||
* @var string name of the catalog (database) that this table belongs to.
|
||||
* Defaults to null, meaning no catalog (or the current database).
|
||||
* This property is only meaningful for MSSQL.
|
||||
*/
|
||||
public $catalogName;
|
||||
/**
|
||||
* @var string name of the schema that this table belongs to.
|
||||
*/
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
namespace yii\db\dao\mysql;
|
||||
|
||||
/**
|
||||
* ColumnSchema class describes the column meta data of a MySQL table.
|
||||
* ColumnSchema class describes the meta data of a MySQL table column.
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
namespace yii\db\dao\mysql;
|
||||
|
||||
use yii\db\dao\TableSchema;
|
||||
use yii\db\dao\ColumnSchema;
|
||||
|
||||
/**
|
||||
* Driver is the class for retrieving meta data from a MySQL database (version 4.1.x and 5.x).
|
||||
@ -20,6 +21,37 @@ use yii\db\dao\TableSchema;
|
||||
*/
|
||||
class Driver extends \yii\db\dao\Driver
|
||||
{
|
||||
/**
|
||||
* @var array mapping from physical types (keys) to abstract types (values)
|
||||
*/
|
||||
public $typeMap = array( // dbType => type
|
||||
'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,
|
||||
);
|
||||
|
||||
/**
|
||||
* Quotes a table name for use in a query.
|
||||
* A simple table name has no schema prefix.
|
||||
@ -79,7 +111,7 @@ class Driver extends \yii\db\dao\Driver
|
||||
/**
|
||||
* Creates a table column.
|
||||
* @param array $column column metadata
|
||||
* @return CDbColumnSchema normalized column metadata
|
||||
* @return ColumnSchema normalized column metadata
|
||||
*/
|
||||
protected function createColumn($column)
|
||||
{
|
||||
@ -90,12 +122,69 @@ class Driver extends \yii\db\dao\Driver
|
||||
$c->allowNull = $column['Null'] === 'YES';
|
||||
$c->isPrimaryKey = strpos($column['Key'], 'PRI') !== false;
|
||||
$c->autoIncrement = stripos($column['Extra'], 'auto_increment') !== false;
|
||||
$c->initTypes($column['Type']);
|
||||
$c->initDefaultValue($column['Default']);
|
||||
|
||||
$c->dbType = $column['Type'];
|
||||
$this->resolveColumnType($c);
|
||||
$c->resolvePhpType();
|
||||
|
||||
$this->resolveDefaultValue($c, $column['Default']);
|
||||
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \yii\db\dao\ColumnSchema $column
|
||||
* @param string $value
|
||||
*/
|
||||
protected function resolveDefaultValue($column, $value)
|
||||
{
|
||||
if ($column->type !== 'timestamp' || $value !== 'CURRENT_TIMESTAMP') {
|
||||
$column->defaultValue = $column->typecast($value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the PHP type from DB type.
|
||||
* @param \yii\db\dao\ColumnSchema $column the column
|
||||
*/
|
||||
public function resolveColumnType($column)
|
||||
{
|
||||
$column->type = self::TYPE_STRING;
|
||||
$column->unsigned = strpos($column->dbType, 'unsigned') !== false;
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the table column metadata.
|
||||
* @param \yii\db\dao\TableSchema $table the table metadata
|
||||
@ -107,7 +196,7 @@ class Driver extends \yii\db\dao\Driver
|
||||
try {
|
||||
$columns = $this->connection->createCommand($sql)->queryAll();
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
foreach ($columns as $column) {
|
||||
@ -164,7 +253,8 @@ class Driver extends \yii\db\dao\Driver
|
||||
if ($schema === '') {
|
||||
return $this->connection->createCommand('SHOW TABLES')->queryColumn();
|
||||
}
|
||||
$names = $this->connection->createCommand('SHOW TABLES FROM ' . $this->quoteSimpleTableName($schema))->queryColumn();
|
||||
$sql = 'SHOW TABLES FROM ' . $this->quoteSimpleTableName($schema);
|
||||
$names = $this->connection->createCommand($sql)->queryColumn();
|
||||
foreach ($names as &$name) {
|
||||
$name = $schema . '.' . $name;
|
||||
}
|
||||
|
||||
@ -23,20 +23,20 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
|
||||
*/
|
||||
public $typeMap = array(
|
||||
'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
|
||||
ColumnSchema::TYPE_STRING => 'varchar(255)',
|
||||
ColumnSchema::TYPE_TEXT => 'text',
|
||||
ColumnSchema::TYPE_SMALLINT => 'smallint(6)',
|
||||
ColumnSchema::TYPE_INTEGER => 'int(11)',
|
||||
ColumnSchema::TYPE_BIGINT => 'bigint(20)',
|
||||
ColumnSchema::TYPE_FLOAT => 'float',
|
||||
ColumnSchema::TYPE_DECIMAL => 'decimal',
|
||||
ColumnSchema::TYPE_DATETIME => 'datetime',
|
||||
ColumnSchema::TYPE_TIMESTAMP => 'timestamp',
|
||||
ColumnSchema::TYPE_TIME => 'time',
|
||||
ColumnSchema::TYPE_DATE => 'date',
|
||||
ColumnSchema::TYPE_BINARY => 'blob',
|
||||
ColumnSchema::TYPE_BOOLEAN => 'tinyint(1)',
|
||||
ColumnSchema::TYPE_MONEY => 'decimal(19,4)',
|
||||
Driver::TYPE_STRING => 'varchar(255)',
|
||||
Driver::TYPE_TEXT => 'text',
|
||||
Driver::TYPE_SMALLINT => 'smallint(6)',
|
||||
Driver::TYPE_INTEGER => 'int(11)',
|
||||
Driver::TYPE_BIGINT => 'bigint(20)',
|
||||
Driver::TYPE_FLOAT => 'float',
|
||||
Driver::TYPE_DECIMAL => 'decimal',
|
||||
Driver::TYPE_DATETIME => 'datetime',
|
||||
Driver::TYPE_TIMESTAMP => 'timestamp',
|
||||
Driver::TYPE_TIME => 'time',
|
||||
Driver::TYPE_DATE => 'date',
|
||||
Driver::TYPE_BINARY => 'blob',
|
||||
Driver::TYPE_BOOLEAN => 'tinyint(1)',
|
||||
Driver::TYPE_MONEY => 'decimal(19,4)',
|
||||
);
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user