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()
{ {
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
if ($this->offset === null && $this->limit === null && $this->where === null) { if ($this->offset === null && $this->limit === null && $this->where === null) {
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
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();
}
} }
...@@ -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