Commit abd3d827 by Qiang Xue

refactoring.

parent 7f6a5568
......@@ -39,7 +39,7 @@ use yii\db\Query;
*
* Please refer to [[Cache]] for common cache operations that are supported by DbCache.
*
* @property Connection $dbConnection The DB connection instance.
* @property Connection $db The DB connection instance.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -71,7 +71,7 @@ class DbCache extends Cache
* @return Connection the DB connection instance
* @throws Exception if [[connectionID]] does not point to a valid application component.
*/
public function getDbConnection()
public function getDb()
{
if ($this->_db === null) {
$db = \Yii::$application->getComponent($this->connectionID);
......@@ -88,7 +88,7 @@ class DbCache extends Cache
* Sets the DB connection used by the cache component.
* @param Connection $value the DB connection instance
*/
public function setDbConnection($value)
public function setDb($value)
{
$this->_db = $value;
}
......@@ -105,7 +105,7 @@ class DbCache extends Cache
$query->select(array('data'))
->from($this->cacheTableName)
->where('id = :id AND (expire = 0 OR expire > :time)', array(':id' => $key, ':time' => time()));
$db = $this->getDbConnection();
$db = $this->getDb();
if ($db->enableQueryCache) {
// temporarily disable and re-enable query caching
$db->enableQueryCache = false;
......@@ -133,7 +133,7 @@ class DbCache extends Cache
->where(array('id' => $keys))
->andWhere("expire = 0 OR expire > " . time() . ")");
$db = $this->getDbConnection();
$db = $this->getDb();
if ($db->enableQueryCache) {
$db->enableQueryCache = false;
$rows = $query->createCommand($db)->queryAll();
......@@ -169,7 +169,7 @@ class DbCache extends Cache
'data' => array($value, \PDO::PARAM_LOB),
), array(
'id' => $key,
))->createCommand($this->getDbConnection());
))->createCommand($this->getDb());
if ($command->execute()) {
$this->gc();
......@@ -203,7 +203,7 @@ class DbCache extends Cache
'id' => $key,
'expire' => $expire,
'data' => array($value, \PDO::PARAM_LOB),
))->createCommand($this->getDbConnection());
))->createCommand($this->getDb());
try {
$command->execute();
return true;
......@@ -222,7 +222,7 @@ class DbCache extends Cache
{
$query = new Query;
$query->delete($this->cacheTableName, array('id' => $key))
->createCommand($this->getDbConnection())
->createCommand($this->getDb())
->execute();
return true;
}
......@@ -237,7 +237,7 @@ class DbCache extends Cache
if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$query = new Query;
$query->delete($this->cacheTableName, 'expire > 0 AND expire < ' . time())
->createCommand($this->getDbConnection())
->createCommand($this->getDb())
->execute();
}
}
......@@ -251,7 +251,7 @@ class DbCache extends Cache
{
$query = new Query;
$query->delete($this->cacheTableName)
->createCommand($this->getDbConnection())
->createCommand($this->getDb())
->execute();
return true;
}
......
......@@ -67,7 +67,7 @@ class DbDependency extends Dependency
*/
protected function generateDependencyData()
{
$db = $this->getDbConnection();
$db = $this->getDb();
$command = $this->query->createCommand($db);
if ($db->enableQueryCache) {
// temporarily disable and re-enable query caching
......@@ -85,7 +85,7 @@ class DbDependency extends Dependency
* @return Connection the DB connection instance
* @throws Exception if [[connectionID]] does not point to a valid application component.
*/
public function getDbConnection()
public function getDb()
{
if ($this->_db === null) {
$db = \Yii::$application->getComponent($this->connectionID);
......@@ -102,7 +102,7 @@ class DbDependency extends Dependency
* Sets the DB connection used by the cache component.
* @param Connection $value the DB connection instance
*/
public function setDbConnection($value)
public function setDb($value)
{
$this->_db = $value;
}
......
......@@ -278,7 +278,7 @@ class MigrateController extends Controller
else
die("Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n");
$db=$this->getDbConnection();
$db=$this->getDb();
// try mark up
$migrations=$this->getNewMigrations();
......@@ -405,7 +405,7 @@ class MigrateController extends Controller
$migration=$this->instantiateMigration($class);
if($migration->up()!==false)
{
$this->getDbConnection()->createCommand()->insert($this->migrationTable, array(
$this->getDb()->createCommand()->insert($this->migrationTable, array(
'version'=>$class,
'apply_time'=>time(),
));
......@@ -430,7 +430,7 @@ class MigrateController extends Controller
$migration=$this->instantiateMigration($class);
if($migration->down()!==false)
{
$db=$this->getDbConnection();
$db=$this->getDb();
$db->createCommand()->delete($this->migrationTable, $db->quoteColumnName('version').'=:version', array(':version'=>$class));
$time=microtime(true)-$start;
echo "*** reverted $class (time: ".sprintf("%.3f",$time)."s)\n\n";
......@@ -448,7 +448,7 @@ class MigrateController extends Controller
$file=$this->migrationPath.DIRECTORY_SEPARATOR.$class.'.php';
require_once($file);
$migration=new $class;
$migration->setDbConnection($this->getDbConnection());
$migration->setDb($this->getDb());
return $migration;
}
......@@ -456,7 +456,7 @@ class MigrateController extends Controller
* @var CDbConnection
*/
private $_db;
protected function getDbConnection()
protected function getDb()
{
if($this->_db!==null)
return $this->_db;
......@@ -468,7 +468,7 @@ class MigrateController extends Controller
protected function getMigrationHistory($limit)
{
$db=$this->getDbConnection();
$db=$this->getDb();
if($db->schema->getTable($this->migrationTable)===null)
{
$this->createMigrationHistoryTable();
......@@ -483,7 +483,7 @@ class MigrateController extends Controller
protected function createMigrationHistoryTable()
{
$db=$this->getDbConnection();
$db=$this->getDb();
echo 'Creating migration history table "'.$this->migrationTable.'"...';
$db->createCommand()->createTable($this->migrationTable,array(
'version'=>'string NOT NULL PRIMARY KEY',
......
......@@ -232,7 +232,7 @@ class ActiveQuery extends Query
/** @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
if ($db === null) {
$db = $modelClass::getDbConnection();
$db = $modelClass::getDb();
}
if ($this->sql === null) {
if ($this->from === null) {
......
......@@ -24,7 +24,7 @@ use yii\util\StringHelper;
*
* @include @yii/db/ActiveRecord.md
*
* @property Connection $dbConnection the database connection used by this AR class.
* @property Connection $db the database connection used by this AR class.
* @property TableSchema $tableSchema the schema information of the DB table associated with this AR class.
* @property array $oldAttributes the old attribute values (name-value pairs).
* @property array $dirtyAttributes the changed attribute values (name-value pairs).
......@@ -69,7 +69,7 @@ class ActiveRecord extends Model
* You may override this method if you want to use a different database connection.
* @return Connection the database connection used by this AR class.
*/
public static function getDbConnection()
public static function getDb()
{
return \Yii::$application->getDb();
}
......@@ -145,7 +145,7 @@ class ActiveRecord extends Model
*/
public static function updateAll($attributes, $condition = '', $params = array())
{
$command = static::getDbConnection()->createCommand();
$command = static::getDb()->createCommand();
$command->update(static::tableName(), $attributes, $condition, $params);
return $command->execute();
}
......@@ -168,7 +168,7 @@ class ActiveRecord extends Model
*/
public static function updateAllCounters($counters, $condition = '', $params = array())
{
$db = static::getDbConnection();
$db = static::getDb();
$n = 0;
foreach ($counters as $name => $value) {
$quotedName = $db->quoteColumnName($name);
......@@ -198,7 +198,7 @@ class ActiveRecord extends Model
*/
public static function deleteAll($condition = '', $params = array())
{
$command = static::getDbConnection()->createCommand();
$command = static::getDb()->createCommand();
$command->delete(static::tableName(), $condition, $params);
return $command->execute();
}
......@@ -235,7 +235,7 @@ class ActiveRecord extends Model
*/
public static function getTableSchema()
{
return static::getDbConnection()->getTableSchema(static::tableName());
return static::getDb()->getTableSchema(static::tableName());
}
/**
......@@ -618,7 +618,7 @@ class ActiveRecord extends Model
$values[$key] = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null;
}
}
$db = static::getDbConnection();
$db = static::getDb();
$command = $db->createCommand()->insert($this->tableName(), $values);
if ($command->execute()) {
$table = $this->getTableSchema();
......@@ -1079,7 +1079,7 @@ class ActiveRecord extends Model
foreach ($extraColumns as $k => $v) {
$columns[$k] = $v;
}
static::getDbConnection()->createCommand()
static::getDb()->createCommand()
->insert($viaTable, $columns)->execute();
} else {
$p1 = $model->isPrimaryKey(array_keys($relation->link));
......@@ -1150,7 +1150,7 @@ class ActiveRecord extends Model
foreach ($relation->link as $a => $b) {
$columns[$b] = $model->$a;
}
$command = static::getDbConnection()->createCommand();
$command = static::getDb()->createCommand();
if ($delete) {
$command->delete($viaTable, $columns)->execute();
} else {
......
......@@ -299,7 +299,7 @@ class ActiveRelation extends ActiveQuery
$this->filterByModels($primaryModels);
/** @var $primaryModel ActiveRecord */
$primaryModel = reset($primaryModels);
$db = $primaryModel->getDbConnection();
$db = $primaryModel->getDb();
$sql = $db->getQueryBuilder()->build($this);
return $db->createCommand($sql, $this->params)->queryAll();
}
......
......@@ -54,7 +54,7 @@ class Command extends \yii\base\Component
/**
* @var Connection the DB connection that this command is associated with
*/
public $connection;
public $db;
/**
* @var \PDOStatement the PDOStatement object that this command is associated with
*/
......@@ -91,7 +91,7 @@ class Command extends \yii\base\Component
public function setSql($sql)
{
if ($sql !== $this->_sql) {
if ($this->connection->enableAutoQuoting && $sql != '') {
if ($this->db->enableAutoQuoting && $sql != '') {
$sql = $this->expandSql($sql);
}
$this->cancel();
......@@ -108,12 +108,13 @@ class Command extends \yii\base\Component
*/
protected function expandSql($sql)
{
return preg_replace_callback('/(\\{\\{(.*?)\\}\\}|\\[\\[(.*?)\\]\\])/', function($matches) {
$db = $this->db;
return preg_replace_callback('/(\\{\\{(.*?)\\}\\}|\\[\\[(.*?)\\]\\])/', function($matches) use($db) {
if (isset($matches[3])) {
return $this->connection->quoteColumnName($matches[3]);
return $db->quoteColumnName($matches[3]);
} else {
$name = str_replace('%', $this->connection->tablePrefix, $matches[2]);
return $this->connection->quoteTableName($name);
$name = str_replace('%', $db->tablePrefix, $matches[2]);
return $db->quoteTableName($name);
}
}, $sql);
}
......@@ -131,7 +132,7 @@ class Command extends \yii\base\Component
if ($this->pdoStatement == null) {
$sql = $this->getSql();
try {
$this->pdoStatement = $this->connection->pdo->prepare($sql);
$this->pdoStatement = $this->db->pdo->prepare($sql);
} catch (\Exception $e) {
\Yii::error($e->getMessage() . "\nFailed to prepare SQL: $sql", __CLASS__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
......@@ -276,7 +277,7 @@ class Command extends \yii\base\Component
}
try {
if ($this->connection->enableProfiling) {
if ($this->db->enableProfiling) {
\Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
}
......@@ -288,12 +289,12 @@ class Command extends \yii\base\Component
}
$n = $this->pdoStatement->rowCount();
if ($this->connection->enableProfiling) {
if ($this->db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
return $n;
} catch (\Exception $e) {
if ($this->connection->enableProfiling) {
if ($this->db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
$message = $e->getMessage();
......@@ -400,7 +401,7 @@ class Command extends \yii\base\Component
*/
private function queryInternal($method, $params, $fetchMode = null)
{
$db = $this->connection;
$db = $this->db;
$sql = $this->getSql();
$this->_params = array_merge($this->_params, $params);
if ($this->_params === array()) {
......@@ -489,7 +490,7 @@ class Command extends \yii\base\Component
*/
public function insert($table, $columns, $params = array())
{
$sql = $this->connection->getQueryBuilder()->insert($table, $columns, $params);
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
return $this->setSql($sql)->bindValues($params);
}
......@@ -516,7 +517,7 @@ class Command extends \yii\base\Component
*/
public function update($table, $columns, $condition = '', $params = array())
{
$sql = $this->connection->getQueryBuilder()->update($table, $columns, $condition, $params);
$sql = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params);
return $this->setSql($sql)->bindValues($params);
}
......@@ -540,7 +541,7 @@ class Command extends \yii\base\Component
*/
public function delete($table, $condition = '', $params = array())
{
$sql = $this->connection->getQueryBuilder()->delete($table, $condition);
$sql = $this->db->getQueryBuilder()->delete($table, $condition);
return $this->setSql($sql)->bindValues($params);
}
......@@ -551,7 +552,7 @@ class Command extends \yii\base\Component
* 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 method [[\yii\db\QueryBuilder::getColumnType()]] will be called
* The method [[QueryBuilder::getColumnType()]] will be called
* to convert the abstract column types to physical ones. For example, `string` will be converted
* as `varchar(255)`, and `string not null` becomes `varchar(255) not null`.
*
......@@ -565,7 +566,7 @@ class Command extends \yii\base\Component
*/
public function createTable($table, $columns, $options = null)
{
$sql = $this->connection->getQueryBuilder()->createTable($table, $columns, $options);
$sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options);
return $this->setSql($sql);
}
......@@ -577,7 +578,7 @@ class Command extends \yii\base\Component
*/
public function renameTable($table, $newName)
{
$sql = $this->connection->getQueryBuilder()->renameTable($table, $newName);
$sql = $this->db->getQueryBuilder()->renameTable($table, $newName);
return $this->setSql($sql);
}
......@@ -588,7 +589,7 @@ class Command extends \yii\base\Component
*/
public function dropTable($table)
{
$sql = $this->connection->getQueryBuilder()->dropTable($table);
$sql = $this->db->getQueryBuilder()->dropTable($table);
return $this->setSql($sql);
}
......@@ -599,7 +600,7 @@ class Command extends \yii\base\Component
*/
public function truncateTable($table)
{
$sql = $this->connection->getQueryBuilder()->truncateTable($table);
$sql = $this->db->getQueryBuilder()->truncateTable($table);
return $this->setSql($sql);
}
......@@ -614,7 +615,7 @@ class Command extends \yii\base\Component
*/
public function addColumn($table, $column, $type)
{
$sql = $this->connection->getQueryBuilder()->addColumn($table, $column, $type);
$sql = $this->db->getQueryBuilder()->addColumn($table, $column, $type);
return $this->setSql($sql);
}
......@@ -626,7 +627,7 @@ class Command extends \yii\base\Component
*/
public function dropColumn($table, $column)
{
$sql = $this->connection->getQueryBuilder()->dropColumn($table, $column);
$sql = $this->db->getQueryBuilder()->dropColumn($table, $column);
return $this->setSql($sql);
}
......@@ -639,7 +640,7 @@ class Command extends \yii\base\Component
*/
public function renameColumn($table, $oldName, $newName)
{
$sql = $this->connection->getQueryBuilder()->renameColumn($table, $oldName, $newName);
$sql = $this->db->getQueryBuilder()->renameColumn($table, $oldName, $newName);
return $this->setSql($sql);
}
......@@ -654,7 +655,7 @@ class Command extends \yii\base\Component
*/
public function alterColumn($table, $column, $type)
{
$sql = $this->connection->getQueryBuilder()->alterColumn($table, $column, $type);
$sql = $this->db->getQueryBuilder()->alterColumn($table, $column, $type);
return $this->setSql($sql);
}
......@@ -672,7 +673,7 @@ class Command extends \yii\base\Component
*/
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
$sql = $this->connection->getQueryBuilder()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update);
$sql = $this->db->getQueryBuilder()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update);
return $this->setSql($sql);
}
......@@ -684,7 +685,7 @@ class Command extends \yii\base\Component
*/
public function dropForeignKey($name, $table)
{
$sql = $this->connection->getQueryBuilder()->dropForeignKey($name, $table);
$sql = $this->db->getQueryBuilder()->dropForeignKey($name, $table);
return $this->setSql($sql);
}
......@@ -699,7 +700,7 @@ class Command extends \yii\base\Component
*/
public function createIndex($name, $table, $columns, $unique = false)
{
$sql = $this->connection->getQueryBuilder()->createIndex($name, $table, $columns, $unique);
$sql = $this->db->getQueryBuilder()->createIndex($name, $table, $columns, $unique);
return $this->setSql($sql);
}
......@@ -711,7 +712,7 @@ class Command extends \yii\base\Component
*/
public function dropIndex($name, $table)
{
$sql = $this->connection->getQueryBuilder()->dropIndex($name, $table);
$sql = $this->db->getQueryBuilder()->dropIndex($name, $table);
return $this->setSql($sql);
}
......@@ -727,7 +728,7 @@ class Command extends \yii\base\Component
*/
public function resetSequence($table, $value = null)
{
$sql = $this->connection->getQueryBuilder()->resetSequence($table, $value);
$sql = $this->db->getQueryBuilder()->resetSequence($table, $value);
return $this->setSql($sql);
}
......@@ -741,7 +742,7 @@ class Command extends \yii\base\Component
*/
public function checkIntegrity($check = true, $schema = '')
{
$sql = $this->connection->getQueryBuilder()->checkIntegrity($check, $schema);
$sql = $this->db->getQueryBuilder()->checkIntegrity($check, $schema);
return $this->setSql($sql);
}
}
......@@ -395,7 +395,7 @@ class Connection extends \yii\base\ApplicationComponent
{
$this->open();
$command = new Command(array(
'connection' => $this,
'db' => $this,
'sql' => $sql,
));
return $command->bindValues($params);
......@@ -418,7 +418,7 @@ class Connection extends \yii\base\ApplicationComponent
{
$this->open();
$this->_transaction = new Transaction(array(
'connection' => $this,
'db' => $this,
));
$this->_transaction->begin();
return $this->_transaction;
......@@ -437,7 +437,7 @@ class Connection extends \yii\base\ApplicationComponent
$driver = $this->getDriverName();
if (isset($this->schemaMap[$driver])) {
$this->_schema = \Yii::createObject($this->schemaMap[$driver]);
$this->_schema->connection = $this;
$this->_schema->db = $this;
return $this->_schema;
} else {
throw new NotSupportedException("Connection does not support reading schema information for '$driver' DBMS.");
......
......@@ -48,7 +48,7 @@ abstract class Schema extends \yii\base\Object
/**
* @var Connection the database connection
*/
public $connection;
public $db;
/**
* @var array list of ALL table names in the database
*/
......@@ -82,7 +82,7 @@ abstract class Schema extends \yii\base\Object
return $this->_tables[$name];
}
$db = $this->connection;
$db = $this->db;
$realName = $this->getRealTableName($name);
/** @var $cache \yii\caching\Cache */
......@@ -109,7 +109,7 @@ abstract class Schema extends \yii\base\Object
*/
public function getCacheKey($name)
{
return __CLASS__ . "/{$this->connection->dsn}/{$this->connection->username}/{$name}";
return __CLASS__ . "/{$this->db->dsn}/{$this->db->username}/{$name}";
}
/**
......@@ -169,7 +169,7 @@ abstract class Schema extends \yii\base\Object
public function refresh()
{
/** @var $cache \yii\caching\Cache */
if ($this->connection->enableSchemaCache && ($cache = \Yii::$application->getComponent($this->connection->schemaCacheID)) !== null) {
if ($this->db->enableSchemaCache && ($cache = \Yii::$application->getComponent($this->db->schemaCacheID)) !== null) {
foreach ($this->_tables as $name => $table) {
$cache->delete($this->getCacheKey($name));
}
......@@ -185,7 +185,7 @@ abstract class Schema extends \yii\base\Object
*/
public function createQueryBuilder()
{
return new QueryBuilder($this->connection);
return new QueryBuilder($this->db);
}
/**
......@@ -210,8 +210,8 @@ abstract class Schema extends \yii\base\Object
*/
public function getLastInsertID($sequenceName = '')
{
if ($this->connection->isActive) {
return $this->connection->pdo->lastInsertId($sequenceName);
if ($this->db->isActive) {
return $this->db->pdo->lastInsertId($sequenceName);
} else {
throw new InvalidCallException('DB Connection is not active.');
}
......@@ -230,8 +230,8 @@ abstract class Schema extends \yii\base\Object
return $str;
}
$this->connection->open();
if (($value = $this->connection->pdo->quote($str)) !== false) {
$this->db->open();
if (($value = $this->db->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") . "'";
......@@ -319,9 +319,9 @@ abstract class Schema extends \yii\base\Object
*/
public function getRealTableName($name)
{
if ($this->connection->enableAutoQuoting && strpos($name, '{{') !== false) {
if ($this->db->enableAutoQuoting && strpos($name, '{{') !== false) {
$name = preg_replace('/\\{\\{(.*?)\\}\\}/', '\1', $name);
return str_replace('%', $this->connection->tablePrefix, $name);
return str_replace('%', $this->db->tablePrefix, $name);
} else {
return $name;
}
......
......@@ -41,7 +41,7 @@ class Transaction extends \yii\base\Object
/**
* @var Connection the database connection that this transaction is associated with.
*/
public $connection;
public $db;
/**
* @var boolean whether this transaction is active. Only an active transaction
* can [[commit()]] or [[rollBack()]]. This property is set true when the transaction is started.
......@@ -65,12 +65,12 @@ class Transaction extends \yii\base\Object
public function begin()
{
if (!$this->_active) {
if ($this->connection === null) {
throw new InvalidConfigException('Transaction.connection must be set.');
if ($this->db === null) {
throw new InvalidConfigException('Transaction::db must be set.');
}
\Yii::trace('Starting transaction', __CLASS__);
$this->connection->open();
$this->connection->pdo->beginTransaction();
$this->db->open();
$this->db->pdo->beginTransaction();
$this->_active = true;
}
}
......@@ -81,9 +81,9 @@ class Transaction extends \yii\base\Object
*/
public function commit()
{
if ($this->_active && $this->connection && $this->connection->isActive) {
if ($this->_active && $this->db && $this->db->isActive) {
\Yii::trace('Committing transaction', __CLASS__);
$this->connection->pdo->commit();
$this->db->pdo->commit();
$this->_active = false;
} else {
throw new Exception('Failed to commit transaction: transaction was inactive.');
......@@ -96,9 +96,9 @@ class Transaction extends \yii\base\Object
*/
public function rollback()
{
if ($this->_active && $this->connection && $this->connection->isActive) {
if ($this->_active && $this->db && $this->db->isActive) {
\Yii::trace('Rolling back transaction', __CLASS__);
$this->connection->pdo->commit();
$this->db->pdo->commit();
$this->_active = false;
} else {
throw new Exception('Failed to roll back transaction: transaction was inactive.');
......
......@@ -51,8 +51,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
public function renameColumn($table, $oldName, $newName)
{
$quotedTable = $this->connection->quoteTableName($table);
$row = $this->connection->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryRow();
$quotedTable = $this->db->quoteTableName($table);
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryRow();
if ($row === false) {
throw new Exception("Unable to find '$oldName' in table '$table'.");
}
......@@ -66,16 +66,16 @@ class QueryBuilder extends \yii\db\QueryBuilder
foreach ($matches[1] as $i => $c) {
if ($c === $oldName) {
return "ALTER TABLE $quotedTable CHANGE "
. $this->connection->quoteColumnName($oldName) . ' '
. $this->connection->quoteColumnName($newName) . ' '
. $this->db->quoteColumnName($oldName) . ' '
. $this->db->quoteColumnName($newName) . ' '
. $matches[2][$i];
}
}
}
// try to give back a SQL anyway
return "ALTER TABLE $quotedTable CHANGE "
. $this->connection->quoteColumnName($oldName) . ' '
. $this->connection->quoteColumnName($newName);
. $this->db->quoteColumnName($oldName) . ' '
. $this->db->quoteColumnName($newName);
}
/**
......@@ -86,8 +86,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
public function dropForeignKey($name, $table)
{
return 'ALTER TABLE ' . $this->connection->quoteTableName($table)
. ' DROP FOREIGN KEY ' . $this->connection->quoteColumnName($name);
return 'ALTER TABLE ' . $this->db->quoteTableName($table)
. ' DROP FOREIGN KEY ' . $this->db->quoteColumnName($name);
}
/**
......@@ -102,12 +102,12 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
public function resetSequence($tableName, $value = null)
{
$table = $this->connection->getTableSchema($tableName);
$table = $this->db->getTableSchema($tableName);
if ($table !== null && $table->sequenceName !== null) {
$tableName = $this->connection->quoteTableName($tableName);
$tableName = $this->db->quoteTableName($tableName);
if ($value === null) {
$key = reset($table->primaryKey);
$value = $this->connection->createCommand("SELECT MAX(`$key`) FROM $tableName")->queryScalar() + 1;
$value = $this->db->createCommand("SELECT MAX(`$key`) FROM $tableName")->queryScalar() + 1;
} else {
$value = (int)$value;
}
......
<?php
/**
* Driver class file.
* Schema class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
......@@ -13,7 +13,7 @@ use yii\db\TableSchema;
use yii\db\ColumnSchema;
/**
* Driver is the class for retrieving metadata from a MySQL database (version 4.1.x and 5.x).
* Schema is the class for retrieving metadata from a MySQL database (version 4.1.x and 5.x).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -79,7 +79,7 @@ class Schema extends \yii\db\Schema
*/
public function createQueryBuilder()
{
return new QueryBuilder($this->connection);
return new QueryBuilder($this->db);
}
/**
......@@ -183,9 +183,9 @@ class Schema extends \yii\db\Schema
*/
protected function findColumns($table)
{
$sql = 'SHOW COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
try {
$columns = $this->connection->createCommand($sql)->queryAll();
$columns = $this->db->createCommand($sql)->queryAll();
} catch (\Exception $e) {
return false;
}
......@@ -208,7 +208,7 @@ class Schema extends \yii\db\Schema
*/
protected function findConstraints($table)
{
$row = $this->connection->createCommand('SHOW CREATE TABLE ' . $this->quoteSimpleTableName($table->name))->queryRow();
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->quoteSimpleTableName($table->name))->queryRow();
if (isset($row['Create Table'])) {
$sql = $row['Create Table'];
} else {
......@@ -243,6 +243,6 @@ class Schema extends \yii\db\Schema
if ($schema !== '') {
$sql .= ' FROM ' . $this->quoteSimpleTableName($schema);
}
return $this->connection->createCommand($sql)->queryColumn();
return $this->db->createCommand($sql)->queryColumn();
}
}
......@@ -54,7 +54,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
public function resetSequence($tableName, $value = null)
{
$db = $this->connection;
$db = $this->db;
$table = $db->getTableSchema($tableName);
if ($table !== null && $table->sequenceName !== null) {
if ($value === null) {
......@@ -94,7 +94,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
public function truncateTable($table)
{
return "DELETE FROM " . $this->connection->quoteTableName($table);
return "DELETE FROM " . $this->db->quoteTableName($table);
}
/**
......@@ -105,7 +105,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
public function dropIndex($name, $table)
{
return 'DROP INDEX ' . $this->connection->quoteTableName($name);
return 'DROP INDEX ' . $this->db->quoteTableName($name);
}
/**
......
<?php
/**
* Driver class file.
* Schema class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
......@@ -13,7 +13,7 @@ use yii\db\TableSchema;
use yii\db\ColumnSchema;
/**
* Driver is the class for retrieving metadata from a SQLite (2/3) database.
* Schema is the class for retrieving metadata from a SQLite (2/3) database.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -58,7 +58,7 @@ class Schema extends \yii\db\Schema
*/
public function createQueryBuilder()
{
return new QueryBuilder($this->connection);
return new QueryBuilder($this->db);
}
/**
......@@ -70,7 +70,7 @@ class Schema extends \yii\db\Schema
protected function findTableNames($schema = '')
{
$sql = "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'";
return $this->connection->createCommand($sql)->queryColumn();
return $this->db->createCommand($sql)->queryColumn();
}
/**
......@@ -99,7 +99,7 @@ class Schema extends \yii\db\Schema
protected function findColumns($table)
{
$sql = "PRAGMA table_info(" . $this->quoteSimpleTableName($table->name) . ')';
$columns = $this->connection->createCommand($sql)->queryAll();
$columns = $this->db->createCommand($sql)->queryAll();
if (empty($columns)) {
return false;
}
......@@ -126,7 +126,7 @@ class Schema extends \yii\db\Schema
protected function findConstraints($table)
{
$sql = "PRAGMA foreign_key_list(" . $this->quoteSimpleTableName($table->name) . ')';
$keys = $this->connection->createCommand($sql)->queryAll();
$keys = $this->db->createCommand($sql)->queryAll();
foreach ($keys as $key) {
$table->foreignKeys[] = array($key['table'], $key['from'] => $key['to']);
}
......
......@@ -65,7 +65,7 @@ class DbTarget extends Target
* @return \yii\db\Connection the DB connection instance
* @throws \yii\base\Exception if [[connectionID]] does not refer to a valid application component ID.
*/
public function getDbConnection()
public function getDb()
{
if ($this->_db === null) {
$this->_db = \Yii::$application->getComponent($this->connectionID);
......@@ -85,7 +85,7 @@ class DbTarget extends Target
$sql = "INSERT INTO {$this->tableName}
(level, category, log_time, message) VALUES
(:level, :category, :log_time, :message)";
$command = $this->getDbConnection()->createCommand($sql);
$command = $this->getDb()->createCommand($sql);
foreach ($this->messages as $message) {
$command->bindValues(array(
':level' => $message[1],
......
......@@ -21,7 +21,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
{
public static $db;
public static function getDbConnection()
public static function getDb()
{
return self::$db;
}
......
......@@ -20,11 +20,8 @@
* a way to invalidate/clear cached data
* a command to clear cached data
- db
* sqlite, pgsql, sql server, oracle, db2 drivers
* pgsql, sql server, oracle, db2 drivers
* write a guide on creating own schema definitions
* AR
* saving related records
* collection support for results
* document-based (should allow storage-specific methods additionally to generic ones)
* mongodb
* key-value-based (should allow storage-specific methods additionally to generic ones)
......@@ -61,4 +58,3 @@
* if we're going to supply default ones, these should generate really unique IDs. This will solve a lot of AJAX-nesting problems.
- Make sure type hinting is used when components are passed to methods
- Decouple controller from application (by passing web application instance to controller and if not passed, using Yii::app())?
- Decouple view renderer from controller so it can be used separately (useful for sending emails from console etc.)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment