Browse Source

cleanup redis AR

tags/2.0.0-alpha
Carsten Brandt 11 years ago
parent
commit
4459cb4f2b
  1. 11
      framework/yii/redis/ActiveQuery.php
  2. 40
      framework/yii/redis/LuaScriptBuilder.php

11
framework/yii/redis/ActiveQuery.php

@ -127,6 +127,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
public function count($q = '*', $db = null)
{
if ($this->offset === null && $this->limit === null && $this->where === null) {
/** @var ActiveRecord $modelClass */
$modelClass = $this->modelClass;
if ($db === null) {
$db = $modelClass::getDb();
@ -157,7 +158,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
*/
public function column($column, $db = null)
{
// TODO add support for indexBy and orderBy
// TODO add support for orderBy
return $this->executeScript($db, 'Column', $column);
}
@ -242,6 +243,10 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
*/
protected function executeScript($db, $type, $columnName = null)
{
if (!empty($this->orderBy)) {
throw new NotSupportedException('orderBy is currently not supported by redis ActiveRecord.');
}
/** @var ActiveRecord $modelClass */
$modelClass = $this->modelClass;
@ -266,6 +271,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
* @param string $type the type of the script to generate
* @param string $columnName
* @return array|bool|null|string
* @throws \yii\base\InvalidParamException
* @throws \yii\base\NotSupportedException
*/
private function findByPk($db, $type, $columnName = null)
@ -276,7 +282,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
foreach($this->where as $column => $values) {
if (is_array($values)) {
// TODO support composite IN for composite PK
throw new NotSupportedException('find by composite PK is not yet implemented.');
throw new NotSupportedException('Find by composite PK is not supported by redis ActiveRecord.');
}
}
$pks = [$this->where];
@ -310,7 +316,6 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
case 'Count':
return count($data);
case 'Column':
// TODO support indexBy
$column = [];
foreach($data as $dataRow) {
$row = [];

40
framework/yii/redis/LuaScriptBuilder.php

@ -27,6 +27,7 @@ class LuaScriptBuilder extends \yii\base\Object
public function buildAll($query)
{
// TODO add support for orderBy
/** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "n=n+1 pks[n]=redis.call('HGETALL',$key .. pk)", 'pks');
@ -40,6 +41,7 @@ class LuaScriptBuilder extends \yii\base\Object
public function buildOne($query)
{
// TODO add support for orderBy
/** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "do return redis.call('HGETALL',$key .. pk) end", 'pks');
@ -54,6 +56,7 @@ class LuaScriptBuilder extends \yii\base\Object
public function buildColumn($query, $column)
{
// TODO add support for orderBy and indexBy
/** @var ActiveRecord $modelClass */
$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');
@ -77,6 +80,7 @@ class LuaScriptBuilder extends \yii\base\Object
*/
public function buildSum($query, $column)
{
/** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "n=n+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'n');
@ -90,6 +94,7 @@ class LuaScriptBuilder extends \yii\base\Object
*/
public function buildAverage($query, $column)
{
/** @var ActiveRecord $modelClass */
$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');
@ -103,6 +108,7 @@ class LuaScriptBuilder extends \yii\base\Object
*/
public function buildMin($query, $column)
{
/** @var ActiveRecord $modelClass */
$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 n<v then v=n end", 'v');
@ -116,6 +122,7 @@ class LuaScriptBuilder extends \yii\base\Object
*/
public function buildMax($query, $column)
{
/** @var ActiveRecord $modelClass */
$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 n>v then v=n end", 'v');
@ -225,7 +232,7 @@ EOF;
];
if (!is_array($condition)) {
throw new NotSupportedException('Where must be an array.');
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]);
@ -353,35 +360,6 @@ EOF;
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 = [];
foreach ($values as $value) {
// TODO implement matching here correctly
$value = $this->quoteValue($value);
$parts[] = "$column $operator $value";
}
return implode($andor, $parts);
throw new NotSupportedException('LIKE conditions are not suppoerted by redis ActiveRecord.');
}
}

Loading…
Cancel
Save