Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Tomáš Trávníček
doctrine-dbal
Commits
7241a651
Unverified
Commit
7241a651
authored
Oct 13, 2019
by
Sergei Morozov
Committed by
GitHub
Oct 13, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3696 from bingo-soft/master
Add support for DISTINCT clause
parents
81922a27
f90c2997
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
14 deletions
+65
-14
query-builder.rst
docs/en/reference/query-builder.rst
+15
-0
QueryBuilder.php
lib/Doctrine/DBAL/Query/QueryBuilder.php
+39
-14
QueryBuilderTest.php
tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
+11
-0
No files found.
docs/en/reference/query-builder.rst
View file @
7241a651
...
@@ -87,6 +87,21 @@ and ``delete($tableName)``:
...
@@ -87,6 +87,21 @@ and ``delete($tableName)``:
You can convert a query builder to its SQL string representation
You can convert a query builder to its SQL string representation
by calling ``$queryBuilder->getSQL()`` or casting the object to string.
by calling ``$queryBuilder->getSQL()`` or casting the object to string.
DISTINCT-Clause
~~~~~~~~~~~~~~~
The ``SELECT`` statement can be specified with a ``DISTINCT`` clause:
.. code-block:: php
<?php
$queryBuilder
->select('name')
->distinct()
->from('users')
;
WHERE-Clause
WHERE-Clause
~~~~~~~~~~~~
~~~~~~~~~~~~
...
...
lib/Doctrine/DBAL/Query/QueryBuilder.php
View file @
7241a651
...
@@ -52,22 +52,28 @@ class QueryBuilder
...
@@ -52,22 +52,28 @@ class QueryBuilder
*/
*/
private
$connection
;
private
$connection
;
/*
* The default values of SQL parts collection
*/
private
const
SQL_PARTS_DEFAULTS
=
[
'select'
=>
[],
'distinct'
=>
false
,
'from'
=>
[],
'join'
=>
[],
'set'
=>
[],
'where'
=>
null
,
'groupBy'
=>
[],
'having'
=>
null
,
'orderBy'
=>
[],
'values'
=>
[],
];
/**
/**
* The array of SQL parts collected.
* The array of SQL parts collected.
*
*
* @var mixed[]
* @var mixed[]
*/
*/
private
$sqlParts
=
[
private
$sqlParts
=
self
::
SQL_PARTS_DEFAULTS
;
'select'
=>
[],
'from'
=>
[],
'join'
=>
[],
'set'
=>
[],
'where'
=>
null
,
'groupBy'
=>
[],
'having'
=>
null
,
'orderBy'
=>
[],
'values'
=>
[],
];
/**
/**
* The complete SQL string for this query.
* The complete SQL string for this query.
...
@@ -469,6 +475,25 @@ class QueryBuilder
...
@@ -469,6 +475,25 @@ class QueryBuilder
return
$this
->
add
(
'select'
,
$selects
);
return
$this
->
add
(
'select'
,
$selects
);
}
}
/**
* Adds DISTINCT to the query.
*
* <code>
* $qb = $conn->createQueryBuilder()
* ->select('u.id')
* ->distinct()
* ->from('users', 'u')
* </code>
*
* @return $this This QueryBuilder instance.
*/
public
function
distinct
()
:
self
{
$this
->
sqlParts
[
'distinct'
]
=
true
;
return
$this
;
}
/**
/**
* Adds an item that is to be returned in the query result.
* Adds an item that is to be returned in the query result.
*
*
...
@@ -1083,8 +1108,7 @@ class QueryBuilder
...
@@ -1083,8 +1108,7 @@ class QueryBuilder
*/
*/
public
function
resetQueryPart
(
$queryPartName
)
public
function
resetQueryPart
(
$queryPartName
)
{
{
$this
->
sqlParts
[
$queryPartName
]
=
is_array
(
$this
->
sqlParts
[
$queryPartName
])
$this
->
sqlParts
[
$queryPartName
]
=
self
::
SQL_PARTS_DEFAULTS
[
$queryPartName
];
?
[]
:
null
;
$this
->
state
=
self
::
STATE_DIRTY
;
$this
->
state
=
self
::
STATE_DIRTY
;
...
@@ -1098,7 +1122,8 @@ class QueryBuilder
...
@@ -1098,7 +1122,8 @@ class QueryBuilder
*/
*/
private
function
getSQLForSelect
()
private
function
getSQLForSelect
()
{
{
$query
=
'SELECT '
.
implode
(
', '
,
$this
->
sqlParts
[
'select'
]);
$query
=
'SELECT '
.
(
$this
->
sqlParts
[
'distinct'
]
?
'DISTINCT '
:
''
)
.
implode
(
', '
,
$this
->
sqlParts
[
'select'
]);
$query
.=
(
$this
->
sqlParts
[
'from'
]
?
' FROM '
.
implode
(
', '
,
$this
->
getFromClauses
())
:
''
)
$query
.=
(
$this
->
sqlParts
[
'from'
]
?
' FROM '
.
implode
(
', '
,
$this
->
getFromClauses
())
:
''
)
.
(
$this
->
sqlParts
[
'where'
]
!==
null
?
' WHERE '
.
((
string
)
$this
->
sqlParts
[
'where'
])
:
''
)
.
(
$this
->
sqlParts
[
'where'
]
!==
null
?
' WHERE '
.
((
string
)
$this
->
sqlParts
[
'where'
])
:
''
)
...
...
tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
View file @
7241a651
...
@@ -50,6 +50,17 @@ class QueryBuilderTest extends DbalTestCase
...
@@ -50,6 +50,17 @@ class QueryBuilderTest extends DbalTestCase
self
::
assertEquals
(
'SELECT u.id FROM users u'
,
(
string
)
$qb
);
self
::
assertEquals
(
'SELECT u.id FROM users u'
,
(
string
)
$qb
);
}
}
public
function
testSimpleSelectWithDistinct
()
:
void
{
$qb
=
new
QueryBuilder
(
$this
->
conn
);
$qb
->
select
(
'u.id'
)
->
distinct
()
->
from
(
'users'
,
'u'
);
self
::
assertEquals
(
'SELECT DISTINCT u.id FROM users u'
,
(
string
)
$qb
);
}
public
function
testSelectWithSimpleWhere
()
:
void
public
function
testSelectWithSimpleWhere
()
:
void
{
{
$qb
=
new
QueryBuilder
(
$this
->
conn
);
$qb
=
new
QueryBuilder
(
$this
->
conn
);
...
...
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