LuaScriptBuilder.php 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\redis;

10
use yii\base\InvalidParamException;
11
use yii\base\NotSupportedException;
Carsten Brandt committed
12 13
use yii\db\Exception;
use yii\db\Expression;
14 15 16 17 18 19 20 21 22

/**
 * LuaScriptBuilder builds lua scripts used for retrieving data from redis.
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
class LuaScriptBuilder extends \yii\base\Object
{
Carsten Brandt committed
23 24 25 26 27
	/**
	 * Builds a Lua script for finding a list of records
	 * @param ActiveQuery $query the query used to build the script
	 * @return string
	 */
28 29
	public function buildAll($query)
	{
30
		// TODO add support for orderBy
Carsten Brandt committed
31
		/** @var ActiveRecord $modelClass */
32
		$modelClass = $query->modelClass;
33
		$key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
34
		return $this->build($query, "n=n+1 pks[n]=redis.call('HGETALL',$key .. pk)", 'pks');
35 36
	}

Carsten Brandt committed
37 38 39 40 41
	/**
	 * Builds a Lua script for finding one record
	 * @param ActiveQuery $query the query used to build the script
	 * @return string
	 */
42 43
	public function buildOne($query)
	{
44
		// TODO add support for orderBy
Carsten Brandt committed
45
		/** @var ActiveRecord $modelClass */
46
		$modelClass = $query->modelClass;
47
		$key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
48
		return $this->build($query, "do return redis.call('HGETALL',$key .. pk) end", 'pks');
49 50
	}

Carsten Brandt committed
51 52 53 54 55 56 57
	/**
	 * Builds a Lua script for finding a column
	 * @param ActiveQuery $query the query used to build the script
	 * @param string $column name of the column
	 * @return string
	 */
	public function buildColumn($query, $column)
