Commit d794a813 by Qiang Xue

...

parent 62336b43
...@@ -243,10 +243,6 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA ...@@ -243,10 +243,6 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA
protected function findRecords() protected function findRecords()
{ {
$finder = new ActiveFinder($this->getDbConnection()); $finder = new ActiveFinder($this->getDbConnection());
if (!empty($this->with)) { return $finder->findRecords($this);
return $finder->findRecordsWithRelations($this);
} else {
return $finder->findRecords($this);
}
} }
} }
...@@ -49,9 +49,10 @@ class JoinElement extends \yii\base\Object ...@@ -49,9 +49,10 @@ class JoinElement extends \yii\base\Object
public $relatedRecords; public $relatedRecords;
/** /**
* @param integer $id
* @param ActiveRelation|ActiveQuery $query * @param ActiveRelation|ActiveQuery $query
* @param JoinElement $parent * @param null|JoinElement $parent
* @param JoinElement $container * @param null|JoinElement $container
*/ */
public function __construct($id, $query, $parent, $container) public function __construct($id, $query, $parent, $container)
{ {
...@@ -70,15 +71,24 @@ class JoinElement extends \yii\base\Object ...@@ -70,15 +71,24 @@ class JoinElement extends \yii\base\Object
*/ */
public function createRecord($row) public function createRecord($row)
{ {
$pk = array(); if ($this->query->indexBy === null) {
foreach ($this->pkAlias as $alias) { $pk = array();
if (isset($row[$alias])) { foreach ($this->pkAlias as $alias) {
$pk[] = $row[$alias]; if (isset($row[$alias])) {
$pk[] = $row[$alias];
} else {
return null;
}
}
$pk = count($pk) === 1 ? $pk[0] : serialize($pk);
} else {
$pk = array_search($this->query->indexBy, $this->columnAliases);
if ($pk !== false) {
$pk = $row[$pk];
} else { } else {
return null; throw new Exception("Invalid indexBy: {$this->query->modelClass} has no attribute named '{$this->query->indexBy}'.");
} }
} }
$pk = count($pk) === 1 ? $pk[0] : serialize($pk);
// create record // create record
if (isset($this->records[$pk])) { if (isset($this->records[$pk])) {
...@@ -125,111 +135,4 @@ class JoinElement extends \yii\base\Object ...@@ -125,111 +135,4 @@ class JoinElement extends \yii\base\Object
return $record; return $record;
} }
public function buildQuery($query)
{
$prefixes = array(
'@.' => $this->query->tableAlias . '.',
'?.' => $this->parent->query->tableAlias . '.',
);
$quotedPrefixes = '';
foreach ($this->buildSelect($this->query->select) as $column) {
$query->select[] = strtr($column, $prefixes);
}
if ($this->query->where !== null) {
$query->where[] = strtr($this->query->where, $prefixes);
}
if ($this->query->having !== null) {
$query->having[] = strtr($this->query->having, $prefixes);
}
if ($this->query->via !== null) {
$query->join[] = $this->query->via;
}
$modelClass = $this->query->modelClass;
$tableName = $modelClass::tableName();
$joinType = $this->query->joinType === null ? 'LEFT JOIN' : $this->query->joinType;
$join = "$joinType $tableName {$this->query->tableAlias}";
if ($this->query->on !== null) {
$join .= ' ON ' . strtr($this->query->on, $prefixes);
}
$query->join[] = $join;
if ($this->query->join !== null) {
$query->join[] = strtr($this->query->join, $prefixes);
}
// todo: convert orderBy to array first
if ($this->query->orderBy !== null) {
$query->orderBy[] = strtr($this->query->orderBy, $prefixes);
}
// todo: convert groupBy to array first
if ($this->query->groupBy !== null) {
$query->groupBy[] = strtr($this->query->groupBy, $prefixes);
}
if ($this->query->params !== null) {
foreach ($this->query->params as $name => $value) {
if (is_integer($name)) {
$query->params[] = $value;
} else {
$query->params[$name] = $value;
}
}
}
foreach ($this->children as $child) {
$child->buildQuery($query);
}
}
public function buildSelect($select)
{
if ($select === false) {
return array();
}
$modelClass = $this->query->modelClass;
$table = $modelClass::getMetaData()->table;
$columns = array();
$columnCount = 0;
$prefix = $this->query->tableAlias;
if (empty($select) || $select === '*') {
foreach ($table->columns as $column) {
$alias = "t{$this->id}c" . ($columnCount++);
$columns[] = "$prefix.{$column->name} AS $alias";
$this->columnAliases[$alias] = $column->name;
if ($column->isPrimaryKey) {
$this->pkAlias[$column->name] = $alias;
}
}
} else {
if (is_string($select)) {
$select = explode(',', $select);
}
foreach ($table->primaryKey as $column) {
$alias = "t{$this->id}c" . ($columnCount++);
$columns[] = "$prefix.$column AS $alias";
$this->pkAlias[$column] = $alias;
}
foreach ($select as $column) {
$column = trim($column);
if (preg_match('/^(.*?)\s+AS\s+(\w+)$/im', $column, $matches)) {
// if the column is already aliased
$this->columnAliases[$matches[2]] = $matches[2];
$columns[] = $column;
} elseif (!isset($this->pkAlias[$column])) {
$alias = "t{$this->id}c" . ($columnCount++);
$columns[] = "$prefix.$column AS $alias";
$this->columnAliases[$alias] = $column;
}
}
}
return $columns;
}
} }
\ No newline at end of file
...@@ -355,7 +355,7 @@ class Command extends \yii\base\Component ...@@ -355,7 +355,7 @@ class Command extends \yii\base\Component
} }
\Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__); \Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__);
echo $sql . "\n\n";
if ($db->queryCachingCount > 0 && $db->queryCachingDuration >= 0 && $method !== '') { if ($db->queryCachingCount > 0 && $db->queryCachingDuration >= 0 && $method !== '') {
$cache = \Yii::$application->getComponent($db->queryCacheID); $cache = \Yii::$application->getComponent($db->queryCacheID);
} }
......
...@@ -43,7 +43,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -43,7 +43,7 @@ class QueryBuilder extends \yii\base\Object
*/ */
public $typeMap = array(); public $typeMap = array();
/** /**
* @var Query the Query object that is currently processed by the query builder to generate a SQL statement. * @var Query the Query object that is currently being processed by the query builder to generate a SQL statement.
*/ */
public $query; public $query;
...@@ -63,17 +63,16 @@ class QueryBuilder extends \yii\base\Object ...@@ -63,17 +63,16 @@ class QueryBuilder extends \yii\base\Object
*/ */
public function build($query) public function build($query)
{ {
$this->query = $query;
$clauses = array( $clauses = array(
$this->buildSelect(), $this->buildSelect($query->select, $query->distinct, $query->selectOption),
$this->buildFrom(), $this->buildFrom($query->from),
$this->buildJoin(), $this->buildJoin($query->join),
$this->buildWhere(), $this->buildWhere($query->where),
$this->buildGroupBy(), $this->buildGroupBy($query->groupBy),
$this->buildHaving(), $this->buildHaving($query->having),
$this->buildUnion(), $this->buildUnion($query->union),
$this->buildOrderBy(), $this->buildOrderBy($query->orderBy),
$this->buildLimit(), $this->buildLimit($query->limit, $query->offset),
); );
return implode($this->separator, array_filter($clauses)); return implode($this->separator, array_filter($clauses));
} }
...@@ -161,7 +160,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -161,7 +160,7 @@ class QueryBuilder extends \yii\base\Object
$this->query->addParams($params); $this->query->addParams($params);
} }
$sql = 'UPDATE ' . $this->quoteTableName($table) . ' SET ' . implode(', ', $lines); $sql = 'UPDATE ' . $this->quoteTableName($table) . ' SET ' . implode(', ', $lines);
if (($where = $this->buildCondition($condition)) != '') { if (($where = $this->buildCondition($condition)) !== '') {
$sql .= ' WHERE ' . $where; $sql .= ' WHERE ' . $where;
} }
...@@ -185,7 +184,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -185,7 +184,7 @@ class QueryBuilder extends \yii\base\Object
public function delete($table, $condition = '', $params = array()) public function delete($table, $condition = '', $params = array())
{ {
$sql = 'DELETE FROM ' . $this->quoteTableName($table); $sql = 'DELETE FROM ' . $this->quoteTableName($table);
if (($where = $this->buildCondition($condition)) != '') { if (($where = $this->buildCondition($condition)) !== '') {
$sql .= ' WHERE ' . $where; $sql .= ' WHERE ' . $where;
} }
if ($params !== array() && $this->query instanceof BaseQuery) { if ($params !== array() && $this->query instanceof BaseQuery) {
...@@ -620,16 +619,18 @@ class QueryBuilder extends \yii\base\Object ...@@ -620,16 +619,18 @@ class QueryBuilder extends \yii\base\Object
} }
/** /**
* @param string|array $columns
* @param boolean $distinct
* @param string $selectOption
* @return string the SELECT clause built from [[query]]. * @return string the SELECT clause built from [[query]].
*/ */
protected function buildSelect() public function buildSelect($columns, $distinct = false, $selectOption = null)
{ {
$select = $this->query->distinct ? 'SELECT DISTINCT' : 'SELECT'; $select = $distinct ? 'SELECT DISTINCT' : 'SELECT';
if ($this->query->selectOption !== null) { if ($selectOption !== null) {
$select .= ' ' . $this->query->selectOption; $select .= ' ' . $selectOption;
} }
$columns = $this->query->select;
if (empty($columns)) { if (empty($columns)) {
return $select . ' *'; return $select . ' *';
} }
...@@ -664,16 +665,15 @@ class QueryBuilder extends \yii\base\Object ...@@ -664,16 +665,15 @@ class QueryBuilder extends \yii\base\Object
} }
/** /**
* @param string|array $tables
* @return string the FROM clause built from [[query]]. * @return string the FROM clause built from [[query]].
*/ */
protected function buildFrom() public function buildFrom($tables)
{ {
if (empty($this->query->from)) { if (empty($tables)) {
return ''; return '';
} }
$tables = $this->query->from;
if ($this->autoQuote) { if ($this->autoQuote) {
$driver = $this->connection->driver; $driver = $this->connection->driver;
if (!is_array($tables)) { if (!is_array($tables)) {
...@@ -702,11 +702,11 @@ class QueryBuilder extends \yii\base\Object ...@@ -702,11 +702,11 @@ class QueryBuilder extends \yii\base\Object
} }
/** /**
* @param string|array $joins
* @return string the JOIN clause built from [[query]]. * @return string the JOIN clause built from [[query]].
*/ */
protected function buildJoin() public function buildJoin($joins)
{ {
$joins = $this->query->join;
if (empty($joins)) { if (empty($joins)) {
return ''; return '';
} }
...@@ -715,7 +715,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -715,7 +715,7 @@ class QueryBuilder extends \yii\base\Object
} }
foreach ($joins as $i => $join) { foreach ($joins as $i => $join) {
if (is_array($join)) { // join type, table name, on-condition if (is_array($join)) { // 0:join type, 1:table name, 2:on-condition
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) {
...@@ -743,44 +743,47 @@ class QueryBuilder extends \yii\base\Object ...@@ -743,44 +743,47 @@ class QueryBuilder extends \yii\base\Object
} }
/** /**
* @param string|array $condition
* @return string the WHERE clause built from [[query]]. * @return string the WHERE clause built from [[query]].
*/ */
protected function buildWhere() public function buildWhere($condition)
{ {
$where = $this->buildCondition($this->query->where); $where = $this->buildCondition($condition);
return empty($where) ? '' : 'WHERE ' . $where; return $where === '' ? '' : 'WHERE ' . $where;
} }
/** /**
* @return string the GROUP BY clause built from [[query]]. * @param string|array $columns
* @return string the GROUP BY clause
*/ */
protected function buildGroupBy() public function buildGroupBy($columns)
{ {
if (empty($this->query->groupBy)) { if (empty($columns)) {
return ''; return '';
} else { } else {
return 'GROUP BY ' . $this->buildColumns($this->query->groupBy); return 'GROUP BY ' . $this->buildColumns($columns);
} }
} }
/** /**
* @param string|array $condition
* @return string the HAVING clause built from [[query]]. * @return string the HAVING clause built from [[query]].
*/ */
protected function buildHaving() public function buildHaving($condition)
{ {
$having = $this->buildCondition($this->query->having); $having = $this->buildCondition($condition);
return empty($having) ? '' : 'HAVING ' . $having; return $having === '' ? '' : 'HAVING ' . $having;
} }
/** /**
* @param string|array $columns
* @return string the ORDER BY clause built from [[query]]. * @return string the ORDER BY clause built from [[query]].
*/ */
protected function buildOrderBy() public function buildOrderBy($columns)
{ {
if (empty($this->query->orderBy)) { if (empty($columns)) {
return ''; return '';
} }
$columns = $this->query->orderBy;
if ($this->autoQuote) { if ($this->autoQuote) {
$driver = $this->connection->driver; $driver = $this->connection->driver;
if (!is_array($columns)) { if (!is_array($columns)) {
...@@ -809,26 +812,28 @@ class QueryBuilder extends \yii\base\Object ...@@ -809,26 +812,28 @@ class QueryBuilder extends \yii\base\Object
} }
/** /**
* @param integer $limit
* @param integer $offset
* @return string the LIMIT and OFFSET clauses built from [[query]]. * @return string the LIMIT and OFFSET clauses built from [[query]].
*/ */
protected function buildLimit() public function buildLimit($limit, $offset)
{ {
$sql = ''; $sql = '';
if ($this->query->limit !== null && $this->query->limit >= 0) { if ($limit !== null && $limit >= 0) {
$sql = 'LIMIT ' . (int)$this->query->limit; $sql = 'LIMIT ' . (int)$limit;
} }
if ($this->query->offset > 0) { if ($offset > 0) {
$sql .= ' OFFSET ' . (int)$this->query->offset; $sql .= ' OFFSET ' . (int)$offset;
} }
return ltrim($sql); return ltrim($sql);
} }
/** /**
* @param string|array $unions
* @return string the UNION clause built from [[query]]. * @return string the UNION clause built from [[query]].
*/ */
protected function buildUnion() public function buildUnion($unions)
{ {
$unions = $this->query->union;
if (empty($unions)) { if (empty($unions)) {
return ''; return '';
} }
...@@ -836,8 +841,8 @@ class QueryBuilder extends \yii\base\Object ...@@ -836,8 +841,8 @@ class QueryBuilder extends \yii\base\Object
$unions = array($unions); $unions = array($unions);
} }
foreach ($unions as $i => $union) { foreach ($unions as $i => $union) {
if ($union instanceof Query) { if ($union instanceof BaseQuery) {
$unions[$i] = $union->getSql($this->connection); $unions[$i] = $this->build($union);
} }
} }
return "UNION (\n" . implode("\n) UNION (\n", $unions) . "\n)"; return "UNION (\n" . implode("\n) UNION (\n", $unions) . "\n)";
......
...@@ -219,17 +219,18 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase ...@@ -219,17 +219,18 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
// indexBy // indexBy
$customers = Customer::find()->orderBy('id')->indexBy('name')->all(); $customers = Customer::find()->orderBy('id')->indexBy('name')->all();
$this->assertEquals(2, $customers['user2']['id']);
} }
public function testEagerLoading() public function testEagerLoading()
{ {
$customers = Customer::find()->with('orders')->orderBy('t0.id')->all(); $customers = Customer::find()->with('orders')->orderBy('@.id')->all();
$this->assertEquals(3, count($customers)); $this->assertEquals(3, count($customers));
$this->assertEquals(1, count($customers[0]->orders)); $this->assertEquals(1, count($customers[0]->orders));
$this->assertEquals(2, count($customers[1]->orders)); $this->assertEquals(2, count($customers[1]->orders));
$this->assertEquals(0, count($customers[2]->orders)); $this->assertEquals(0, count($customers[2]->orders));
$customers = Customer::find()->with('orders.customer')->orderBy('t0.id')->all(); $customers = Customer::find()->with('orders.customer')->orderBy('@.id')->all();
} }
/* /*
......
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