Passed
Pull Request — master (#21)
by Vincent
07:14
created

Query::fromAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Bdf\Prime\Query;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Exception\PrimeException;
7
use Bdf\Prime\Query\Compiler\Preprocessor\DefaultPreprocessor;
8
use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface;
9
use Bdf\Prime\Query\Contract\Paginable;
10
use Bdf\Prime\Query\Contract\ReadOperation;
11
use Bdf\Prime\Query\Contract\WriteOperation;
12
use Bdf\Prime\Query\Expression\Raw;
13
use Bdf\Prime\Query\Extension\EntityJoinTrait;
14
use Bdf\Prime\Query\Extension\LimitableTrait;
15
use Bdf\Prime\Query\Extension\LockableTrait;
16
use Bdf\Prime\Query\Extension\OrderableTrait;
17
use Bdf\Prime\Query\Extension\PaginableTrait;
18
use Bdf\Prime\Query\Extension\SimpleJoinTrait;
19
use Doctrine\DBAL\Query\Expression\CompositeExpression;
20
21
/**
22
 * Sql Query
23
 * 
24
 * @package Bdf\Prime\Query
0 ignored issues
show
Coding Style Documentation introduced by
@package tag is not allowed in class comment
Loading history...
25
 * 
26
 * @todo comment reset un statement (ex ecraser les orders). Prendre en compte le reset du compiler
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
Coding Style Documentation introduced by
@todo tag is not allowed in class comment
Loading history...
27
 */
28
class Query extends AbstractQuery implements SqlQueryInterface, Paginable
29
{
30
    use EntityJoinTrait;
31
    use PaginableTrait;
32
    use LimitableTrait;
33
    use OrderableTrait;
34
    use SimpleJoinTrait;
35
    use LockableTrait;
36
37
    /**
38
     * Initializes a new <tt>Query</tt>.
39
     *
40
     * @param ConnectionInterface $connection The DBAL Connection.
41
     * @param PreprocessorInterface|null $preprocessor
42
     */
43 733
    public function __construct(ConnectionInterface $connection, PreprocessorInterface $preprocessor = null)
44
    {
45 733
        parent::__construct($connection, $preprocessor ?: new DefaultPreprocessor());
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
46
47 733
        $this->statements = [
48
            'ignore'     => null,
49
            'replace'    => null,
50
            'values'     => [],
51
            'columns'    => [],
52
            'distinct'   => null,
53
            'tables'     => [],
54
            'joins'      => [],
55
            'where'      => [],
56
            'groups'     => [],
57
            'having'     => [],
58
            'orders'     => [],
59
            'limit'      => null,
60
            'offset'     => null,
61
            'aggregate'  => null,
62
            'lock'       => null,
63
        ];
64 733
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 583
    public function getBindings()
70
    {
71 583
        return $this->compiler->getBindings($this);
72
    }
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sql should have a doc-comment as per coding-style.
Loading history...
75
     * {@inheritdoc}
76
     */
77 1
    public function raw($sql)
78
    {
79 1
        return new Raw($sql);
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
83
     * {@inheritdoc}
84
     */
85
    public function quote($value, $type = null)
86
    {
87
        return $this->compiler->quote($value, $type);
0 ignored issues
show
Bug introduced by
The method quote() does not exist on Bdf\Prime\Query\Compiler\CompilerInterface. It seems like you code against a sub-type of Bdf\Prime\Query\Compiler\CompilerInterface such as Bdf\Prime\Query\Compiler\SqlCompiler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        return $this->compiler->/** @scrutinizer ignore-call */ quote($value, $type);
Loading history...
88
    }
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
91
     * {@inheritdoc}
92
     *
93
     * @todo Utile ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
94
     */
95
    public function quoteIdentifier($column)
96
    {
97
        return $this->compiler->quoteIdentifier($this, $column);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
104 26
    public function delete()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
105
    {
106 26
        return $this->executeUpdate(self::TYPE_DELETE);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
113 21
    public function update(array $data = [], array $types = [])
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
114
    {
115 21
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
116 11
            $this->statements['values'] = [
117 11
                'data'   => $data,
118 11
                'types'  => $types,
119
            ];
120
        }
121
        
122 21
        return $this->executeUpdate(self::TYPE_UPDATE);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
129 443
    public function insert(array $data = [])
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
130
    {
131 443
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
132 443
            $this->statements['values'] = [
133 443
                'data'   => $data,
134
            ];
135
        }
136
        
137 443
        return $this->executeUpdate(self::TYPE_INSERT);
138
    }
139
140
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
141
     * {@inheritdoc}
142
     */
143 1
    public function ignore($flag = true)
144
    {
145 1
        $this->statements['ignore'] = (bool)$flag;
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
146
        
147 1
        return $this;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
154 2
    public function replace(array $values = [])
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
155
    {
156 2
        $this->statements['replace'] = true;
157
158 2
        return $this->insert($values);
159
    }
160
161
    /**
162
     * Set values to insert or update
163
     *
164
     * @todo Remonter sur une interface ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
165
     *
166
     * <code>
167
     * // Perform a INSERT INTO ... SELECT ... query
168
     * $query
169
     *     ->from('users_bck')
170
     *     ->values($connection->builder()->from('users'))
171
     *     ->insert()
172
     * ;
173
     *
174
     * // Perform a REPLACE INTO ... SELECT ... with column mapping
175
     * $query
176
     *     ->from('users_bck')
177
     *     ->values(
178
     *         $connection->builder()
179
     *             ->from('users')
180
     *             ->select([
181
     *                 'backup_name' => 'name', // Map "name" column from select table to "backup_name" column to insert table
182
     *                 'id'                     // Use "id" column without mapping other than ORM data mapping
183
     *             ])
184
     *     )
185
     *     ->replace() // Replace can also be used
186
     * ;
187
     *
188
     * // Simple UPDATE query
189
     * $query->values(['foo' => 'bar'])->update();
190
     * </code>
191
     *
192
     * @param QueryInterface|array $data The values, in form [column] => [value], or the SELECT query
0 ignored issues
show
Coding Style introduced by
Parameter tags must be defined first in a doc comment
Loading history...
193
     * @param array $types The binding types Do not works with INSERT INTO SELECT query
194
     *
195
     * @return $this
196
     */
197 19
    public function values($data = [], array $types = [])
198
    {
199 19
        $this->statements['values'] = [
200 19
            'data' => $data,
201 19
            'type' => $types
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: $types
Loading history...
202
        ];
203
204 19
        return $this;
205
    }
206
207
    /**
208
     * Executes this query as an update query
209
     *
210
     * @param string $type The query type
211
     *
212
     * @return int The number of updated rows
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
213
     *
214
     * @throws PrimeException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
215
     */
216 456
    protected function executeUpdate($type)
217
    {
218 456
        $this->setType($type);
219
220 456
        $nb = $this->connection->execute($this)->count();
221
222 456
        if ($nb > 0) {
223 447
            $this->clearCacheOnWrite();
224
        }
225
226 456
        return $nb;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     *
232
     * @todo Return statement instead of array ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
233
     */
234
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
235 502
    public function execute($columns = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
236
    {
237 502
        if (!empty($columns)) {
238 52
            $this->select($columns);
239
        }
240
241 502
        $this->setType(self::TYPE_SELECT);
242
243 502
        return $this->executeCached();
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 10
    protected function cacheKey()
250
    {
251 10
        return sha1($this->toSql().'-'.serialize($this->getBindings()));
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
258 13
    public function paginationCount($columns = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
259
    {
260 13
        $statements = $this->statements;
261
262 13
        $this->compilerState->invalidate(['columns', 'orders']);
263
264 13
        $this->statements['orders'] = [];
265 13
        $this->statements['limit'] = null;
266 13
        $this->statements['offset'] = null;
267 13
        $this->statements['aggregate'] = ['pagination', $this->getPaginationColumns($columns)];
268
269 13
        $count = (int)$this->execute()[0]['aggregate'];
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
270
271 13
        $this->compilerState->invalidate(['columns', 'orders']);
272 13
        $this->statements = $statements;
273
274 13
        return $count;
275
    }
276
277
    /**
278
     * Get the column to count for pagination
279
     * @todo Voir pour count sur PK quand une entité est liée ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
280
     *
281
     * @param array|string|null $column
0 ignored issues
show
Coding Style introduced by
Parameter tags must be defined first in a doc comment
Loading history...
282
     *
283
     * @return string
284
     */
285 13
    protected function getPaginationColumns($column)
286
    {
287 13
        if (!empty($column)) {
288
            return $column;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $column also could return the type array which is incompatible with the documented return type string.
Loading history...
289
        }
290
291
        // If distinct is on and no column are given, we use the current column
292 13
        if ($this->statements['distinct'] && !empty($this->statements['columns'])) {
293 3
            return $this->statements['columns'][0]['column'];
294
        }
295
296
        // If group by we use the columns of the group by
297 11
        if ($this->statements['groups']) {
298 2
            $this->statements['distinct'] = true;
299 2
            $column = $this->statements['groups'][0];
300 2
            $this->statements['groups'] = [];
301
302 2
            return $column;
303
        }
304
305 10
        return '*';
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
312 346
    public function count($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
313
    {
314 346
        return (int)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
315
    }
316
317
    /**
318
     * {@inheritdoc}
319
     */
320
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
321 2
    public function avg($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
322
    {
323 2
        return (float)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
330 2
    public function min($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
331
    {
332 2
        return (float)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
333
    }
334
335
    /**
336
     * {@inheritdoc}
337
     */
338
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
339 3
    public function max($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
340
    {
341 3
        return (float)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
342
    }
343
344
    /**
345
     * {@inheritdoc}
346
     */
347
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
348 2
    public function sum($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
349
    {
350 2
        return (float)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     */
356
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
357 355
    public function aggregate($function, $column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
358
    {
359 355
        $statements = $this->statements;
360
361 355
        $this->compilerState->invalidate('columns');
362
363 355
        $this->statements['aggregate'] = [$function, $column ?: '*'];
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
364
365 355
        $aggregate = $this->execute()[0]['aggregate'];
366
367 355
        $this->compilerState->invalidate('columns');
368 355
        $this->statements = $statements;
369
370 355
        return $aggregate;
371
    }
372
373
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
374
     * {@inheritdoc}
375
     */
376 18
    public function distinct($flag = true)
377
    {
378 18
        $this->compilerState->invalidate('columns');
379
        
380 18
        $this->statements['distinct'] = (bool)$flag;
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
381
        
382 18
        return $this;
383
    }
384
385
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $from should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $alias should have a doc-comment as per coding-style.
Loading history...
386
     * {@inheritdoc}
387
     */
388 699
    public function from($from, $alias = null)
389
    {
390 699
        $this->compilerState->invalidate('from');
391
392
        $table = [
393 699
            'table' => $from,
394 699
            'alias' => $alias,
395
        ];
396 699
        $key = $alias ?: $from;
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
397
398 699
        if (is_string($key)) {
399 699
            $this->statements['tables'][$key] = $table;
400
        } else {
401
            $this->statements['tables'][] = $table;
402
        }
403
404 699
        return $this;
405
    }
406
407
    /**
408
     * Change a FROM alias for a table (or previously defined alias)
409
     *
410
     * Usage:
411
     * <code>
412
     * // Change alias of the current table : FROM my_table as my_alias
413
     * $query->from('my_table')->fromAlias('my_alias');
414
     *
415
     * // Change alias of the foo table : FROM foo as my_alias, bar
416
     * $query->from('foo')->from('bar')->fromAlias('my_alias', 'foo');
417
     *
418
     * // Redefine alias of foo : FROM foo as my_alias, bar
419
     * $query->from('foo', 'f')->from('bar')->fromAlias('my_alias', 'f');
420
     * </code>
421
     *
422
     * @param string $alias The new alias name
423
     * @param string|null $table The last alias / table name. If null, will define the last table
424
     *
425
     * @return $this
426
     */
427 3
    public function fromAlias(string $alias, ?string $table = null)
428
    {
429 3
        $this->compilerState->invalidate('from');
430
431 3
        $table = $table ?? key($this->statements['tables']);
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
432
433 3
        $this->statements['tables'][$alias] = $this->statements['tables'][$table];
434 3
        $this->statements['tables'][$alias]['alias'] = $alias;
435
436 3
        unset($this->statements['tables'][$table]);
437
438 3
        return $this;
439
    }
440
441
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
442
     * {@inheritdoc}
443
     */
444 5
    public function group($column)
445
    {
446 5
        $this->compilerState->invalidate('groups');
447
        
448 5
        $this->statements['groups'] = is_array($column) ? $column : func_get_args();
449
        
450 5
        return $this;
451
    }
452
453
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
454
     * {@inheritdoc}
455
     */
456
    public function addGroup($column)
457
    {
458
        $this->compilerState->invalidate('groups');
459
        
460
        $this->statements['groups'] = array_merge($this->statements['groups'], is_array($column) ? $column : func_get_args());
461
        
462
        return $this;
463
    }
464
465
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $operator should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
466
     * {@inheritdoc}
467
     */
468 2
    public function having($column, $operator = null, $value = null)
469
    {
470 2
        $this->compilerState->invalidate('having');
471
472 2
        return $this->buildClause('having', $column, $operator, $value);
473
    }
474
475
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $operator should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
476
     * {@inheritdoc}
477
     */
478 3
    public function orHaving($column, $operator = null, $value = null)
479
    {
480 3
        $this->compilerState->invalidate('having');
481
482 3
        return $this->buildClause('having', $column, $operator, $value, CompositeExpression::TYPE_OR);
483
    }
484
485
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
486
     * {@inheritdoc}
487
     */
488 2
    public function havingNull($column, $type = CompositeExpression::TYPE_AND)
489
    {
490 2
        $this->compilerState->invalidate('having');
491
492 2
        return $this->buildClause('having', $column, '=', null, $type);
493
    }
494
495
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
496
     * {@inheritdoc}
497
     */
498 2
    public function havingNotNull($column, $type = CompositeExpression::TYPE_AND)
499
    {
500 2
        $this->compilerState->invalidate('having');
501
502 2
        return $this->buildClause('having', $column, '!=', null, $type);
503
    }
504
505
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
506
     * {@inheritdoc}
507
     */
508 1
    public function orHavingNull($column)
509
    {
510 1
        return $this->havingNull($column, CompositeExpression::TYPE_OR);
511
    }
512
513
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
514
     * {@inheritdoc}
515
     */
516 1
    public function orHavingNotNull($column)
517
    {
518 1
        return $this->havingNotNull($column, CompositeExpression::TYPE_OR);
519
    }
520
521
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $raw should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
522
     * {@inheritdoc}
523
     */
524 2
    public function havingRaw($raw, $type = CompositeExpression::TYPE_AND)
525
    {
526 2
        $this->compilerState->invalidate('having');
527
528 2
        return $this->buildRaw('having', $raw, $type);
529
    }
530
531
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $raw should have a doc-comment as per coding-style.
Loading history...
532
     * {@inheritdoc}
533
     */
534 1
    public function orHavingRaw($raw)
535
    {
536 1
        return $this->havingRaw($raw, CompositeExpression::TYPE_OR);
537
    }
538
539
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $command should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
540
     * {@inheritdoc}
541
     */
542 6
    public function addCommand($command, $value)
543
    {
544
        switch ($command) {
545 6
            case ':limit':
546 4
                if (is_array($value)) {
547
                    $this->limit($value[0], $value[1]);
548
                } else {
549 4
                    $this->limit($value);
550
                }
551 4
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
552
            
553 4
            case ':limitPage':
554 1
                if (is_array($value)) {
555 1
                    $this->limitPage($value[0], $value[1]);
556
                } else {
557
                    $this->limitPage($value);
558
                }
559 1
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
560
            
561 3
            case ':offset':
562
                $this->offset($value);
563
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
564
            
565 3
            case ':order':
566 2
                $this->order($value);
567 2
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
568
            
569 1
            case ':distinct':
570 1
                $this->distinct($value);
571 1
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
572
                
573
            case ':group':
574
                $this->group($value);
575
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
576
            
577
            case ':having':
578
                $this->having($value);
579
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
580
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end switch"
Loading history...
581
        
582 6
        return $this;
583
    }
584
585
    /**
586
     * {@inheritdoc}
587
     */
588 133
    public function toSql()
589
    {
590 133
        return $this->compile();
591
    }
592
593
    /**
594
     * {@inheritdoc}
595
     * @todo A reprendre: utiliser les types des bindings
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
596
     */
597 15
    public function toRawSql()
598
    {
599 15
        $keys   = [];
600 15
        $sql    = $this->toSql();
601 15
        $values = $this->compiler->getBindings($this);
602
603
        # build a regular expression for each parameter
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
604 15
        foreach ($values as $key => $value) {
605 15
            if (is_string($key)) {
606
                $keys[] = '/:' . $key . '/';
607
            } else {
608 15
                $keys[] = '/[?]/';
609
            }
610
            
611 15
            if (is_array($value)) {
612
                $values[$key] = implode(',', $this->connection->quote($value));
0 ignored issues
show
Bug introduced by
The method quote() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

612
                $values[$key] = implode(',', $this->connection->/** @scrutinizer ignore-call */ quote($value));
Loading history...
613 15
            } elseif (is_null($value)) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
614
                $values[$key] = 'NULL';
615 15
            } elseif ($value instanceof \DateTimeInterface) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
616
                $values[$key] = $value->format($this->connection->platform()->grammar()->getDateTimeFormatString());
617 15
            } elseif (is_string($value)) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
618 8
                $values[$key] = $this->connection->quote($value);
619
            } else {
620 15
                $values[$key] = $value;
621
            }
622
        }
623
        
624 15
        return preg_replace($keys, $values, $sql, 1);
625
    }
626
    
627
    /**
628
     * Gets a string representation of this Query which corresponds to
629
     * the final SQL query being constructed.
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
630
     *
631
     * @return string The string representation of this Query.
632
     */
633
    public function __toString()
634
    {
635
        return $this->toSql();
636
    }
637
}
638