Commit 1978a4ef by Qiang Xue

...

parent e5534b3c
...@@ -358,25 +358,25 @@ class YiiBase ...@@ -358,25 +358,25 @@ class YiiBase
$class = static::import($class, true); $class = static::import($class, true);
} }
if (($n = func_num_args()-1) > 0) { if (($n = func_num_args()) > 1) {
$args = func_get_args(); $args = func_get_args();
array_shift($args); // remove $config if ($n === 2) {
} $object = new $class($args[1]);
} elseif ($n === 3) {
if ($n === 0) { $object = new $class($args[1], $args[2]);
$object = new $class; } elseif ($n === 4) {
} elseif ($n === 1) { $object = new $class($args[1], $args[2], $args[3]);
$object = new $class($args[0]); } else {
} elseif ($n === 2) { array_shift($args); // remove $config
$object = new $class($args[0], $args[1]); $r = new \ReflectionClass($class);
} elseif ($n === 3) { $object = $r->newInstanceArgs($args);
$object = new $class($args[0], $args[1], $args[2]); }
} else { } else {
$r = new \ReflectionClass($class); $object = new $class;
$object = $r->newInstanceArgs($args);
} }
$class = '\\' . get_class($object); $class = '\\' . get_class($object);
if (isset(\Yii::$objectConfig[$class])) { if (isset(\Yii::$objectConfig[$class])) {
$config = array_merge(\Yii::$objectConfig[$class], $config); $config = array_merge(\Yii::$objectConfig[$class], $config);
} }
......
...@@ -302,22 +302,21 @@ class Object ...@@ -302,22 +302,21 @@ class Object
{ {
$class = '\\' . get_called_class(); $class = '\\' . get_called_class();
if (($n = func_num_args()-1) > 0) { if (($n = func_num_args()) > 1) {
$args = func_get_args(); $args = func_get_args();
array_shift($args); // remove $config if ($n === 2) {
} $object = new $class($args[1]);
} elseif ($n === 3) {
if ($n === 0) { $object = new $class($args[1], $args[2]);
$object = new $class; } elseif ($n === 4) {
} elseif ($n === 1) { $object = new $class($args[1], $args[2], $args[3]);
$object = new $class($args[0]); } else {
} elseif ($n === 2) { array_shift($args); // remove $config
$object = new $class($args[0], $args[1]); $r = new \ReflectionClass($class);
} elseif ($n === 3) { $object = $r->newInstanceArgs($args);
$object = new $class($args[0], $args[1], $args[2]); }
} else { } else {
$r = new \ReflectionClass($class); $object = new $class;
$object = $r->newInstanceArgs($args);
} }
if (isset(\Yii::$objectConfig[$class])) { if (isset(\Yii::$objectConfig[$class])) {
......
...@@ -17,5 +17,22 @@ namespace yii\db\ar; ...@@ -17,5 +17,22 @@ namespace yii\db\ar;
*/ */
class ActiveQueryBuilder extends \yii\base\Object class ActiveQueryBuilder extends \yii\base\Object
{ {
/**
* @var \yii\db\dao\QueryBuilder
*/
public $queryBuilder;
/**
* @var ActiveQuery
*/
public $query;
public function __construct($query)
{
$this->query = $query;
}
public function build()
{
}
} }
\ No newline at end of file
...@@ -53,23 +53,25 @@ abstract class ActiveRecord extends \yii\base\Model ...@@ -53,23 +53,25 @@ abstract class ActiveRecord extends \yii\base\Model
/** /**
* @static * @static
* @param string|array|ActiveQuery $q * @param string|array|Query $q
* @return ActiveQuery * @return ActiveQuery
* @throws \yii\db\Exception * @throws \yii\db\Exception
*/ */
public static function find($q = null) public static function find($q = null)
{ {
$query = $q instanceof ActiveQuery? $q : static::createQuery(); $query = static::createActiveQuery();
$query->modelClass = '\\' . get_called_class(); if ($q instanceof Query) {
$query->from = static::tableName(); $query->query = $q;
if (is_array($q)) { } elseif (is_array($q)) {
// query by attributes
$query->where($q); $query->where($q);
} elseif ($q !== null && $query !== $q) { } elseif ($q !== null) {
// query by primary key
$primaryKey = static::getMetaData()->table->primaryKey; $primaryKey = static::getMetaData()->table->primaryKey;
if (is_string($primaryKey)) { if (is_string($primaryKey)) {
$query->where(array($primaryKey => $q)); $query->where(array($primaryKey => $q));
} else { } else {
throw new Exception("Multiple column values are required to find by composite primary keys."); throw new Exception('Composite primary keys require multiple column values.');
} }
} }
return $query; return $query;
...@@ -77,21 +79,15 @@ abstract class ActiveRecord extends \yii\base\Model ...@@ -77,21 +79,15 @@ abstract class ActiveRecord extends \yii\base\Model
public static function findBySql($sql, $params = array()) public static function findBySql($sql, $params = array())
{ {
$query = static::createQuery();
if (!is_array($params)) { if (!is_array($params)) {
$params = func_get_args(); $params = func_get_args();
array_shift($params); array_shift($params);
} }
$query->setSql($sql); $query = static::createActiveQuery();
$query->modelClass = '\\' . get_called_class(); $query->sql = $sql;
return $query->params($params); return $query->params($params);
} }
public static function exists($condition, $params)
{
}
public static function updateAll() public static function updateAll()
{ {
...@@ -107,7 +103,7 @@ abstract class ActiveRecord extends \yii\base\Model ...@@ -107,7 +103,7 @@ abstract class ActiveRecord extends \yii\base\Model
} }
public static function createQuery() public static function createActiveQuery()
{ {
return new ActiveQuery('\\' . get_called_class()); return new ActiveQuery('\\' . get_called_class());
} }
......
...@@ -24,33 +24,28 @@ use yii\db\Exception; ...@@ -24,33 +24,28 @@ use yii\db\Exception;
class QueryBuilder extends \yii\base\Object class QueryBuilder extends \yii\base\Object
{ {
/** /**
* @var array the abstract column types mapped to physical column types.
* This is mainly used to support creating/modifying tables using DB-independent data type specifications.
* Child classes should override this property to declare supported type mappings.
*/
public $typeMap = array();
/**
* @var Connection the database connection. * @var Connection the database connection.
*/ */
public $connection; public $connection;
/** /**
* @var Driver the database driver used for this query builder.
*/
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.
*/ */
public $separator = " "; public $separator = " ";
/** /**
* @var Query the Query object that is currently processed by the query builder to generate a SQL statement.
* This property will be set null upon completion of [[build()]].
*/
public $query;
/**
* @var boolean whether to automatically quote table and column names when generating SQL statements. * @var boolean whether to automatically quote table and column names when generating SQL statements.
*/ */
public $autoQuote = true; public $autoQuote = true;
/**
* @var array the abstract column types mapped to physical column types.
* This is mainly used to support creating/modifying tables using DB-independent data type specifications.
* Child classes should override this property to declare supported type mappings.
*/
public $typeMap = array();
/**
* @var Query the Query object that is currently processed by the query builder to generate a SQL statement.
*/
public $query;
/** /**
* Constructor. * Constructor.
...@@ -59,7 +54,6 @@ class QueryBuilder extends \yii\base\Object ...@@ -59,7 +54,6 @@ class QueryBuilder extends \yii\base\Object
public function __construct($connection) public function __construct($connection)
{ {
$this->connection = $connection; $this->connection = $connection;
$this->driver = $connection->getDriver();
} }
/** /**
...@@ -74,8 +68,9 @@ class QueryBuilder extends \yii\base\Object ...@@ -74,8 +68,9 @@ class QueryBuilder extends \yii\base\Object
$this->query = $query; $this->query = $query;
if ($query->operation !== null) { if ($query->operation !== null) {
// non-SELECT query // non-SELECT query
$method = array_shift($query->operation); $params = $query->operation;
$sql = call_user_func_array(array($this, $method), $query->operation); $method = array_shift($params);
return call_user_func_array(array($this, $method), $params);
} else { } else {
// SELECT query // SELECT query
$clauses = array( $clauses = array(
...@@ -89,10 +84,8 @@ class QueryBuilder extends \yii\base\Object ...@@ -89,10 +84,8 @@ class QueryBuilder extends \yii\base\Object
$this->buildOrderBy(), $this->buildOrderBy(),
$this->buildLimit(), $this->buildLimit(),
); );
$sql = implode($this->separator, array_filter($clauses)); return implode($this->separator, array_filter($clauses));
} }
$this->query = null;
return $sql;
} }
/** /**
...@@ -477,7 +470,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -477,7 +470,7 @@ class QueryBuilder extends \yii\base\Object
/** /**
* Parses the condition specification and generates the corresponding SQL expression. * Parses the condition specification and generates the corresponding SQL expression.
* @param string|array $condition the condition specification. Please refer to [[Query::where()]] * @param string|array $condition the condition specification. Please refer to [[BaseQuery::where()]]
* on how to specify a condition. * on how to specify a condition.
* @return string the generated SQL expression * @return string the generated SQL expression
* @throws \yii\db\Exception if the condition is in bad format * @throws \yii\db\Exception if the condition is in bad format
...@@ -651,6 +644,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -651,6 +644,7 @@ class QueryBuilder extends \yii\base\Object
} }
if ($this->autoQuote) { if ($this->autoQuote) {
$driver = $this->connection->driver;
if (!is_array($columns)) { if (!is_array($columns)) {
if (strpos($columns, '(') !== false) { if (strpos($columns, '(') !== false) {
return $select . ' ' . $columns; return $select . ' ' . $columns;
...@@ -663,9 +657,9 @@ class QueryBuilder extends \yii\base\Object ...@@ -663,9 +657,9 @@ 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->driver->quoteColumnName($matches[1]) . ' AS ' . $this->driver->quoteSimpleColumnName($matches[2]); $columns[$i] = $driver->quoteColumnName($matches[1]) . ' AS ' . $driver->quoteSimpleColumnName($matches[2]);
} else { } else {
$columns[$i] = $this->driver->quoteColumnName($column); $columns[$i] = $driver->quoteColumnName($column);
} }
} }
} }
...@@ -690,6 +684,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -690,6 +684,7 @@ class QueryBuilder extends \yii\base\Object
$tables = $this->query->from; $tables = $this->query->from;
if ($this->autoQuote) { if ($this->autoQuote) {
$driver = $this->connection->driver;
if (!is_array($tables)) { if (!is_array($tables)) {
if (strpos($tables, '(') !== false) { if (strpos($tables, '(') !== false) {
return 'FROM ' . $tables; return 'FROM ' . $tables;
...@@ -700,9 +695,9 @@ class QueryBuilder extends \yii\base\Object ...@@ -700,9 +695,9 @@ class QueryBuilder extends \yii\base\Object
foreach ($tables as $i => $table) { foreach ($tables as $i => $table) {
if (strpos($table, '(') === false) { if (strpos($table, '(') === false) {
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/i', $table, $matches)) { // with alias if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/i', $table, $matches)) { // with alias
$tables[$i] = $this->driver->quoteTableName($matches[1]) . ' ' . $this->driver->quoteTableName($matches[2]); $tables[$i] = $driver->quoteTableName($matches[1]) . ' ' . $driver->quoteTableName($matches[2]);
} else { } else {
$tables[$i] = $this->driver->quoteTableName($table); $tables[$i] = $driver->quoteTableName($table);
} }
} }
} }
...@@ -733,10 +728,11 @@ class QueryBuilder extends \yii\base\Object ...@@ -733,10 +728,11 @@ class QueryBuilder extends \yii\base\Object
if (isset($join[0], $join[1])) { if (isset($join[0], $join[1])) {
$table = $join[1]; $table = $join[1];
if ($this->autoQuote && strpos($table, '(') === false) { if ($this->autoQuote && strpos($table, '(') === false) {
$driver = $this->connection->driver;
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/', $table, $matches)) { // with alias if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/', $table, $matches)) { // with alias
$table = $this->driver->quoteTableName($matches[1]) . ' ' . $this->driver->quoteTableName($matches[2]); $table = $driver->quoteTableName($matches[1]) . ' ' . $driver->quoteTableName($matches[2]);
} else { } else {
$table = $this->driver->quoteTableName($table); $table = $driver->quoteTableName($table);
} }
} }
$joins[$i] = strtoupper($join[0]) . ' ' . $table; $joins[$i] = strtoupper($join[0]) . ' ' . $table;
...@@ -792,6 +788,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -792,6 +788,7 @@ class QueryBuilder extends \yii\base\Object
} }
$columns = $this->query->orderBy; $columns = $this->query->orderBy;
if ($this->autoQuote) { if ($this->autoQuote) {
$driver = $this->connection->driver;
if (!is_array($columns)) { if (!is_array($columns)) {
if (strpos($columns, '(') !== false) { if (strpos($columns, '(') !== false) {
return 'ORDER BY ' . $columns; return 'ORDER BY ' . $columns;
...@@ -804,9 +801,9 @@ class QueryBuilder extends \yii\base\Object ...@@ -804,9 +801,9 @@ 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('/^(.*?)\s+(asc|desc)$/i', $column, $matches)) { if (preg_match('/^(.*?)\s+(asc|desc)$/i', $column, $matches)) {
$columns[$i] = $this->driver->quoteColumnName($matches[1]) . ' ' . $matches[2]; $columns[$i] = $driver->quoteColumnName($matches[1]) . ' ' . $matches[2];
} else { } else {
$columns[$i] = $this->driver->quoteColumnName($column); $columns[$i] = $driver->quoteColumnName($column);
} }
} }
} }
...@@ -873,7 +870,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -873,7 +870,7 @@ class QueryBuilder extends \yii\base\Object
if (is_object($column)) { if (is_object($column)) {
$columns[$i] = (string)$column; $columns[$i] = (string)$column;
} elseif (strpos($column, '(') === false) { } elseif (strpos($column, '(') === false) {
$columns[$i] = $this->driver->quoteColumnName($column); $columns[$i] = $this->quoteColumnName($column);
} }
} }
} }
...@@ -890,7 +887,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -890,7 +887,7 @@ class QueryBuilder extends \yii\base\Object
protected function quoteTableName($name, $simple = false) protected function quoteTableName($name, $simple = false)
{ {
if ($this->autoQuote) { if ($this->autoQuote) {
return $simple ? $this->driver->quoteSimpleTableName($name) : $this->driver->quoteTableName($name); return $this->connection->quoteTableName($name, $simple);
} else { } else {
return $name; return $name;
} }
...@@ -906,7 +903,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -906,7 +903,7 @@ class QueryBuilder extends \yii\base\Object
protected function quoteColumnName($name, $simple = false) protected function quoteColumnName($name, $simple = false)
{ {
if ($this->autoQuote) { if ($this->autoQuote) {
return $simple ? $this->driver->quoteSimpleColumnName($name) : $this->driver->quoteColumnName($name); return $this->connection->quoteColumnName($name, $simple);
} else { } else {
return $name; return $name;
} }
......
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