Browse Source

...

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
a1a69c5e0e
  1. 18
      framework/db/dao/ColumnSchema.php
  2. 22
      framework/db/dao/QueryBuilder.php
  3. 40
      framework/db/dao/Schema.php
  4. 56
      framework/db/dao/mysql/ColumnSchema.php
  5. 30
      framework/db/dao/mysql/QueryBuilder.php

18
framework/db/dao/ColumnSchema.php

@ -19,6 +19,24 @@ namespace yii\db\dao;
class ColumnSchema extends \yii\base\Component
{
/**
* The followings are 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).
*/
public $name;

22
framework/db/dao/QueryBuilder.php

@ -15,6 +15,9 @@ use yii\db\Exception;
/**
* QueryBuilder builds a SQL statement based on the specification given as a [[Query]] object.
*
* QueryBuilder is often used behind the scenes by [[Query]] to build a DBMS-dependent SQL statement
* from a [[Query]] object.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
@ -22,7 +25,8 @@ class QueryBuilder extends \yii\base\Object
{
/**
* @var array the abstract column types mapped to physical column types.
* Child classes should override this property to declare possible type mappings.
* This is mainly used to support creating/modifying tables using DB-independent data type specifications.
* Child classes should override this property to declare supported type mappings.
*/
public $typeMap = array();
/**
@ -45,12 +49,12 @@ class QueryBuilder extends \yii\base\Object
/**
* Constructor.
* @param Schema $schema the database schema information
* @param Connection $connection the database connection.
*/
public function __construct($schema)
public function __construct($connection)
{
$this->connection = $schema->connection;
$this->schema = $schema;
$this->connection = $connection;
$this->schema = $connection->getSchema();
}
/**
@ -182,7 +186,7 @@ class QueryBuilder extends \yii\base\Object
* The columns in the new table should be specified as name-definition pairs (e.g. 'name'=>'string'),
* where name stands for a column name which will be properly quoted by the method, and definition
* stands for the column type which can contain an abstract DB type.
* The {@link getColumnType} method will be invoked to convert any abstract type into a physical one.
* The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one.
*
* If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
* inserted into the generated SQL.
@ -242,7 +246,7 @@ class QueryBuilder extends \yii\base\Object
* Builds a SQL statement for adding a new DB column.
* @param string $table the table that the new column will be added to. The table name will be properly quoted by the method.
* @param string $column the name of the new column. The name will be properly quoted by the method.
* @param string $type the column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
* @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any)
* into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
* @return string the SQL statement for adding a new column.
@ -284,7 +288,7 @@ class QueryBuilder extends \yii\base\Object
* Builds a SQL statement for changing the definition of a column.
* @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
* @param string $column the name of the column to be changed. The name will be properly quoted by the method.
* @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
* @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any)
* into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
* @return string the SQL statement for changing the definition of a column.
@ -406,7 +410,7 @@ class QueryBuilder extends \yii\base\Object
/**
* Converts an abstract column type into a physical column type.
* The conversion is done using the type map specified in {@link typeMap}.
* The conversion is done using the type map specified in [[typeMap]].
* These abstract column types are supported (using MySQL as example to explain the corresponding
* physical types):
* <ul>

40
framework/db/dao/Schema.php

@ -26,22 +26,6 @@ use yii\db\Exception;
*/
abstract class Schema extends \yii\base\Object
{
const TYPE_PK = 'pk';
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
*/
@ -148,11 +132,13 @@ abstract class Schema extends \yii\base\Object
* 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.
* @param boolean $refresh whether to fetch the latest available table names. If this is false,
* table names fetched previously (if available) will be returned.
* @return array all table names in the database.
*/
public function getTableNames($schema = '')
public function getTableNames($schema = '', $refresh = false)
{
if (!isset($this->_tableNames[$schema])) {
if (!isset($this->_tableNames[$schema]) || $refresh) {
$this->_tableNames[$schema] = $this->findTableNames($schema);
}
return $this->_tableNames[$schema];
@ -171,19 +157,25 @@ abstract class Schema extends \yii\base\Object
/**
* Refreshes the schema.
* This method resets the loaded table metadata and command builder
* so that they can be recreated to reflect the change of schema.
* This method cleans up the cached table schema and names
* so that they can be recreated to reflect the database schema change.
* @param string $tableName the name of the table that needs to be refreshed.
* If null, all currently loaded tables will be refreshed.
*/
public function refresh()
public function refresh($tableName = null)
{
$db = $this->connection;
if ($db->schemaCachingDuration >= 0 && ($cache = \Yii::$application->getComponent($db->schemaCacheID)) !== null) {
if ($tableName === null) {
foreach ($this->_tables as $name => $table) {
$cache->delete($this->getCacheKey($name));
}
}
$this->_tables = array();
$this->_tableNames = array();
} else {
$cache->delete($this->getCacheKey($tableName));
unset($this->_tables[$tableName]);
}
}
}
/**
@ -253,7 +245,7 @@ abstract class Schema extends \yii\base\Object
*/
public function createQueryBuilder()
{
return new QueryBuilder($this);
return new QueryBuilder($this->connection);
}
/**

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

@ -25,35 +25,35 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
public function initTypes($dbType)
{
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',
'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,
);
$this->dbType = $dbType;
$this->type = 'string';
$this->type = self::TYPE_STRING;
$this->unsigned = strpos($this->dbType, 'unsigned') !== false;
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $this->dbType, $matches)) {
@ -63,7 +63,7 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
}
if (!empty($matches[2])) {
if ($this->type === 'enum') {
if ($type === 'enum') {
$values = explode(',', $matches[2]);
foreach ($values as $i => $value) {
$values[$i] = trim($value, "'");
@ -93,7 +93,7 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
/**
* Extracts the default value for the column.
* The value is typecasted to correct PHP type.
* The value is typecast to correct PHP type.
* @param mixed $defaultValue the default value obtained from metadata
*/
public function initDefaultValue($defaultValue)

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

@ -22,21 +22,21 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
* @var array the abstract column types mapped to physical column types.
*/
public $typeMap = array(
Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
Schema::TYPE_STRING => 'varchar(255)',
Schema::TYPE_TEXT => 'text',
Schema::TYPE_SMALLINT => 'smallint(6)',
Schema::TYPE_INTEGER => 'int(11)',
Schema::TYPE_BIGINT => 'bigint(20)',
Schema::TYPE_FLOAT => 'float',
Schema::TYPE_DECIMAL => 'decimal',
Schema::TYPE_DATETIME => 'datetime',
Schema::TYPE_TIMESTAMP => 'timestamp',
Schema::TYPE_TIME => 'time',
Schema::TYPE_DATE => 'date',
Schema::TYPE_BINARY => 'blob',
Schema::TYPE_BOOLEAN => 'tinyint(1)',
Schema::TYPE_MONEY => 'decimal(19,4)',
'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)',
);
/**

Loading…
Cancel
Save