Class yii\redis\LuaScriptBuilder

Inheritanceyii\redis\LuaScriptBuilder » yii\base\BaseObject
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-redis/blob/master/src/LuaScriptBuilder.php

LuaScriptBuilder builds lua scripts used for retrieving data from redis.

Public Methods

Hide inherited methods

Method Description Defined By
buildAll() Builds a Lua script for finding a list of records yii\redis\LuaScriptBuilder
buildAverage() Builds a Lua script for finding the average of a column yii\redis\LuaScriptBuilder
buildColumn() Builds a Lua script for finding a column yii\redis\LuaScriptBuilder
buildCondition() Parses the condition specification and generates the corresponding Lua expression. yii\redis\LuaScriptBuilder
buildCount() Builds a Lua script for getting count of records yii\redis\LuaScriptBuilder
buildMax() Builds a Lua script for finding the max value of a column yii\redis\LuaScriptBuilder
buildMin() Builds a Lua script for finding the min value of a column yii\redis\LuaScriptBuilder
buildOne() Builds a Lua script for finding one record yii\redis\LuaScriptBuilder
buildSum() Builds a Lua script for finding the sum of a column yii\redis\LuaScriptBuilder

Method Details

Hide inherited methods

buildAll() public method

Builds a Lua script for finding a list of records

public string buildAll ( $query )
$query yii\redis\ActiveQuery

The query used to build the script

                public function buildAll($query)
{
    /* @var $modelClass ActiveRecord */
    $modelClass = $query->modelClass;
    $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
    return $this->build($query, "n=n+1 pks[n]=redis.call('HGETALL',$key .. pk)", 'pks');
}

            
buildAverage() public method

Builds a Lua script for finding the average of a column

public string buildAverage ( $query, $column )
$query yii\redis\ActiveQuery

The query used to build the script

$column string

Name of the column

                public function buildAverage($query, $column)
{
    /* @var $modelClass ActiveRecord */
    $modelClass = $query->modelClass;
    $key = $this->quoteValue($modelClass::keyPrefix() . ':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');
}

            
buildColumn() public method

Builds a Lua script for finding a column

public string buildColumn ( $query, $column )
$query yii\redis\ActiveQuery

The query used to build the script

$column string

Name of the column

                public function buildColumn($query, $column)
{
    // TODO add support for indexBy
    /* @var $modelClass ActiveRecord */
    $modelClass = $query->modelClass;
    $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
    return $this->build($query, "n=n+1 pks[n]=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'pks');
}

            
buildCompareCondition() protected method

protected void buildCompareCondition ( $operator, $operands, &$columns )
$operator
$operands
$columns

                protected function buildCompareCondition($operator, $operands, &$columns)
{
    if (!isset($operands[0], $operands[1])) {
        throw new Exception("Operator '$operator' requires two operands.");
    }
    list($column, $value) = $operands;
    $column = $this->addColumn($column, $columns);
    if (is_numeric($value)){
        return "tonumber($column) $operator $value";
    }
    $value = $this->quoteValue($value);
    return "$column $operator $value";
}

            
buildCompositeInCondition() protected method

protected void buildCompositeInCondition ( $operator, $inColumns, $values, &$columns )
$operator
$inColumns
$values
$columns

                protected function buildCompositeInCondition($operator, $inColumns, $values, &$columns)
{
    $vss = [];
    foreach ($values as $value) {
        $vs = [];
        foreach ($inColumns as $column) {
            if (isset($value[$column])) {
                $columnAlias = $this->addColumn($column, $columns);
                $vs[] = "$columnAlias==" . $this->quoteValue($value[$column]);
            } else {
                $vs[] = "redis.call('HEXISTS',key .. ':a:' .. pk, ".$this->quoteValue($column).")==0";
            }
        }
        $vss[] = '(' . implode(' and ', $vs) . ')';
    }
    $operator = $operator === 'in' ? '' : 'not ';
    return "$operator(" . implode(' or ', $vss) . ')';
}

            
buildCondition() public method

Parses the condition specification and generates the corresponding Lua expression.

public string buildCondition ( $condition, &$columns )
$condition string|array

The condition specification. Please refer to ActiveQuery::where() on how to specify a condition.

$columns array

The list of columns and aliases to be used

return string

The generated SQL expression

throws \yii\db\Exception

if the condition is in bad format

throws \yii\base\NotSupportedException

if the condition is not an array

                public function buildCondition($condition, &$columns)
{
    static $builders = [
        'not' => 'buildNotCondition',
        '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',
        '>' => 'buildCompareCondition',
        '>=' => 'buildCompareCondition',
        '<' => 'buildCompareCondition',
        '<=' => 'buildCompareCondition',
    ];
    if (!is_array($condition)) {
        throw new NotSupportedException('Where condition must be an array in redis ActiveRecord.');
    }
    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);
        }
        throw new Exception('Found unknown operator in query: ' . $operator);
    }
    // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
    return $this->buildHashCondition($condition, $columns);
}

            
buildCount() public method

Builds a Lua script for getting count of records

public string buildCount ( $query )
$query yii\redis\ActiveQuery

The query used to build the script

                public function buildCount($query)
{
    return $this->build($query, 'n=n+1', 'n');
}

            
buildMax() public method

Builds a Lua script for finding the max value of a column

public string buildMax ( $query, $column )
$query yii\redis\ActiveQuery

The query used to build the script

$column string

Name of the column

                public function buildMax($query, $column)
{
    /* @var $modelClass ActiveRecord */
    $modelClass = $query->modelClass;
    $key = $this->quoteValue($modelClass::keyPrefix() . ':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');
}

            
buildMin() public method

Builds a Lua script for finding the min value of a column

public string buildMin ( $query, $column )
$query yii\redis\ActiveQuery

The query used to build the script

$column string

Name of the column

                public function buildMin($query, $column)
{
    /* @var $modelClass ActiveRecord */
    $modelClass = $query->modelClass;
    $key = $this->quoteValue($modelClass::keyPrefix() . ':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');
}

            
buildOne() public method

Builds a Lua script for finding one record

public string buildOne ( $query )
$query yii\redis\ActiveQuery

The query used to build the script

                public function buildOne($query)
{
    /* @var $modelClass ActiveRecord */
    $modelClass = $query->modelClass;
    $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
    return $this->build($query, "do return redis.call('HGETALL',$key .. pk) end", 'pks');
}

            
buildSum() public method

Builds a Lua script for finding the sum of a column

public string buildSum ( $query, $column )
$query yii\redis\ActiveQuery

The query used to build the script

$column string

Name of the column

                public function buildSum($query, $column)
{
    /* @var $modelClass ActiveRecord */
    $modelClass = $query->modelClass;
    $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
    return $this->build($query, "n=n+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'n');
}