Completed
Push — ezp-31420-merge-up ( ec14fb...141a64 )
by
unknown
40:13 queued 27:42
created

SelectDoctrineQuery::getQuery()   F

Complexity

Conditions 14
Paths 323

Size

Total Lines 63

Duplication

Lines 12
Ratio 19.05 %

Importance

Changes 0
Metric Value
cc 14
nc 323
nop 0
dl 12
loc 63
rs 3.8958
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Persistence\Doctrine;
8
9
use eZ\Publish\Core\Persistence\Database\SelectQuery;
10
use eZ\Publish\Core\Persistence\Database\QueryException;
11
12
/**
13
 * Class SelectDoctrineQuery.
14
 *
15
 * @deprecated Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection)
16
 *             it provides richer and more powerful DB abstraction which is also easier to use.
17
 */
18
class SelectDoctrineQuery extends AbstractDoctrineQuery implements SelectQuery
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\Persiste...e\AbstractDoctrineQuery has been deprecated with message: Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection) it provides richer and more powerful DB abstraction which is also easier to use.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The interface eZ\Publish\Core\Persistence\Database\SelectQuery has been deprecated with message: Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection) it provides richer and more powerful DB abstraction which is also easier to use.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    private $parts = [
21
        'select' => [],
22
        'from' => [],
23
        'where' => [],
24
        'orderBy' => [],
25
        'groupBy' => [],
26
        'having' => [],
27
    ];
28
29
    /** @var bool */
30
    private $distinct = false;
31
32
    /** @var int */
33
    private $limit;
34
35
    /** @var int */
36
    private $offset;
37
38
    /**
39
     * Holds the state of permission subtree join, which is LEFT JOIN on 'ezcontentobject_tree' table
40
     * with alias 'permission_subtree'.
41
     *
42
     * @internal This is intended for use by PermissionSubtree criterion handler only
43
     *
44
     * @see \eZ\Publish\Core\Search\Legacy\Content\Gateway\CriterionHandler\PermissionSubtree
45
     * @see https://jira.ez.no/browse/EZP-23037
46
     *
47
     * @var bool
48
     */
49
    public $permissionSubtreeJoinAdded = false;
50
51
    /**
52
     * Opens the query and selects which columns you want to return with
53
     * the query.
54
     *
55
     * select() accepts an arbitrary number of parameters. Each parameter
56
     * must contain either the name of a column or an array containing
57
     * the names of the columns.
58
     * Each call to select() appends columns to the list of columns that will be
59
     * used in the query.
60
     *
61
     * Example:
62
     * <code>
63
     * $q->select( 'column1', 'column2' );
64
     * </code>
65
     * The same could also be written
66
     * <code>
67
     * $columns[] = 'column1';
68
     * $columns[] = 'column2;
69
     * $q->select( $columns );
70
     * </code>
71
     * or using several calls
72
     * <code>
73
     * $q->select( 'column1' )->select( 'column2' );
74
     * </code>
75
     *
76
     * Each of above code produce SQL clause 'SELECT column1, column2' for the query.
77
     *
78
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if called with no parameters..
79
     *
80
     * @param string|array(string) $... Either a string with a column name or an array of column names.
0 ignored issues
show
Documentation introduced by
The doc-type string|array(string) could not be parsed: Expected "|" or "end of type", but got "(" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
81
     *
82
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery returns a pointer to $this.
83
     */
84
    public function select()
85
    {
86
        $args = $this->parseArguments(func_get_args());
87
88
        foreach ($args as $selectPart) {
89
            $this->parts['select'][] = $selectPart;
90
        }
91
92
        return $this;
93
    }
94
95
    /**
96
     * Returns SQL to create an alias.
97
     *
98
     * This method can be used to create an alias for either a
99
     * table or a column.
100
     * Example:
101
     * <code>
102
     * // this will make the table users have the alias employees
103
     * // and the column user_id the alias employee_id
104
     * $q->select( $q->alias( 'user_id', 'employee_id' )
105
     *   ->from( $q->alias( 'users', 'employees' ) );
106
     * </code>
107
     *
108
     * @param string $name
109
     * @param string $alias
110
     *
111
     * @return string the query string "columnname as targetname"
112
     */
