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
e150b7fa
Commit
e150b7fa
authored
Jan 15, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renamed Driver to Schema.
parent
39254a80
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
106 additions
and
89 deletions
+106
-89
Connection.php
framework/db/Connection.php
+46
-56
Schema.php
framework/db/Schema.php
+42
-15
Transaction.php
framework/db/Transaction.php
+1
-1
QueryBuilder.php
framework/db/mysql/QueryBuilder.php
+15
-15
Schema.php
framework/db/mysql/Schema.php
+1
-1
Schema.php
framework/db/sqlite/Schema.php
+1
-1
No files found.
framework/db/Connection.php
View file @
e150b7fa
...
...
@@ -10,6 +10,7 @@
namespace
yii\db
;
use
yii\db\Exception
;
use
yii\base\BadConfigException
;
/**
* Connection represents a connection to a database via [PDO](http://www.php.net/manual/en/ref.pdo.php).
...
...
@@ -87,8 +88,8 @@ use yii\db\Exception;
* ~~~
*
* @property boolean $isActive Whether the DB connection is established. This property is read-only.
* @property Transaction $
currentT
ransaction The currently active transaction. Null if no active transaction.
* @property
Driver $driver The database driver
for the current connection.
* @property Transaction $
t
ransaction The currently active transaction. Null if no active transaction.
* @property
Schema $schema The database schema information
for the current connection.
* @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 $driverName Name of the DB driver currently being used.
...
...
@@ -237,34 +238,34 @@ class Connection extends \yii\base\ApplicationComponent
*/
public
$initSQLs
;
/**
* @var array mapping between PDO driver names and [[
Driver
]] classes.
* @var array mapping between PDO driver names and [[
Schema
]] classes.
* The keys of the array are PDO driver names while the values the corresponding
*
driver class name or configuration. Please refer to [[\Yii::createObject
]] for
*
schema class name or configuration. Please refer to [[\Yii::createObject()
]] for
* details on how to specify a configuration.
*
* This property is mainly used by [[get
Driver
()]] when fetching the database schema information.
* This property is mainly used by [[get
Schema
()]] when fetching the database schema information.
* You normally do not need to set this property unless you want to use your own
* [[
Driver
]] class to support DBMS that is not supported by Yii.
*/
public
$
driver
Map
=
array
(
'pgsql'
=>
'yii\db\pgsql\
Driver
'
,
// PostgreSQL
'mysqli'
=>
'yii\db\mysql\
Driver
'
,
// MySQL
'mysql'
=>
'yii\db\mysql\
Driver
'
,
// MySQL
'sqlite'
=>
'yii\db\sqlite\
Driver
'
,
// sqlite 3
'sqlite2'
=>
'yii\db\sqlite\
Driver
'
,
// sqlite 2
'mssql'
=>
'yi\db\dao\mssql\
Driver
'
,
// Mssql driver on windows hosts
'dblib'
=>
'yii\db\mssql\
Driver
'
,
// dblib drivers on linux (and maybe others os) hosts
'sqlsrv'
=>
'yii\db\mssql\
Driver
'
,
// Mssql
'oci'
=>
'yii\db\oci\
Driver
'
,
// Oracle driver
* [[
Schema
]] class to support DBMS that is not supported by Yii.
*/
public
$
schema
Map
=
array
(
'pgsql'
=>
'yii\db\pgsql\
Schema
'
,
// PostgreSQL
'mysqli'
=>
'yii\db\mysql\
Schema
'
,
// MySQL
'mysql'
=>
'yii\db\mysql\
Schema
'
,
// MySQL
'sqlite'
=>
'yii\db\sqlite\
Schema
'
,
// sqlite 3
'sqlite2'
=>
'yii\db\sqlite\
Schema
'
,
// sqlite 2
'mssql'
=>
'yi\db\dao\mssql\
Schema
'
,
// Mssql driver on windows hosts
'dblib'
=>
'yii\db\mssql\
Schema
'
,
// dblib drivers on linux (and maybe others os) hosts
'sqlsrv'
=>
'yii\db\mssql\
Schema
'
,
// Mssql
'oci'
=>
'yii\db\oci\
Schema
'
,
// Oracle driver
);
/**
* @var Transaction the currently active transaction
*/
private
$_transaction
;
/**
* @var
Driver the database driver
* @var
Schema the database schema
*/
private
$_
driver
;
private
$_
schema
;
/**
* Closes the connection when this component is being serialized.
...
...
@@ -330,7 +331,7 @@ class Connection extends \yii\base\ApplicationComponent
{
if
(
$this
->
pdo
===
null
)
{
if
(
empty
(
$this
->
dsn
))
{
throw
new
Exception
(
'Connection.dsn cannot be empty.'
);
throw
new
BadConfig
Exception
(
'Connection.dsn cannot be empty.'
);
}
try
{
\Yii
::
trace
(
'Opening DB connection: '
.
$this
->
dsn
,
__CLASS__
);
...
...
@@ -354,7 +355,7 @@ class Connection extends \yii\base\ApplicationComponent
if
(
$this
->
pdo
!==
null
)
{
\Yii
::
trace
(
'Closing DB connection: '
.
$this
->
dsn
,
__CLASS__
);
$this
->
pdo
=
null
;
$this
->
_
driver
=
null
;
$this
->
_
schema
=
null
;
$this
->
_transaction
=
null
;
}
}
...
...
@@ -372,7 +373,7 @@ class Connection extends \yii\base\ApplicationComponent
if
((
$pos
=
strpos
(
$this
->
dsn
,
':'
))
!==
false
)
{
$driver
=
strtolower
(
substr
(
$this
->
dsn
,
0
,
$pos
));
if
(
$driver
===
'mssql'
||
$driver
===
'dblib'
||
$driver
===
'sqlsrv'
)
{
$pdoClass
=
'mssql\PDO'
;
$pdoClass
=
'
yii\db\
mssql\PDO'
;
}
}
return
new
$pdoClass
(
$this
->
dsn
,
$this
->
username
,
$this
->
password
,
$this
->
attributes
);
...
...
@@ -421,13 +422,9 @@ class Connection extends \yii\base\ApplicationComponent
* Returns the currently active transaction.
* @return Transaction the currently active transaction. Null if no active transaction.
*/
public
function
get
Current
Transaction
()
public
function
getTransaction
()
{
if
(
$this
->
_transaction
!==
null
&&
$this
->
_transaction
->
isActive
)
{
return
$this
->
_transaction
;
}
else
{
return
null
;
}
return
$this
->
_transaction
&&
$this
->
_transaction
->
isActive
?
$this
->
_transaction
:
null
;
}
/**
...
...
@@ -445,19 +442,22 @@ class Connection extends \yii\base\ApplicationComponent
}
/**
* Returns the metadata information for the underlying database.
* @return Driver the metadata information for the underlying database.
* Returns the schema information for the database opened by this connection.
* @return Schema the schema information for the database opened by this connection.
* @throws BadConfigException if there is no support for the current driver type
*/
public
function
get
Driver
()
public
function
get
Schema
()
{
if
(
$this
->
_
driver
!==
null
)
{
return
$this
->
_
driver
;
if
(
$this
->
_
schema
!==
null
)
{
return
$this
->
_
schema
;
}
else
{
$driver
=
$this
->
getDriverName
();
if
(
isset
(
$this
->
driverMap
[
$driver
]))
{
return
$this
->
_driver
=
\Yii
::
createObject
(
$this
->
driverMap
[
$driver
],
$this
);
if
(
isset
(
$this
->
schemaMap
[
$driver
]))
{
$this
->
_schema
=
\Yii
::
createObject
(
$this
->
schemaMap
[
$driver
]);
$this
->
_schema
->
connection
=
$this
;
return
$this
->
_schema
;
}
else
{
throw
new
Exception
(
"Connection does not support reading metadata for '
$driver
' database
."
);
throw
new
BadConfigException
(
"Connection does not support reading schema information for '
$driver
' DBMS
."
);
}
}
}
...
...
@@ -468,18 +468,18 @@ class Connection extends \yii\base\ApplicationComponent
*/
public
function
getQueryBuilder
()
{
return
$this
->
get
Driver
()
->
getQueryBuilder
();
return
$this
->
get
Schema
()
->
getQueryBuilder
();
}
/**
* Obtains the
metadata
for the named table.
* @param string $name table name.
The table name may contain schema name if any. Do not quote the table name.
* Obtains the
schema information
for the named table.
* @param string $name table name.
* @param boolean $refresh whether to reload the table schema even if it is found in the cache.
* @return TableSchema table
metadata
. Null if the named table does not exist.
* @return TableSchema table
schema information
. Null if the named table does not exist.
*/
public
function
getTableSchema
(
$name
,
$refresh
=
false
)
{
return
$this
->
get
Driver
()
->
getTableSchema
(
$name
,
$refresh
);
return
$this
->
get
Schema
()
->
getTableSchema
(
$name
,
$refresh
);
}
/**
...
...
@@ -490,8 +490,7 @@ class Connection extends \yii\base\ApplicationComponent
*/
public
function
getLastInsertID
(
$sequenceName
=
''
)
{
$this
->
open
();
return
$this
->
pdo
->
lastInsertId
(
$sequenceName
);
return
$this
->
getSchema
()
->
getLastInsertID
(
$sequenceName
);
}
/**
...
...
@@ -503,16 +502,7 @@ class Connection extends \yii\base\ApplicationComponent
*/
public
function
quoteValue
(
$str
)
{
if
(
!
is_string
(
$str
))
{
return
$str
;
}
$this
->
open
();
if
((
$value
=
$this
->
pdo
->
quote
(
$str
))
!==
false
)
{
return
$value
;
}
else
{
// the driver doesn't support quote (e.g. oci)
return
"'"
.
addcslashes
(
str_replace
(
"'"
,
"''"
,
$str
),
"
\000\n\r\\\032
"
)
.
"'"
;
}
return
$this
->
getSchema
()
->
quoteValue
(
$str
);
}
/**
...
...
@@ -525,7 +515,7 @@ class Connection extends \yii\base\ApplicationComponent
*/
public
function
quoteTableName
(
$name
)
{
return
$this
->
get
Driver
()
->
quoteTableName
(
$name
);
return
$this
->
get
Schema
()
->
quoteTableName
(
$name
);
}
/**
...
...
@@ -538,7 +528,7 @@ class Connection extends \yii\base\ApplicationComponent
*/
public
function
quoteColumnName
(
$name
)
{
return
$this
->
get
Driver
()
->
quoteColumnName
(
$name
);
return
$this
->
get
Schema
()
->
quoteColumnName
(
$name
);
}
/**
...
...
@@ -587,7 +577,7 @@ class Connection extends \yii\base\ApplicationComponent
* and the second element the total time spent in SQL execution.
* @see \yii\logging\Logger::getProfiling()
*/
public
function
get
Stats
()
public
function
get
ExecutionSummary
()
{
$logger
=
\Yii
::
getLogger
();
$timings
=
$logger
->
getProfiling
(
array
(
'yii\db\Command::query'
,
'yii\db\Command::execute'
));
...
...
framework/db/
Driver
.php
→
framework/db/
Schema
.php
View file @
e150b7fa
...
...
@@ -12,18 +12,18 @@ namespace yii\db;
use
yii\db\Exception
;
/**
*
Driver is the base class for all database driver
classes.
*
Schema is the base class for concrete DBMS-specific schema
classes.
*
*
Driver implements the DBMS-specific methods to support retrieving metadata of a database
.
*
Schema represents the database schema information that is DBMS specific
.
*
* @property QueryBuilder $queryBuilder the query builder for th
is connection.
* @property QueryBuilder $queryBuilder the query builder for th
e DBMS represented by this schema
* @property array $tableNames the names of all tables in this database.
* @property array $tableSchemas the
metadata
for all tables in this database.
* @property array $tableSchemas the
schema information
for all tables in this database.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
abstract
class
Driver
extends
\yii\base\Object
abstract
class
Schema
extends
\yii\base\Object
{
/**
* The followings are the supported abstract column data types.
...
...
@@ -68,16 +68,6 @@ abstract class Driver extends \yii\base\Object
*/
abstract
protected
function
loadTableSchema
(
$name
);
/**
* Constructor.
* @param Connection $connection database connection.
* @param array $config name-value pairs that will be used to initialize the object properties
*/
public
function
__construct
(
$connection
,
$config
=
array
())
{
$this
->
connection
=
$connection
;
parent
::
__construct
(
$config
);
}
/**
* Obtains the metadata for the named table.
...
...
@@ -205,6 +195,43 @@ abstract class Driver extends \yii\base\Object
}
/**
* Returns the ID of the last inserted row or sequence value.
* @param string $sequenceName name of the sequence object (required by some DBMS)
* @return string the row ID of the last row inserted, or the last value retrieved from the sequence object
* @see http://www.php.net/manual/en/function.PDO-lastInsertId.php
*/
public
function
getLastInsertID
(
$sequenceName
=
''
)
{
if
(
$this
->
connection
->
isActive
)
{
return
$this
->
connection
->
pdo
->
lastInsertId
(
$sequenceName
);
}
else
{
throw
new
Exception
(
'DB Connection is not active.'
);
}
}
/**
* Quotes a string value for use in a query.
* Note that if the parameter is not a string, it will be returned without change.
* @param string $str string to be quoted
* @return string the properly quoted string
* @see http://www.php.net/manual/en/function.PDO-quote.php
*/
public
function
quoteValue
(
$str
)
{
if
(
!
is_string
(
$str
))
{
return
$str
;
}
$this
->
connection
->
open
();
if
((
$value
=
$this
->
connection
->
pdo
->
quote
(
$str
))
!==
false
)
{
return
$value
;
}
else
{
// the driver doesn't support quote (e.g. oci)
return
"'"
.
addcslashes
(
str_replace
(
"'"
,
"''"
,
$str
),
"
\000\n\r\\\032
"
)
.
"'"
;
}
}
/**
* Quotes a table name for use in a query.
* If the table name contains schema prefix, the prefix will also be properly quoted.
* If the table name is already quoted or contains special characters including '(', '[[' and '{{',
...
...
framework/db/Transaction.php
View file @
e150b7fa
...
...
@@ -67,7 +67,7 @@ class Transaction extends \yii\base\Object
{
if
(
!
$this
->
_active
)
{
if
(
$this
->
connection
===
null
)
{
throw
new
BadConfigException
(
'Transaction
::
connection must be set.'
);
throw
new
BadConfigException
(
'Transaction
.
connection must be set.'
);
}
\Yii
::
trace
(
'Starting transaction'
,
__CLASS__
);
$this
->
connection
->
open
();
...
...
framework/db/mysql/QueryBuilder.php
View file @
e150b7fa
...
...
@@ -23,21 +23,21 @@ class QueryBuilder extends \yii\db\QueryBuilder
* @var array mapping from abstract column types (keys) to physical column types (values).
*/
public
$typeMap
=
array
(
Driver
::
TYPE_PK
=>
'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'
,
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)'
,
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)'
,
);
/**
...
...
framework/db/mysql/
Driver
.php
→
framework/db/mysql/
Schema
.php
View file @
e150b7fa
...
...
@@ -18,7 +18,7 @@ use yii\db\ColumnSchema;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
Driver
extends
\yii\db\Driver
class
Schema
extends
\yii\db\Schema
{
/**
* @var array mapping from physical column types (keys) to abstract column types (values)
...
...
framework/db/sqlite/
Driver
.php
→
framework/db/sqlite/
Schema
.php
View file @
e150b7fa
...
...
@@ -18,7 +18,7 @@ use yii\db\ColumnSchema;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
Driver
extends
\yii\db\Driver
class
Schema
extends
\yii\db\Schema
{
/**
* @var array mapping from physical column types (keys) to abstract column types (values)
...
...
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