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
58b1538b
Commit
58b1538b
authored
Nov 24, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored elasticsearch Querybuilder build conditions
parent
f8b53197
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
83 deletions
+73
-83
QueryBuilder.php
framework/yii/db/QueryBuilder.php
+3
-3
Command.php
framework/yii/elasticsearch/Command.php
+3
-1
QueryBuilder.php
framework/yii/elasticsearch/QueryBuilder.php
+67
-79
No files found.
framework/yii/db/QueryBuilder.php
View file @
58b1538b
...
@@ -863,12 +863,12 @@ class QueryBuilder extends \yii\base\Object
...
@@ -863,12 +863,12 @@ class QueryBuilder extends \yii\base\Object
* describe the interval that column value should be in.
* describe the interval that column value should be in.
* @param array $params the binding parameters to be populated
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @return string the generated SQL expression
* @throws Exception if wrong number of operands have been given.
* @throws
InvalidParam
Exception if wrong number of operands have been given.
*/
*/
public
function
buildBetweenCondition
(
$operator
,
$operands
,
&
$params
)
public
function
buildBetweenCondition
(
$operator
,
$operands
,
&
$params
)
{
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
],
$operands
[
2
]))
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
],
$operands
[
2
]))
{
throw
new
Exception
(
"Operator '
$operator
' requires three operands."
);
throw
new
InvalidParam
Exception
(
"Operator '
$operator
' requires three operands."
);
}
}
list
(
$column
,
$value1
,
$value2
)
=
$operands
;
list
(
$column
,
$value1
,
$value2
)
=
$operands
;
...
@@ -983,7 +983,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -983,7 +983,7 @@ class QueryBuilder extends \yii\base\Object
public
function
buildLikeCondition
(
$operator
,
$operands
,
&
$params
)
public
function
buildLikeCondition
(
$operator
,
$operands
,
&
$params
)
{
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
]))
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
]))
{
throw
new
Exception
(
"Operator '
$operator
' requires two operands."
);
throw
new
InvalidParam
Exception
(
"Operator '
$operator
' requires two operands."
);
}
}
list
(
$column
,
$values
)
=
$operands
;
list
(
$column
,
$values
)
=
$operands
;
...
...
framework/yii/elasticsearch/Command.php
View file @
58b1538b
...
@@ -60,7 +60,9 @@ class Command extends Component
...
@@ -60,7 +60,9 @@ class Command extends Component
try
{
try
{
$response
=
$this
->
db
->
http
()
->
post
(
$this
->
createUrl
(
$url
,
$options
),
null
,
$query
)
->
send
();
$response
=
$this
->
db
->
http
()
->
post
(
$this
->
createUrl
(
$url
,
$options
),
null
,
$query
)
->
send
();
}
catch
(
ClientErrorResponseException
$e
)
{
}
catch
(
ClientErrorResponseException
$e
)
{
throw
new
Exception
(
"elasticsearch error:
\n\n
"
.
$query
.
"
\n\n
"
.
$e
->
getMessage
()
.
$e
->
getResponse
()
->
getBody
(
true
),
[],
0
,
$e
);
throw
new
Exception
(
"elasticsearch error:
\n\n
"
.
$query
.
"
\n\n
"
.
$e
->
getMessage
()
.
print_r
(
Json
::
decode
(
$e
->
getResponse
()
->
getBody
(
true
)),
true
),
[],
0
,
$e
);
}
}
return
Json
::
decode
(
$response
->
getBody
(
true
))[
'hits'
];
return
Json
::
decode
(
$response
->
getBody
(
true
))[
'hits'
];
}
}
...
...
framework/yii/elasticsearch/QueryBuilder.php
View file @
58b1538b
...
@@ -55,8 +55,19 @@ class QueryBuilder extends \yii\base\Object
...
@@ -55,8 +55,19 @@ class QueryBuilder extends \yii\base\Object
$parts
[
'from'
]
=
(
int
)
$query
->
offset
;
$parts
[
'from'
]
=
(
int
)
$query
->
offset
;
}
}
$this
->
buildCondition
(
$parts
,
$query
->
where
);
$filters
=
empty
(
$query
->
filter
)
?
[]
:
[
$query
->
filter
];
$this
->
buildOrderBy
(
$parts
,
$query
->
orderBy
);
$whereFilter
=
$this
->
buildCondition
(
$query
->
where
);
if
(
!
empty
(
$whereFilter
))
{
$filters
[]
=
$whereFilter
;
}
if
(
!
empty
(
$filters
))
{
$parts
[
'filter'
]
=
count
(
$filters
)
>
1
?
[
'and'
=>
$filters
]
:
$filters
[
0
];
}
$sort
=
$this
->
buildOrderBy
(
$query
->
orderBy
);
if
(
!
empty
(
$sort
))
{
$parts
[
'sort'
]
=
$sort
;
}
if
(
empty
(
$parts
[
'query'
]))
{
if
(
empty
(
$parts
[
'query'
]))
{
$parts
[
'query'
]
=
[
"match_all"
=>
(
object
)[]];
$parts
[
'query'
]
=
[
"match_all"
=>
(
object
)[]];
...
@@ -75,12 +86,12 @@ class QueryBuilder extends \yii\base\Object
...
@@ -75,12 +86,12 @@ class QueryBuilder extends \yii\base\Object
/**
/**
* adds order by condition to the query
* adds order by condition to the query
*/
*/
public
function
buildOrderBy
(
&
$query
,
$columns
)
public
function
buildOrderBy
(
$columns
)
{
{
if
(
empty
(
$columns
))
{
if
(
empty
(
$columns
))
{
return
;
return
[]
;
}
}
$orders
=
array
()
;
$orders
=
[]
;
foreach
(
$columns
as
$name
=>
$direction
)
{
foreach
(
$columns
as
$name
=>
$direction
)
{
// allow elasticsearch extended syntax as described in http://www.elasticsearch.org/guide/reference/api/search/sort/
// allow elasticsearch extended syntax as described in http://www.elasticsearch.org/guide/reference/api/search/sort/
if
(
is_array
(
$direction
))
{
if
(
is_array
(
$direction
))
{
...
@@ -91,7 +102,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -91,7 +102,7 @@ class QueryBuilder extends \yii\base\Object
$orders
[]
=
array
(
$name
=>
(
$direction
===
SORT_DESC
?
'desc'
:
'asc'
));
$orders
[]
=
array
(
$name
=>
(
$direction
===
SORT_DESC
?
'desc'
:
'asc'
));
}
}
}
}
$query
[
'sort'
]
=
$orders
;
return
$orders
;
}
}
/**
/**
...
@@ -102,121 +113,102 @@ class QueryBuilder extends \yii\base\Object
...
@@ -102,121 +113,102 @@ class QueryBuilder extends \yii\base\Object
* @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
*/
*/
public
function
buildCondition
(
&
$query
,
$condition
)
public
function
buildCondition
(
$condition
)
{
{
static
$builders
=
array
(
static
$builders
=
array
(
'
AND
'
=>
'buildAndCondition'
,
'
and
'
=>
'buildAndCondition'
,
'
OR
'
=>
'buildAndCondition'
,
'
or
'
=>
'buildAndCondition'
,
'
BETWEEN
'
=>
'buildBetweenCondition'
,
'
between
'
=>
'buildBetweenCondition'
,
'
NOT BETWEEN
'
=>
'buildBetweenCondition'
,
'
not between
'
=>
'buildBetweenCondition'
,
'
IN
'
=>
'buildInCondition'
,
'
in
'
=>
'buildInCondition'
,
'
NOT IN
'
=>
'buildInCondition'
,
'
not in
'
=>
'buildInCondition'
,
'
LIKE
'
=>
'buildLikeCondition'
,
'
like
'
=>
'buildLikeCondition'
,
'
NOT LIKE
'
=>
'buildLikeCondition'
,
'
not like
'
=>
'buildLikeCondition'
,
'
OR LIKE
'
=>
'buildLikeCondition'
,
'
or like
'
=>
'buildLikeCondition'
,
'
OR NOT LIKE
'
=>
'buildLikeCondition'
,
'
or not like
'
=>
'buildLikeCondition'
,
);
);
if
(
empty
(
$condition
))
{
if
(
empty
(
$condition
))
{
return
;
return
[]
;
}
}
if
(
!
is_array
(
$condition
))
{
if
(
!
is_array
(
$condition
))
{
throw
new
NotSupportedException
(
'String conditions in where() are not supported by elasticsearch.'
);
throw
new
NotSupportedException
(
'String conditions in where() are not supported by elasticsearch.'
);
}
}
if
(
isset
(
$condition
[
0
]))
{
// operator format: operator, operand 1, operand 2, ...
if
(
isset
(
$condition
[
0
]))
{
// operator format: operator, operand 1, operand 2, ...
$operator
=
strto
upp
er
(
$condition
[
0
]);
$operator
=
strto
low
er
(
$condition
[
0
]);
if
(
isset
(
$builders
[
$operator
]))
{
if
(
isset
(
$builders
[
$operator
]))
{
$method
=
$builders
[
$operator
];
$method
=
$builders
[
$operator
];
array_shift
(
$condition
);
array_shift
(
$condition
);
$this
->
$method
(
$query
,
$operator
,
$condition
);
return
$this
->
$method
(
$operator
,
$condition
);
}
else
{
}
else
{
throw
new
InvalidParamException
(
'Found unknown operator in query: '
.
$operator
);
throw
new
InvalidParamException
(
'Found unknown operator in query: '
.
$operator
);
}
}
}
else
{
// hash format: 'column1' => 'value1', 'column2' => 'value2', ...
}
else
{
// hash format: 'column1' => 'value1', 'column2' => 'value2', ...
$this
->
buildHashCondition
(
$query
,
$condition
);
return
$this
->
buildHashCondition
(
$condition
);
}
}
}
}
private
function
buildHashCondition
(
&
$query
,
$condition
)
private
function
buildHashCondition
(
$condition
)
{
{
$parts
=
[];
foreach
(
$condition
as
$attribute
=>
$value
)
{
foreach
(
$condition
as
$attribute
=>
$value
)
{
// ['query']['filteredQuery']
$query
[
'filter'
][
'bool'
][
'must'
][]
=
array
(
'term'
=>
array
(
$attribute
=>
$value
),
);
}
return
;
// TODO more
$parts
=
array
();
foreach
(
$condition
as
$column
=>
$value
)
{
if
(
is_array
(
$value
))
{
// IN condition
if
(
is_array
(
$value
))
{
// IN condition
$parts
[]
=
$this
->
buildInCondition
(
'IN'
,
array
(
$column
,
$value
),
$params
)
;
$parts
[]
=
[
'in'
=>
[
$attribute
=>
$value
]]
;
}
else
{
}
else
{
if
(
$value
===
null
)
{
if
(
$value
===
null
)
{
$parts
[]
=
"
$column
IS NULL"
;
// TODO null
$parts
[]
=
[
'missing'
=>
[
'field'
=>
$attribute
,
'existence'
=>
true
,
'null_value'
=>
true
]];
}
elseif
(
$value
instanceof
Expression
)
{
$parts
[]
=
"
$column
="
.
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
$params
[
$n
]
=
$v
;
}
}
else
{
}
else
{
$phName
=
self
::
PARAM_PREFIX
.
count
(
$params
);
$parts
[]
=
[
'term'
=>
[
$attribute
=>
$value
]];
$parts
[]
=
"
$column
=
$phName
"
;
$params
[
$phName
]
=
$value
;
}
}
}
}
}
}
return
count
(
$parts
)
===
1
?
$parts
[
0
]
:
'('
.
implode
(
') AND ('
,
$parts
)
.
')'
;
return
count
(
$parts
)
===
1
?
$parts
[
0
]
:
[
'and'
=>
$parts
]
;
}
}
private
function
buildAndCondition
(
$operator
,
$operands
,
&
$params
)
private
function
buildAndCondition
(
$operator
,
$operands
)
{
{
$parts
=
array
()
;
$parts
=
[]
;
foreach
(
$operands
as
$operand
)
{
foreach
(
$operands
as
$operand
)
{
if
(
is_array
(
$operand
))
{
if
(
is_array
(
$operand
))
{
$operand
=
$this
->
buildCondition
(
$operand
,
$params
);
$operand
=
$this
->
buildCondition
(
$operand
);
}
}
if
(
$operand
!==
''
)
{
if
(
!
empty
(
$operand
)
)
{
$parts
[]
=
$operand
;
$parts
[]
=
$operand
;
}
}
}
}
if
(
!
empty
(
$parts
))
{
if
(
!
empty
(
$parts
))
{
return
'('
.
implode
(
")
$operator
("
,
$parts
)
.
')'
;
return
[
$operator
=>
$parts
]
;
}
else
{
}
else
{
return
''
;
return
[]
;
}
}
}
}
private
function
buildBetweenCondition
(
$operator
,
$operands
,
&
$params
)
private
function
buildBetweenCondition
(
$operator
,
$operands
)
{
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
],
$operands
[
2
]))
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
],
$operands
[
2
]))
{
throw
new
Exception
(
"Operator '
$operator
' requires three operands."
);
throw
new
InvalidParam
Exception
(
"Operator '
$operator
' requires three operands."
);
}
}
list
(
$column
,
$value1
,
$value2
)
=
$operands
;
list
(
$column
,
$value1
,
$value2
)
=
$operands
;
$filter
=
[
'range'
=>
[
$column
=>
[
'gte'
=>
$value1
,
'lte'
=>
$value2
]]];
if
(
strpos
(
$column
,
'('
)
===
false
)
{
if
(
$operator
==
'not between'
)
{
$
column
=
$this
->
db
->
quoteColumnName
(
$column
)
;
$
filter
=
[
'not'
=>
$filter
]
;
}
}
$phName1
=
self
::
PARAM_PREFIX
.
count
(
$params
);
return
$filter
;
$params
[
$phName1
]
=
$value1
;
$phName2
=
self
::
PARAM_PREFIX
.
count
(
$params
);
$params
[
$phName2
]
=
$value2
;
return
"
$column
$operator
$phName1
AND
$phName2
"
;
}
}
private
function
buildInCondition
(
$operator
,
$operands
,
&
$params
)
private
function
buildInCondition
(
$operator
,
$operands
,
&
$params
)
{
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
]))
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
]))
{
throw
new
Exception
(
"Operator '
$operator
' requires two operands."
);
throw
new
InvalidParam
Exception
(
"Operator '
$operator
' requires two operands."
);
}
}
list
(
$column
,
$values
)
=
$operands
;
list
(
$column
,
$values
)
=
$operands
;
$values
=
(
array
)
$values
;
$values
=
(
array
)
$values
;
if
(
empty
(
$values
)
||
$column
===
array
()
)
{
if
(
empty
(
$values
)
||
$column
===
[]
)
{
return
$operator
===
'
IN'
?
'0=1'
:
''
;
return
$operator
===
'
in'
?
[
'script'
=>
[
'script'
=>
'0=1'
]]
:
[]
;
}
}
if
(
count
(
$column
)
>
1
)
{
if
(
count
(
$column
)
>
1
)
{
...
@@ -224,37 +216,33 @@ class QueryBuilder extends \yii\base\Object
...
@@ -224,37 +216,33 @@ class QueryBuilder extends \yii\base\Object
}
elseif
(
is_array
(
$column
))
{
}
elseif
(
is_array
(
$column
))
{
$column
=
reset
(
$column
);
$column
=
reset
(
$column
);
}
}
$canBeNull
=
false
;
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
s
[
$i
]
=
$value
=
isset
(
$value
[
$column
])
?
$value
[
$column
]
:
null
;
}
}
if
(
$value
===
null
)
{
if
(
$value
===
null
)
{
$values
[
$i
]
=
'NULL'
;
$canBeNull
=
true
;
}
elseif
(
$value
instanceof
Expression
)
{
unset
(
$values
[
$i
]);
$values
[
$i
]
=
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
$params
[
$n
]
=
$v
;
}
}
}
else
{
$phName
=
self
::
PARAM_PREFIX
.
count
(
$params
);
$params
[
$phName
]
=
$value
;
$values
[
$i
]
=
$phName
;
}
}
if
(
empty
(
$values
)
&&
$canBeNull
)
{
return
[
'missing'
=>
[
'field'
=>
$column
,
'existence'
=>
true
,
'null_value'
=>
true
]];
}
else
{
$filter
=
[
'in'
=>
[
$column
=>
$values
]];
if
(
$canBeNull
)
{
$filter
=
[
'or'
=>
[
$filter
,
[
'missing'
=>
[
'field'
=>
$column
,
'existence'
=>
true
,
'null_value'
=>
true
]]]];
}
}
if
(
strpos
(
$column
,
'('
)
===
false
)
{
if
(
$operator
==
'not in'
)
{
$column
=
$this
->
db
->
quoteColumnName
(
$column
)
;
$filter
=
[
'not'
=>
$filter
]
;
}
}
return
$filter
;
if
(
count
(
$values
)
>
1
)
{
return
"
$column
$operator
("
.
implode
(
', '
,
$values
)
.
')'
;
}
else
{
$operator
=
$operator
===
'IN'
?
'='
:
'<>'
;
return
"
$column$operator
{
$values
[
0
]
}
"
;
}
}
}
}
protected
function
buildCompositeInCondition
(
$operator
,
$columns
,
$values
,
&
$params
)
protected
function
buildCompositeInCondition
(
$operator
,
$columns
,
$values
,
&
$params
)
{
{
throw
new
NotSupportedException
(
'composite in is not supported by elasticsearch.'
);
$vss
=
array
();
$vss
=
array
();
foreach
(
$values
as
$value
)
{
foreach
(
$values
as
$value
)
{
$vs
=
array
();
$vs
=
array
();
...
...
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