113
    public function alias($name, $alias)
114
    {
115
        return $name . ' ' . $alias;
116
    }
117
118
    /**
119
     * Opens the query and uses a distinct select on the columns you want to
120
     * return with the query.
121
     *
122
     * selectDistinct() accepts an arbitrary number of parameters. Each
123
     * parameter  must contain either the name of a column or an array
124
     * containing the names of the columns.
125
     * Each call to selectDistinct() appends columns to the list of columns
126
     * that will be used in the query.
127
     *
128
     * Example:
129
     * <code>
130
     * $q->selectDistinct( 'column1', 'column2' );
131
     * </code>
132
     * The same could also be written
133
     * <code>
134
     * $columns[] = 'column1';
135
     * $columns[] = 'column2;
136
     * $q->selectDistinct( $columns );
137
     * </code>
138
     * or using several calls
139
     * <code>
140
     * $q->selectDistinct( 'column1' )->select( 'column2' );
141
     * </code>
142
     *
143
     * Each of above code produce SQL clause 'SELECT DISTINCT column1, column2'
144
     * for the query.
145
     *
146
     * You may call select() after calling selectDistinct() which will result
147
     * in the additional columns beein added. A call of selectDistinct() after
148
     * select() will result in an \eZ\Publish\Core\Persistence\Database\SelectQueryInvalidException.
149
     *
150
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if called with no parameters..
151
     *
152
     * @param string|array(string) $... Either a string with a column name or an array of column names.
0 ignored issues
show
Documentation introduced by
The doc-type string|array(string) could not be parsed: Expected "|" or "end of type", but got "(" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
153
     *
154
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery returns a pointer to $this.
155
     */
156
    public function selectDistinct()
157
    {
158
        $this->distinct = true;
159
160
        return call_user_func_array([$this, 'select'], func_get_args());
161
    }
162
163
    /**
164
     * Select which tables you want to select from.
165
     *
166
     * from() accepts an arbitrary number of parameters. Each parameter
167
     * must contain either the name of a table or an array containing
168
     * the names of tables..
169
     * Each call to from() appends tables to the list of tables that will be
170
     * used in the query.
171
     *
172
     * Example:
173
     * <code>
174
     * // the following code will produce the SQL
175
     * // SELECT id FROM table_name
176
     * $q->select( 'id' )->from( 'table_name' );
177
     * </code>
178
     *
179
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if called with no parameters.
180
     *
181
     * @param string|array(string) $... Either a string with a table name or an array of table names.
0 ignored issues
show
Documentation introduced by
The doc-type string|array(string) could not be parsed: Expected "|" or "end of type", but got "(" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
182
     *
183
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery a pointer to $this
184
     */
185
    public function from()
186
    {
187
        $args = $this->parseArguments(func_get_args());
188
189
        foreach ($args as $tableName) {
190
            $this->parts['from'][] = [
191
                'table' => $tableName,
192
                'type' => 'FROM',
193
            ];
194
        }
195
196
        return $this;
197
    }
