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
![]() |
|||
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
|
|||
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 |