Passed
Pull Request — master (#13)
by Vincent
07:15
created

Query::replace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
c 1
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
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 722
    public function __construct(ConnectionInterface $connection, PreprocessorInterface $preprocessor = null)
44
    {
45 722
        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 722
        $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 722
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 581
    public function getBindings()
70
    {
71 581
        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 442
    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 442
        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 442
            $this->statements['values'] = [
133 442
                'data'   => $data,
134
            ];
135
        }
136
        
137 442
        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 17
    public function values($data = [], array $types = [])
198
    {
199 17
        $this->statements['values'] = [
200 17
            'data' => $data,
201 17
            'type' => $types
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: $types
Loading history...
202
        ];
203
204 17
        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 455
    protected function executeUpdate($type)
217
    {
218 455
        $this->setType($type);
219
220 455
        $nb = $this->connection->execute($this)->count();
221
222 455
        if ($nb > 0) {
223 446
            $this->clearCacheOnWrite();
224
        }
225
226 455
        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 501
    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 501
        if (!empty($columns)) {
238 52
            $this->select($columns);
239
        }
240
241 501
        $this->setType(self::TYPE_SELECT);
242
243 501
        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 345
    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 345
        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 354
    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 354
        $statements = $this->statements;
360
361 354
        $this->compilerState->invalidate('columns');
362
363 354
        $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 354
        $aggregate = $this->execute()[0]['aggregate'];
366
367 354
        $this->compilerState->invalidate('columns');
368 354
        $this->statements = $statements;
369
370 354
        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 692
    public function from($from, $alias = null)
389
    {
390 692
        $this->compilerState->invalidate('from');
391
        
392 692
        $this->statements['tables'][] = [
393 692
            'table' => $from,
394 692
            'alias' => $alias,
395
        ];
396
        
397 692
        return $this;
398
    }
399
400
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
401
     * {@inheritdoc}
402
     */
403 5
    public function group($column)
404
    {
405 5
        $this->compilerState->invalidate('groups');
406
        
407 5
        $this->statements['groups'] = is_array($column) ? $column : func_get_args();
408
        
409 5
        return $this;
410
    }
411
412
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
413
     * {@inheritdoc}
414
     */
415
    public function addGroup($column)
416
    {
417
        $this->compilerState->invalidate('groups');
418
        
419
        $this->statements['groups'] = array_merge($this->statements['groups'], is_array($column) ? $column : func_get_args());
420
        
421
        return $this;
422
    }
423
424
    /**
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...
425
     * {@inheritdoc}
426
     */
427 2
    public function having($column, $operator = null, $value = null)
428
    {
429 2
        $this->compilerState->invalidate('having');
430
431 2
        return $this->buildClause('having', $column, $operator, $value);
432
    }
433
434
    /**
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...
435
     * {@inheritdoc}
436
     */
437 3
    public function orHaving($column, $operator = null, $value = null)
438
    {
439 3
        $this->compilerState->invalidate('having');
440
441 3
        return $this->buildClause('having', $column, $operator, $value, CompositeExpression::TYPE_OR);
442
    }
443
444
    /**
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...
445
     * {@inheritdoc}
446
     */
447 2
    public function havingNull($column, $type = CompositeExpression::TYPE_AND)
448
    {
449 2
        $this->compilerState->invalidate('having');
450
451 2
        return $this->buildClause('having', $column, '=', null, $type);
452
    }
453
454
    /**
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...
455
     * {@inheritdoc}
456
     */
457 2
    public function havingNotNull($column, $type = CompositeExpression::TYPE_AND)
458
    {
459 2
        $this->compilerState->invalidate('having');
460
461 2
        return $this->buildClause('having', $column, '!=', null, $type);
462
    }
463
464
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
465
     * {@inheritdoc}
466
     */
467 1
    public function orHavingNull($column)
468
    {
469 1
        return $this->havingNull($column, CompositeExpression::TYPE_OR);
470
    }
471
472
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
473
     * {@inheritdoc}
474
     */
475 1
    public function orHavingNotNull($column)
476
    {
477 1
        return $this->havingNotNull($column, CompositeExpression::TYPE_OR);
478
    }
479
480
    /**
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...
481
     * {@inheritdoc}
482
     */
483 2
    public function havingRaw($raw, $type = CompositeExpression::TYPE_AND)
484
    {
485 2
        $this->compilerState->invalidate('having');
486
487 2
        return $this->buildRaw('having', $raw, $type);
488
    }
489
490
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $raw should have a doc-comment as per coding-style.
Loading history...
491
     * {@inheritdoc}
492
     */
493 1
    public function orHavingRaw($raw)
494
    {
495 1
        return $this->havingRaw($raw, CompositeExpression::TYPE_OR);
496
    }
497
498
    /**
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...
499
     * {@inheritdoc}
500
     */
501 6
    public function addCommand($command, $value)
502
    {
503
        switch ($command) {
504 6
            case ':limit':
505 4
                if (is_array($value)) {
506
                    $this->limit($value[0], $value[1]);
507
                } else {
508 4
                    $this->limit($value);
509
                }
510 4
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
511
            
512 4
            case ':limitPage':
513 1
                if (is_array($value)) {
514 1
                    $this->limitPage($value[0], $value[1]);
515
                } else {
516
                    $this->limitPage($value);
517
                }
518 1
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
519
            
520 3
            case ':offset':
521
                $this->offset($value);
522
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
523
            
524 3
            case ':order':
525 2
                $this->order($value);
526 2
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
527
            
528 1
            case ':distinct':
529 1
                $this->distinct($value);
530 1
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
531
                
532
            case ':group':
533
                $this->group($value);
534
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
535
            
536
            case ':having':
537
                $this->having($value);
538
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
539
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end switch"
Loading history...
540
        
541 6
        return $this;
542
    }
543
544
    /**
545
     * {@inheritdoc}
546
     */
547 126
    public function toSql()
548
    {
549 126
        return $this->compile();
550
    }
551
552
    /**
553
     * {@inheritdoc}
554
     * @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...
555
     */
556 15
    public function toRawSql()
557
    {
558 15
        $keys   = [];
559 15
        $sql    = $this->toSql();
560 15
        $values = $this->compiler->getBindings($this);
561
562
        # 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...
563 15
        foreach ($values as $key => $value) {
564 15
            if (is_string($key)) {
565
                $keys[] = '/:' . $key . '/';
566
            } else {
567 15
                $keys[] = '/[?]/';
568
            }
569
            
570 15
            if (is_array($value)) {
571
                $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

571
                $values[$key] = implode(',', $this->connection->/** @scrutinizer ignore-call */ quote($value));
Loading history...
572 15
            } elseif (is_null($value)) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
573
                $values[$key] = 'NULL';
574 15
            } elseif ($value instanceof \DateTimeInterface) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
575
                $values[$key] = $value->format($this->connection->platform()->grammar()->getDateTimeFormatString());
576 15
            } elseif (is_string($value)) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
577 8
                $values[$key] = $this->connection->quote($value);
578
            } else {
579 15
                $values[$key] = $value;
580
            }
581
        }
582
        
583 15
        return preg_replace($keys, $values, $sql, 1);
584
    }
585
    
586
    /**
587
     * Gets a string representation of this Query which corresponds to
588
     * 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...
589
     *
590
     * @return string The string representation of this Query.
591
     */
592
    public function __toString()
593
    {
594
        return $this->toSql();
595
    }
596
}
597