198
199
    /**
200
     * Returns the SQL for an inner join or prepares $fromString for an inner join.
201
     *
202
     * This method could be used in two forms:
203
     *
204
     * <b>innerJoin( 't2', $joinCondition )</b>
205
     *
206
     * Takes 2 string arguments and returns \eZ\Publish\Core\Persistence\Database\SelectQuery.
207
     *
208
     * The first parameter is the name of the table to join with. The table to
209
     * which is joined should have been previously set with the from() method.
210
     *
211
     * The second parameter should be a string containing a join condition that
212
     * is returned by an \eZ\Publish\Core\Persistence\Database\SelectQueryExpression.
213
     *
214
     * Example:
215
     * <code>
216
     * // the following code will produce the SQL
217
     * // SELECT id FROM t1 INNER JOIN t2 ON t1.id = t2.id
218
     * $q->select( 'id' )->from( 't1' )->innerJoin( 't2', $q->expr->eq('t1.id', 't2.id' ) );
219
     * </code>
220
     *
221
     * <b>innerJoin( 't2', 't1.id', 't2.id' )</b>
222
     *
223
     * Takes 3 string arguments and returns \eZ\Publish\Core\Persistence\Database\SelectQuery. This is a simplified form
224
     * of the 2 parameter version.  innerJoin( 't2', 't1.id', 't2.id' ) is
225
     * equal to innerJoin( 't2', $this->expr->eq('t1.id', 't2.id' ) );
226
     *
227
     * The first parameter is the name of the table to join with. The table to
228
     * which is joined should have been previously set with the from() method.
229
     *
230
     * The second parameter is the name of the column on the table set
231
     * previously with the from() method and the third parameter the name of
232
     * the column to join with on the table that was specified in the first
233
     * parameter.
234
     *
235
     * Example:
236
     * <code>
237
     * // the following code will produce the SQL
238
     * // SELECT id FROM t1 INNER JOIN t2 ON t1.id = t2.id
239
     * $q->select( 'id' )->from( 't1' )->innerJoin( 't2', 't1.id', 't2.id' );
240
     * </code>
241
     *
242
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if called with inconsistent parameters or if
243
     *         invoked without preceding call to from().
244
     *
245
     * @param string $table2,... The table to join with, followed by either the
0 ignored issues
show
Bug introduced by
There is no parameter named $table2,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
246
     *                           two join columns, or a join condition.
247
     *
248
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
249
     */
250
    public function innerJoin()
251
    {
252
        return $this->doJoin('INNER', func_get_args());
253
    }
254
255
    /**
256
     * Helper function to generate join.
257
     *
258
     * @param string $type
259
     * @param array $args
260
     *
261
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
262
     */
263
    protected function doJoin($type, array $args)
264
    {
265
        if (count($args) === 0) {
266
            throw new QueryException('No arguments given');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\Persiste...Database\QueryException has been deprecated with message: Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection) it provides richer and more powerful DB abstraction which is also easier to use.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
267
        }
268
269
        $tableName = $args[0];
270
        $condition = '';
271
272
        if (count($args) == 2) {
273
            $condition = $args[1];
274
        } elseif (count($args) == 3) {
275
            $condition = $args[1] . ' = ' . $args[2];
276
        }
277
278
        $this->parts['from'][] = [
279
            'table' => $tableName,
280
            'type' => $type,
281
            'condition' => $condition,
282
        ];
283
284
        return $this;
285
    }
286
287
    /**
288
     * Returns the SQL for a left join or prepares $fromString for a left join.
289
     *
290
     * This method could be used in two forms:
291
     *
292
     * <b>leftJoin( 't2', $joinCondition )</b>
293
     *
294
     * Takes 2 string arguments and returns \eZ\Publish\Core\Persistence\Database\SelectQuery.
295
     *
296
     * The first parameter is the name of the table to join with. The table to
297
     * which is joined should have been previously set with the from() method.
298
     *
299
     * The second parameter should be a string containing a join condition that
300
     * is returned by an \eZ\Publish\Core\Persistence\Database\SelectQueryExpression.
301
     *
302
     * Example:
303
     * <code>
304
     * // the following code will produce the SQL
305
     * // SELECT id FROM t1 LEFT JOIN t2 ON t1.id = t2.id
306
     * $q->select( 'id' )->from( 't1' )->leftJoin( 't2', $q->expr->eq('t1.id', 't2.id' ) );
307
     * </code>
308
     *
309
     * <b>leftJoin( 't2', 't1.id', 't2.id' )</b>
310
     *
311
     * Takes 3 string arguments and returns \eZ\Publish\Core\Persistence\Database\SelectQuery. This is a simplified form
312
     * of the 2 parameter version.  leftJoin( 't2', 't1.id', 't2.id' ) is
313
     * equal to leftJoin( 't2', $this->expr->eq('t1.id', 't2.id' ) );
314
     *
315
     * The first parameter is the name of the table to join with. The table to
316
     * which is joined should have been previously set with the from() method.
317
     *
318
     * The second parameter is the name of the column on the table set
319
     * previously with the from() method and the third parameter the name of
320
     * the column to join with on the table that was specified in the first
321
     * parameter.
322
     *
323
     * Example:
324
     * <code>
325
     * // the following code will produce the SQL
326
     * // SELECT id FROM t1 LEFT JOIN t2 ON t1.id = t2.id
327
     * $q->select( 'id' )->from( 't1' )->leftJoin( 't2', 't1.id', 't2.id' );
328
     * </code>
329
     *
330
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if called with inconsistent parameters or if
331
     *         invoked without preceding call to from().
332
     *
333
     * @param string $table2,... The table to join with, followed by either the
0 ignored issues
show
Bug introduced by
There is no parameter named $table2,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
334
     *                           two join columns, or a join condition.
335
     *
336
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
337
     */
