Commit 130b6346 by Carsten Brandt

redis WIP

- relation support - completed and refactored lua script builder
parent e62e8487
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
*/ */
namespace yii\redis; namespace yii\redis;
use yii\base\NotSupportedException;
/** /**
* ActiveQuery represents a DB query associated with an Active Record class. * ActiveQuery represents a DB query associated with an Active Record class.
...@@ -89,18 +90,30 @@ class ActiveQuery extends \yii\base\Component ...@@ -89,18 +90,30 @@ class ActiveQuery extends \yii\base\Component
public $indexBy; public $indexBy;
/** /**
* Executes query and returns all results as an array. * Executes a script created by [[LuaScriptBuilder]]
* @return array the query results. If the query results in nothing, an empty array will be returned. * @param $type
* @param null $column
* @return array|bool|null|string
*/ */
public function all() private function executeScript($type, $column=null)
{ {
$modelClass = $this->modelClass; $modelClass = $this->modelClass;
/** @var Connection $db */ /** @var Connection $db */
$db = $modelClass::getDb(); $db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildAll($this); $method = 'build' . $type;
$data = $db->executeCommand('EVAL', array($script, 0)); $script = $db->getLuaScriptBuilder()->$method($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
}
/**
* Executes query and returns all results as an array.
* @return array the query results. If the query results in nothing, an empty array will be returned.
*/
public function all()
{
// TODO add support for orderBy
$data = $this->executeScript('All');
$rows = array(); $rows = array();
foreach($data as $dataRow) { foreach($data as $dataRow) {
$row = array(); $row = array();
...@@ -130,13 +143,8 @@ class ActiveQuery extends \yii\base\Component ...@@ -130,13 +143,8 @@ class ActiveQuery extends \yii\base\Component
*/ */
public function one() public function one()
{ {
$modelClass = $this->modelClass; // TODO add support for orderBy
/** @var Connection $db */ $data = $this->executeScript('One');
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildOne($this);
$data = $db->executeCommand('EVAL', array($script, 0));
if ($data === array()) { if ($data === array()) {
return null; return null;
} }
...@@ -168,11 +176,7 @@ class ActiveQuery extends \yii\base\Component ...@@ -168,11 +176,7 @@ class ActiveQuery extends \yii\base\Component
public function column($column) public function column($column)
{ {
// TODO add support for indexBy and orderBy // TODO add support for indexBy and orderBy
$modelClass = $this->modelClass; return $this->executeScript('Column', $column);
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildColumn($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
} }
/** /**
...@@ -183,14 +187,13 @@ class ActiveQuery extends \yii\base\Component ...@@ -183,14 +187,13 @@ class ActiveQuery extends \yii\base\Component
*/ */
public function count() public function count()
{ {
if ($this->offset === null && $this->limit === null && $this->where === null) {
$modelClass = $this->modelClass; $modelClass = $this->modelClass;
/** @var Connection $db */ /** @var Connection $db */
$db = $modelClass::getDb(); $db = $modelClass::getDb();
if ($this->offset === null && $this->limit === null && $this->where === null) {
return $db->executeCommand('LLEN', array($modelClass::tableName())); return $db->executeCommand('LLEN', array($modelClass::tableName()));
} else { } else {
$script = $db->luaScriptBuilder->buildCount($this); return $this->executeScript('Count');
return $db->executeCommand('EVAL', array($script, 0));
} }
} }
...@@ -201,11 +204,7 @@ class ActiveQuery extends \yii\base\Component ...@@ -201,11 +204,7 @@ class ActiveQuery extends \yii\base\Component
*/ */
public function sum($column) public function sum($column)
{ {
$modelClass = $this->modelClass; return $this->executeScript('Sum', $column);
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildSum($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
} }
/** /**
...@@ -216,11 +215,7 @@ class ActiveQuery extends \yii\base\Component ...@@ -216,11 +215,7 @@ class ActiveQuery extends \yii\base\Component
*/ */
public function average($column) public function average($column)
{ {
$modelClass = $this->modelClass; return $this->executeScript('Average', $column);
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildAverage($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
} }
/** /**
...@@ -231,11 +226,7 @@ class ActiveQuery extends \yii\base\Component ...@@ -231,11 +226,7 @@ class ActiveQuery extends \yii\base\Component
*/ */
public function min($column) public function min($column)
{ {
$modelClass = $this->modelClass; return $this->executeScript('Min', $column);
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildMin($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
} }
/** /**
...@@ -246,11 +237,7 @@ class ActiveQuery extends \yii\base\Component ...@@ -246,11 +237,7 @@ class ActiveQuery extends \yii\base\Component
*/ */
public function max($column) public function max($column)
{ {
$modelClass = $this->modelClass; return $this->executeScript('Max', $column);
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildMax($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
} }
/** /**
...@@ -294,7 +281,7 @@ class ActiveQuery extends \yii\base\Component ...@@ -294,7 +281,7 @@ class ActiveQuery extends \yii\base\Component
* (e.g. `array('id' => Query::SORT_ASC, 'name' => Query::SORT_DESC)`). * (e.g. `array('id' => Query::SORT_ASC, 'name' => Query::SORT_DESC)`).
* The method will automatically quote the column names unless a column contains some parenthesis * The method will automatically quote the column names unless a column contains some parenthesis
* (which means the column contains a DB expression). * (which means the column contains a DB expression).
* @return Query the query object itself * @return ActiveQuery the query object itself
* @see addOrderBy() * @see addOrderBy()
*/ */
public function orderBy($columns) public function orderBy($columns)
...@@ -310,7 +297,7 @@ class ActiveQuery extends \yii\base\Component ...@@ -310,7 +297,7 @@ class ActiveQuery extends \yii\base\Component
* (e.g. `array('id' => Query::SORT_ASC, 'name' => Query::SORT_DESC)`). * (e.g. `array('id' => Query::SORT_ASC, 'name' => Query::SORT_DESC)`).
* The method will automatically quote the column names unless a column contains some parenthesis * The method will automatically quote the column names unless a column contains some parenthesis
* (which means the column contains a DB expression). * (which means the column contains a DB expression).
* @return Query the query object itself * @return ActiveQuery the query object itself
* @see orderBy() * @see orderBy()
*/ */
public function addOrderBy($columns) public function addOrderBy($columns)
...@@ -326,6 +313,7 @@ class ActiveQuery extends \yii\base\Component ...@@ -326,6 +313,7 @@ class ActiveQuery extends \yii\base\Component
protected function normalizeOrderBy($columns) protected function normalizeOrderBy($columns)
{ {
throw new NotSupportedException('orderBy is currently not supported');
if (is_array($columns)) { if (is_array($columns)) {
return $columns; return $columns;
} else { } else {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
*/ */
namespace yii\redis; namespace yii\redis;
use yii\base\InvalidConfigException;
/** /**
* ActiveRecord is the base class for classes representing relational data in terms of objects. * ActiveRecord is the base class for classes representing relational data in terms of objects.
...@@ -42,20 +43,70 @@ class ActiveRelation extends \yii\redis\ActiveQuery ...@@ -42,20 +43,70 @@ class ActiveRelation extends \yii\redis\ActiveQuery
public $via; public $via;
/** /**
* Clones internal objects.
*/
public function __clone()
{
if (is_object($this->via)) {
// make a clone of "via" object so that the same query object can be reused multiple times
$this->via = clone $this->via;
}
}
/**
* Specifies the relation associated with the pivot table. * Specifies the relation associated with the pivot table.
* @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]]. * @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]].
* @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
* Its signature should be `function($query)`, where `$query` is the query to be customized.
* @return ActiveRelation the relation object itself. * @return ActiveRelation the relation object itself.
*/ */
public function via($relationName) public function via($relationName, $callable = null)
{ {
$relation = $this->primaryModel->getRelation($relationName); $relation = $this->primaryModel->getRelation($relationName);
$this->via = array($relationName, $relation); $this->via = array($relationName, $relation);
if ($callable !== null) {
call_user_func($callable, $relation);
}
return $this; return $this;
} }
/** /**
* Creates a DB command that can be used to execute this query.
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return Command the created DB command instance.
*/
protected function executeScript($type, $column=null)
{
if ($this->primaryModel !== null) {
// lazy loading
if ($this->via instanceof self) {
// via pivot table
$viaModels = $this->via->findPivotRows(array($this->primaryModel));
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/** @var $viaQuery ActiveRelation */
list($viaName, $viaQuery) = $this->via;
if ($viaQuery->multiple) {
$viaModels = $viaQuery->all();
$this->primaryModel->populateRelation($viaName, $viaModels);
} else {
$model = $viaQuery->one();
$this->primaryModel->populateRelation($viaName, $model);
$viaModels = $model === null ? array() : array($model);
}
$this->filterByModels($viaModels);
} else {
$this->filterByModels(array($this->primaryModel));
}
}
return parent::executeScript($type, $column);
}
/**
* Finds the related records and populates them into the primary models. * Finds the related records and populates them into the primary models.
* This method is internally by [[ActiveQuery]]. Do not call it directly. * This method is internally used by [[ActiveQuery]]. Do not call it directly.
* @param string $name the relation name * @param string $name the relation name
* @param array $primaryModels primary models * @param array $primaryModels primary models
* @return array the related models * @return array the related models
...@@ -68,14 +119,12 @@ class ActiveRelation extends \yii\redis\ActiveQuery ...@@ -68,14 +119,12 @@ class ActiveRelation extends \yii\redis\ActiveQuery
} }
if ($this->via instanceof self) { if ($this->via instanceof self) {
// TODO
// via pivot table // via pivot table
/** @var $viaQuery ActiveRelation */ /** @var $viaQuery ActiveRelation */
$viaQuery = $this->via; $viaQuery = $this->via;
$viaModels = $viaQuery->findPivotRows($primaryModels); $viaModels = $viaQuery->findPivotRows($primaryModels);
$this->filterByModels($viaModels); $this->filterByModels($viaModels);
} elseif (is_array($this->via)) { } elseif (is_array($this->via)) {
// TODO
// via relation // via relation
/** @var $viaQuery ActiveRelation */ /** @var $viaQuery ActiveRelation */
list($viaName, $viaQuery) = $this->via; list($viaName, $viaQuery) = $this->via;
...@@ -185,7 +234,6 @@ class ActiveRelation extends \yii\redis\ActiveQuery ...@@ -185,7 +234,6 @@ class ActiveRelation extends \yii\redis\ActiveQuery
} }
} }
/** /**
* @param array $models * @param array $models
*/ */
...@@ -193,7 +241,7 @@ class ActiveRelation extends \yii\redis\ActiveQuery ...@@ -193,7 +241,7 @@ class ActiveRelation extends \yii\redis\ActiveQuery
{ {
$attributes = array_keys($this->link); $attributes = array_keys($this->link);
$values = array(); $values = array();
if (count($attributes) ===1) { if (count($attributes) === 1) {
// single key // single key
$attribute = reset($this->link); $attribute = reset($this->link);
foreach ($models as $model) { foreach ($models as $model) {
...@@ -209,7 +257,22 @@ class ActiveRelation extends \yii\redis\ActiveQuery ...@@ -209,7 +257,22 @@ class ActiveRelation extends \yii\redis\ActiveQuery
$values[] = $v; $values[] = $v;
} }
} }
$this->primaryKeys($values); $this->andWhere(array('in', $attributes, array_unique($values, SORT_REGULAR)));
} }
/**
* @param ActiveRecord[] $primaryModels
* @return array
*/
private function findPivotRows($primaryModels)
{
if (empty($primaryModels)) {
return array();
}
$this->filterByModels($primaryModels);
/** @var $primaryModel ActiveRecord */
$primaryModel = reset($primaryModels);
$db = $primaryModel->getDb(); // TODO use different db in db overlapping relations
return $this->all();
}
} }
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
namespace yii\redis; namespace yii\redis;
use yii\base\NotSupportedException; use yii\base\NotSupportedException;
use yii\db\Exception;
use yii\db\Expression;
/** /**
* LuaScriptBuilder builds lua scripts used for retrieving data from redis. * LuaScriptBuilder builds lua scripts used for retrieving data from redis.
...@@ -17,67 +19,115 @@ use yii\base\NotSupportedException; ...@@ -17,67 +19,115 @@ use yii\base\NotSupportedException;
*/ */
class LuaScriptBuilder extends \yii\base\Object 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) public function buildAll($query)
{ {
// TODO add support for orderBy // TODO add support for orderBy
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $modelClass::tableName(); $key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "n=n+1 pks[n]=redis.call('HGETALL','$key:a:' .. pk)", 'pks'); // TODO quote 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) public function buildOne($query)
{ {
// TODO add support for orderBy // TODO add support for orderBy
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $modelClass::tableName(); $key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "do return redis.call('HGETALL','$key:a:' .. pk) end", 'pks'); // TODO quote return $this->build($query, "do return redis.call('HGETALL',$key .. pk) end", 'pks'); // TODO properly hash pk
} }
public function buildColumn($query, $field) /**
* 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 // TODO add support for orderBy and indexBy
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $modelClass::tableName(); $key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "n=n+1 pks[n]=redis.call('HGET','$key:a:' .. pk,'$field')", 'pks'); // TODO quote 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) public function buildCount($query)
{ {
return $this->build($query, 'n=n+1', 'n'); return $this->build($query, 'n=n+1', 'n');
} }
public function buildSum($query, $field) /**
* 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; $modelClass = $query->modelClass;
$key = $modelClass::tableName(); $key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "n=n+redis.call('HGET','$key:a:' .. pk,'$field')", 'n'); // TODO quote return $this->build($query, "n=n+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'n'); // TODO properly hash pk
} }
public function buildAverage($query, $field) /**
* 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; $modelClass = $query->modelClass;
$key = $modelClass::tableName(); $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:a:' .. pk,'$field')", 'v/n'); // TODO quote 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
} }
public function buildMin($query, $field) /**
* 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; $modelClass = $query->modelClass;
$key = $modelClass::tableName(); $key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "n=redis.call('HGET','$key:a:' .. pk,'$field') if v==nil or n<v then v=n end", 'v'); // TODO quote 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
} }
public function buildMax($query, $field) /**
* 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)
{ {
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $modelClass::tableName(); $key = $this->quoteValue($modelClass::tableName() . ':a:');
return $this->build($query, "n=redis.call('HGET','$key:a:' .. pk,'$field') if v==nil or n>v then v=n end", 'v'); // TODO quote 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 * @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
*/ */
public function build($query, $buildResult, $return) private function build($query, $buildResult, $return)
{ {
$columns = array(); $columns = array();
if ($query->where !== null) { if ($query->where !== null) {
...@@ -90,10 +140,10 @@ class LuaScriptBuilder extends \yii\base\Object ...@@ -90,10 +140,10 @@ class LuaScriptBuilder extends \yii\base\Object
$limitCondition = 'i>' . $start . ($query->limit === null ? '' : ' and i<=' . ($start + $query->limit)); $limitCondition = 'i>' . $start . ($query->limit === null ? '' : ' and i<=' . ($start + $query->limit));
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $modelClass::tableName(); $key = $this->quoteValue($modelClass::tableName() . ':a:');
$loadColumnValues = ''; $loadColumnValues = '';
foreach($columns as $column) { foreach($columns as $column => $alias) {
$loadColumnValues .= "local $column=redis.call('HGET','$key:a:' .. pk, '$column')\n"; // TODO properly hash pk $loadColumnValues .= "local $alias=redis.call('HGET',$key .. pk, '$column')\n"; // TODO properly hash pk
} }
return <<<EOF return <<<EOF
...@@ -116,13 +166,27 @@ EOF; ...@@ -116,13 +166,27 @@ EOF;
} }
/** /**
* 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;
}
/**
* Quotes a string value for use in a query. * Quotes a string value for use in a query.
* Note that if the parameter is not a string, it will be returned without change. * Note that if the parameter is not a string or int, it will be returned without change.
* @param string $str string to be quoted * @param string $str string to be quoted
* @return string the properly quoted string * @return string the properly quoted string
* @see http://www.php.net/manual/en/function.PDO-quote.php
*/ */
public function quoteValue($str) private function quoteValue($str)
{ {
if (!is_string($str) && !is_int($str)) { if (!is_string($str) && !is_int($str)) {
return $str; return $str;
...@@ -132,12 +196,13 @@ EOF; ...@@ -132,12 +196,13 @@ EOF;
} }
/** /**
* Parses the condition specification and generates the corresponding SQL expression. * Parses the condition specification and generates the corresponding Lua expression.
* @param string|array $condition the condition specification. Please refer to [[Query::where()]] * @param string|array $condition the condition specification. Please refer to [[ActiveQuery::where()]]
* on how to specify a condition. * on how to specify a condition.
* @param array $params the binding parameters to be populated * @param array $columns the list of columns and aliases to be used
* @return string the generated SQL expression * @return string the generated SQL expression
* @throws \yii\db\Exception if the condition is in bad format * @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) public function buildCondition($condition, &$columns)
{ {
...@@ -175,13 +240,12 @@ EOF; ...@@ -175,13 +240,12 @@ EOF;
{ {
$parts = array(); $parts = array();
foreach ($condition as $column => $value) { foreach ($condition as $column => $value) {
// TODO replace special chars and keywords in column name
$columns[$column] = $column;
if (is_array($value)) { // IN condition if (is_array($value)) { // IN condition
$parts[] = $this->buildInCondition('IN', array($column, $value), $columns); $parts[] = $this->buildInCondition('in', array($column, $value), $columns);
} else { } else {
$column = $this->addColumn($column, $columns);
if ($value === null) { if ($value === null) {
$parts[] = $column.'==nil'; $parts[] = "$column==nil";
} elseif ($value instanceof Expression) { } elseif ($value instanceof Expression) {
$parts[] = "$column==" . $value->expression; $parts[] = "$column==" . $value->expression;
} else { } else {
...@@ -219,16 +283,14 @@ EOF; ...@@ -219,16 +283,14 @@ EOF;
list($column, $value1, $value2) = $operands; list($column, $value1, $value2) = $operands;
// TODO replace special chars and keywords in column name
$value1 = $this->quoteValue($value1); $value1 = $this->quoteValue($value1);
$value2 = $this->quoteValue($value2); $value2 = $this->quoteValue($value2);
$columns[$column] = $column; $column = $this->addColumn($column, $columns);
return "$column > $value1 and $column < $value2"; return "$column > $value1 and $column < $value2";
} }
private function buildInCondition($operator, $operands, &$columns) private function buildInCondition($operator, $operands, &$columns)
{ {
// TODO adjust implementation to respect NOT IN operator
if (!isset($operands[0], $operands[1])) { if (!isset($operands[0], $operands[1])) {
throw new Exception("Operator '$operator' requires two operands."); throw new Exception("Operator '$operator' requires two operands.");
} }
...@@ -238,7 +300,7 @@ EOF; ...@@ -238,7 +300,7 @@ EOF;
$values = (array)$values; $values = (array)$values;
if (empty($values) || $column === array()) { if (empty($values) || $column === array()) {
return $operator === 'IN' ? '0==1' : ''; return $operator === 'in' ? 'false' : 'true';
} }
if (count($column) > 1) { if (count($column) > 1) {
...@@ -246,59 +308,47 @@ EOF; ...@@ -246,59 +308,47 @@ EOF;
} elseif (is_array($column)) { } elseif (is_array($column)) {
$column = reset($column); $column = reset($column);
} }
$columnAlias = $this->addColumn($column, $columns);
$parts = array(); $parts = array();
foreach ($values as $i => $value) { foreach ($values as $i => $value) {
if (is_array($value)) { if (is_array($value)) {
$value = isset($value[$column]) ? $value[$column] : null; $value = isset($value[$column]) ? $value[$column] : null;
} }
// TODO replace special chars and keywords in column name
if ($value === null) { if ($value === null) {
$parts[] = 'type('.$column.')=="nil"'; $parts[] = "$columnAlias==nil";
} elseif ($value instanceof Expression) { } elseif ($value instanceof Expression) {
$parts[] = "$column==" . $value->expression; $parts[] = "$columnAlias==" . $value->expression;
} else { } else {
$value = $this->quoteValue($value); $value = $this->quoteValue($value);
$parts[] = "$column==$value"; $parts[] = "$columnAlias==$value";
}
} }
if (count($parts) > 1) {
return "(" . implode(' or ', $parts) . ')';
} else {
$operator = $operator === 'IN' ? '' : '!';
return "$operator({$values[0]})";
} }
$operator = $operator === 'in' ? '' : 'not ';
return "$operator(" . implode(' or ', $parts) . ')';
} }
protected function buildCompositeInCondition($operator, $columns, $values, &$params) protected function buildCompositeInCondition($operator, $inColumns, $values, &$columns)
{ {
throw new NotSupportedException('composie IN is not yet supported.');
// TODO implement correclty
$vss = array(); $vss = array();
foreach ($values as $value) { foreach ($values as $value) {
$vs = array(); $vs = array();
foreach ($columns as $column) { foreach ($inColumns as $column) {
$column = $this->addColumn($column, $columns);
if (isset($value[$column])) { if (isset($value[$column])) {
$phName = self::PARAM_PREFIX . count($params); $vs[] = "$column==" . $this->quoteValue($value[$column]);
$params[$phName] = $value[$column];
$vs[] = $phName;
} else { } else {
$vs[] = 'NULL'; $vs[] = "$column==nil";
}
} }
$vss[] = '(' . implode(', ', $vs) . ')';
} }
foreach ($columns as $i => $column) { $vss[] = '(' . implode(' and ', $vs) . ')';
if (strpos($column, '(') === false) {
$columns[$i] = $this->db->quoteColumnName($column);
} }
} $operator = $operator === 'in' ? '' : 'not ';
return '(' . implode(', ', $columns) . ") $operator (" . implode(', ', $vss) . ')'; return "$operator(" . implode(' or ', $vss) . ')';
} }
private function buildLikeCondition($operator, $operands, &$params) private function buildLikeCondition($operator, $operands, &$columns)
{ {
throw new NotSupportedException('LIKE is not yet supported.'); throw new NotSupportedException('LIKE is not yet supported.');
// TODO implement correclty
if (!isset($operands[0], $operands[1])) { if (!isset($operands[0], $operands[1])) {
throw new Exception("Operator '$operator' requires two operands."); throw new Exception("Operator '$operator' requires two operands.");
} }
...@@ -308,25 +358,23 @@ EOF; ...@@ -308,25 +358,23 @@ EOF;
$values = (array)$values; $values = (array)$values;
if (empty($values)) { if (empty($values)) {
return $operator === 'LIKE' || $operator === 'OR LIKE' ? '0=1' : ''; return $operator === 'like' || $operator === 'or like' ? 'false' : 'true';
} }
if ($operator === 'LIKE' || $operator === 'NOT LIKE') { if ($operator === 'like' || $operator === 'not like') {
$andor = ' AND '; $andor = ' and ';
} else { } else {
$andor = ' OR '; $andor = ' or ';
$operator = $operator === 'OR LIKE' ? 'LIKE' : 'NOT LIKE'; $operator = $operator === 'or like' ? 'like' : 'not like';
} }
if (strpos($column, '(') === false) { $column = $this->addColumn($column, $columns);
$column = $this->db->quoteColumnName($column);
}
$parts = array(); $parts = array();
foreach ($values as $value) { foreach ($values as $value) {
$phName = self::PARAM_PREFIX . count($params); // TODO implement matching here correctly
$params[$phName] = $value; $value = $this->quoteValue($value);
$parts[] = "$column $operator $phName"; $parts[] = "$column $operator $value";
} }
return implode($andor, $parts); return implode($andor, $parts);
......
...@@ -239,17 +239,17 @@ class ActiveRecordTest extends RedisTestCase ...@@ -239,17 +239,17 @@ class ActiveRecordTest extends RedisTestCase
$this->assertFalse(Customer::find()->where(array('id' => 5))->exists()); $this->assertFalse(Customer::find()->where(array('id' => 5))->exists());
} }
// public function testFindLazy() public function testFindLazy()
// { {
// /** @var $customer Customer */ /** @var $customer Customer */
// $customer = Customer::find(2); $customer = Customer::find(2);
// $orders = $customer->orders; $orders = $customer->orders;
// $this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
//
// $orders = $customer->getOrders()->primaryKeys(array(3))->all(); $orders = $customer->getOrders()->where(array('id' => 3))->all();
// $this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
// $this->assertEquals(3, $orders[0]->id); $this->assertEquals(3, $orders[0]->id);
// } }
// public function testFindEager() // public function testFindEager()
// { // {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment