Commit 130b6346 by Carsten Brandt

redis WIP

- relation support - completed and refactored lua script builder
parent e62e8487
......@@ -9,6 +9,7 @@
*/
namespace yii\redis;
use yii\base\NotSupportedException;
/**
* ActiveQuery represents a DB query associated with an Active Record class.
......@@ -89,18 +90,30 @@ class ActiveQuery extends \yii\base\Component
public $indexBy;
/**
* 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.
* Executes a script created by [[LuaScriptBuilder]]
* @param $type
* @param null $column
* @return array|bool|null|string
*/
public function all()
private function executeScript($type, $column=null)
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildAll($this);
$data = $db->executeCommand('EVAL', array($script, 0));
$method = 'build' . $type;
$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();
foreach($data as $dataRow) {
$row = array();
......@@ -130,13 +143,8 @@ class ActiveQuery extends \yii\base\Component
*/
public function one()
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildOne($this);
$data = $db->executeCommand('EVAL', array($script, 0));
// TODO add support for orderBy
$data = $this->executeScript('One');
if ($data === array()) {
return null;
}
......@@ -168,11 +176,7 @@ class ActiveQuery extends \yii\base\Component
public function column($column)
{
// TODO add support for indexBy and orderBy
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildColumn($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
return $this->executeScript('Column', $column);
}
/**
......@@ -183,14 +187,13 @@ class ActiveQuery extends \yii\base\Component
*/
public function count()
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
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()));
} else {
$script = $db->luaScriptBuilder->buildCount($this);
return $db->executeCommand('EVAL', array($script, 0));
return $this->executeScript('Count');
}
}
......@@ -201,11 +204,7 @@ class ActiveQuery extends \yii\base\Component
*/
public function sum($column)
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildSum($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
return $this->executeScript('Sum', $column);
}
/**
......@@ -216,11 +215,7 @@ class ActiveQuery extends \yii\base\Component
*/
public function average($column)
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildAverage($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
return $this->executeScript('Average', $column);
}
/**
......@@ -231,11 +226,7 @@ class ActiveQuery extends \yii\base\Component
*/
public function min($column)
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildMin($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
return $this->executeScript('Min', $column);
}
/**
......@@ -246,11 +237,7 @@ class ActiveQuery extends \yii\base\Component
*/
public function max($column)
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
$script = $db->luaScriptBuilder->buildMax($this, $column);
return $db->executeCommand('EVAL', array($script, 0));
return $this->executeScript('Max', $column);
}
/**
......@@ -294,7 +281,7 @@ class ActiveQuery extends \yii\base\Component
* (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
* (which means the column contains a DB expression).
* @return Query the query object itself
* @return ActiveQuery the query object itself
* @see addOrderBy()
*/
public function orderBy($columns)
......@@ -310,7 +297,7 @@ class ActiveQuery extends \yii\base\Component
* (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
* (which means the column contains a DB expression).
* @return Query the query object itself
* @return ActiveQuery the query object itself
* @see orderBy()
*/
public function addOrderBy($columns)
......@@ -326,6 +313,7 @@ class ActiveQuery extends \yii\base\Component
protected function normalizeOrderBy($columns)
{
throw new NotSupportedException('orderBy is currently not supported');
if (is_array($columns)) {
return $columns;
} else {
......
......@@ -9,6 +9,7 @@
*/
namespace yii\redis;
use yii\base\InvalidConfigException;
/**
* ActiveRecord is the base class for classes representing relational data in terms of objects.
......@@ -42,20 +43,70 @@ class ActiveRelation extends \yii\redis\ActiveQuery
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.
* @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.
*/
public function via($relationName)
public function via($relationName, $callable = null)
{
$relation = $this->primaryModel->getRelation($relationName);
$this->via = array($relationName, $relation);
if ($callable !== null) {
call_user_func($callable, $relation);
}
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.
* 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 array $primaryModels primary models
* @return array the related models
......@@ -68,14 +119,12 @@ class ActiveRelation extends \yii\redis\ActiveQuery
}
if ($this->via instanceof self) {
// TODO
// via pivot table
/** @var $viaQuery ActiveRelation */
$viaQuery = $this->via;
$viaModels = $viaQuery->findPivotRows($primaryModels);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// TODO
// via relation
/** @var $viaQuery ActiveRelation */
list($viaName, $viaQuery) = $this->via;
......@@ -185,7 +234,6 @@ class ActiveRelation extends \yii\redis\ActiveQuery
}
}
/**
* @param array $models
*/
......@@ -193,7 +241,7 @@ class ActiveRelation extends \yii\redis\ActiveQuery
{
$attributes = array_keys($this->link);
$values = array();
if (count($attributes) ===1) {
if (count($attributes) === 1) {
// single key
$attribute = reset($this->link);
foreach ($models as $model) {
......@@ -209,7 +257,22 @@ class ActiveRelation extends \yii\redis\ActiveQuery
$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
$this->assertFalse(Customer::find()->where(array('id' => 5))->exists());
}
// public function testFindLazy()
// {
// /** @var $customer Customer */
// $customer = Customer::find(2);
// $orders = $customer->orders;
// $this->assertEquals(2, count($orders));
//
// $orders = $customer->getOrders()->primaryKeys(array(3))->all();
// $this->assertEquals(1, count($orders));
// $this->assertEquals(3, $orders[0]->id);
// }
public function testFindLazy()
{
/** @var $customer Customer */
$customer = Customer::find(2);
$orders = $customer->orders;
$this->assertEquals(2, count($orders));
$orders = $customer->getOrders()->where(array('id' => 3))->all();
$this->assertEquals(1, count($orders));
$this->assertEquals(3, $orders[0]->id);
}
// 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