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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
454174a6
Commit
454174a6
authored
Dec 17, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added NOT operator for db, elasticsearch, redis
issue #1523
parent
d076da69
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
0 deletions
+54
-0
QueryBuilder.php
extensions/yii/elasticsearch/QueryBuilder.php
+14
-0
LuaScriptBuilder.php
extensions/yii/redis/LuaScriptBuilder.php
+14
-0
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
QueryBuilder.php
framework/yii/db/QueryBuilder.php
+25
-0
No files found.
extensions/yii/elasticsearch/QueryBuilder.php
View file @
454174a6
...
...
@@ -139,6 +139,7 @@ class QueryBuilder extends \yii\base\Object
public
function
buildCondition
(
$condition
)
{
static
$builders
=
array
(
'not'
=>
'buildNotCondition'
,
'and'
=>
'buildAndCondition'
,
'or'
=>
'buildAndCondition'
,
'between'
=>
'buildBetweenCondition'
,
...
...
@@ -196,6 +197,19 @@ class QueryBuilder extends \yii\base\Object
return
count
(
$parts
)
===
1
?
$parts
[
0
]
:
[
'and'
=>
$parts
];
}
private
function
buildNotCondition
(
$operator
,
$operands
,
&
$params
)
{
if
(
count
(
$operands
)
!=
1
)
{
throw
new
InvalidParamException
(
"Operator '
$operator
' requires exactly one operand."
);
}
$operand
=
reset
(
$operands
);
if
(
is_array
(
$operand
))
{
$operand
=
$this
->
buildCondition
(
$operand
,
$params
);
}
return
[
$operator
=>
$operand
];
}
private
function
buildAndCondition
(
$operator
,
$operands
)
{
$parts
=
[];
...
...
extensions/yii/redis/LuaScriptBuilder.php
View file @
454174a6
...
...
@@ -219,6 +219,7 @@ EOF;
public
function
buildCondition
(
$condition
,
&
$columns
)
{
static
$builders
=
[
'not'
=>
'buildNotCondition'
,
'and'
=>
'buildAndCondition'
,
'or'
=>
'buildAndCondition'
,
'between'
=>
'buildBetweenCondition'
,
...
...
@@ -269,6 +270,19 @@ EOF;
return
count
(
$parts
)
===
1
?
$parts
[
0
]
:
'('
.
implode
(
') and ('
,
$parts
)
.
')'
;
}
private
function
buildNotCondition
(
$operator
,
$operands
,
&
$params
)
{
if
(
count
(
$operands
)
!=
1
)
{
throw
new
InvalidParamException
(
"Operator '
$operator
' requires exactly one operand."
);
}
$operand
=
reset
(
$operands
);
if
(
is_array
(
$operand
))
{
$operand
=
$this
->
buildCondition
(
$operand
,
$params
);
}
return
"!(
$operand
)"
;
}
private
function
buildAndCondition
(
$operator
,
$operands
,
&
$columns
)
{
$parts
=
[];
...
...
framework/CHANGELOG.md
View file @
454174a6
...
...
@@ -14,6 +14,7 @@ Yii Framework 2 Change Log
-
Enh #1406: DB Schema support for Oracle Database (p0larbeer, qiangxue)
-
Enh #1437: Added ListView::viewParams (qiangxue)
-
Enh #1469: ActiveRecord::find() now works with default conditions (default scope) applied by createQuery (cebe)
-
Enh #1523: Query conditions now allow to use the NOT operator (cebe)
-
Enh #1552: It is now possible to use multiple bootstrap NavBar in a single page (Alex-Code)
-
Enh: Added
`favicon.ico`
and
`robots.txt`
to defauly application templates (samdark)
-
Enh: Added
`Widget::autoIdPrefix`
to support prefixing automatically generated widget IDs (qiangxue)
...
...
framework/yii/db/QueryBuilder.php
View file @
454174a6
...
...
@@ -788,6 +788,7 @@ class QueryBuilder extends \yii\base\Object
public
function
buildCondition
(
$condition
,
&
$params
)
{
static
$builders
=
[
'NOT'
=>
'buildNotCondition'
,
'AND'
=>
'buildAndCondition'
,
'OR'
=>
'buildAndCondition'
,
'BETWEEN'
=>
'buildBetweenCondition'
,
...
...
@@ -878,6 +879,30 @@ class QueryBuilder extends \yii\base\Object
}
/**
* Inverts an SQL expressions with `NOT` operator.
* @param string $operator the operator to use for connecting the given operands
* @param array $operands the SQL expressions to connect.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @throws InvalidParamException if wrong number of operands have been given.
*/
public
function
buildNotCondition
(
$operator
,
$operands
,
&
$params
)
{
if
(
count
(
$operands
)
!=
1
)
{
throw
new
InvalidParamException
(
"Operator '
$operator
' requires exactly one operand."
);
}
$operand
=
reset
(
$operands
);
if
(
is_array
(
$operand
))
{
$operand
=
$this
->
buildCondition
(
$operand
,
$params
);
}
if
(
$operand
===
''
)
{
return
''
;
}
return
"
$operator
(
$operand
)"
;
}
/**
* Creates an SQL expressions with the `BETWEEN` operator.
* @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`)
* @param array $operands the first operand is the column name. The second and third operands
...
...
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