* @since 2.0 */ class LuaScriptBuilder extends \yii\base\Object { /** * Builds a Lua script for finding a list of records * @param ActiveQuery $query the query used to build the script * @return string */ public function buildAll($query) { // TODO add support for orderBy $modelClass = $query->modelClass; $key = $this->quoteValue($modelClass::tableName() . ':a:'); return $this->build($query, "n=n+1 pks[n]=redis.call('HGETALL',$key .. pk)", 'pks'); // TODO properly hash pk } /** * Builds a Lua script for finding one record * @param ActiveQuery $query the query used to build the script * @return string */ public function buildOne($query) { // TODO add support for orderBy $modelClass = $query->modelClass; $key = $this->quoteValue($modelClass::tableName() . ':a:'); return $this->build($query, "do return redis.call('HGETALL',$key .. pk) end", 'pks'); // TODO properly hash pk } /** * 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) { // TODO add support for orderBy and indexBy $modelClass = $query->modelClass; $key = $this->quoteValue($modelClass::tableName() . ':a:'); return $this->build($query, "n=n+1 pks[n]=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'pks'); // TODO properly hash pk } /** * Builds a Lua script for getting count of records * @param ActiveQuery $query the query used to build the script * @return string */ public function buildCount($query) { return $this->build($query, 'n=n+1', 'n'); } /** * 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) { $modelClass = $query->modelClass; $key = $this->quoteValue($modelClass::tableName() . ':a:'); return $this->build($query, "n=n+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'n'); // TODO properly hash pk } /** * 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) { $modelClass = $query->modelClass; $key = $this->quoteValue($modelClass::tableName() . ':a:'); 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'); // TODO properly hash pk } /** * 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) { $modelClass = $query->modelClass; $key = $this->quoteValue($modelClass::tableName() . ':a:'); return $this->build($query, "n=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ") if v==nil or nmodelClass; $key = $this->quoteValue($modelClass::tableName() . ':a:'); return $this->build($query, "n=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ") if v==nil or n>v then v=n end", 'v'); // TODO properly hash pk } /** * @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 * @return string */ private function build($query, $buildResult, $return) { $columns = array(); 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)); $modelClass = $query->modelClass; $key = $this->quoteValue($modelClass::tableName() . ':a:'); $loadColumnValues = ''; foreach($columns as $column => $alias) { $loadColumnValues .= "local $alias=redis.call('HGET',$key .. pk, '$column')\n"; // TODO properly hash pk } return << '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', ); if (!is_array($condition)) { throw new NotSupportedException('Where must be an array.'); } 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) { $parts = array(); foreach ($condition as $column => $value) { if (is_array($value)) { // IN condition $parts[] = $this->buildInCondition('in', array($column, $value), $columns); } else { $column = $this->addColumn($column, $columns); if ($value === null) { $parts[] = "$column==nil"; } 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) . ')'; } private function buildAndCondition($operator, $operands, &$columns) { $parts = array(); 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); $column = $this->addColumn($column, $columns); return "$column > $value1 and $column < $value2"; } 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; if (empty($values) || $column === array()) { return $operator === 'in' ? 'false' : 'true'; } if (count($column) > 1) { return $this->buildCompositeInCondition($operator, $column, $values, $columns); } elseif (is_array($column)) { $column = reset($column); } $columnAlias = $this->addColumn($column, $columns); $parts = array(); foreach ($values as $i => $value) { if (is_array($value)) { $value = isset($value[$column]) ? $value[$column] : null; } if ($value === null) { $parts[] = "$columnAlias==nil"; } elseif ($value instanceof Expression) { $parts[] = "$columnAlias==" . $value->expression; } else { $value = $this->quoteValue($value); $parts[] = "$columnAlias==$value"; } } $operator = $operator === 'in' ? '' : 'not '; return "$operator(" . implode(' or ', $parts) . ')'; } protected function buildCompositeInCondition($operator, $inColumns, $values, &$columns) { $vss = array(); foreach ($values as $value) { $vs = array(); foreach ($inColumns as $column) { $column = $this->addColumn($column, $columns); if (isset($value[$column])) { $vs[] = "$column==" . $this->quoteValue($value[$column]); } else { $vs[] = "$column==nil"; } } $vss[] = '(' . implode(' and ', $vs) . ')'; } $operator = $operator === 'in' ? '' : 'not '; return "$operator(" . implode(' or ', $vss) . ')'; } private function buildLikeCondition($operator, $operands, &$columns) { throw new NotSupportedException('LIKE is not yet supported.'); if (!isset($operands[0], $operands[1])) { throw new Exception("Operator '$operator' requires two operands."); } list($column, $values) = $operands; $values = (array)$values; if (empty($values)) { return $operator === 'like' || $operator === 'or like' ? 'false' : 'true'; } if ($operator === 'like' || $operator === 'not like') { $andor = ' and '; } else { $andor = ' or '; $operator = $operator === 'or like' ? 'like' : 'not like'; } $column = $this->addColumn($column, $columns); $parts = array(); foreach ($values as $value) { // TODO implement matching here correctly $value = $this->quoteValue($value); $parts[] = "$column $operator $value"; } return implode($andor, $parts); } }