-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMySQLCompiler.php
More file actions
110 lines (94 loc) · 3.08 KB
/
MySQLCompiler.php
File metadata and controls
110 lines (94 loc) · 3.08 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
<?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\MySQL;
use Cycle\Database\Query\Enum\LockMode;
use Cycle\Database\Query\Enum\LockBehavior;
use Cycle\Database\Driver\CachingCompilerInterface;
use Cycle\Database\Driver\Compiler;
use Cycle\Database\Driver\MySQL\Injection\CompileJson;
use Cycle\Database\Driver\Quoter;
use Cycle\Database\Injection\FragmentInterface;
use Cycle\Database\Injection\Parameter;
use Cycle\Database\Query\QueryParameters;
/**
* MySQL syntax specific compiler.
*/
class MySQLCompiler extends Compiler implements CachingCompilerInterface
{
protected function insertQuery(QueryParameters $params, Quoter $q, array $tokens): string
{
if ($tokens['columns'] === []) {
return \sprintf(
'INSERT INTO %s () VALUES ()',
$this->name($params, $q, $tokens['table'], true),
);
}
return parent::insertQuery($params, $q, $tokens);
}
/**
*
*
* @link http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990
*/
protected function limit(QueryParameters $params, Quoter $q, ?int $limit = null, ?int $offset = null): string
{
if ($limit === null && $offset === null) {
return '';
}
$statement = '';
if ($limit === null) {
// When limit is not provided (or 0) but offset does we can replace
// limit value with PHP_INT_MAX
$statement .= 'LIMIT 18446744073709551615 ';
} else {
$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);
}
/**
* @param array{mode: LockMode, behavior: LockBehavior}|null $forUpdate
*/
protected function forUpdate(?array $forUpdate): string
{
if ($forUpdate !== null) {
$arguments = [];
switch ($forUpdate['mode']) {
case LockMode::Share:
case LockMode::KeyShare:
$arguments[] = 'SHARE';
break;
case LockMode::Update:
case LockMode::NoKeyUpdate:
$arguments[] = 'UPDATE';
break;
}
switch ($forUpdate['behavior']) {
case LockBehavior::Wait:
break;
case LockBehavior::NoWait:
$arguments[] = 'NOWAIT';
break;
case LockBehavior::SkipLocked:
$arguments[] = 'SKIP LOCKED';
break;
}
return \sprintf('FOR %s', \implode(' ', $arguments));
}
return '';
}
}