-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathPostgresCompiler.php
More file actions
119 lines (99 loc) · 3.32 KB
/
PostgresCompiler.php
File metadata and controls
119 lines (99 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* This file is part of Cycle ORM package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cycle\Database\Driver\Postgres;
use Cycle\Database\Driver\CachingCompilerInterface;
use Cycle\Database\Driver\Compiler;
use Cycle\Database\Driver\Postgres\Injection\CompileJson;
use Cycle\Database\Driver\Quoter;
use Cycle\Database\Injection\FragmentInterface;
use Cycle\Database\Injection\Parameter;
use Cycle\Database\Query\QueryParameters;
/**
* Postgres syntax specific compiler.
*/
class PostgresCompiler extends Compiler implements CachingCompilerInterface
{
protected const ORDER_OPTIONS = [
'ASC', 'ASC NULLS LAST', 'ASC NULLS FIRST' ,
'DESC', 'DESC NULLS LAST', 'DESC NULLS FIRST',
];
/**
* @psalm-return non-empty-string
*/
protected function insertQuery(QueryParameters $params, Quoter $q, array $tokens): string
{
$result = parent::insertQuery($params, $q, $tokens);
if (empty($tokens['return'])) {
return $result;
}
return \sprintf(
'%s RETURNING %s',
$result,
\implode(',', \array_map(
fn(string|FragmentInterface|null $return) => $return instanceof FragmentInterface
? $this->fragment($params, $q, $return)
: $this->quoteIdentifier($return),
$tokens['return'],
)),
);
}
/**
* @psalm-return non-empty-string
*/
protected function upsertQuery(QueryParameters $params, Quoter $q, array $tokens): string
{
$query = parent::upsertQuery($params, $q, $tokens);
if (empty($tokens['return'])) {
return $query;
}
return \sprintf(
'%s RETURNING %s',
$query,
\implode(',', \array_map(
fn(string|FragmentInterface|null $return) => $return instanceof FragmentInterface
? $this->fragment($params, $q, $return)
: $this->quoteIdentifier($return),
$tokens['return'],
)),
);
}
protected function distinct(QueryParameters $params, Quoter $q, string|bool|array $distinct): string
{
if ($distinct === false) {
return '';
}
if (\is_array($distinct) && isset($distinct['on'])) {
return \sprintf('DISTINCT ON (%s)', $this->name($params, $q, $distinct['on']));
}
if (\is_string($distinct)) {
return \sprintf('DISTINCT (%s)', $this->name($params, $q, $distinct));
}
return 'DISTINCT';
}
protected function limit(QueryParameters $params, Quoter $q, ?int $limit = null, ?int $offset = null): string
{
if ($limit === null && $offset === null) {
return '';
}
$statement = '';
if ($limit !== null) {
$statement = 'LIMIT ? ';
$params->push(new Parameter($limit));
}
if ($offset !== null) {
$statement .= 'OFFSET ?';
$params->push(new Parameter($offset));
}
return \trim($statement);
}
protected function compileJsonOrderBy(string $path): FragmentInterface
{
return new CompileJson($path);
}
}