338
    public function leftJoin()
339
    {
340
        return $this->doJoin('LEFT', func_get_args());
341
    }
342
343
    /**
344
     * Returns the SQL for a right join or prepares $fromString for a right join.
345
     *
346
     * This method could be used in two forms:
347
     *
348
     * <b>rightJoin( 't2', $joinCondition )</b>
349
     *
350
     * Takes 2 string arguments and returns \eZ\Publish\Core\Persistence\Database\SelectQuery.
351
     *
352
     * The first parameter is the name of the table to join with. The table to
353
     * which is joined should have been previously set with the from() method.
354
     *
355
     * The second parameter should be a string containing a join condition that
356
     * is returned by an \eZ\Publish\Core\Persistence\Database\SelectQueryExpression.
357
     *
358
     * Example:
359
     * <code>
360
     * // the following code will produce the SQL
361
     * // SELECT id FROM t1 LEFT JOIN t2 ON t1.id = t2.id
362
     * $q->select( 'id' )->from( 't1' )->rightJoin( 't2', $q->expr->eq('t1.id', 't2.id' ) );
363
     * </code>
364
     *
365
     * <b>rightJoin( 't2', 't1.id', 't2.id' )</b>
366
     *
367
     * Takes 3 string arguments and returns \eZ\Publish\Core\Persistence\Database\SelectQuery. This is a simplified form
368
     * of the 2 parameter version.  rightJoin( 't2', 't1.id', 't2.id' ) is
369
     * equal to rightJoin( 't2', $this->expr->eq('t1.id', 't2.id' ) );
370
     *
371
     * The first parameter is the name of the table to join with. The table to
372
     * which is joined should have been previously set with the from() method.
373
     *
374
     * The second parameter is the name of the column on the table set
375
     * previously with the from() method and the third parameter the name of
376
     * the column to join with on the table that was specified in the first
377
     * parameter.
378
     *
379
     * Example:
380
     * <code>
381
     * // the following code will produce the SQL
382
     * // SELECT id FROM t1 LEFT JOIN t2 ON t1.id = t2.id
383
     * $q->select( 'id' )->from( 't1' )->rightJoin( 't2', 't1.id', 't2.id' );
384
     * </code>
385
     *
386
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if called with inconsistent parameters or if
387
     *         invoked without preceding call to from().
388
     *
389
     * @param string $table2,... The table to join with, followed by either the
0 ignored issues
show
Bug introduced by
There is no parameter named $table2,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
390
     *                           two join columns, or a join condition.
391
     *
392
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
393
     */
394
    public function rightJoin()
395
    {
396
        return $this->doJoin('RIGHT', func_get_args());
397
    }
