Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
a1a69c5e
Commit
a1a69c5e
authored
Dec 27, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
cdfcc5d3
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
92 additions
and
78 deletions
+92
-78
ColumnSchema.php
framework/db/dao/ColumnSchema.php
+18
-0
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+13
-9
Schema.php
framework/db/dao/Schema.php
+18
-26
ColumnSchema.php
framework/db/dao/mysql/ColumnSchema.php
+28
-28
QueryBuilder.php
framework/db/dao/mysql/QueryBuilder.php
+15
-15
No files found.
framework/db/dao/ColumnSchema.php
View file @
a1a69c5e
...
@@ -19,6 +19,24 @@ namespace yii\db\dao;
...
@@ -19,6 +19,24 @@ namespace yii\db\dao;
class
ColumnSchema
extends
\yii\base\Component
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).
* @var string name of this column (without quotes).
*/
*/
public
$name
;
public
$name
;
...
...
framework/db/dao/QueryBuilder.php
View file @
a1a69c5e
...
@@ -15,6 +15,9 @@ use yii\db\Exception;
...
@@ -15,6 +15,9 @@ use yii\db\Exception;
/**
/**
* QueryBuilder builds a SQL statement based on the specification given as a [[Query]] object.
* 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>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
...
@@ -22,7 +25,8 @@ class QueryBuilder extends \yii\base\Object
...
@@ -22,7 +25,8 @@ class QueryBuilder extends \yii\base\Object
{
{
/**
/**
* @var array the abstract column types mapped to physical column types.
* @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
();
public
$typeMap
=
array
();
/**
/**
...
@@ -45,12 +49,12 @@ class QueryBuilder extends \yii\base\Object
...
@@ -45,12 +49,12 @@ class QueryBuilder extends \yii\base\Object
/**
/**
* Constructor.
* 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
->
connection
=
$connection
;
$this
->
schema
=
$
schema
;
$this
->
schema
=
$
connection
->
getSchema
()
;
}
}
/**
/**
...
@@ -182,7 +186,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -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'),
* 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
* 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.
* 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
* If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
* inserted into the generated SQL.
* inserted into the generated SQL.
...
@@ -242,7 +246,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -242,7 +246,7 @@ class QueryBuilder extends \yii\base\Object
* Builds a SQL statement for adding a new DB column.
* 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 $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 $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.
* 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'.
* 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.
* @return string the SQL statement for adding a new column.
...
@@ -284,7 +288,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -284,7 +288,7 @@ class QueryBuilder extends \yii\base\Object
* Builds a SQL statement for changing the definition of a column.
* 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 $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 $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.
* 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'.
* 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.
* @return string the SQL statement for changing the definition of a column.
...
@@ -406,7 +410,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -406,7 +410,7 @@ class QueryBuilder extends \yii\base\Object
/**
/**
* Converts an abstract column type into a physical column type.
* 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
* These abstract column types are supported (using MySQL as example to explain the corresponding
* physical types):
* physical types):
* <ul>
* <ul>
...
...
framework/db/dao/Schema.php
View file @
a1a69c5e
...
@@ -26,22 +26,6 @@ use yii\db\Exception;
...
@@ -26,22 +26,6 @@ use yii\db\Exception;
*/
*/
abstract
class
Schema
extends
\yii\base\Object
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
* @var Connection the database connection
*/
*/
...
@@ -148,11 +132,13 @@ abstract class Schema extends \yii\base\Object
...
@@ -148,11 +132,13 @@ abstract class Schema extends \yii\base\Object
* Returns all table names in the database.
* 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.
* @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.
* @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.
* @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
);
$this
->
_tableNames
[
$schema
]
=
$this
->
findTableNames
(
$schema
);
}
}
return
$this
->
_tableNames
[
$schema
];
return
$this
->
_tableNames
[
$schema
];
...
@@ -171,19 +157,25 @@ abstract class Schema extends \yii\base\Object
...
@@ -171,19 +157,25 @@ abstract class Schema extends \yii\base\Object
/**
/**
* Refreshes the schema.
* Refreshes the schema.
* This method resets the loaded table metadata and command builder
* This method cleans up the cached table schema and names
* so that they can be recreated to reflect the change of schema.
* 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
;
$db
=
$this
->
connection
;
if
(
$db
->
schemaCachingDuration
>=
0
&&
(
$cache
=
\Yii
::
$application
->
getComponent
(
$db
->
schemaCacheID
))
!==
null
)
{
if
(
$db
->
schemaCachingDuration
>=
0
&&
(
$cache
=
\Yii
::
$application
->
getComponent
(
$db
->
schemaCacheID
))
!==
null
)
{
foreach
(
$this
->
_tables
as
$name
=>
$table
)
{
if
(
$tableName
===
null
)
{
$cache
->
delete
(
$this
->
getCacheKey
(
$name
));
foreach
(
$this
->
_tables
as
$name
=>
$table
)
{
$cache
->
delete
(
$this
->
getCacheKey
(
$name
));
}
$this
->
_tables
=
array
();
}
else
{
$cache
->
delete
(
$this
->
getCacheKey
(
$tableName
));
unset
(
$this
->
_tables
[
$tableName
]);
}
}
}
}
$this
->
_tables
=
array
();
$this
->
_tableNames
=
array
();
}
}
/**
/**
...
@@ -253,7 +245,7 @@ abstract class Schema extends \yii\base\Object
...
@@ -253,7 +245,7 @@ abstract class Schema extends \yii\base\Object
*/
*/
public
function
createQueryBuilder
()
public
function
createQueryBuilder
()
{
{
return
new
QueryBuilder
(
$this
);
return
new
QueryBuilder
(
$this
->
connection
);
}
}
/**
/**
...
...
framework/db/dao/mysql/ColumnSchema.php
View file @
a1a69c5e
...
@@ -25,35 +25,35 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
...
@@ -25,35 +25,35 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
public
function
initTypes
(
$dbType
)
public
function
initTypes
(
$dbType
)
{
{
static
$typeMap
=
array
(
// dbType => type
static
$typeMap
=
array
(
// dbType => type
'tinyint'
=>
'smallint'
,
'tinyint'
=>
self
::
TYPE_SMALLINT
,
'bit'
=>
'smallint'
,
'bit'
=>
self
::
TYPE_SMALLINT
,
'smallint'
=>
'smallint'
,
'smallint'
=>
self
::
TYPE_SMALLINT
,
'mediumint'
=>
'integer'
,
'mediumint'
=>
self
::
TYPE_INTEGER
,
'int'
=>
'integer'
,
'int'
=>
self
::
TYPE_INTEGER
,
'integer'
=>
'integer'
,
'integer'
=>
self
::
TYPE_INTEGER
,
'bigint'
=>
'bigint'
,
'bigint'
=>
self
::
TYPE_BIGINT
,
'float'
=>
'float'
,
'float'
=>
self
::
TYPE_FLOAT
,
'double'
=>
'float'
,
'double'
=>
self
::
TYPE_FLOAT
,
'real'
=>
'float'
,
'real'
=>
self
::
TYPE_FLOAT
,
'decimal'
=>
'decimal'
,
'decimal'
=>
self
::
TYPE_DECIMAL
,
'numeric'
=>
'decimal'
,
'numeric'
=>
self
::
TYPE_DECIMAL
,
'tinytext'
=>
'text'
,
'tinytext'
=>
self
::
TYPE_TEXT
,
'mediumtext'
=>
'text'
,
'mediumtext'
=>
self
::
TYPE_TEXT
,
'longtext'
=>
'text'
,
'longtext'
=>
self
::
TYPE_TEXT
,
'text'
=>
'text'
,
'text'
=>
self
::
TYPE_TEXT
,
'varchar'
=>
'string'
,
'varchar'
=>
self
::
TYPE_STRING
,
'string'
=>
'string'
,
'string'
=>
self
::
TYPE_STRING
,
'char'
=>
'string'
,
'char'
=>
self
::
TYPE_STRING
,
'datetime'
=>
'datetime'
,
'datetime'
=>
self
::
TYPE_DATETIME
,
'year'
=>
'date'
,
'year'
=>
self
::
TYPE_DATE
,
'date'
=>
'date'
,
'date'
=>
self
::
TYPE_DATE
,
'time'
=>
'time'
,
'time'
=>
self
::
TYPE_TIME
,
'timestamp'
=>
'timestamp'
,
'timestamp'
=>
self
::
TYPE_TIMESTAMP
,
'enum'
=>
'enum'
,
'enum'
=>
self
::
TYPE_STRING
,
);
);
$this
->
dbType
=
$dbType
;
$this
->
dbType
=
$dbType
;
$this
->
type
=
'string'
;
$this
->
type
=
self
::
TYPE_STRING
;
$this
->
unsigned
=
strpos
(
$this
->
dbType
,
'unsigned'
)
!==
false
;
$this
->
unsigned
=
strpos
(
$this
->
dbType
,
'unsigned'
)
!==
false
;
if
(
preg_match
(
'/^(\w+)(?:\(([^\)]+)\))?/'
,
$this
->
dbType
,
$matches
))
{
if
(
preg_match
(
'/^(\w+)(?:\(([^\)]+)\))?/'
,
$this
->
dbType
,
$matches
))
{
...
@@ -63,7 +63,7 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
...
@@ -63,7 +63,7 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
}
}
if
(
!
empty
(
$matches
[
2
]))
{
if
(
!
empty
(
$matches
[
2
]))
{
if
(
$t
his
->
t
ype
===
'enum'
)
{
if
(
$type
===
'enum'
)
{
$values
=
explode
(
','
,
$matches
[
2
]);
$values
=
explode
(
','
,
$matches
[
2
]);
foreach
(
$values
as
$i
=>
$value
)
{
foreach
(
$values
as
$i
=>
$value
)
{
$values
[
$i
]
=
trim
(
$value
,
"'"
);
$values
[
$i
]
=
trim
(
$value
,
"'"
);
...
@@ -93,7 +93,7 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
...
@@ -93,7 +93,7 @@ class ColumnSchema extends \yii\db\dao\ColumnSchema
/**
/**
* Extracts the default value for the column.
* Extracts the default value for the column.
* The value is typecast
ed
to correct PHP type.
* The value is typecast to correct PHP type.
* @param mixed $defaultValue the default value obtained from metadata
* @param mixed $defaultValue the default value obtained from metadata
*/
*/
public
function
initDefaultValue
(
$defaultValue
)
public
function
initDefaultValue
(
$defaultValue
)
...
...
framework/db/dao/mysql/QueryBuilder.php
View file @
a1a69c5e
...
@@ -22,21 +22,21 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
...
@@ -22,21 +22,21 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
* @var array the abstract column types mapped to physical column types.
* @var array the abstract column types mapped to physical column types.
*/
*/
public
$typeMap
=
array
(
public
$typeMap
=
array
(
Schema
::
TYPE_PK
=>
'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'
,
'pk'
=>
'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'
,
Schema
::
TYPE_STRING
=>
'varchar(255)'
,
Column
Schema
::
TYPE_STRING
=>
'varchar(255)'
,
Schema
::
TYPE_TEXT
=>
'text'
,
Column
Schema
::
TYPE_TEXT
=>
'text'
,
Schema
::
TYPE_SMALLINT
=>
'smallint(6)'
,
Column
Schema
::
TYPE_SMALLINT
=>
'smallint(6)'
,
Schema
::
TYPE_INTEGER
=>
'int(11)'
,
Column
Schema
::
TYPE_INTEGER
=>
'int(11)'
,
Schema
::
TYPE_BIGINT
=>
'bigint(20)'
,
Column
Schema
::
TYPE_BIGINT
=>
'bigint(20)'
,
Schema
::
TYPE_FLOAT
=>
'float'
,
Column
Schema
::
TYPE_FLOAT
=>
'float'
,
Schema
::
TYPE_DECIMAL
=>
'decimal'
,
Column
Schema
::
TYPE_DECIMAL
=>
'decimal'
,
Schema
::
TYPE_DATETIME
=>
'datetime'
,
Column
Schema
::
TYPE_DATETIME
=>
'datetime'
,
Schema
::
TYPE_TIMESTAMP
=>
'timestamp'
,
Column
Schema
::
TYPE_TIMESTAMP
=>
'timestamp'
,
Schema
::
TYPE_TIME
=>
'time'
,
Column
Schema
::
TYPE_TIME
=>
'time'
,
Schema
::
TYPE_DATE
=>
'date'
,
Column
Schema
::
TYPE_DATE
=>
'date'
,
Schema
::
TYPE_BINARY
=>
'blob'
,
Column
Schema
::
TYPE_BINARY
=>
'blob'
,
Schema
::
TYPE_BOOLEAN
=>
'tinyint(1)'
,
Column
Schema
::
TYPE_BOOLEAN
=>
'tinyint(1)'
,
Schema
::
TYPE_MONEY
=>
'decimal(19,4)'
,
Column
Schema
::
TYPE_MONEY
=>
'decimal(19,4)'
,
);
);
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment