|
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), |
|
|
|
|
|
|
50
|
2 |
|
$tokens['return'], |
|
51
|
|
|
)), |
|
52
|
|
|
); |
|
53
|
50 |
|
} |
|
54
|
2 |
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @psalm-return non-empty-string |
|
57
|
48 |
|
*/ |
|
58
|
|
|
protected function upsertQuery(QueryParameters $params, Quoter $q, array $tokens): string |
|
59
|
|
|
{ |
|
60
|
338 |
|
$query = parent::upsertQuery($params, $q, $tokens); |
|
61
|
|
|
|
|
62
|
338 |
|
if (empty($tokens['return'])) { |
|
63
|
322 |
|
return $query; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
16 |
|
return \sprintf( |
|
67
|
16 |
|
'%s RETURNING %s', |
|
68
|
12 |
|
$query, |
|
69
|
12 |
|
\implode(',', \array_map( |
|
70
|
|
|
fn(string|FragmentInterface|null $return) => $return instanceof FragmentInterface |
|
71
|
|
|
? $this->fragment($params, $q, $return) |
|
72
|
16 |
|
: $this->quoteIdentifier($return), |
|
|
|
|
|
|
73
|
10 |
|
$tokens['return'], |
|
74
|
10 |
|
)), |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
16 |
|
|
|
78
|
|
|
protected function distinct(QueryParameters $params, Quoter $q, string|bool|array $distinct): string |
|
79
|
|
|
{ |
|
80
|
|
|
if ($distinct === false) { |
|
|
|
|
|
|
81
|
|
|
return ''; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (\is_array($distinct) && isset($distinct['on'])) { |
|
85
|
|
|
return \sprintf('DISTINCT ON (%s)', $this->name($params, $q, $distinct['on'])); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (\is_string($distinct)) { |
|
89
|
|
|
return \sprintf('DISTINCT (%s)', $this->name($params, $q, $distinct)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return 'DISTINCT'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function limit(QueryParameters $params, Quoter $q, ?int $limit = null, ?int $offset = null): string |
|
96
|
|
|
{ |
|
97
|
|
|
if ($limit === null && $offset === null) { |
|
98
|
|
|
return ''; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$statement = ''; |
|
102
|
|
|
if ($limit !== null) { |
|
103
|
|
|
$statement = 'LIMIT ? '; |
|
104
|
|
|
$params->push(new Parameter($limit)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($offset !== null) { |
|
108
|
|
|
$statement .= 'OFFSET ?'; |
|
109
|
|
|
$params->push(new Parameter($offset)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return \trim($statement); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function compileJsonOrderBy(string $path): FragmentInterface |
|
116
|
|
|
{ |
|
117
|
|
|
return new CompileJson($path); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|