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
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											12 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											13 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											12 years ago
										 | namespace yii\redis;
 | ||
| 
											12 years ago
										 | use yii\base\InvalidParamException;
 | ||
| 
											12 years ago
										 | use yii\base\NotSupportedException;
 | ||
| 
											12 years ago
										 | use yii\db\ActiveQueryInterface;
 | ||
|  | use yii\db\ActiveQueryTrait;
 | ||
|  | use yii\db\QueryTrait;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | /**
 | ||
| 
											12 years ago
										 |  * ActiveQuery represents a query associated with an Active Record class.
 | ||
| 
											13 years ago
										 |  *
 | ||
| 
											12 years ago
										 |  * ActiveQuery instances are usually created by [[ActiveRecord::find()]]
 | ||
|  |  * and [[ActiveRecord::count()]].
 | ||
| 
											13 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.
 | ||
|  |  *
 | ||
| 
											12 years ago
										 |  * You can use query methods, such as [[where()]], [[limit()]] and [[orderBy()]] to customize the query options.
 | ||
| 
											13 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();
 | ||
|  |  * ~~~
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * @author Carsten Brandt <mail@cebe.cc>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											12 years ago
										 | class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
 | ||
| 
											13 years ago
										 | {
 | ||
| 
											12 years ago
										 | 	use QueryTrait;
 | ||
|  | 	use ActiveQueryTrait;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											12 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.
 | ||
| 
											12 years ago
										 | 	 * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public function all($db = null)
 | ||
| 
											12 years ago
										 | 	{
 | ||
|  | 		// TODO add support for orderBy
 | ||
| 
											12 years ago
										 | 		$data = $this->executeScript($db, 'All');
 | ||
|  | 		$rows = [];
 | ||
| 
											12 years ago
										 | 		foreach($data as $dataRow) {
 | ||
| 
											12 years ago
										 | 			$row = [];
 | ||
| 
											12 years ago
										 | 			$c = count($dataRow);
 | ||
|  | 			for($i = 0; $i < $c; ) {
 | ||
|  | 				$row[$dataRow[$i++]] = $dataRow[$i++];
 | ||
| 
											13 years ago
										 | 			}
 | ||
|  | 			$rows[] = $row;
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 		if (!empty($rows)) {
 | ||
| 
											13 years ago
										 | 			$models = $this->createModels($rows);
 | ||
|  | 			if (!empty($this->with)) {
 | ||
| 
											12 years ago
										 | 				$this->findWith($this->with, $models);
 | ||
| 
											13 years ago
										 | 			}
 | ||
|  | 			return $models;
 | ||
|  | 		} else {
 | ||
| 
											12 years ago
										 | 			return [];
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 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.
 | ||
| 
											13 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.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function one($db = null)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		// TODO add support for orderBy
 | ||
| 
											12 years ago
										 | 		$data = $this->executeScript($db, 'One');
 | ||
|  | 		if (empty($data)) {
 | ||
| 
											13 years ago
										 | 			return null;
 | ||
|  | 		}
 | ||
| 
											12 years ago
										 | 		$row = [];
 | ||
| 
											12 years ago
										 | 		$c = count($data);
 | ||
|  | 		for($i = 0; $i < $c; ) {
 | ||
| 
											13 years ago
										 | 			$row[$data[$i++]] = $data[$i++];
 | ||
|  | 		}
 | ||
| 
											12 years ago
										 | 		if ($this->asArray) {
 | ||
|  | 			$model = $row;
 | ||
|  | 		} else {
 | ||
| 
											12 years ago
										 | 			/** @var ActiveRecord $class */
 | ||
| 
											13 years ago
										 | 			$class = $this->modelClass;
 | ||
|  | 			$model = $class::create($row);
 | ||
|  | 		}
 | ||
| 
											12 years ago
										 | 		if (!empty($this->with)) {
 | ||
| 
											12 years ago
										 | 			$models = [$model];
 | ||
|  | 			$this->findWith($this->with, $models);
 | ||
| 
											12 years ago
										 | 			$model = $models[0];
 | ||
|  | 		}
 | ||
|  | 		return $model;
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns the number of records.
 | ||
| 
											12 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.
 | ||
| 
											13 years ago
										 | 	 * @return integer number of records
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function count($q = '*', $db = null)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		if ($this->offset === null && $this->limit === null && $this->where === null) {
 | ||
| 
											12 years ago
										 | 			/** @var ActiveRecord $modelClass */
 | ||
| 
											12 years ago
										 | 			$modelClass = $this->modelClass;
 | ||
| 
											12 years ago
										 | 			if ($db === null) {
 | ||
|  | 				$db = $modelClass::getDb();
 | ||
|  | 			}
 | ||
|  | 			return $db->executeCommand('LLEN', [$modelClass::tableName()]);
 | ||
| 
											12 years ago
										 | 		} else {
 | ||
| 
											12 years ago
										 | 			return $this->executeScript($db, 'Count');
 | ||
| 
											12 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 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)
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		// TODO add support for orderBy
 | ||