58 59
	{
		// TODO add support for orderBy and indexBy
Carsten Brandt committed
60
		/** @var ActiveRecord $modelClass */
61
		$modelClass = $query->modelClass;
62
		$key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
63
		return $this->build($query, "n=n+1 pks[n]=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'pks');
64 65
	}

Carsten Brandt committed
66 67 68 69 70
	/**
	 * Builds a Lua script for getting count of records
	 * @param ActiveQuery $query the query used to build the script
	 * @return string
	 */
71 72 73 74 75
	public function buildCount($query)
	{
		return $this->build($query, 'n=n+1', 'n');
	}

Carsten Brandt committed
76 77 78 79 80 81 82
	/**
	 * Builds a Lua script for finding the sum of a column
	 * @param ActiveQuery $query the query used to build the script
	 * @param string $column name of the column
	 * @return string
	 */
	public function buildSum($query, $column)
83
	{
Carsten Brandt committed
84
		/** @var ActiveRecord $modelClass */
85
		$modelClass = $query->modelClass;
86
		$key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
87
		return $this->build($query, "n=n+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'n');
88 89
	}

Carsten Brandt committed
90 91 92 93 94 95 96
	/**
	 * Builds a Lua script for finding the average of a column
	 * @param ActiveQuery $query the query used to build the script
	 * @param string $column name of the column
	 * @return string
	 */
	public function buildAverage($query, $column)
97
	{
Carsten Brandt committed
98
		/** @var ActiveRecord $modelClass */
99
		$modelClass = $query->modelClass;
100
		$key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
101
		return $this->build($query, "n=n+1 if v==nil then v=0 end v=v+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'v/n');
102 103
	}

Carsten Brandt committed
104 105 106 107 108 109 110
	/**
	 * Builds a Lua script for finding the min value of a column
	 * @param ActiveQuery $query the query used to build the script
	 * @param string $column name of the column
	 * @return string
	 */
	public function buildMin($query, $column)
111
	{
Carsten Brandt committed
112
		/** @var ActiveRecord $modelClass */
113
		$modelClass = $query->modelClass;
114
		$key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
115
		return $this->build($query, "n=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ") if v==nil or n<v then v=n end", 'v');
116 117
	}

Carsten Brandt committed
118 119 120 121 122 123 124
	/**
	 * Builds a Lua script for finding the max value of a column
	 * @param ActiveQuery $query the query used to build the script
	 * @param string $column name of the column
	 * @return string
	 */
	public function buildMax($query, $column)
125
	{
Carsten Brandt committed
126
		/** @var ActiveRecord $modelClass */
127
		$modelClass = $query->modelClass;
128
		$key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
129
		return $this->build($query, "n=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ") if v==nil or n>v then v=n end", 'v');
130 131
	}

132
	/**
Carsten Brandt committed
133 134 135
	 * @param ActiveQuery $query the query used to build the script
	 * @param string $buildResult the lua script for building the result
	 * @param string $return the lua variable that should be returned
136
	 * @throws yii\base\NotSupportedException when query contains unsupported order by condition
Carsten Brandt committed
137
	 * @return string
138
	 */
Carsten Brandt committed
139
	private function build($query, $buildResult, $return)
140
	{
141 142 143 144
		if (!empty($query->orderBy)) {
			throw new NotSupportedException('orderBy is currently not supported by redis ActiveRecord.');
		}

145
		$columns = [];
146 147 148 149 150 151 152 153 154
		if ($query->where !== null) {
			$condition = $this->buildCondition($query->where, $columns);
		} else {
			$condition = 'true';
		}

		$start = $query->offset === null ? 0 : $query->offset;
		$limitCondition = 'i>' . $start . ($query->limit === null ? '' : ' and i<=' . ($start + $query->limit));

155
		/** @var ActiveRecord $modelClass */
156
		$modelClass = $query->modelClass;
157
		$key = $this->quoteValue($modelClass::keyPrefix());
158
		$loadColumnValues = '';
AlexGx committed
159
		foreach ($columns as $column => $alias) {
160
			$loadColumnValues .= "local $alias=redis.call('HGET',$key .. ':a:' .. pk, '$column')\n";
161 162 163
		}

		return <<<EOF
164
local allpks=redis.call('LRANGE',$key,0,-1)
165 166
local pks={}
local n=0
167
local v=nil
168 169 170 171 172 173 174 175 176 177 178 179 180 181
local i=0
for k,pk in ipairs(allpks) do
    $loadColumnValues
    if $condition then
      i=i+1
      if $limitCondition then
        $buildResult
      end
    end
end
return $return
EOF;
	}

Carsten Brandt committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	/**
	 * Adds a column to the list of columns to retrieve and creates an alias
	 * @param string $column the column name to add
	 * @param array $columns list of columns given by reference
	 * @return string the alias generated for the column name
	 */
	private function addColumn($column, &$columns)
	{
		if (isset($columns[$column])) {
			return $columns[$column];
		}
		$name = 'c' . preg_replace("/[^A-z]+/", "", $column) . count($columns);
		return $columns[$column] = $name;
	}

197 198
	/**
	 * Quotes a string value for use in a query.
Carsten Brandt committed
199
	 * Note that if the parameter is not a string or int, it will be returned without change.
200 201 202
	 * @param string $str string to be quoted
	 * @return string the properly quoted string
	 */
Carsten Brandt committed
203
	private function quoteValue($str)
204 205 206 207 208 209 210 211 212
	{
		if (!is_string($str) && !is_int($str)) {
			return $str;
		}

		return "'" . addcslashes(str_replace("'", "\\'", $str), "\000\n\r\\\032") . "'";
	}

	/**
Carsten Brandt committed
213 214
	 * Parses the condition specification and generates the corresponding Lua expression.
	 * @param string|array $condition the condition specification. Please refer to [[ActiveQuery::where()]]
215
	 * on how to specify a condition.
Carsten Brandt committed
216
	 * @param array $columns the list of columns and aliases to be used
217 218
	 * @return string the generated SQL expression
	 * @throws \yii\db\Exception if the condition is in bad format
Carsten Brandt committed
219
	 * @throws \yii\base\NotSupportedException if the condition is not an array
220 221 222
	 */
	public function buildCondition($condition, &$columns)
	{
223
		static $builders = [
224
			'not' => 'buildNotCondition',
225 226 227 228 229 230 231 232 233 234
			'and' => 'buildAndCondition',
			'or' => 'buildAndCondition',
			'between' => 'buildBetweenCondition',
			'not between' => 'buildBetweenCondition',
			'in' => 'buildInCondition',
			'not in' => 'buildInCondition',
			'like' => 'buildLikeCondition',
			'not like' => 'buildLikeCondition',
			'or like' => 'buildLikeCondition',
			'or not like' => 'buildLikeCondition',
235
		];
236 237

		if (!is_array($condition)) {
Carsten Brandt committed
238
			throw new NotSupportedException('Where condition must be an array in redis ActiveRecord.');
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		}
		if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ...
			$operator = strtolower($condition[0]);
			if (isset($builders[$operator])) {
				$method = $builders[$operator];
				array_shift($condition);
				return $this->$method($operator, $condition, $columns);
			} else {
				throw new Exception('Found unknown operator in query: ' . $operator);
			}
		} else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
			return $this->buildHashCondition($condition, $columns);
		}
	}

	private function buildHashCondition($condition, &$columns)
	{
256
		$parts = [];
257 258
		foreach ($condition as $column => $value) {
			if (is_array($value)) { // IN condition
259
				$parts[] = $this->buildInCondition('in', [$column, $value], $columns);
260
			} else {
Carsten Brandt committed
261
				$column = $this->addColumn($column, $columns);
262
				if ($value === null) {
Carsten Brandt committed
263
					$parts[] = "$column==nil";
264 265 266 267 268 269 270 271 272 273 274
				} elseif ($value instanceof Expression) {
					$parts[] = "$column==" . $value->expression;
				} else {
					$value = $this->quoteValue($value);
					$parts[] = "$column==$value";
				}
			}
		}
		return count($parts) === 1 ? $parts[0] : '(' . implode(') and (', $parts) . ')';
	}

275 276 277 278 279 280 281 282 283 284 285 286 287
	private function buildNotCondition($operator, $operands, &$params)
	{
		if (count($operands) != 1) {
			throw new InvalidParamException("Operator '$operator' requires exactly one operand.");
		}

		$operand = reset($operands);
		if (is_array($operand)) {
			$operand = $this->buildCondition($operand, $params);
		}
		return "!($operand)";
	}

288 289
	private function buildAndCondition($operator, $operands, &$columns)
	{
290
		$parts = [];
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
		foreach ($operands as $operand) {
			if (is_array($operand)) {
				$operand = $this->buildCondition($operand, $columns);
			}
			if ($operand !== '') {
				$parts[] = $operand;
			}
		}
		if (!empty($parts)) {
			return '(' . implode(") $operator (", $parts) . ')';
		} else {
			return '';
		}
	}

	private function buildBetweenCondition($operator, $operands, &$columns)
	{
		if (!isset($operands[0], $operands[1], $operands[2])) {
			throw new Exception("Operator '$operator' requires three operands.");
		}

		list($column, $value1, $value2) = $operands;

		$value1 = $this->quoteValue($value1);
		$value2 = $this->quoteValue($value2);
Carsten Brandt committed
316
		$column = $this->addColumn($column, $columns);
317
		return "$column >= $value1 and $column <= $value2";
318 319 320 321 322 323 324 325 326 327 328 329
	}

	private function buildInCondition($operator, $operands, &$columns)
	{
		if (!isset($operands[0], $operands[1])) {
			throw new Exception("Operator '$operator' requires two operands.");
		}

		list($column, $values) = $operands;

		$values = (array)$values;

330
		if (empty($values) || $column === []) {
Carsten Brandt committed
331
			return $operator === 'in' ? 'false' : 'true';
332 333 334 335 336 337 338
		}

		if (count($column) > 1) {
			return $this->buildCompositeInCondition($operator, $column, $values, $columns);
		} elseif (is_array($column)) {
			$column = reset($column);
		}
Carsten Brandt committed
339
		$columnAlias = $this->addColumn($column, $columns);
340
		$parts = [];
341
		foreach ($values as $value) {
342 343 344 345
			if (is_array($value)) {
				$value = isset($value[$column]) ? $value[$column] : null;
			}
			if ($value === null) {
Carsten Brandt committed
346
				$parts[] = "$columnAlias==nil";
347
			} elseif ($value instanceof Expression) {
Carsten Brandt committed
348
				$parts[] = "$columnAlias==" . $value->expression;
349 350
			} else {
				$value = $this->quoteValue($value);
Carsten Brandt committed
351
				$parts[] = "$columnAlias==$value";
352 353
			}
		}
Carsten Brandt committed
354 355
		$operator = $operator === 'in' ? '' : 'not ';
		return "$operator(" . implode(' or ', $parts) . ')';
356 357
	}

Carsten Brandt committed
358
	protected function buildCompositeInCondition($operator, $inColumns, $values, &$columns)
359
	{
360
		$vss = [];
361
		foreach ($values as $value) {
362
			$vs = [];
Carsten Brandt committed
363 364
			foreach ($inColumns as $column) {
				$column = $this->addColumn($column, $columns);
365
				if (isset($value[$column])) {
Carsten Brandt committed
366
					$vs[] = "$column==" . $this->quoteValue($value[$column]);
367
				} else {
Carsten Brandt committed
368
					$vs[] = "$column==nil";
369 370
				}
			}
Carsten Brandt committed
371
			$vss[] = '(' . implode(' and ', $vs) . ')';
372
		}
Carsten Brandt committed
373 374
		$operator = $operator === 'in' ? '' : 'not ';
		return "$operator(" . implode(' or ', $vss) . ')';
375 376
	}

Carsten Brandt committed
377
	private function buildLikeCondition($operator, $operands, &$columns)
378
	{
Carsten Brandt committed
379
		throw new NotSupportedException('LIKE conditions are not suppoerted by redis ActiveRecord.');
380 381
	}
}