398
399
    /**
400
     * Adds a where clause with logical expressions to the query.
401
     *
402
     * where() accepts an arbitrary number of parameters. Each parameter
403
     * must contain a logical expression or an array with logical expressions.
404
     * If you specify multiple logical expression they are connected using
405
     * a logical and.
406
     *
407
     * Multiple calls to where() will join the expressions using a logical and.
408
     *
409
     * Example:
410
     * <code>
411
     * $q->select( '*' )->from( 'table' )->where( $q->expr->eq( 'id', 1 ) );
412
     * </code>
413
     *
414
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if called with no parameters.
415
     *
416
     * @param string|array(string) $... Either a string with a logical expression name
0 ignored issues
show
Documentation introduced by
The doc-type string|array(string) could not be parsed: Expected "|" or "end of type", but got "(" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
417
     * or an array with logical expressions.
418
     *
419
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
420
     */
421 View Code Duplication
    public function where()
422
    {
423
        $args = func_get_args();
424
425
        if (count($args) === 0) {
426
            throw new QueryException('No arguments given');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\Persiste...Database\QueryException has been deprecated with message: Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection) it provides richer and more powerful DB abstraction which is also easier to use.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
427
        }
428
429
        foreach ($args as $whereCondition) {
430
            $this->parts['where'][] = $whereCondition;
431
        }
432
433
        return $this;
434
    }
435
436
    /**
437
     * Returns SQL that limits the result set.
438
     *
439
     * $limit controls the maximum number of rows that will be returned.
440
     * $offset controls which row that will be the first in the result
441
     * set from the total amount of matching rows.
442
     *
443
     * Example:
444
     * <code>
445
     * $q->select( '*' )->from( 'table' )
446
     *                  ->limit( 10, 0 );
447
     * </code>
448
     *
449
     * LIMIT is not part of SQL92. It is implemented here anyway since all
450
     * databases support it one way or the other and because it is
451
     * essential.
452
     *
453
     * @param string $limit integer expression
454
     * @param string $offset integer expression
455
     *
456
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
457
     */
458
    public function limit($limit, $offset = '')
459
    {
460
        $this->limit = $limit;
0 ignored issues
show
Documentation Bug introduced by
The property $limit was declared of type integer, but $limit is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
461
        $this->offset = $offset;
0 ignored issues
show
Documentation Bug introduced by
The property $offset was declared of type integer, but $offset is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
462
    }
463
464
    /**
465
     * Returns SQL that orders the result set by a given column.
466
     *
467
     * You can call orderBy multiple times. Each call will add a
468
     * column to order by.
469
     *
470
     * Example:
471
     * <code>
472
     * $q->select( '*' )->from( 'table' )
473
     *                  ->orderBy( 'id' );
474
     * </code>
475
     *
476
     * @param string $column a column name in the result set
477
     * @param string $type if the column should be sorted ascending or descending.
478
     *        you can specify this using \eZ\Publish\Core\Persistence\Database\SelectQuery::ASC
479
     *        or \eZ\Publish\Core\Persistence\Database\SelectQuery::DESC
480
     *
481
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery a pointer to $this
482
     */
483
    public function orderBy($column, $type = self::ASC)
484
    {
485
        if ($type !== self::ASC && $type !== self::DESC) {
486
            throw new QueryException($type . ' is invalid order direction value');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\Persiste...Database\QueryException has been deprecated with message: Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection) it provides richer and more powerful DB abstraction which is also easier to use.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
487
        }
488
489
        $this->parts['orderBy'][] = $column . ' ' . $type;
490
491
        return $this;
492
    }
493
494
    /**
495
     * Returns SQL that groups the result set by a given column.
496
     *
497
     * You can call groupBy multiple times. Each call will add a
498
     * column to group by.
499
     *
500
     * Example:
501
     * <code>
502
     * $q->select( '*' )->from( 'table' )
503
     *                  ->groupBy( 'id' );
504
     * </code>
505
     *
506
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if called with no parameters.
507
     *
508
     * @param string $column a column name in the result set
0 ignored issues
show
Bug introduced by
There is no parameter named $column. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
509
     *
510
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery a pointer to $this
511
     */
512
    public function groupBy()
513
    {
514
        $args = $this->parseArguments(func_get_args());
515
516
        foreach ($args as $groupByExpression) {
517
            $this->parts['groupBy'][] = $groupByExpression;
518
        }
519
520
        return $this;
521
    }
