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
d75d9326
Commit
d75d9326
authored
Dec 27, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
a1a69c5e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
84 additions
and
81 deletions
+84
-81
Connection.php
framework/db/dao/Connection.php
+29
-30
Driver.php
framework/db/dao/Driver.php
+4
-4
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+36
-36
Driver.php
framework/db/dao/mysql/Driver.php
+5
-5
QueryBuilder.php
framework/db/dao/mysql/QueryBuilder.php
+6
-6
upgrade.md
upgrade.md
+4
-0
No files found.
framework/db/dao/Connection.php
View file @
d75d9326
...
@@ -85,7 +85,7 @@ use yii\db\Exception;
...
@@ -85,7 +85,7 @@ use yii\db\Exception;
*
*
* @property boolean $active Whether the DB connection is established.
* @property boolean $active Whether the DB connection is established.
* @property Transaction $currentTransaction The currently active transaction. Null if no active transaction.
* @property Transaction $currentTransaction The currently active transaction. Null if no active transaction.
* @property
Schema $schema The database schema
for the current connection.
* @property
Driver $driver The database driver
for the current connection.
* @property QueryBuilder $queryBuilder The query builder.
* @property QueryBuilder $queryBuilder The query builder.
* @property string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the sequence object.
* @property string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the sequence object.
* @property string $driverName Name of the DB driver currently being used.
* @property string $driverName Name of the DB driver currently being used.
...
@@ -225,35 +225,34 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -225,35 +225,34 @@ class Connection extends \yii\base\ApplicationComponent
*/
*/
public
$initSQLs
;
public
$initSQLs
;
/**
/**
* @var array mapping between PDO driver names and [[
Schema
]] classes.
* @var array mapping between PDO driver names and [[
Driver
]] classes.
* The keys of the array are PDO driver names while the values the corresponding
* The keys of the array are PDO driver names while the values the corresponding
*
schema
class name or configuration. Please refer to [[\Yii::createObject]] for
*
driver
class name or configuration. Please refer to [[\Yii::createObject]] for
* details on how to specify a configuration.
* details on how to specify a configuration.
*
*
* This property is mainly used by [[get
Schema
]] when fetching the database schema information.
* This property is mainly used by [[get
Driver()
]] when fetching the database schema information.
* You normally do not need to set this property unless you want to use your own
* You normally do not need to set this property unless you want to use your own
* [[
Schema
]] class to support DBMS that is not supported by Yii.
* [[
Driver
]] class to support DBMS that is not supported by Yii.
*/
*/
public
$
schema
Map
=
array
(
public
$
driver
Map
=
array
(
'pgsql'
=>
'\yii\db\dao\pgsql\
Schema
'
,
// PostgreSQL
'pgsql'
=>
'\yii\db\dao\pgsql\
Driver
'
,
// PostgreSQL
'mysqli'
=>
'\yii\db\dao\mysql\
Schema
'
,
// MySQL
'mysqli'
=>
'\yii\db\dao\mysql\
Driver
'
,
// MySQL
'mysql'
=>
'\yii\db\dao\mysql\
Schema
'
,
// MySQL
'mysql'
=>
'\yii\db\dao\mysql\
Driver
'
,
// MySQL
'sqlite'
=>
'\yii\db\dao\sqlite\
Schema
'
,
// sqlite 3
'sqlite'
=>
'\yii\db\dao\sqlite\
Driver
'
,
// sqlite 3
'sqlite2'
=>
'\yii\db\dao\sqlite\
Schema
'
,
// sqlite 2
'sqlite2'
=>
'\yii\db\dao\sqlite\
Driver
'
,
// sqlite 2
'mssql'
=>
'\yii\db\dao\mssql\
Schema
'
,
// Mssql driver on windows hosts
'mssql'
=>
'\yii\db\dao\mssql\
Driver
'
,
// Mssql driver on windows hosts
'dblib'
=>
'\yii\db\dao\mssql\
Schema
'
,
// dblib drivers on linux (and maybe others os) hosts
'dblib'
=>
'\yii\db\dao\mssql\
Driver
'
,
// dblib drivers on linux (and maybe others os) hosts
'sqlsrv'
=>
'\yii\db\dao\mssql\
Schema
'
,
// Mssql
'sqlsrv'
=>
'\yii\db\dao\mssql\
Driver
'
,
// Mssql
'oci'
=>
'\yii\db\dao\oci\
Schema
'
,
// Oracle driver
'oci'
=>
'\yii\db\dao\oci\
Driver
'
,
// Oracle driver
);
);
/**
/**
* @var Transaction the currently active transaction
* @var Transaction the currently active transaction
*/
*/
private
$_transaction
;
private
$_transaction
;
/**
/**
* @var
Schema the database schema
* @var
Driver the database driver
*/
*/
private
$_
schema
;
private
$_
driver
;
/**
/**
* Constructor.
* Constructor.
...
@@ -366,7 +365,7 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -366,7 +365,7 @@ class Connection extends \yii\base\ApplicationComponent
if
(
$this
->
pdo
!==
null
)
{
if
(
$this
->
pdo
!==
null
)
{
\Yii
::
trace
(
'Closing DB connection: '
.
$this
->
dsn
,
__CLASS__
);
\Yii
::
trace
(
'Closing DB connection: '
.
$this
->
dsn
,
__CLASS__
);
$this
->
pdo
=
null
;
$this
->
pdo
=
null
;
$this
->
_
schema
=
null
;
$this
->
_
driver
=
null
;
$this
->
_transaction
=
null
;
$this
->
_transaction
=
null
;
}
}
}
}
...
@@ -454,18 +453,18 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -454,18 +453,18 @@ class Connection extends \yii\base\ApplicationComponent
/**
/**
* Returns the metadata information for the underlying database.
* Returns the metadata information for the underlying database.
* @return
Schema
the metadata information for the underlying database.
* @return
Driver
the metadata information for the underlying database.
*/
*/
public
function
get
Schema
()
public
function
get
Driver
()
{
{
if
(
$this
->
_
schema
!==
null
)
{
if
(
$this
->
_
driver
!==
null
)
{
return
$this
->
_
schema
;
return
$this
->
_
driver
;
}
else
{
}
else
{
$driver
=
$this
->
getDriverName
();
$driver
=
$this
->
getDriverName
();
if
(
isset
(
$this
->
schema
Map
[
$driver
]))
{
if
(
isset
(
$this
->
driver
Map
[
$driver
]))
{
return
$this
->
_
schema
=
\Yii
::
createObject
(
$this
->
schema
Map
[
$driver
],
$this
);
return
$this
->
_
driver
=
\Yii
::
createObject
(
$this
->
driver
Map
[
$driver
],
$this
);
}
else
{
}
else
{
throw
new
Exception
(
"Connection does not support reading
schem
a for '
$driver
' database."
);
throw
new
Exception
(
"Connection does not support reading
meta dat
a for '
$driver
' database."
);
}
}
}
}
}
}
...
@@ -476,7 +475,7 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -476,7 +475,7 @@ class Connection extends \yii\base\ApplicationComponent
*/
*/
public
function
getQueryBuilder
()
public
function
getQueryBuilder
()
{
{
return
$this
->
get
Schema
()
->
getQueryBuilder
();
return
$this
->
get
Driver
()
->
getQueryBuilder
();
}
}
/**
/**
...
@@ -521,7 +520,7 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -521,7 +520,7 @@ class Connection extends \yii\base\ApplicationComponent
*/
*/
public
function
quoteTableName
(
$name
,
$simple
=
false
)
public
function
quoteTableName
(
$name
,
$simple
=
false
)
{
{
return
$simple
?
$this
->
get
Schema
()
->
quoteSimpleTableName
(
$name
)
:
$this
->
getSchema
()
->
quoteTableName
(
$name
);
return
$simple
?
$this
->
get
Driver
()
->
quoteSimpleTableName
(
$name
)
:
$this
->
getDriver
()
->
quoteTableName
(
$name
);
}
}
/**
/**
...
@@ -533,7 +532,7 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -533,7 +532,7 @@ class Connection extends \yii\base\ApplicationComponent
*/
*/
public
function
quoteColumnName
(
$name
,
$simple
=
false
)
public
function
quoteColumnName
(
$name
,
$simple
=
false
)
{
{
return
$simple
?
$this
->
get
Schema
()
->
quoteSimpleColumnName
(
$name
)
:
$this
->
getSchema
()
->
quoteColumnName
(
$name
);
return
$simple
?
$this
->
get
Driver
()
->
quoteSimpleColumnName
(
$name
)
:
$this
->
getDriver
()
->
quoteColumnName
(
$name
);
}
}
/**
/**
...
...
framework/db/dao/
Schema
.php
→
framework/db/dao/
Driver
.php
View file @
d75d9326
<?php
<?php
/**
/**
*
Schema
class file.
*
Driver
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/
...
@@ -13,9 +13,9 @@ namespace yii\db\dao;
...
@@ -13,9 +13,9 @@ namespace yii\db\dao;
use
yii\db\Exception
;
use
yii\db\Exception
;
/**
/**
*
Schema represents the meta data of a database
.
*
Driver is the base class for all database driver classes
.
*
*
*
Schema retrieves and maintains the meta data of database tables and columns
.
*
Driver implements the DBMS-specific methods to support retrieving meta data of a database
.
*
*
* @property QueryBuilder $queryBuilder the query builder for this connection.
* @property QueryBuilder $queryBuilder the query builder for this connection.
* @property array $tableNames the names of all tables in this database.
* @property array $tableNames the names of all tables in this database.
...
@@ -24,7 +24,7 @@ use yii\db\Exception;
...
@@ -24,7 +24,7 @@ use yii\db\Exception;
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
abstract
class
Schema
extends
\yii\base\Object
abstract
class
Driver
extends
\yii\base\Object
{
{
/**
/**
* @var Connection the database connection
* @var Connection the database connection
...
...
framework/db/dao/QueryBuilder.php
View file @
d75d9326
...
@@ -34,9 +34,9 @@ class QueryBuilder extends \yii\base\Object
...
@@ -34,9 +34,9 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
$connection
;
public
$connection
;
/**
/**
* @var
Schema the database schema
* @var
Driver the database driver
*/
*/
public
$
schema
;
public
$
driver
;
/**
/**
* @var string the separator between different fragments of a SQL statement.
* @var string the separator between different fragments of a SQL statement.
* Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement.
* Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement.
...
@@ -54,7 +54,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -54,7 +54,7 @@ class QueryBuilder extends \yii\base\Object
public
function
__construct
(
$connection
)
public
function
__construct
(
$connection
)
{
{
$this
->
connection
=
$connection
;
$this
->
connection
=
$connection
;
$this
->
schema
=
$connection
->
getSchema
();
$this
->
driver
=
$connection
->
getDriver
();
}
}
/**
/**
...
@@ -105,7 +105,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -105,7 +105,7 @@ class QueryBuilder extends \yii\base\Object
$placeholders
=
array
();
$placeholders
=
array
();
$count
=
0
;
$count
=
0
;
foreach
(
$columns
as
$name
=>
$value
)
{
foreach
(
$columns
as
$name
=>
$value
)
{
$names
[]
=
$this
->
schema
->
quoteColumnName
(
$name
);
$names
[]
=
$this
->
driver
->
quoteColumnName
(
$name
);
if
(
$value
instanceof
Expression
)
{
if
(
$value
instanceof
Expression
)
{
$placeholders
[]
=
$value
->
expression
;
$placeholders
[]
=
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
foreach
(
$value
->
params
as
$n
=>
$v
)
{
...
@@ -121,7 +121,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -121,7 +121,7 @@ class QueryBuilder extends \yii\base\Object
$this
->
_query
->
addParams
(
$params
);
$this
->
_query
->
addParams
(
$params
);
}
}
return
'INSERT INTO '
.
$this
->
schema
->
quoteTableName
(
$table
)
return
'INSERT INTO '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' ('
.
implode
(
', '
,
$names
)
.
') VALUES ('
.
' ('
.
implode
(
', '
,
$names
)
.
') VALUES ('
.
implode
(
', '
,
$placeholders
)
.
')'
;
.
implode
(
', '
,
$placeholders
)
.
')'
;
}
}
...
@@ -143,12 +143,12 @@ class QueryBuilder extends \yii\base\Object
...
@@ -143,12 +143,12 @@ class QueryBuilder extends \yii\base\Object
$count
=
0
;
$count
=
0
;
foreach
(
$columns
as
$name
=>
$value
)
{
foreach
(
$columns
as
$name
=>
$value
)
{
if
(
$value
instanceof
Expression
)
{
if
(
$value
instanceof
Expression
)
{
$lines
[]
=
$this
->
schema
->
quoteSimpleColumnName
(
$name
)
.
'='
.
$value
->
expression
;
$lines
[]
=
$this
->
driver
->
quoteSimpleColumnName
(
$name
)
.
'='
.
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
foreach
(
$value
->
params
as
$n
=>
$v
)
{
$params
[
$n
]
=
$v
;
$params
[
$n
]
=
$v
;
}
}
}
else
{
}
else
{
$lines
[]
=
$this
->
schema
->
quoteSimpleColumnName
(
$name
)
.
'=:p'
.
$count
;
$lines
[]
=
$this
->
driver
->
quoteSimpleColumnName
(
$name
)
.
'=:p'
.
$count
;
$params
[
':p'
.
$count
]
=
$value
;
$params
[
':p'
.
$count
]
=
$value
;
$count
++
;
$count
++
;
}
}
...
@@ -156,7 +156,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -156,7 +156,7 @@ class QueryBuilder extends \yii\base\Object
if
(
$this
->
_query
instanceof
Query
)
{
if
(
$this
->
_query
instanceof
Query
)
{
$this
->
_query
->
addParams
(
$params
);
$this
->
_query
->
addParams
(
$params
);
}
}
$sql
=
'UPDATE '
.
$this
->
schema
->
quoteTableName
(
$table
)
.
' SET '
.
implode
(
', '
,
$lines
);
$sql
=
'UPDATE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' SET '
.
implode
(
', '
,
$lines
);
if
((
$where
=
$this
->
buildCondition
(
$condition
))
!=
''
)
{
if
((
$where
=
$this
->
buildCondition
(
$condition
))
!=
''
)
{
$sql
.=
' WHERE '
.
$where
;
$sql
.=
' WHERE '
.
$where
;
}
}
...
@@ -173,7 +173,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -173,7 +173,7 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
delete
(
$table
,
$condition
=
''
)
public
function
delete
(
$table
,
$condition
=
''
)
{
{
$sql
=
'DELETE FROM '
.
$this
->
schema
->
quoteTableName
(
$table
);
$sql
=
'DELETE FROM '
.
$this
->
driver
->
quoteTableName
(
$table
);
if
((
$where
=
$this
->
buildCondition
(
$condition
))
!=
''
)
{
if
((
$where
=
$this
->
buildCondition
(
$condition
))
!=
''
)
{
$sql
.=
' WHERE '
.
$where
;
$sql
.=
' WHERE '
.
$where
;
}
}
...
@@ -201,13 +201,13 @@ class QueryBuilder extends \yii\base\Object
...
@@ -201,13 +201,13 @@ class QueryBuilder extends \yii\base\Object
$cols
=
array
();
$cols
=
array
();
foreach
(
$columns
as
$name
=>
$type
)
{
foreach
(
$columns
as
$name
=>
$type
)
{
if
(
is_string
(
$name
))
{
if
(
is_string
(
$name
))
{
$cols
[]
=
"
\t
"
.
$this
->
schema
->
quoteColumnName
(
$name
)
.
' '
.
$this
->
schema
->
getColumnType
(
$type
);
$cols
[]
=
"
\t
"
.
$this
->
driver
->
quoteColumnName
(
$name
)
.
' '
.
$this
->
driver
->
getColumnType
(
$type
);
}
else
}
else
{
{
$cols
[]
=
"
\t
"
.
$type
;
$cols
[]
=
"
\t
"
.
$type
;
}
}
}
}
$sql
=
"CREATE TABLE "
.
$this
->
schema
->
quoteTableName
(
$table
)
.
" (
\n
"
.
implode
(
",
\n
"
,
$cols
)
.
"
\n
)"
;
$sql
=
"CREATE TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
)
.
" (
\n
"
.
implode
(
",
\n
"
,
$cols
)
.
"
\n
)"
;
return
$options
===
null
?
$sql
:
$sql
.
' '
.
$options
;
return
$options
===
null
?
$sql
:
$sql
.
' '
.
$options
;
}
}
...
@@ -219,7 +219,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -219,7 +219,7 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
renameTable
(
$table
,
$newName
)
public
function
renameTable
(
$table
,
$newName
)
{
{
return
'RENAME TABLE '
.
$this
->
schema
->
quoteTableName
(
$table
)
.
' TO '
.
$this
->
schema
->
quoteTableName
(
$newName
);
return
'RENAME TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' TO '
.
$this
->
driver
->
quoteTableName
(
$newName
);
}
}
/**
/**
...
@@ -229,7 +229,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -229,7 +229,7 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
dropTable
(
$table
)
public
function
dropTable
(
$table
)
{
{
return
"DROP TABLE "
.
$this
->
schema
->
quoteTableName
(
$table
);
return
"DROP TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
);
}
}
/**
/**
...
@@ -239,7 +239,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -239,7 +239,7 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
truncateTable
(
$table
)
public
function
truncateTable
(
$table
)
{
{
return
"TRUNCATE TABLE "
.
$this
->
schema
->
quoteTableName
(
$table
);
return
"TRUNCATE TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
);
}
}
/**
/**
...
@@ -253,8 +253,8 @@ class QueryBuilder extends \yii\base\Object
...
@@ -253,8 +253,8 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
addColumn
(
$table
,
$column
,
$type
)
public
function
addColumn
(
$table
,
$column
,
$type
)
{
{
return
'ALTER TABLE '
.
$this
->
schema
->
quoteTableName
(
$table
)
return
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' ADD '
.
$this
->
schema
->
quoteColumnName
(
$column
)
.
' '
.
' ADD '
.
$this
->
driver
->
quoteColumnName
(
$column
)
.
' '
.
$this
->
getColumnType
(
$type
);
.
$this
->
getColumnType
(
$type
);
}
}
...
@@ -266,8 +266,8 @@ class QueryBuilder extends \yii\base\Object
...
@@ -266,8 +266,8 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
dropColumn
(
$table
,
$column
)
public
function
dropColumn
(
$table
,
$column
)
{
{
return
"ALTER TABLE "
.
$this
->
schema
->
quoteTableName
(
$table
)
return
"ALTER TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
)
.
" DROP COLUMN "
.
$this
->
schema
->
quoteSimpleColumnName
(
$column
);
.
" DROP COLUMN "
.
$this
->
driver
->
quoteSimpleColumnName
(
$column
);
}
}
/**
/**
...
@@ -279,9 +279,9 @@ class QueryBuilder extends \yii\base\Object
...
@@ -279,9 +279,9 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
renameColumn
(
$table
,
$name
,
$newName
)
public
function
renameColumn
(
$table
,
$name
,
$newName
)
{
{
return
"ALTER TABLE "
.
$this
->
schema
->
quoteTableName
(
$table
)
return
"ALTER TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
)
.
" RENAME COLUMN "
.
$this
->
schema
->
quoteSimpleColumnName
(
$name
)
.
" RENAME COLUMN "
.
$this
->
driver
->
quoteSimpleColumnName
(
$name
)
.
" TO "
.
$this
->
schema
->
quoteSimpleColumnName
(
$newName
);
.
" TO "
.
$this
->
driver
->
quoteSimpleColumnName
(
$newName
);
}
}
/**
/**
...
@@ -295,9 +295,9 @@ class QueryBuilder extends \yii\base\Object
...
@@ -295,9 +295,9 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
alterColumn
(
$table
,
$column
,
$type
)
public
function
alterColumn
(
$table
,
$column
,
$type
)
{
{
return
'ALTER TABLE '
.
$this
->
schema
->
quoteTableName
(
$table
)
.
' CHANGE '
return
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' CHANGE '
.
$this
->
schema
->
quoteSimpleColumnName
(
$column
)
.
' '
.
$this
->
driver
->
quoteSimpleColumnName
(
$column
)
.
' '
.
$this
->
schema
->
quoteSimpleColumnName
(
$column
)
.
' '
.
$this
->
driver
->
quoteSimpleColumnName
(
$column
)
.
' '
.
$this
->
getColumnType
(
$type
);
.
$this
->
getColumnType
(
$type
);
}
}
...
@@ -317,16 +317,16 @@ class QueryBuilder extends \yii\base\Object
...
@@ -317,16 +317,16 @@ class QueryBuilder extends \yii\base\Object
{
{
$columns
=
preg_split
(
'/\s*,\s*/'
,
$columns
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
$columns
=
preg_split
(
'/\s*,\s*/'
,
$columns
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
foreach
(
$columns
as
$i
=>
$col
)
{
foreach
(
$columns
as
$i
=>
$col
)
{
$columns
[
$i
]
=
$this
->
schema
->
quoteColumnName
(
$col
);
$columns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$col
);
}
}
$refColumns
=
preg_split
(
'/\s*,\s*/'
,
$refColumns
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
$refColumns
=
preg_split
(
'/\s*,\s*/'
,
$refColumns
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
foreach
(
$refColumns
as
$i
=>
$col
)
{
foreach
(
$refColumns
as
$i
=>
$col
)
{
$refColumns
[
$i
]
=
$this
->
schema
->
quoteColumnName
(
$col
);
$refColumns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$col
);
}
}
$sql
=
'ALTER TABLE '
.
$this
->
schema
->
quoteTableName
(
$table
)
$sql
=
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' ADD CONSTRAINT '
.
$this
->
schema
->
quoteColumnName
(
$name
)
.
' ADD CONSTRAINT '
.
$this
->
driver
->
quoteColumnName
(
$name
)
.
' FOREIGN KEY ('
.
implode
(
', '
,
$columns
)
.
')'
.
' FOREIGN KEY ('
.
implode
(
', '
,
$columns
)
.
')'
.
' REFERENCES '
.
$this
->
schema
->
quoteTableName
(
$refTable
)
.
' REFERENCES '
.
$this
->
driver
->
quoteTableName
(
$refTable
)
.
' ('
.
implode
(
', '
,
$refColumns
)
.
')'
;
.
' ('
.
implode
(
', '
,
$refColumns
)
.
')'
;
if
(
$delete
!==
null
)
{
if
(
$delete
!==
null
)
{
$sql
.=
' ON DELETE '
.
$delete
;
$sql
.=
' ON DELETE '
.
$delete
;
...
@@ -345,8 +345,8 @@ class QueryBuilder extends \yii\base\Object
...
@@ -345,8 +345,8 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
dropForeignKey
(
$name
,
$table
)
public
function
dropForeignKey
(
$name
,
$table
)
{
{
return
'ALTER TABLE '
.
$this
->
schema
->
quoteTableName
(
$table
)
return
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' DROP CONSTRAINT '
.
$this
->
schema
->
quoteColumnName
(
$name
);
.
' DROP CONSTRAINT '
.
$this
->
driver
->
quoteColumnName
(
$name
);
}
}
/**
/**
...
@@ -368,12 +368,12 @@ class QueryBuilder extends \yii\base\Object
...
@@ -368,12 +368,12 @@ class QueryBuilder extends \yii\base\Object
$cols
[]
=
$col
;
$cols
[]
=
$col
;
}
else
}
else
{
{
$cols
[]
=
$this
->
schema
->
quoteColumnName
(
$col
);
$cols
[]
=
$this
->
driver
->
quoteColumnName
(
$col
);
}
}
}
}
return
(
$unique
?
'CREATE UNIQUE INDEX '
:
'CREATE INDEX '
)
return
(
$unique
?
'CREATE UNIQUE INDEX '
:
'CREATE INDEX '
)
.
$this
->
schema
->
quoteTableName
(
$name
)
.
' ON '
.
$this
->
driver
->
quoteTableName
(
$name
)
.
' ON '
.
$this
->
schema
->
quoteTableName
(
$table
)
.
' ('
.
implode
(
', '
,
$cols
)
.
')'
;
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' ('
.
implode
(
', '
,
$cols
)
.
')'
;
}
}
/**
/**
...
@@ -384,7 +384,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -384,7 +384,7 @@ class QueryBuilder extends \yii\base\Object
*/
*/
public
function
dropIndex
(
$name
,
$table
)
public
function
dropIndex
(
$name
,
$table
)
{
{
return
'DROP INDEX '
.
$this
->
schema
->
quoteTableName
(
$name
)
.
' ON '
.
$this
->
schema
->
quoteTableName
(
$table
);
return
'DROP INDEX '
.
$this
->
driver
->
quoteTableName
(
$name
)
.
' ON '
.
$this
->
driver
->
quoteTableName
(
$table
);
}
}
/**
/**
...
@@ -470,7 +470,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -470,7 +470,7 @@ class QueryBuilder extends \yii\base\Object
$columns
[
$i
]
=
(
string
)
$column
;
$columns
[
$i
]
=
(
string
)
$column
;
}
elseif
(
strpos
(
$column
,
'('
)
===
false
)
{
}
elseif
(
strpos
(
$column
,
'('
)
===
false
)
{
if
(
preg_match
(
'/^(.*?)(?i:\s+as\s+|\s+)([\w\-\.])$/'
,
$column
,
$matches
))
{
if
(
preg_match
(
'/^(.*?)(?i:\s+as\s+|\s+)([\w\-\.])$/'
,
$column
,
$matches
))
{
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$matches
[
1
])
.
' AS '
.
$this
->
connection
->
quoteSimpleColumnName
(
$matches
[
2
]);
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$matches
[
1
])
.
' AS '
.
$this
->
driver
->
quoteSimpleColumnName
(
$matches
[
2
]);
}
else
{
}
else
{
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$column
);
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$column
);
}
}
...
...
framework/db/dao/mysql/
Schema
.php
→
framework/db/dao/mysql/
Driver
.php
View file @
d75d9326
<?php
<?php
/**
/**
*
Schema
class file.
*
Driver
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/
...
@@ -13,16 +13,16 @@ namespace yii\db\dao\mysql;
...
@@ -13,16 +13,16 @@ namespace yii\db\dao\mysql;
use
yii\db\dao\TableSchema
;
use
yii\db\dao\TableSchema
;
/**
/**
*
Schema is the class for retrieving metadata information
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).
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Schema
extends
\yii\db\dao\Schema
class
Driver
extends
\yii\db\dao\Driver
{
{
/**
/**
* 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
has no
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
*/
*/
...
@@ -33,7 +33,7 @@ class Schema extends \yii\db\dao\Schema
...
@@ -33,7 +33,7 @@ class Schema extends \yii\db\dao\Schema
/**
/**
* Quotes a column name for use in a query.
* Quotes a column name for use in a query.
* A simple column name
does not contain
prefix.
* A simple column name
has no
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
*/
*/
...
...
framework/db/dao/mysql/QueryBuilder.php
View file @
d75d9326
...
@@ -48,7 +48,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
...
@@ -48,7 +48,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
*/
public
function
renameColumn
(
$table
,
$name
,
$newName
)
public
function
renameColumn
(
$table
,
$name
,
$newName
)
{
{
$quotedTable
=
$this
->
schema
->
quoteTableName
(
$table
);
$quotedTable
=
$this
->
driver
->
quoteTableName
(
$table
);
$row
=
$this
->
connection
->
createCommand
(
'SHOW CREATE TABLE '
.
$quotedTable
)
->
queryRow
();
$row
=
$this
->
connection
->
createCommand
(
'SHOW CREATE TABLE '
.
$quotedTable
)
->
queryRow
();
if
(
$row
===
false
)
if
(
$row
===
false
)
throw
new
CDbException
(
Yii
::
t
(
'yii'
,
'Unable to find "{column}" in table "{table}".'
,
array
(
'{column}'
=>
$name
,
'{table}'
=>
$table
)));
throw
new
CDbException
(
Yii
::
t
(
'yii'
,
'Unable to find "{column}" in table "{table}".'
,
array
(
'{column}'
=>
$name
,
'{table}'
=>
$table
)));
...
@@ -61,13 +61,13 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
...
@@ -61,13 +61,13 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
if
(
preg_match_all
(
'/^\s*`(.*?)`\s+(.*?),?$/m'
,
$sql
,
$matches
))
{
if
(
preg_match_all
(
'/^\s*`(.*?)`\s+(.*?),?$/m'
,
$sql
,
$matches
))
{
foreach
(
$matches
[
1
]
as
$i
=>
$c
)
{
foreach
(
$matches
[
1
]
as
$i
=>
$c
)
{
if
(
$c
===
$name
)
{
if
(
$c
===
$name
)
{
return
"ALTER TABLE
$quotedTable
CHANGE "
.
$this
->
schema
->
quoteColumnName
(
$name
)
return
"ALTER TABLE
$quotedTable
CHANGE "
.
$this
->
driver
->
quoteColumnName
(
$name
)
.
' '
.
$this
->
schema
->
quoteColumnName
(
$newName
)
.
' '
.
$matches
[
2
][
$i
];
.
' '
.
$this
->
driver
->
quoteColumnName
(
$newName
)
.
' '
.
$matches
[
2
][
$i
];
}
}
}
}
}
}
// try to give back a SQL anyway
// try to give back a SQL anyway
return
"ALTER TABLE
$quotedTable
CHANGE "
.
$this
->
schema
->
quoteColumnName
(
$name
)
.
' '
.
$newName
;
return
"ALTER TABLE
$quotedTable
CHANGE "
.
$this
->
driver
->
quoteColumnName
(
$name
)
.
' '
.
$newName
;
}
}
/**
/**
...
@@ -78,7 +78,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
...
@@ -78,7 +78,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
*/
public
function
dropForeignKey
(
$name
,
$table
)
public
function
dropForeignKey
(
$name
,
$table
)
{
{
return
'ALTER TABLE '
.
$this
->
schema
->
quoteTableName
(
$table
)
return
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' DROP FOREIGN KEY '
.
$this
->
schema
->
quoteColumnName
(
$name
);
.
' DROP FOREIGN KEY '
.
$this
->
driver
->
quoteColumnName
(
$name
);
}
}
}
}
upgrade.md
View file @
d75d9326
...
@@ -24,6 +24,7 @@ Upgrading from v1.1.x
...
@@ -24,6 +24,7 @@ Upgrading from v1.1.x
---------------------
---------------------
-
All framework classes are now namespaced, and the name prefix
`C`
is removed.
-
All framework classes are now namespaced, and the name prefix
`C`
is removed.
-
The format of path alias is changed to
`@yii/base/Component`
.
-
The format of path alias is changed to
`@yii/base/Component`
.
In 1.x, this would be
`system.base.CComponent`
. See guide for more details.
In 1.x, this would be
`system.base.CComponent`
. See guide for more details.
...
@@ -36,4 +37,7 @@ Upgrading from v1.1.x
...
@@ -36,4 +37,7 @@ Upgrading from v1.1.x
-
`CFormModel`
is removed. Please use
`yii\base\Model`
instead.
-
`CFormModel`
is removed. Please use
`yii\base\Model`
instead.
-
`CDbCriteria`
is replaced by
`yii\db\dao\Query`
which includes methods for
building a query.
`CDbCommandBuilder`
is replaced by
`yii\db\dao\QueryBuilder`
which has cleaner and more complete support of query building capabilities.
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