Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
4459cb4f
Commit
4459cb4f
authored
Nov 22, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleanup redis AR
parent
cb4504a1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
34 deletions
+17
-34
ActiveQuery.php
framework/yii/redis/ActiveQuery.php
+8
-3
LuaScriptBuilder.php
framework/yii/redis/LuaScriptBuilder.php
+9
-31
No files found.
framework/yii/redis/ActiveQuery.php
View file @
4459cb4f
...
...
@@ -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 implemente
d.'
);
throw
new
NotSupportedException
(
'
Find by composite PK is not supported by redis ActiveRecor
d.'
);
}
}
$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
=
[];
...
...
framework/yii/redis/LuaScriptBuilder.php
View file @
4459cb4f
...
...
@@ -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.'
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment