forked from crodas/SQLParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL.php
More file actions
237 lines (207 loc) · 6.29 KB
/
Copy pathMySQL.php
File metadata and controls
237 lines (207 loc) · 6.29 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2021 César Rodas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* -
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* -
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace SQL\Writer;
use SQL\BeginTransaction;
use SQL\CommitTransaction;
use SQL\Insert;
use SQL\RollbackTransaction;
use SQL\Select;
use SQL\Table;
use SQL\Writer;
use SQLParser\Stmt;
/**
* MySQL Query writer.
*
* This class extends the standard SQL writer to add MySQL flavoured
* SQL statements.
*
* Class MySQL
*/
class MySQL extends Writer
{
/**
* Returns "escaped" value.
*
* @param $value
*
* @return string
*/
public function escape($value)
{
if (!\is_string($value)) {
return $this->value($value);
}
return "`{$value}`";
}
/**
* Returns any special "option" defined in the query. Options are
* unique to MySQL.
*
* @return string
*/
public function selectOptions(Select $select)
{
$options = $select->getOptions();
if (!empty($options)) {
return implode(' ', $options) . ' ';
}
}
/**
* Returns the SQL statement for a commit or a save point (for nested transactions).
*
* @return string
*/
public function commit(CommitTransaction $transaction)
{
if ($transaction->getName()) {
return 'RELEASE SAVEPOINT ' . $this->escape($transaction->getName());
}
return 'COMMIT WORK';
}
/**
* Returns the SQL statement for rollback a transaction or a save point (for nested transactions).
*
* @return string
*/
public function rollback(RollbackTransaction $transaction)
{
if ($transaction->getName()) {
return 'ROLLBACK TO ' . $this->escape($transaction->getName());
}
return 'ROLLBACK WORK';
}
/**
* Returns the SQL statement for start a transaction or any save point (for nested transactions).
*
* @return string
*/
public function begin(BeginTransaction $transaction)
{
if ($transaction->getName()) {
return 'SAVEPOINT ' . $this->escape($transaction->getName());
}
return 'BEGIN WORK';
}
/**
* Returns the SQL statement for creating a table.
*
* Create statements in MySQL are special because they may include indexes
* definitions within the same CREATE TABLE statement.
*
* @return string
*/
public function createTable(Table $table)
{
$columns = [];
foreach ($table->getColumns() as $column) {
$columns[] = $this->columnDefinition($column);
}
$primaryKey = [];
foreach ($table->getPrimaryKey() as $column) {
$primaryKey[] = $this->escape($column->getName());
}
$keys = [];
if (!empty($primaryKey)) {
$keys[] = 'PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
}
foreach ($table->getIndexes() as $name => $definition) {
$keys[] = ($definition['unique'] ? 'UNIQUE KEY ' : 'KEY ')
. $this->escape($name) . '(' . $this->exprList($definition['cols']) . ')';
}
$sql = 'CREATE TABLE ' . $this->escape($table->getName()) . '('
. implode(',', array_merge($columns, $keys))
. ')';
foreach ($table->getOptions() as $key => $value) {
$sql .= " {$key} = {$value}";
}
return $sql;
}
/**
* Returns the SQL statement for an INSERT.
*
* This method adds MySQL support ON DUPLICATE KEY UPDATE.
*
* @return string
*/
public function insert(Insert $insert)
{
$stmt = parent::insert($insert);
if ($insert->getOnDuplicate()) {
$stmt .= ' ON DUPLICATE KEY UPDATE ' . $this->exprList($insert->getOnDuplicate());
}
return $stmt;
}
/**
* Returns SQL statements for definition of columns.
*
* @return string
*/
public function columnDefinition(Stmt\Column $column)
{
$sql = $this->escape($column->GetName())
. ' '
. $this->dataType($column->getType(), $column->getTypeSize())
. $column->getModifier();
if ($column->isNotNull()) {
$sql .= ' NOT NULL';
}
if ($column->isAutoIncrement()) {
$sql .= ' AUTO_INCREMENT';
}
if ($column->defaultValue()) {
$sql .= ' DEFAULT ' . $this->value($column->defaultValue());
}
if ($column->collate()) {
$sql .= ' COLLATE' . $this->value($column->collate());
}
return $sql;
}
/**
* Write MySQL GLOB equivalent statement using LIKE BINARY.
*
* @return string
*/
public function exprGlob(Stmt\Expr $expr)
{
$members = [];
foreach ($expr->getMembers() as $part) {
$members[] = $this->value($part);
}
return "{$members[0]} LIKE BINARY {$members[1]}";
}
/**
* Write MySQL NOT GLOB equivalent statement using NOT LIKE BINARY.
*
* @return string
*/
public function exprNotGlob(Stmt\Expr $expr)
{
$members = [];
foreach ($expr->getMembers() as $part) {
$members[] = $this->value($part);
}
return "{$members[0]} NOT LIKE BINARY {$members[1]}";
}
}