From 4459cb4f2be8146664be3abaa87434cd93db2dc2 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Fri, 22 Nov 2013 19:01:33 +0100 Subject: [PATCH] cleanup redis AR --- framework/yii/redis/ActiveQuery.php | 11 ++++++--- framework/yii/redis/LuaScriptBuilder.php | 40 +++++++------------------------- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/framework/yii/redis/ActiveQuery.php b/framework/yii/redis/ActiveQuery.php index ae6d9d1..eabd843 100644 --- a/framework/yii/redis/ActiveQuery.php +++ b/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 = []; diff --git a/framework/yii/redis/LuaScriptBuilder.php b/framework/yii/redis/LuaScriptBuilder.php index c151d49..81dff3f 100644 --- a/framework/yii/redis/LuaScriptBuilder.php +++ b/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 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'); @@ -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.'); } }