522
523
    /**
524
     * Returns SQL that set having by a given expression.
525
     *
526
     * You can call having multiple times. Each call will add an
527
     * expression with a logical and.
528
     *
529
     * Example:
530
     * <code>
531
     * $q->select( '*' )->from( 'table' )->groupBy( 'id' )
532
     *                  ->having( $q->expr->eq('id',1) );
533
     * </code>
534
     *
535
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException
536
     *         if called with no parameters.
537
     *
538
     * @param string|array(string) $... Either a string with a logical expression name
0 ignored issues
show
Documentation introduced by
The doc-type string|array(string) could not be parsed: Expected "|" or "end of type", but got "(" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
539
     *                             or an array with logical expressions.
540
     *
541
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery a pointer to $this
542
     */
543 View Code Duplication
    public function having()
544
    {
545
        $args = func_get_args();
546
547
        if (count($args) === 0) {
548
            throw new QueryException('No arguments given');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\Persiste...Database\QueryException has been deprecated with message: Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection) it provides richer and more powerful DB abstraction which is also easier to use.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
549
        }
550
551
        foreach ($args as $whereCondition) {
552
            $this->parts['having'][] = $whereCondition;
553
        }
554
555
        return $this;
556
    }
557
558
    /**
559
     * Returns the query string for this query object.
560
     *
561
     * @throws \eZ\Publish\Core\Persistence\Database\QueryException if it was not possible to build a valid query.
562
     *
563
     * @return string
564
     */
565
    public function getQuery()
566
    {
567
        if (count($this->parts['select']) === 0) {
568
            throw new QueryException('Missing "select" parts to generate query.');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\Persiste...Database\QueryException has been deprecated with message: Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection) it provides richer and more powerful DB abstraction which is also easier to use.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
569
        }
570
571
        $sql = 'SELECT ';
572
573
        if ($this->distinct) {
574
            $sql .= 'DISTINCT ';
575
        }
576
577
        $sql .= implode(', ', $this->parts['select']) . ' FROM';
578
579
        if (count($this->parts['from']) === 0) {
580
            throw new QueryException('Missing "from" parts to generate query.');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\Persiste...Database\QueryException has been deprecated with message: Since 6.13, please use Doctrine DBAL instead (@ezpublish.persistence.connection) it provides richer and more powerful DB abstraction which is also easier to use.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
581
        }
582
583
        $renderedFromBefore = false;
584
585
        foreach ($this->parts['from'] as $fromPart) {
586
            if ($fromPart['type'] === 'FROM') {
587
                if ($renderedFromBefore === true) {
588
                    $sql .= ',';
589
                }
590
591
                $sql .= ' ' . $fromPart['table'];
592
                $renderedFromBefore = true;
593
            } else {
594
                $sql .= ' ' . $fromPart['type'] . ' JOIN ' . $fromPart['table'];
595
596
                if ($fromPart['condition']) {
597
                    $sql .= ' ON ' . $fromPart['condition'];
598
                }
599
            }
600
        }
601
602 View Code Duplication
        if (count($this->parts['where']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
603
            $sql .= ' WHERE ' . implode(' AND ', $this->parts['where']);
604
        }
605
606 View Code Duplication
        if (count($this->parts['groupBy']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
607
            $sql .= ' GROUP BY ' . implode(', ', $this->parts['groupBy']);
608
        }
609
610 View Code Duplication
        if (count($this->parts['having']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
611
            $sql .= ' HAVING ' . implode(' AND ', $this->parts['having']);
612
        }
613
614 View Code Duplication
        if (count($this->parts['orderBy']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
615
            $sql .= ' ORDER BY ' . implode(', ', $this->parts['orderBy']);
616
        }
617
618
        if ($this->limit || $this->offset) {
619
            $sql = $this->connection->getDatabasePlatform()->modifyLimitQuery(
620
                $sql,
621
                $this->limit,
622
                $this->offset
623
            );
624
        }
625
626
        return $sql;
627
    }
628
}
629