Browse Source

...

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
3301b9d6b2
  1. 31
      framework/db/dao/ColumnSchema.php
  2. 19
      framework/db/dao/Driver.php
  3. 10
      framework/db/dao/TableSchema.php
  4. 2
      framework/db/dao/mysql/ColumnSchema.php
  5. 100
      framework/db/dao/mysql/Driver.php
  6. 28
      framework/db/dao/mysql/QueryBuilder.php

31
framework/db/dao/ColumnSchema.php

@ -19,24 +19,6 @@ namespace yii\db\dao;
class ColumnSchema extends \yii\base\Component 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). * @var string name of this column (without quotes).
*/ */
public $name; public $name;
@ -99,9 +81,8 @@ class ColumnSchema extends \yii\base\Component
/** /**
* Extracts the PHP type from DB type. * 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 static $typeMap = array( // logical type => php type
'smallint' => 'integer', 'smallint' => 'integer',
@ -112,13 +93,15 @@ class ColumnSchema extends \yii\base\Component
); );
if (isset($typeMap[$this->type])) { if (isset($typeMap[$this->type])) {
if ($this->type === 'bigint') { 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') { } 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';
} }
/** /**

19
framework/db/dao/Driver.php

@ -27,6 +27,24 @@ use yii\db\Exception;
abstract class Driver extends \yii\base\Object 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 * @var Connection the database connection
*/ */
public $connection; public $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.'); throw new Exception(get_class($this) . 'does not support fetching all table names.');
} }
} }

10
framework/db/dao/TableSchema.php

@ -11,9 +11,7 @@
namespace yii\db\dao; namespace yii\db\dao;
/** /**
* TableSchema is the base class for representing the metadata of a database table. * TableSchema represents the meta data of a database table.
*
* It may be extended by different DBMS driver to represent DBMS-specific table metadata.
* *
* @property array $columnNames list of column names * @property array $columnNames list of column names
* *
@ -23,6 +21,12 @@ namespace yii\db\dao;
class TableSchema extends \yii\base\Object 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. * @var string name of the schema that this table belongs to.
*/ */
public $schemaName; public $schemaName;

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

@ -11,7 +11,7 @@
namespace yii\db\dao\mysql; 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> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0

100
framework/db/dao/mysql/Driver.php

@ -11,6 +11,7 @@
namespace yii\db\dao\mysql; namespace yii\db\dao\mysql;
use yii\db\dao\TableSchema; 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). * Driver is the class for retrieving meta data from a MySQL database (version 4.1.x and 5.x).
@ -21,6 +22,37 @@ use yii\db\dao\TableSchema;
class Driver extends \yii\db\dao\Driver 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. * Quotes a table name for use in a query.
* A simple table name has no schema prefix. * A simple table name has no schema prefix.
* @param string $name table name * @param string $name table name
@ -79,7 +111,7 @@ class Driver extends \yii\db\dao\Driver
/** /**
* Creates a table column. * Creates a table column.
* @param array $column column metadata * @param array $column column metadata
* @return CDbColumnSchema normalized column metadata * @return ColumnSchema normalized column metadata
*/ */
protected function createColumn($column) protected function createColumn($column)
{ {
@ -90,13 +122,70 @@ class Driver extends \yii\db\dao\Driver
$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->autoIncrement = stripos($column['Extra'], 'auto_increment') !== 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; 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. * Collects the table column metadata.
* @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
@ -107,7 +196,7 @@ class Driver extends \yii\db\dao\Driver
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) {
@ -164,7 +253,8 @@ class Driver extends \yii\db\dao\Driver
if ($schema === '') { if ($schema === '') {
return $this->connection->createCommand('SHOW TABLES')->queryColumn(); 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) { foreach ($names as &$name) {
$name = $schema . '.' . $name; $name = $schema . '.' . $name;
} }

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

@ -23,20 +23,20 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/ */
public $typeMap = array( public $typeMap = array(
'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', 'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
ColumnSchema::TYPE_STRING => 'varchar(255)', Driver::TYPE_STRING => 'varchar(255)',
ColumnSchema::TYPE_TEXT => 'text', Driver::TYPE_TEXT => 'text',
ColumnSchema::TYPE_SMALLINT => 'smallint(6)', Driver::TYPE_SMALLINT => 'smallint(6)',
ColumnSchema::TYPE_INTEGER => 'int(11)', Driver::TYPE_INTEGER => 'int(11)',
ColumnSchema::TYPE_BIGINT => 'bigint(20)', Driver::TYPE_BIGINT => 'bigint(20)',
ColumnSchema::TYPE_FLOAT => 'float', Driver::TYPE_FLOAT => 'float',
ColumnSchema::TYPE_DECIMAL => 'decimal', Driver::TYPE_DECIMAL => 'decimal',
ColumnSchema::TYPE_DATETIME => 'datetime', Driver::TYPE_DATETIME => 'datetime',
ColumnSchema::TYPE_TIMESTAMP => 'timestamp', Driver::TYPE_TIMESTAMP => 'timestamp',
ColumnSchema::TYPE_TIME => 'time', Driver::TYPE_TIME => 'time',
ColumnSchema::TYPE_DATE => 'date', Driver::TYPE_DATE => 'date',
ColumnSchema::TYPE_BINARY => 'blob', Driver::TYPE_BINARY => 'blob',
ColumnSchema::TYPE_BOOLEAN => 'tinyint(1)', Driver::TYPE_BOOLEAN => 'tinyint(1)',
ColumnSchema::TYPE_MONEY => 'decimal(19,4)', Driver::TYPE_MONEY => 'decimal(19,4)',
); );
/** /**

Loading…
Cancel
Save