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
6a5b8d19
Commit
6a5b8d19
authored
Nov 20, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sphinx Query refactored.
parent
9a45a043
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
152 deletions
+19
-152
Query.php
extensions/sphinx/Query.php
+5
-138
QueryBuilder.php
extensions/sphinx/QueryBuilder.php
+2
-2
QueryTest.php
tests/unit/extensions/sphinx/QueryTest.php
+12
-12
No files found.
extensions/sphinx/Query.php
View file @
6a5b8d19
...
...
@@ -12,25 +12,20 @@ use yii\base\Component;
use
yii\base\InvalidCallException
;
use
yii\db\Expression
;
use
yii\db\QueryInterface
;
use
yii\db\QueryTrait
;
/**
* Class Query
*
*
* Note: implicit LIMIT 0,20 is present by default.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class
Query
extends
Component
implements
QueryInterface
{
/**
* Sort ascending
* @see orderBy
*/
const
SORT_ASC
=
false
;
/**
* Sort descending
* @see orderBy
*/
const
SORT_DESC
=
true
;
use
QueryTrait
;
/**
* @var array the columns being selected. For example, `['id', 'group_id']`.
...
...
@@ -59,30 +54,6 @@ class Query extends Component implements QueryInterface
*/
public
$match
;
/**
* @var string|array query condition. This refers to the WHERE clause in a SQL statement.
* For example, `group_id > 5 AND team = 1`.
* @see where()
*/
public
$where
;
/**
* @var integer maximum number of records to be returned.
* Note: if not set implicit LIMIT 0,20 is present by default.
*/
public
$limit
;
/**
* @var integer zero-based offset from where the records are to be returned. If not set or
* less than 0, it means starting from the beginning.
* Note: implicit LIMIT 0,20 is present by default.
*/
public
$offset
;
/**
* @var array how to sort the query results. This is used to construct the ORDER BY clause in a SQL statement.
* The array keys are the columns to be sorted by, and the array values are the corresponding sort directions which
* can be either [[Query::SORT_ASC]] or [[Query::SORT_DESC]]. The array may also contain [[Expression]] objects.
* If that is the case, the expressions will be converted into strings without any change.
*/
public
$orderBy
;
/**
* @var array how to group the query results. For example, `['company', 'department']`.
* This is used to construct the GROUP BY clause in a SQL statement.
*/
...
...
@@ -105,12 +76,6 @@ class Query extends Component implements QueryInterface
*/
public
$params
;
/**
* @var string|callable $column the name of the column by which the query results should be indexed by.
* This can also be a callable (e.g. anonymous function) that returns the index value based on the given
* row data. For more details, see [[indexBy()]]. This property is only used by [[all()]].
*/
public
$indexBy
;
/**
* @var callback PHP callback, which should be used to fetch source data for the snippets.
* Such callback will receive array of query result rows as an argument and must return the
* array of snippet source strings in the order, which match one of incoming rows.
...
...
@@ -183,27 +148,6 @@ class Query extends Component implements QueryInterface
}
/**
* Sets the [[indexBy]] property.
* @param string|callable $column the name of the column by which the query results should be indexed by.
* This can also be a callable (e.g. anonymous function) that returns the index value based on the given
* row data. The signature of the callable should be:
*
* ~~~
* function ($row)
* {
* // return the index value corresponding to $row
* }
* ~~~
*
* @return static the query object itself
*/
public
function
indexBy
(
$column
)
{
$this
->
indexBy
=
$column
;
return
$this
;
}
/**
* Executes the query and returns all results as an array.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
...
...
@@ -571,83 +515,6 @@ class Query extends Component implements QueryInterface
}
/**
* Sets the ORDER BY part of the query.
* @param string|array $columns the columns (and the directions) to be ordered by.
* Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array
* (e.g. `['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 static the query object itself
* @see addOrderBy()
*/
public
function
orderBy
(
$columns
)
{
$this
->
orderBy
=
$this
->
normalizeOrderBy
(
$columns
);
return
$this
;
}
/**
* Adds additional ORDER BY columns to the query.
* @param string|array $columns the columns (and the directions) to be ordered by.
* Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array
* (e.g. `['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 static the query object itself
* @see orderBy()
*/
public
function
addOrderBy
(
$columns
)
{
$columns
=
$this
->
normalizeOrderBy
(
$columns
);
if
(
$this
->
orderBy
===
null
)
{
$this
->
orderBy
=
$columns
;
}
else
{
$this
->
orderBy
=
array_merge
(
$this
->
orderBy
,
$columns
);
}
return
$this
;
}
protected
function
normalizeOrderBy
(
$columns
)
{
if
(
is_array
(
$columns
))
{
return
$columns
;
}
else
{
$columns
=
preg_split
(
'/\s*,\s*/'
,
trim
(
$columns
),
-
1
,
PREG_SPLIT_NO_EMPTY
);
$result
=
[];
foreach
(
$columns
as
$column
)
{
if
(
preg_match
(
'/^(.*?)\s+(asc|desc)$/i'
,
$column
,
$matches
))
{
$result
[
$matches
[
1
]]
=
strcasecmp
(
$matches
[
2
],
'desc'
)
?
self
::
SORT_ASC
:
self
::
SORT_DESC
;
}
else
{
$result
[
$column
]
=
self
::
SORT_ASC
;
}
}
return
$result
;
}
}
/**
* Sets the LIMIT part of the query.
* @param integer $limit the limit. Use null or negative value to disable limit.
* @return static the query object itself
*/
public
function
limit
(
$limit
)
{
$this
->
limit
=
$limit
;
return
$this
;
}
/**
* Sets the OFFSET part of the query.
* @param integer $offset the offset. Use null or negative value to disable offset.
* @return static the query object itself
*/
public
function
offset
(
$offset
)
{
$this
->
offset
=
$offset
;
return
$this
;
}
/**
* Sets the parameters to be bound to the query.
* @param array $params list of query parameter values indexed by parameter placeholders.
* For example, `[':name' => 'Dan', ':age' => 31]`.
...
...
extensions/sphinx/QueryBuilder.php
View file @
6a5b8d19
...
...
@@ -488,7 +488,7 @@ class QueryBuilder extends Object
if
(
is_object
(
$direction
))
{
$orders
[]
=
(
string
)
$direction
;
}
else
{
$orders
[]
=
$this
->
db
->
quoteColumnName
(
$name
)
.
(
$direction
===
Query
::
SORT_DESC
?
' DESC'
:
''
);
$orders
[]
=
$this
->
db
->
quoteColumnName
(
$name
)
.
(
$direction
===
SORT_DESC
?
' DESC'
:
''
);
}
}
...
...
@@ -806,7 +806,7 @@ class QueryBuilder extends Object
if
(
is_object
(
$direction
))
{
$orders
[]
=
(
string
)
$direction
;
}
else
{
$orders
[]
=
$this
->
db
->
quoteColumnName
(
$name
)
.
(
$direction
===
Query
::
SORT_DESC
?
' DESC'
:
''
);
$orders
[]
=
$this
->
db
->
quoteColumnName
(
$name
)
.
(
$direction
===
SORT_DESC
?
' DESC'
:
''
);
}
}
return
'WITHIN GROUP ORDER BY '
.
implode
(
', '
,
$orders
);
...
...
tests/unit/extensions/sphinx/QueryTest.php
View file @
6a5b8d19
...
...
@@ -77,19 +77,19 @@ class QueryTest extends SphinxTestCase
{
$query
=
new
Query
;
$query
->
orderBy
(
'team'
);
$this
->
assertEquals
([
'team'
=>
false
],
$query
->
orderBy
);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
],
$query
->
orderBy
);
$query
->
addOrderBy
(
'company'
);
$this
->
assertEquals
([
'team'
=>
false
,
'company'
=>
false
],
$query
->
orderBy
);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
,
'company'
=>
SORT_ASC
],
$query
->
orderBy
);
$query
->
addOrderBy
(
'age'
);
$this
->
assertEquals
([
'team'
=>
false
,
'company'
=>
false
,
'age'
=>
false
],
$query
->
orderBy
);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
,
'company'
=>
SORT_ASC
,
'age'
=>
SORT_ASC
],
$query
->
orderBy
);
$query
->
addOrderBy
([
'age'
=>
true
]);
$this
->
assertEquals
([
'team'
=>
false
,
'company'
=>
false
,
'age'
=>
true
],
$query
->
orderBy
);
$query
->
addOrderBy
([
'age'
=>
SORT_DESC
]);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
,
'company'
=>
SORT_ASC
,
'age'
=>
SORT_DESC
],
$query
->
orderBy
);
$query
->
addOrderBy
(
'age ASC, company DESC'
);
$this
->
assertEquals
([
'team'
=>
false
,
'company'
=>
true
,
'age'
=>
false
],
$query
->
orderBy
);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
,
'company'
=>
SORT_DESC
,
'age'
=>
SORT_ASC
],
$query
->
orderBy
);
}
public
function
testLimitOffset
()
...
...
@@ -104,19 +104,19 @@ class QueryTest extends SphinxTestCase
{
$query
=
new
Query
;
$query
->
within
(
'team'
);
$this
->
assertEquals
([
'team'
=>
false
],
$query
->
within
);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
],
$query
->
within
);
$query
->
addWithin
(
'company'
);
$this
->
assertEquals
([
'team'
=>
false
,
'company'
=>
false
],
$query
->
within
);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
,
'company'
=>
SORT_ASC
],
$query
->
within
);
$query
->
addWithin
(
'age'
);
$this
->
assertEquals
([
'team'
=>
false
,
'company'
=>
false
,
'age'
=>
false
],
$query
->
within
);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
,
'company'
=>
SORT_ASC
,
'age'
=>
SORT_ASC
],
$query
->
within
);
$query
->
addWithin
([
'age'
=>
true
]);
$this
->
assertEquals
([
'team'
=>
false
,
'company'
=>
false
,
'age'
=>
true
],
$query
->
within
);
$query
->
addWithin
([
'age'
=>
SORT_DESC
]);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
,
'company'
=>
SORT_ASC
,
'age'
=>
SORT_DESC
],
$query
->
within
);
$query
->
addWithin
(
'age ASC, company DESC'
);
$this
->
assertEquals
([
'team'
=>
false
,
'company'
=>
true
,
'age'
=>
false
],
$query
->
within
);
$this
->
assertEquals
([
'team'
=>
SORT_ASC
,
'company'
=>
SORT_DESC
,
'age'
=>
SORT_ASC
],
$query
->
within
);
}
public
function
testOptions
()
...
...
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