Commit c63c7838 by Qiang Xue

...

parent 730dc3db
......@@ -64,7 +64,7 @@ class YiiBase
/**
* @var yii\base\Application the application instance
*/
public static $app;
public static $application;
/**
* @var array registered path aliases
* @see getAlias
......
......@@ -116,7 +116,7 @@ abstract class Application extends Module
*/
public function __construct($config = null)
{
\Yii::$app = $this;
\Yii::$application = $this;
// set basePath at early as possible to avoid trouble
if (is_string($config)) {
......
......@@ -30,7 +30,7 @@ abstract class ApplicationComponent extends Component implements Initable
/**
* Initializes the application component.
* This method is invoked after the component is created and its property values are
* initialized. The default implementation will call [[Component::attachBehaviors]]
* initialized. The default implementation will call [[Component::attachBehaviors()]]
* to attach behaviors declared in [[behaviors]].
* If you override this method, make sure to call the parent implementation.
*/
......
......@@ -294,7 +294,7 @@ class Object
* $model->init();
* ~~~
*
* @return object the created object
* @return Object the created object
* @throws Exception if the configuration is invalid.
*/
public static function newInstance()
......
......@@ -120,7 +120,7 @@ abstract class CDbMigration extends yii\base\Component
public function getDbConnection()
{
if ($this->_db === null) {
$this->_db = Yii::app()->getComponent('db');
$this->_db = \Yii::$application->getComponent('db');
if (!$this->_db instanceof CDbConnection)
throw new CException(Yii::t('yii', 'The "db" application component must be configured to be a CDbConnection object.'));
}
......
......@@ -611,7 +611,7 @@ abstract class ActiveRecord extends \yii\base\Model
return self::$db;
else
{
self::$db = Yii::app()->getDb();
self::$db = \Yii::$application->getDb();
if (self::$db instanceof CDbConnection)
return self::$db;
else
......
......@@ -23,7 +23,7 @@ class ColumnSchema extends \yii\base\Component
*/
public $name;
/**
* @var string raw name of this column. This is the quoted name that can be used in SQL queries.
* @var string raw name of this column. This is the quoted name that can be directly used in SQL queries.
*/
public $quotedName;
/**
......@@ -32,7 +32,8 @@ class ColumnSchema extends \yii\base\Component
public $allowNull;
/**
* @var string logical type of this column. Possible logic types include:
* string, text, boolean, smallint, integer, bigint, float, decimal, datetime, timestamp, time, date, binary, money
* string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
* timestamp, time, date, binary, and money.
*/
public $type;
/**
......@@ -41,7 +42,7 @@ class ColumnSchema extends \yii\base\Component
*/
public $phpType;
/**
* @var string the DB type of this column.
* @var string the DB type of this column. Possible DB types vary according to the DBMS.
*/
public $dbType;
/**
......
......@@ -40,6 +40,10 @@ class QueryBuilder extends \yii\base\Object
$this->schema = $schema;
}
/**
* @param Query $query
* @return string
*/
public function build($query)
{
$clauses = array(
......@@ -126,7 +130,6 @@ class QueryBuilder extends \yii\base\Object
* @param string $table the table where the data will be deleted from.
* @param mixed $conditions the conditions that will be put in the WHERE part. Please
* refer to {@link where} on how to specify conditions.
* @param array $params the parameters to be bound to the query.
* @return integer number of rows affected by the execution.
*/
public function delete($table, $conditions = '')
......@@ -440,6 +443,10 @@ class QueryBuilder extends \yii\base\Object
protected function buildFrom($query)
{
if (empty($query->from)) {
return '';
}
$tables = $query->from;
if (is_string($tables) && strpos($tables, '(') !== false) {
return 'FROM ' . $tables;
......@@ -609,7 +616,7 @@ class QueryBuilder extends \yii\base\Object
}
if (!isset($conditions[1], $conditions[2])) {
return '';
throw new Exception("Operator $operator requires at least two operands.");
}
$column = $conditions[1];
......@@ -617,6 +624,15 @@ class QueryBuilder extends \yii\base\Object
$column = $this->connection->quoteColumnName($column);
}
if ($operator === 'BETWEEN' || $operator === 'NOT BETWEEN') {
if (!isset($conditions[3])) {
throw new Exception("Operator $operator requires three operands.");
}
$value1 = is_string($conditions[2]) ? $this->connection->quoteValue($conditions[2]) : (string)$conditions[2];
$value2 = is_string($conditions[3]) ? $this->connection->quoteValue($conditions[3]) : (string)$conditions[3];
return "$column $operator $value1 AND $value2";
}
$values = $conditions[2];
if (!is_array($values)) {
$values = array($values);
......
......@@ -86,7 +86,7 @@ abstract class Schema extends \yii\base\Object
$db->queryCachingDuration = -1;
}
if (!in_array($name, $db->schemaCachingExclude) && $db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) {
if (!in_array($name, $db->schemaCachingExclude) && $db->schemaCachingDuration >= 0 && ($cache = \Yii::$application->getComponent($db->schemaCacheID)) !== null) {
$key = __CLASS__ . "/{$db->dsn}/{$db->username}/{$name}";
if (($table = $cache->get($key)) === false) {
$table = $this->loadTableSchema($realName);
......@@ -156,7 +156,7 @@ abstract class Schema extends \yii\base\Object
public function refresh()
{
$db = $this->connection;
if ($db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) {
if ($db->schemaCachingDuration >= 0 && ($cache = \Yii::$application->getComponent($db->schemaCacheID)) !== null) {
foreach ($this->_tables as $name => $table) {
$key = __CLASS__ . ":{$db->dsn}/{$db->username}/{$name}";
$cache->delete($key);
......
......@@ -71,7 +71,7 @@ class DbTarget extends Target
if ($this->_db !== null) {
return $this->_db;
}
$this->_db = \Yii::app()->getComponent($this->connectionID);
$this->_db = \Yii::$application->getComponent($this->connectionID);
if (!$this->_db instanceof \yii\db\dao\Connection) {
throw new \yii\base\Exception('DbTarget.connectionID must refer to a valid application component ID');
}
......
......@@ -52,7 +52,7 @@ class FileTarget extends Target
{
parent::init();
if ($this->logPath === null) {
$this->logPath = \Yii::app()->getRuntimePath();
$this->logPath = \Yii::$application->getRuntimePath();
}
if (!is_dir($this->logPath) || !is_writable($this->logPath)) {
throw new \yii\base\Exception("Directory '{$this->logPath}' does not exist or is not writable.");
......
......@@ -72,7 +72,7 @@ class CProfileLogRoute extends CWebLogRoute
*/
public function processLogs($logs)
{
$app = Yii::app();
$app = \Yii::$application;
if (!($app instanceof CWebApplication) || $app->getRequest()->getIsAjaxRequest())
return;
......
......@@ -49,7 +49,7 @@ namespace yii\logging;
* as follows:
*
* ~~~
* \Yii::app()->log->targets['file']->enabled = false;
* \Yii::$application->log->targets['file']->enabled = false;
* ~~~
*
* @author Qiang Xue <qiang.xue@gmail.com>
......@@ -77,7 +77,7 @@ class Router extends \yii\base\ApplicationComponent
{
parent::init();
\Yii::getLogger()->attachEventHandler('onFlush', array($this, 'processMessages'));
if (($app = \Yii::app()) !== null) {
if (($app = \Yii::$application) !== null) {
$app->attachEventHandler('onEndRequest', array($this, 'processMessages'));
}
}
......
......@@ -128,7 +128,7 @@ abstract class Target extends \yii\base\Component implements \yii\base\Initable
if ($this->prefixSession && ($id = session_id()) !== '') {
$prefix[] = "[$id]";
}
if ($this->prefixUser && ($user = \Yii::app()->getComponent('user', false)) !== null) {
if ($this->prefixUser && ($user = \Yii::$application->getComponent('user', false)) !== null) {
$prefix[] = '[' . $user->getName() . ']';
$prefix[] = '[' . $user->getId() . ']';
}
......@@ -151,7 +151,7 @@ abstract class Target extends \yii\base\Component implements \yii\base\Initable
protected function getContextMessage()
{
$context = array();
if ($this->logUser && ($user = \Yii::app()->getComponent('user', false)) !== null) {
if ($this->logUser && ($user = \Yii::$application->getComponent('user', false)) !== null) {
$context[] = 'User: ' . $user->getName() . ' (ID: ' . $user->getId() . ')';
}
......
......@@ -47,7 +47,7 @@ class CWebLogRoute extends CLogRoute
*/
protected function render($view, $data)
{
$app = Yii::app();
$app = \Yii::$application;
$isAjax = $app->getRequest()->getIsAjaxRequest();
if ($this->showInFireBug)
......
......@@ -61,13 +61,13 @@ class CaptchaValidator extends Validator
public function getCaptchaAction()
{
if (strpos($this->captchaAction, '/') !== false) { // contains controller or module
$ca = Yii::app()->createController($this->captchaAction);
$ca = \Yii::$application->createController($this->captchaAction);
if ($ca !== null) {
list($controller, $actionID) = $ca;
$action = $controller->createAction($actionID);
}
} else {
$action = Yii::app()->getController()->createAction($this->captchaAction);
$action = \Yii::$application->getController()->createAction($this->captchaAction);
}
if ($action === null) {
......
......@@ -82,7 +82,7 @@ class StringValidator extends Validator
}
if (function_exists('mb_strlen') && $this->encoding !== false) {
$length = mb_strlen($value, $this->encoding ? $this->encoding : Yii::app()->charset);
$length = mb_strlen($value, $this->encoding ? $this->encoding : \Yii::$application->charset);
} else {
$length = strlen($value);
}
......
......@@ -13,34 +13,27 @@ class CommandTest extends \yiiunit\MysqlTestCase
{
$db = $this->getConnection(false);
// null
$command = $db->createCommand();
$this->assertEquals("SELECT *\nFROM ", $command->sql);
$this->assertEquals(null, $command->sql);
// string
$sql = 'SELECT * FROM yii_post';
$command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql);
// Query object
$query = new Query;
$query->select('id')->from('tbl_user');
$command = $db->createCommand($query);
$this->assertEquals($query, $command->query);
$query = array('select' => 'id', 'from' => 'yii_post');
$command = $db->createCommand($query);
$this->assertEquals($query, $command->query->toArray());
}
function testReset()
{
$db = $this->getConnection();
$command = $db->createCommand('SELECT * FROM yii_user');
$command->queryRow();
$this->assertNotEquals(null, $command->pdoStatement);
$this->assertEquals('SELECT * FROM yii_user', $command->sql);
$command->reset();
$this->assertEquals(null, $command->pdoStatement);
$this->assertNotEquals('SELECT * FROM yii_user', $command->sql);
$this->assertEquals("SELECT `id`\nFROM `tbl_user`", $command->sql);
// array
$command = $db->createCommand(array(
'select' => 'name',
'from' => 'tbl_user',
));
$this->assertEquals("SELECT `name`\nFROM `tbl_user`", $command->sql);
}
function testGetSetSql()
......
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