| 
											12 years ago
										 | 		return $this->executeScript($db, 'Column', $column);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Returns the number of records.
 | ||
|  | 	 * @param string $column the column to sum up
 | ||
| 
											12 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.
 | ||
| 
											12 years ago
										 | 	 * @return integer number of records
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function sum($column, $db = null)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		return $this->executeScript($db, 'Sum', $column);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 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.
 | ||
| 
											12 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.
 | ||
| 
											12 years ago
										 | 	 * @return integer the average of the specified column values.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function average($column, $db = null)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		return $this->executeScript($db, 'Average', $column);
 | ||
| 
											12 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.
 | ||
| 
											12 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.
 | ||
| 
											12 years ago
										 | 	 * @return integer the minimum of the specified column values.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function min($column, $db = null)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		return $this->executeScript($db, 'Min', $column);
 | ||
| 
											12 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.
 | ||
| 
											12 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.
 | ||
| 
											12 years ago
										 | 	 * @return integer the maximum of the specified column values.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function max($column, $db = null)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		return $this->executeScript($db, 'Max', $column);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Returns the query result as a scalar value.
 | ||
| 
											12 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
 | ||
| 
											12 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.
 | ||
| 
											12 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.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public function scalar($attribute, $db = null)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$record = $this->one($db);
 | ||
| 
											12 years ago
										 | 		if ($record !== null) {
 | ||
|  | 			return $record->$attribute;
 | ||
| 
											12 years ago
										 | 		} else {
 | ||
| 
											12 years ago
										 | 			return null;
 | ||
| 
											12 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * Executes a script created by [[LuaScriptBuilder]]
 | ||
| 
											12 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
 | ||
| 
											12 years ago
										 | 	 * @return array|bool|null|string
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	protected function executeScript($db, $type, $columnName = null)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		if (!empty($this->orderBy)) {
 | ||
|  | 			throw new NotSupportedException('orderBy is currently not supported by redis ActiveRecord.');
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 		/** @var ActiveRecord $modelClass */
 | ||
|  | 		$modelClass = $this->modelClass;
 | ||
|  | 
 | ||
|  | 		if ($db === null) {
 | ||
| 
											12 years ago
										 | 			$db = $modelClass::getDb();
 | ||
| 
											12 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 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);
 | ||
| 
											12 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 		$method = 'build' . $type;
 | ||
|  | 		$script = $db->getLuaScriptBuilder()->$method($this, $columnName);
 | ||
|  | 		return $db->executeCommand('EVAL', [$script, 0]);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Fetch by pk if possible as this is much faster
 | ||
| 
											12 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
 | ||
| 
											12 years ago
										 | 	 * @throws \yii\base\InvalidParamException
 | ||
| 
											12 years ago
										 | 	 * @throws \yii\base\NotSupportedException
 | ||
| 
											12 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	private function findByPk($db, $type, $columnName = null)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 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
 | ||
| 
											12 years ago
										 | 					throw new NotSupportedException('Find by composite PK is not supported by redis ActiveRecord.');
 | ||
| 
											12 years ago
										 | 				}
 | ||
| 
											12 years ago
										 | 			}
 | ||
| 
											12 years ago
										 | 			$pks = [$this->where];
 | ||
|  | 		}
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 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)) {
 | ||
|  | 				$key = $modelClass::tableName() . ':a:' . $modelClass::buildKey($pk);
 | ||
|  | 				$result = $db->executeCommand('HGETALL', [$key]);
 | ||
|  | 				if (!empty($result)) {
 | ||
|  | 					$data[] = $result;
 | ||
|  | 					if ($type === 'One' && $this->orderBy === null) {
 | ||
|  | 						break;
 | ||
| 
											12 years ago
										 | 					}
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
| 
											12 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++];
 | ||
| 
											12 years ago
										 | 					}
 | ||
| 
											12 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;
 | ||
| 
											12 years ago
										 | 						}
 | ||
|  | 					}
 | ||
| 
											12 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;
 | ||
| 
											12 years ago
										 | 						}
 | ||
|  | 					}
 | ||
| 
											12 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;
 | ||
| 
											12 years ago
										 | 						}
 | ||
|  | 					}
 | ||
| 
											12 years ago
										 | 				}
 | ||
| 
											12 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;
 | ||
|  | 						}
 | ||
| 
											12 years ago
										 | 					}
 | ||
| 
											13 years ago
										 | 				}
 | ||
| 
											12 years ago
										 | 				return $max;
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 		throw new InvalidParamException('Unknown fetch type: ' . $type);
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | }
 |