You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
383 lines
12 KiB
383 lines
12 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
11 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
12 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
11 years ago
|
namespace yii\redis;
|
||
11 years ago
|
use yii\base\InvalidParamException;
|
||
11 years ago
|
use yii\base\NotSupportedException;
|
||
11 years ago
|
use yii\db\ActiveQueryInterface;
|
||
|
use yii\db\ActiveQueryTrait;
|
||
|
use yii\db\QueryTrait;
|
||
12 years ago
|
|
||
|
/**
|
||
11 years ago
|
* ActiveQuery represents a query associated with an Active Record class.
|
||
12 years ago
|
*
|
||
11 years ago
|
* ActiveQuery instances are usually created by [[ActiveRecord::find()]]
|
||
|
* and [[ActiveRecord::count()]].
|
||
12 years ago
|
*
|
||
|
* ActiveQuery mainly provides the following methods to retrieve the query results:
|
||
|
*
|
||
|
* - [[one()]]: returns a single record populated with the first row of data.
|
||
|
* - [[all()]]: returns all records based on the query results.
|
||
|
* - [[count()]]: returns the number of records.
|
||
|
* - [[sum()]]: returns the sum over the specified column.
|
||
|
* - [[average()]]: returns the average over the specified column.
|
||
|
* - [[min()]]: returns the min over the specified column.
|
||
|
* - [[max()]]: returns the max over the specified column.
|
||
|
* - [[scalar()]]: returns the value of the first column in the first row of the query result.
|
||
|
* - [[exists()]]: returns a value indicating whether the query result has data or not.
|
||
|
*
|
||
11 years ago
|
* You can use query methods, such as [[where()]], [[limit()]] and [[orderBy()]] to customize the query options.
|
||
12 years ago
|
*
|
||
|
* ActiveQuery also provides the following additional query options:
|
||
|
*
|
||
|
* - [[with()]]: list of relations that this query should be performed with.
|
||
|
* - [[indexBy()]]: the name of the column by which the query result should be indexed.
|
||
|
* - [[asArray()]]: whether to return each record as an array.
|
||
|
*
|
||
|
* These options can be configured using methods of the same name. For example:
|
||
|
*
|
||
|
* ~~~
|
||
|
* $customers = Customer::find()->with('orders')->asArray()->all();
|
||
|
* ~~~
|
||
12 years ago
|
*
|
||
|
* @author Carsten Brandt <mail@cebe.cc>
|
||
|
* @since 2.0
|
||
|
*/
|
||
11 years ago
|
class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
|
||
12 years ago
|
{
|
||
11 years ago
|
use QueryTrait;
|
||
|
use ActiveQueryTrait;
|
||
12 years ago
|
|
||
|
/**
|
||
11 years ago
|
* Executes the query and returns all results as an array.
|
||
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
11 years ago
|
* @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
|
||
12 years ago
|
*/
|
||
11 years ago
|
public function all($db = null)
|
||
11 years ago
|
{
|
||
|
// TODO add support for orderBy
|
||
11 years ago
|
$data = $this->executeScript($db, 'All');
|
||
|
$rows = [];
|
||
11 years ago
|
foreach($data as $dataRow) {
|
||
11 years ago
|
$row = [];
|
||
11 years ago
|
$c = count($dataRow);
|
||
|
for($i = 0; $i < $c; ) {
|
||
|
$row[$dataRow[$i++]] = $dataRow[$i++];
|
||
12 years ago
|
}
|
||
|
$rows[] = $row;
|
||
12 years ago
|
}
|
||
11 years ago
|
if (!empty($rows)) {
|
||
12 years ago
|
$models = $this->createModels($rows);
|
||
|
if (!empty($this->with)) {
|
||
11 years ago
|
$this->findWith($this->with, $models);
|
||
12 years ago
|
}
|
||
|
return $models;
|
||
|
} else {
|
||
11 years ago
|
return [];
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Executes the query and returns a single row of result.
|
||
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
12 years ago
|
* @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
|
||
|
* the query result may be either an array or an ActiveRecord object. Null will be returned
|
||
|
* if the query results in nothing.
|
||
|
*/
|
||
11 years ago
|
public function one($db = null)
|
||
12 years ago
|
{
|
||
11 years ago
|
// TODO add support for orderBy
|
||
11 years ago
|
$data = $this->executeScript($db, 'One');
|
||
|
if (empty($data)) {
|
||
12 years ago
|
return null;
|
||
|
}
|
||
11 years ago
|
$row = [];
|
||
11 years ago
|
$c = count($data);
|
||
|
for($i = 0; $i < $c; ) {
|
||
12 years ago
|
$row[$data[$i++]] = $data[$i++];
|
||
|
}
|
||
11 years ago
|
if ($this->asArray) {
|
||
|
$model = $row;
|
||
|
} else {
|
||
11 years ago
|
/** @var ActiveRecord $class */
|
||
12 years ago
|
$class = $this->modelClass;
|
||
|
$model = $class::create($row);
|
||
|
}
|
||
11 years ago
|
if (!empty($this->with)) {
|
||
11 years ago
|
$models = [$model];
|
||
|
$this->findWith($this->with, $models);
|
||
11 years ago
|
$model = $models[0];
|
||
|
}
|
||
|
return $model;
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the number of records.
|
||
11 years ago
|
* @param string $q the COUNT expression. This parameter is ignored by this implementation.
|
||
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
12 years ago
|
* @return integer number of records
|
||
|
*/
|
||
11 years ago
|
public function count($q = '*', $db = null)
|
||
12 years ago
|
{
|
||
11 years ago
|
if ($this->offset === null && $this->limit === null && $this->where === null) {
|
||
11 years ago
|
/** @var ActiveRecord $modelClass */
|
||
11 years ago
|
$modelClass = $this->modelClass;
|
||
11 years ago
|
if ($db === null) {
|
||
|
$db = $modelClass::getDb();
|
||
|
}
|
||
11 years ago
|
return $db->executeCommand('LLEN', [$modelClass::keyPrefix()]);
|
||
11 years ago
|
} else {
|
||
11 years ago
|
return $this->executeScript($db, 'Count');
|
||
11 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Returns a value indicating whether the query result contains any row of data.
|
||
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
|
* @return boolean whether the query result contains any row of data.
|
||
|
*/
|
||
|
public function exists($db = null)
|
||
|
{
|
||
|
return $this->one($db) !== null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Executes the query and returns the first column of the result.
|
||
|
* @param string $column name of the column to select
|
||
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
|
* @return array the first column of the query result. An empty array is returned if the query results in nothing.
|
||
|
*/
|
||
|
public function column($column, $db = null)
|
||
|
{
|
||
11 years ago
|
// TODO add support for orderBy
|
||
11 years ago
|
return $this->executeScript($db, 'Column', $column);
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Returns the number of records.
|
||
|
* @param string $column the column to sum up
|
||
11 years ago
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
11 years ago
|
* @return integer number of records
|
||
|
*/
|
||
11 years ago
|
public function sum($column, $db = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
return $this->executeScript($db, 'Sum', $column);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Returns the average of the specified column values.
|
||
|
* @param string $column the column name or expression.
|
||
|
* Make sure you properly quote column names in the expression.
|
||
11 years ago
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
11 years ago
|
* @return integer the average of the specified column values.
|
||
|
*/
|
||
11 years ago
|
public function average($column, $db = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
return $this->executeScript($db, 'Average', $column);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the minimum of the specified column values.
|
||
|
* @param string $column the column name or expression.
|
||
|
* Make sure you properly quote column names in the expression.
|
||
11 years ago
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
11 years ago
|
* @return integer the minimum of the specified column values.
|
||
|
*/
|
||
11 years ago
|
public function min($column, $db = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
return $this->executeScript($db, 'Min', $column);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the maximum of the specified column values.
|
||
|
* @param string $column the column name or expression.
|
||
|
* Make sure you properly quote column names in the expression.
|
||
11 years ago
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
11 years ago
|
* @return integer the maximum of the specified column values.
|
||
|
*/
|
||
11 years ago
|
public function max($column, $db = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
return $this->executeScript($db, 'Max', $column);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Returns the query result as a scalar value.
|
||
11 years ago
|
* The value returned will be the specified attribute in the first record of the query results.
|
||
|
* @param string $attribute name of the attribute to select
|
||
11 years ago
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
11 years ago
|
* @return string the value of the specified attribute in the first record of the query result.
|
||
|
* Null is returned if the query result is empty.
|
||
12 years ago
|
*/
|
||
11 years ago
|
public function scalar($attribute, $db = null)
|
||
12 years ago
|
{
|
||
11 years ago
|
$record = $this->one($db);
|
||
11 years ago
|
if ($record !== null) {
|
||
11 years ago
|
return $record->hasAttribute($attribute) ? $record->$attribute : null;
|
||
11 years ago
|
} else {
|
||
11 years ago
|
return null;
|
||
11 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Executes a script created by [[LuaScriptBuilder]]
|
||
11 years ago
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
|
* @param string $type the type of the script to generate
|
||
|
* @param string $columnName
|
||
11 years ago
|
* @return array|bool|null|string
|
||
|
*/
|
||
11 years ago
|
protected function executeScript($db, $type, $columnName = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (!empty($this->orderBy)) {
|
||
|
throw new NotSupportedException('orderBy is currently not supported by redis ActiveRecord.');
|
||
|
}
|
||
|
|
||
11 years ago
|
/** @var ActiveRecord $modelClass */
|
||
|
$modelClass = $this->modelClass;
|
||
|
|
||
|
if ($db === null) {
|
||
11 years ago
|
$db = $modelClass::getDb();
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// find by primary key if possible. This is much faster than scanning all records
|
||
|
if (is_array($this->where) && !isset($this->where[0]) && $modelClass::isPrimaryKey(array_keys($this->where))) {
|
||
|
return $this->findByPk($db, $type, $columnName);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
$method = 'build' . $type;
|
||
|
$script = $db->getLuaScriptBuilder()->$method($this, $columnName);
|
||
|
return $db->executeCommand('EVAL', [$script, 0]);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Fetch by pk if possible as this is much faster
|
||
11 years ago
|
* @param Connection $db the database connection used to execute the query.
|
||
|
* If this parameter is not given, the `db` application component will be used.
|
||
|
* @param string $type the type of the script to generate
|
||
|
* @param string $columnName
|
||
|
* @return array|bool|null|string
|
||
11 years ago
|
* @throws \yii\base\InvalidParamException
|
||
11 years ago
|
* @throws \yii\base\NotSupportedException
|
||
11 years ago
|
*/
|
||
11 years ago
|
private function findByPk($db, $type, $columnName = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (count($this->where) == 1) {
|
||
|
$pks = (array) reset($this->where);
|
||
|
} else {
|
||
|
foreach($this->where as $column => $values) {
|
||
|
if (is_array($values)) {
|
||
|
// TODO support composite IN for composite PK
|
||
11 years ago
|
throw new NotSupportedException('Find by composite PK is not supported by redis ActiveRecord.');
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
$pks = [$this->where];
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
/** @var ActiveRecord $modelClass */
|
||
|
$modelClass = $this->modelClass;
|
||
|
|
||
|
$start = $this->offset === null ? 0 : $this->offset;
|
||
|
$i = 0;
|
||
|
$data = [];
|
||
|
foreach($pks as $pk) {
|
||
|
if (++$i > $start && ($this->limit === null || $i <= $start + $this->limit)) {
|
||
11 years ago
|
$key = $modelClass::keyPrefix() . ':a:' . $modelClass::buildKey($pk);
|
||
11 years ago
|
$result = $db->executeCommand('HGETALL', [$key]);
|
||
|
if (!empty($result)) {
|
||
|
$data[] = $result;
|
||
|
if ($type === 'One' && $this->orderBy === null) {
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
// TODO support orderBy
|
||
|
|
||
|
switch($type) {
|
||
|
case 'All':
|
||
|
return $data;
|
||
|
case 'One':
|
||
|
return reset($data);
|
||
|
case 'Count':
|
||
|
return count($data);
|
||
|
case 'Column':
|
||
|
$column = [];
|
||
|
foreach($data as $dataRow) {
|
||
|
$row = [];
|
||
|
$c = count($dataRow);
|
||
|
for($i = 0; $i < $c; ) {
|
||
|
$row[$dataRow[$i++]] = $dataRow[$i++];
|
||
11 years ago
|
}
|
||
11 years ago
|
$column[] = $row[$columnName];
|
||
|
}
|
||
|
return $column;
|
||
|
case 'Sum':
|
||
|
$sum = 0;
|
||
|
foreach($data as $dataRow) {
|
||
|
$c = count($dataRow);
|
||
|
for($i = 0; $i < $c; ) {
|
||
|
if ($dataRow[$i++] == $columnName) {
|
||
|
$sum += $dataRow[$i];
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
return $sum;
|
||
|
case 'Average':
|
||
|
$sum = 0;
|
||
|
$count = 0;
|
||
|
foreach($data as $dataRow) {
|
||
|
$count++;
|
||
|
$c = count($dataRow);
|
||
|
for($i = 0; $i < $c; ) {
|
||
|
if ($dataRow[$i++] == $columnName) {
|
||
|
$sum += $dataRow[$i];
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
return $sum / $count;
|
||
|
case 'Min':
|
||
|
$min = null;
|
||
|
foreach($data as $dataRow) {
|
||
|
$c = count($dataRow);
|
||
|
for($i = 0; $i < $c; ) {
|
||
|
if ($dataRow[$i++] == $columnName && ($min == null || $dataRow[$i] < $min)) {
|
||
|
$min = $dataRow[$i];
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
return $min;
|
||
|
case 'Max':
|
||
|
$max = null;
|
||
|
foreach($data as $dataRow) {
|
||
|
$c = count($dataRow);
|
||
|
for($i = 0; $i < $c; ) {
|
||
|
if ($dataRow[$i++] == $columnName && ($max == null || $dataRow[$i] > $max)) {
|
||
|
$max = $dataRow[$i];
|
||
|
break;
|
||
|
}
|
||
11 years ago
|
}
|
||
12 years ago
|
}
|
||
11 years ago
|
return $max;
|
||
12 years ago
|
}
|
||
11 years ago
|
throw new InvalidParamException('Unknown fetch type: ' . $type);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|