Passed
Push — 2.x ( 18bce1...b13a23 )
by Maxim
17:24
created

PostgresCompiler::distinct()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 3
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Driver\Postgres;
13
14
use Cycle\Database\Driver\CachingCompilerInterface;
15
use Cycle\Database\Driver\Compiler;
16
use Cycle\Database\Driver\Postgres\Injection\CompileJson;
17
use Cycle\Database\Driver\Quoter;
18
use Cycle\Database\Injection\FragmentInterface;
19
use Cycle\Database\Injection\Parameter;
20
use Cycle\Database\Query\QueryParameters;
21
22
/**
23
 * Postgres syntax specific compiler.
24
 */
25
class PostgresCompiler extends Compiler implements CachingCompilerInterface
26
{
27
    protected const ORDER_OPTIONS = [
28 66
        'ASC', 'ASC NULLS LAST', 'ASC NULLS FIRST' ,
29
        'DESC', 'DESC NULLS LAST', 'DESC NULLS FIRST',
30 66
    ];
31
32 66
    /**
33 14
     * @psalm-return non-empty-string
34
     */
35
    protected function insertQuery(QueryParameters $params, Quoter $q, array $tokens): string
36 52
    {
37 52
        $result = parent::insertQuery($params, $q, $tokens);
38
39 52
        if (empty($tokens['return'])) {
40
            return $result;
41
        }
42
43 338
        return \sprintf(
44
            '%s RETURNING %s',
45 338
            $result,
46 286
            \implode(',', \array_map(
47
                fn (string|FragmentInterface|null $return) => $return instanceof FragmentInterface
48
                    ? $this->fragment($params, $q, $return)
49 52
                    : $this->quoteIdentifier($return),
0 ignored issues
show
Bug introduced by
It seems like $return can also be of type null; however, parameter $identifier of Cycle\Database\Driver\Compiler::quoteIdentifier() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

49
                    : $this->quoteIdentifier(/** @scrutinizer ignore-type */ $return),
Loading history...
50 2
                $tokens['return']
51
            ))
52
        );
53 50
    }
54 2
55
    protected function distinct(QueryParameters $params, Quoter $q, string|bool|array $distinct): string
56
    {
57 48
        if ($distinct === false) {
0 ignored issues
show
introduced by
The condition $distinct === false is always false.
Loading history...
58
            return '';
59
        }
60 338
61
        if (\is_array($distinct) && isset($distinct['on'])) {
62 338
            return sprintf('DISTINCT ON (%s)', $this->name($params, $q, $distinct['on']));
63 322
        }
64
65
        if (\is_string($distinct)) {
66 16
            return sprintf('DISTINCT (%s)', $this->name($params, $q, $distinct));
67 16
        }
68 12
69 12
        return 'DISTINCT';
70
    }
71
72 16
    protected function limit(QueryParameters $params, Quoter $q, int $limit = null, int $offset = null): string
73 10
    {
74 10
        if ($limit === null && $offset === null) {
75
            return '';
76
        }
77 16
78
        $statement = '';
79
        if ($limit !== null) {
80
            $statement = 'LIMIT ? ';
81
            $params->push(new Parameter($limit));
82
        }
83
84
        if ($offset !== null) {
85
            $statement .= 'OFFSET ?';
86
            $params->push(new Parameter($offset));
87
        }
88
89
        return trim($statement);
90
    }
91
92
    protected function compileJsonOrderBy(string $path): FragmentInterface
93
    {
94
        return new CompileJson($path);
95
    }
96
}
97