diff --git a/framework/db/dao/Connection.php b/framework/db/dao/Connection.php index cef4148..14f3953 100644 --- a/framework/db/dao/Connection.php +++ b/framework/db/dao/Connection.php @@ -85,7 +85,7 @@ use yii\db\Exception; * * @property boolean $active Whether the DB connection is established. * @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 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. @@ -225,35 +225,34 @@ class Connection extends \yii\base\ApplicationComponent */ 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 - * 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. * - * This property is mainly used by [[getSchema]] when fetching the database schema information. + * This property is mainly used by [[getDriver()]] when fetching the database schema information. * 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. - */ - public $schemaMap = array( - 'pgsql' => '\yii\db\dao\pgsql\Schema', // PostgreSQL - 'mysqli' => '\yii\db\dao\mysql\Schema', // MySQL - 'mysql' => '\yii\db\dao\mysql\Schema', // MySQL - 'sqlite' => '\yii\db\dao\sqlite\Schema', // sqlite 3 - 'sqlite2' => '\yii\db\dao\sqlite\Schema', // sqlite 2 - 'mssql' => '\yii\db\dao\mssql\Schema', // Mssql driver on windows hosts - 'dblib' => '\yii\db\dao\mssql\Schema', // dblib drivers on linux (and maybe others os) hosts - 'sqlsrv' => '\yii\db\dao\mssql\Schema', // Mssql - 'oci' => '\yii\db\dao\oci\Schema', // Oracle driver + * [[Driver]] class to support DBMS that is not supported by Yii. + */ + public $driverMap = array( + 'pgsql' => '\yii\db\dao\pgsql\Driver', // PostgreSQL + 'mysqli' => '\yii\db\dao\mysql\Driver', // MySQL + 'mysql' => '\yii\db\dao\mysql\Driver', // MySQL + 'sqlite' => '\yii\db\dao\sqlite\Driver', // sqlite 3 + 'sqlite2' => '\yii\db\dao\sqlite\Driver', // sqlite 2 + 'mssql' => '\yii\db\dao\mssql\Driver', // Mssql driver on windows hosts + 'dblib' => '\yii\db\dao\mssql\Driver', // dblib drivers on linux (and maybe others os) hosts + 'sqlsrv' => '\yii\db\dao\mssql\Driver', // Mssql + 'oci' => '\yii\db\dao\oci\Driver', // Oracle driver ); - /** * @var Transaction the currently active transaction */ private $_transaction; /** - * @var Schema the database schema + * @var Driver the database driver */ - private $_schema; + private $_driver; /** * Constructor. @@ -366,7 +365,7 @@ class Connection extends \yii\base\ApplicationComponent if ($this->pdo !== null) { \Yii::trace('Closing DB connection: ' . $this->dsn, __CLASS__); $this->pdo = null; - $this->_schema = null; + $this->_driver = null; $this->_transaction = null; } } @@ -454,18 +453,18 @@ class Connection extends \yii\base\ApplicationComponent /** * 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 getSchema() + public function getDriver() { - if ($this->_schema !== null) { - return $this->_schema; + if ($this->_driver !== null) { + return $this->_driver; } else { $driver = $this->getDriverName(); - if (isset($this->schemaMap[$driver])) { - return $this->_schema = \Yii::createObject($this->schemaMap[$driver], $this); + if (isset($this->driverMap[$driver])) { + return $this->_driver = \Yii::createObject($this->driverMap[$driver], $this); } else { - throw new Exception("Connection does not support reading schema for '$driver' database."); + throw new Exception("Connection does not support reading meta data for '$driver' database."); } } } @@ -476,7 +475,7 @@ class Connection extends \yii\base\ApplicationComponent */ public function getQueryBuilder() { - return $this->getSchema()->getQueryBuilder(); + return $this->getDriver()->getQueryBuilder(); } /** @@ -521,7 +520,7 @@ class Connection extends \yii\base\ApplicationComponent */ public function quoteTableName($name, $simple = false) { - return $simple ? $this->getSchema()->quoteSimpleTableName($name) : $this->getSchema()->quoteTableName($name); + return $simple ? $this->getDriver()->quoteSimpleTableName($name) : $this->getDriver()->quoteTableName($name); } /** @@ -533,7 +532,7 @@ class Connection extends \yii\base\ApplicationComponent */ public function quoteColumnName($name, $simple = false) { - return $simple ? $this->getSchema()->quoteSimpleColumnName($name) : $this->getSchema()->quoteColumnName($name); + return $simple ? $this->getDriver()->quoteSimpleColumnName($name) : $this->getDriver()->quoteColumnName($name); } /** diff --git a/framework/db/dao/Schema.php b/framework/db/dao/Driver.php similarity index 97% rename from framework/db/dao/Schema.php rename to framework/db/dao/Driver.php index 88a18ac..d31623e 100644 --- a/framework/db/dao/Schema.php +++ b/framework/db/dao/Driver.php @@ -1,6 +1,6 @@ * @link http://www.yiiframework.com/ @@ -13,9 +13,9 @@ namespace yii\db\dao; 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 array $tableNames the names of all tables in this database. @@ -24,7 +24,7 @@ use yii\db\Exception; * @author Qiang Xue * @since 2.0 */ -abstract class Schema extends \yii\base\Object +abstract class Driver extends \yii\base\Object { /** * @var Connection the database connection diff --git a/framework/db/dao/QueryBuilder.php b/framework/db/dao/QueryBuilder.php index aa272ae..cf2598c 100644 --- a/framework/db/dao/QueryBuilder.php +++ b/framework/db/dao/QueryBuilder.php @@ -34,9 +34,9 @@ class QueryBuilder extends \yii\base\Object */ 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. * 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 public function __construct($connection) { $this->connection = $connection; - $this->schema = $connection->getSchema(); + $this->driver = $connection->getDriver(); } /** @@ -105,7 +105,7 @@ class QueryBuilder extends \yii\base\Object $placeholders = array(); $count = 0; foreach ($columns as $name => $value) { - $names[] = $this->schema->quoteColumnName($name); + $names[] = $this->driver->quoteColumnName($name); if ($value instanceof Expression) { $placeholders[] = $value->expression; foreach ($value->params as $n => $v) { @@ -121,7 +121,7 @@ class QueryBuilder extends \yii\base\Object $this->_query->addParams($params); } - return 'INSERT INTO ' . $this->schema->quoteTableName($table) + return 'INSERT INTO ' . $this->driver->quoteTableName($table) . ' (' . implode(', ', $names) . ') VALUES (' . implode(', ', $placeholders) . ')'; } @@ -143,12 +143,12 @@ class QueryBuilder extends \yii\base\Object $count = 0; foreach ($columns as $name => $value) { if ($value instanceof Expression) { - $lines[] = $this->schema->quoteSimpleColumnName($name) . '=' . $value->expression; + $lines[] = $this->driver->quoteSimpleColumnName($name) . '=' . $value->expression; foreach ($value->params as $n => $v) { $params[$n] = $v; } } else { - $lines[] = $this->schema->quoteSimpleColumnName($name) . '=:p' . $count; + $lines[] = $this->driver->quoteSimpleColumnName($name) . '=:p' . $count; $params[':p' . $count] = $value; $count++; } @@ -156,7 +156,7 @@ class QueryBuilder extends \yii\base\Object if ($this->_query instanceof Query) { $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)) != '') { $sql .= ' WHERE ' . $where; } @@ -173,7 +173,7 @@ class QueryBuilder extends \yii\base\Object */ public function delete($table, $condition = '') { - $sql = 'DELETE FROM ' . $this->schema->quoteTableName($table); + $sql = 'DELETE FROM ' . $this->driver->quoteTableName($table); if (($where = $this->buildCondition($condition)) != '') { $sql .= ' WHERE ' . $where; } @@ -201,13 +201,13 @@ class QueryBuilder extends \yii\base\Object $cols = array(); foreach ($columns as $name => $type) { 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 { $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; } @@ -219,7 +219,7 @@ class QueryBuilder extends \yii\base\Object */ 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 */ 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 */ 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 */ public function addColumn($table, $column, $type) { - return 'ALTER TABLE ' . $this->schema->quoteTableName($table) - . ' ADD ' . $this->schema->quoteColumnName($column) . ' ' + return 'ALTER TABLE ' . $this->driver->quoteTableName($table) + . ' ADD ' . $this->driver->quoteColumnName($column) . ' ' . $this->getColumnType($type); } @@ -266,8 +266,8 @@ class QueryBuilder extends \yii\base\Object */ public function dropColumn($table, $column) { - return "ALTER TABLE " . $this->schema->quoteTableName($table) - . " DROP COLUMN " . $this->schema->quoteSimpleColumnName($column); + return "ALTER TABLE " . $this->driver->quoteTableName($table) + . " DROP COLUMN " . $this->driver->quoteSimpleColumnName($column); } /** @@ -279,9 +279,9 @@ class QueryBuilder extends \yii\base\Object */ public function renameColumn($table, $name, $newName) { - return "ALTER TABLE " . $this->schema->quoteTableName($table) - . " RENAME COLUMN " . $this->schema->quoteSimpleColumnName($name) - . " TO " . $this->schema->quoteSimpleColumnName($newName); + return "ALTER TABLE " . $this->driver->quoteTableName($table) + . " RENAME COLUMN " . $this->driver->quoteSimpleColumnName($name) + . " TO " . $this->driver->quoteSimpleColumnName($newName); } /** @@ -295,9 +295,9 @@ class QueryBuilder extends \yii\base\Object */ public function alterColumn($table, $column, $type) { - return 'ALTER TABLE ' . $this->schema->quoteTableName($table) . ' CHANGE ' - . $this->schema->quoteSimpleColumnName($column) . ' ' - . $this->schema->quoteSimpleColumnName($column) . ' ' + return 'ALTER TABLE ' . $this->driver->quoteTableName($table) . ' CHANGE ' + . $this->driver->quoteSimpleColumnName($column) . ' ' + . $this->driver->quoteSimpleColumnName($column) . ' ' . $this->getColumnType($type); } @@ -317,16 +317,16 @@ class QueryBuilder extends \yii\base\Object { $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); 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); foreach ($refColumns as $i => $col) { - $refColumns[$i] = $this->schema->quoteColumnName($col); + $refColumns[$i] = $this->driver->quoteColumnName($col); } - $sql = 'ALTER TABLE ' . $this->schema->quoteTableName($table) - . ' ADD CONSTRAINT ' . $this->schema->quoteColumnName($name) + $sql = 'ALTER TABLE ' . $this->driver->quoteTableName($table) + . ' ADD CONSTRAINT ' . $this->driver->quoteColumnName($name) . ' FOREIGN KEY (' . implode(', ', $columns) . ')' - . ' REFERENCES ' . $this->schema->quoteTableName($refTable) + . ' REFERENCES ' . $this->driver->quoteTableName($refTable) . ' (' . implode(', ', $refColumns) . ')'; if ($delete !== null) { $sql .= ' ON DELETE ' . $delete; @@ -345,8 +345,8 @@ class QueryBuilder extends \yii\base\Object */ public function dropForeignKey($name, $table) { - return 'ALTER TABLE ' . $this->schema->quoteTableName($table) - . ' DROP CONSTRAINT ' . $this->schema->quoteColumnName($name); + return 'ALTER TABLE ' . $this->driver->quoteTableName($table) + . ' DROP CONSTRAINT ' . $this->driver->quoteColumnName($name); } /** @@ -368,12 +368,12 @@ class QueryBuilder extends \yii\base\Object $cols[] = $col; } else { - $cols[] = $this->schema->quoteColumnName($col); + $cols[] = $this->driver->quoteColumnName($col); } } return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') - . $this->schema->quoteTableName($name) . ' ON ' - . $this->schema->quoteTableName($table) . ' (' . implode(', ', $cols) . ')'; + . $this->driver->quoteTableName($name) . ' ON ' + . $this->driver->quoteTableName($table) . ' (' . implode(', ', $cols) . ')'; } /** @@ -384,7 +384,7 @@ class QueryBuilder extends \yii\base\Object */ 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 $columns[$i] = (string)$column; } elseif (strpos($column, '(') === false) { 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 { $columns[$i] = $this->connection->quoteColumnName($column); } diff --git a/framework/db/dao/mysql/Schema.php b/framework/db/dao/mysql/Driver.php similarity index 94% rename from framework/db/dao/mysql/Schema.php rename to framework/db/dao/mysql/Driver.php index 0f28e98..08f4f7f 100644 --- a/framework/db/dao/mysql/Schema.php +++ b/framework/db/dao/mysql/Driver.php @@ -1,6 +1,6 @@ * @link http://www.yiiframework.com/ @@ -13,16 +13,16 @@ namespace yii\db\dao\mysql; 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 * @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. - * A simple table name does not schema prefix. + * A simple table name has no schema prefix. * @param string $name table name * @return string the properly quoted table name */ @@ -33,7 +33,7 @@ class Schema extends \yii\db\dao\Schema /** * 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 * @return string the properly quoted column name */ diff --git a/framework/db/dao/mysql/QueryBuilder.php b/framework/db/dao/mysql/QueryBuilder.php index 96ac877..100db2d 100644 --- a/framework/db/dao/mysql/QueryBuilder.php +++ b/framework/db/dao/mysql/QueryBuilder.php @@ -48,7 +48,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder */ 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(); if ($row === false) 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 if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) { foreach ($matches[1] as $i => $c) { if ($c === $name) { - return "ALTER TABLE $quotedTable CHANGE " . $this->schema->quoteColumnName($name) - . ' ' . $this->schema->quoteColumnName($newName) . ' ' . $matches[2][$i]; + return "ALTER TABLE $quotedTable CHANGE " . $this->driver->quoteColumnName($name) + . ' ' . $this->driver->quoteColumnName($newName) . ' ' . $matches[2][$i]; } } } // 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 */ public function dropForeignKey($name, $table) { - return 'ALTER TABLE ' . $this->schema->quoteTableName($table) - . ' DROP FOREIGN KEY ' . $this->schema->quoteColumnName($name); + return 'ALTER TABLE ' . $this->driver->quoteTableName($table) + . ' DROP FOREIGN KEY ' . $this->driver->quoteColumnName($name); } } diff --git a/upgrade.md b/upgrade.md index f93c4de..d38beba 100644 --- a/upgrade.md +++ b/upgrade.md @@ -24,6 +24,7 @@ Upgrading from v1.1.x --------------------- - All framework classes are now namespaced, and the name prefix `C` is removed. + - 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. @@ -36,4 +37,7 @@ Upgrading from v1.1.x - `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.