-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBiscuitMySQL.php
307 lines (290 loc) · 11.5 KB
/
BiscuitMySQL.php
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/*
* Class responsible for parsing BISCUIT into MySQL.
*/
class BiscuitMySQL implements Parser
{
private $MySQL_Result;
public function __construct($query_input)
{
try {
$query = BiscuitQuery::convertToChildQueryType($query_input);
if ($query instanceof BiscuitBuildQuery) {
$this->MySQL_Result = BiscuitMySQL::parseBuild($query);
} else if ($query instanceof BiscuitChangeQuery) {
$this->MySQL_Result = BiscuitMySQL::parseChange($query);
} else if ($query instanceof BiscuitFetchQuery) {
$this->MySQL_Result = BiscuitMySQL::parseFetch($query);
} else if ($query instanceof BiscuitUpdateQuery) {
$this->MySQL_Result = BiscuitMySQL::parseUpdate($query);
} else if ($query instanceof BiscuitInsertQuery) {
$this->MySQL_Result = BiscuitMySQL::parseInsert($query);
} else if ($query instanceof BiscuitDestroyQuery) {
$this->MySQL_Result = BiscuitMySQL::parseDestroy($query);
} else if ($query instanceof BiscuitRemoveQuery) {
$this->MySQL_Result = BiscuitMySQL::parseRemove($query);
}
} catch (BiscuitException $e) {
$this->MySQL_Result = $e->getExceptionMessage();
}
}
public function getMySQLResult()
{
return $this->MySQL_Result;
}
/* *** BUILD QUERY *** */
public static function parseBuild(BiscuitBuildQuery $query)
{
// BISCUIT --> BUILD users { user_id:int:autoinc:notnull, username:string:notnull };
// MySQL --> CREATE TABLE users ( user_id BIGINT NOT NULL AUTO_INCREMENT, username TEXT NOT NULL );
try {
$MySQL_Result = "CREATE TABLE " . $query->getTableName() . " ( ";
$attributes = $query->getAttributes();
foreach ($attributes as $attr) {
if ($attr instanceof Attribute) {
$key = $attr->getKey();
$value = BiscuitMySQL::getMySQLTypes($attr->getValue());
$MySQL_Result .= "\n" . $key . ' ' . $value;
// Check for extras
$extras = $attr->getExtras();
if (count($extras) > 0) {
foreach ($extras as $ext) {
$MySQL_Result .= ' ' . BiscuitMySQL::getMySQLAttributes($ext);
}
}
$MySQL_Result .= ", ";
} else {
break;
}
}
// Remove comma
$MySQL_Result = substr($MySQL_Result, 0, -2);
$MySQL_Result .= "\n);";
} catch (BiscuitException $e) {
$MySQL_Result = 'Failed to process Build Query into MySQL: ' . $query->getQuery();
$MySQL_Result .= $e->getExceptionMessage();
return $MySQL_Result;
}
return $MySQL_Result;
}
/* *** FETCH QUERY *** */
public static function parseFetch(BiscuitFetchQuery $query)
{
// BISCUIT --> FETCH users { [user_id, status] WHERE(status:eq:A) SORT(asc:user_id) };
// MySQL --> SELECT user_id, status FROM users WHERE status = 'A' ORDER BY user_id ASC;
try {
$MySQL_Result = "SELECT ";
$columns = $query->getColumnNames();
if ($columns != 'all') {
$columns = implode(', ', $columns);
} else {
$columns = '*';
}
$MySQL_Result .= $columns;
$MySQL_Result .= ' FROM ' . $query->getTableName();
if ($query->getWhereClause()) {
$where_args = $query->getWhereClause()->getWhereArguments();
$MySQL_Result .= ' WHERE ' . $where_args['column'];
$MySQL_Result .= ' ' . BiscuitMySQL::getMySQLComparison($where_args['operator']) . ' ';
$MySQL_Result .= '\'' . $where_args['value'] . '\'';
}
if ($query->getSortClause()) {
$sort_args = $query->getSortClause()->getSortArguments();
$MySQL_Result .= ' ORDER BY ' . $sort_args['column'];
$MySQL_Result .= ' ' . BiscuitMySQL::getMySQLFunction($sort_args['type']);
}
$MySQL_Result .= ';';
} catch (BiscuitException $e) {
$MySQL_Result = 'Failed to process Fetch Query into MySQL: ' . $query->getQuery();
$MySQL_Result .= $e->getExceptionMessage();
}
return $MySQL_Result;
}
/* *** CHANGE QUERY *** */
public static function parseChange(BiscuitChangeQuery $query)
{
// BISCUIT --> CHANGE users { add(join_date:date) };
// MYSQL --> ALTER TABLE users ADD join_date DATETIME;
try {
$MySQL_Result = 'ALTER TABLE ' . $query->getTableName();
$MySQL_Result .= ' ' . BiscuitMySQL::getMySQLAlter($query->getClauseType());
$alter_args = $query->getArguments();
$MySQL_Result .= ' ' . $alter_args['column'];
if (count($alter_args) > 1)
$MySQL_Result .= ' ' . BiscuitMySQL::getMySQLTypes($alter_args['type']);
$MySQL_Result .= ';';
} catch (BiscuitException $e) {
$MySQL_Result = 'Failed to process Change Query into MySQL: ' . $query->getQuery();
$MySQL_Result .= $e->getExceptionMessage();
return $MySQL_Result;
}
return $MySQL_Result;
}
/* *** UPDATE QUERY *** */
public static function parseUpdate(BiscuitUpdateQuery $query)
{
// BISCUIT --> UPDATE users { SET(user_id:123) WHERE(status:eq:A) };
// MYSQL --> UPDATE users SET user_id = '123' WHERE status = 'A';
try {
$MySQL_Result = 'UPDATE ' . $query->getTableName();
$set_args = $query->getSetClause()->getSetArguments();
$MySQL_Result .= ' SET ' . $set_args['column'] . ' = \'' . $set_args['value'] . '\'';
$where_args = $query->getWhereClause()->getWhereArguments();
$MySQL_Result .= ' WHERE ' . $where_args['column'];
$MySQL_Result .= ' ' . BiscuitMySQL::getMySQLComparison($where_args['operator']) . ' ';
$MySQL_Result .= '\'' . $where_args['value'] . '\'';
} catch (BiscuitException $e) {
$MySQL_Result = 'Failed to process Update Query into MySQL: ' . $query->getQuery();
$MySQL_Result .= $e->getExceptionMessage();
}
return $MySQL_Result;
}
/* *** INSERT QUERY *** */
public static function parseInsert(BiscuitInsertQuery $query)
{
// BISCUIT --> INSERT users { user_id:bcd001, age:45, status:A };
// MYSQL --> INSERT INTO users (user_id, age, status) VALUES ('bcd001', '45', 'A');
try {
$MySQL_Result = 'INSERT INTO ' . $query->getTableName();
$cols = array();
$values = array();
foreach ($query->getInsertPairs() as $pair) {
$cols[] = $pair['column'];
$values[] = $pair['value'];
}
$MySQL_Result .= ' (' . implode(', ', $cols) . ')';
$MySQL_Result .= ' VALUES (\'' . implode('\', \'', $values) . '\')';
$MySQL_Result .= ';';
} catch (BiscuitException $e) {
$MySQL_Result = 'Failed to process Insert Query into MySQL: ' . $query->getQuery();
$MySQL_Result .= $e->getExceptionMessage();
return $MySQL_Result;
}
return $MySQL_Result;
}
/* *** DESTROY QUERY *** */
public static function parseDestroy(BiscuitDestroyQuery $query)
{
// BISCUIT --> DESTROY users;
// MySQL --> DROP TABLE IF EXISTS users;
try {
$MySQL_Result = "DROP TABLE IF EXISTS " . $query->getTableName() . ";";
return $MySQL_Result;
} catch (BiscuitException $e) {
$MySQL_Result = 'Failed to process Delete Query into MySQL: ' . $query->getQuery();
$MySQL_Result .= $e->getExceptionMessage();
return $MySQL_Result;
}
}
/* *** REMOVE QUERY *** */
public static function parseRemove(BiscuitRemoveQuery $query)
{
// BISCUIT --> REMOVE users { WHERE(username:eq:user001) };
// MySQL --> DELETE FROM users WHERE username = 'user001';
try {
$MySQL_Result = "DELETE FROM " . $query->getTableName();
$where_args = $query->getWhereClause()->getWhereArguments();
$MySQL_Result .= ' WHERE ' . $where_args['column'];
$MySQL_Result .= ' ' . BiscuitMySQL::getMySQLComparison($where_args['operator']) . ' ';
$MySQL_Result .= '\'' . $where_args['value'] . '\'';
$MySQL_Result .= ';';
} catch (BiscuitException $e) {
$MySQL_Result = 'Failed to process Remove Query into MySQL: ' . $query->getQuery();
$MySQL_Result .= $e->getExceptionMessage();
}
return $MySQL_Result;
}
/*
* MYSQL
* Function to convert alter
*/
public static function getMySQLAlter($alter)
{
switch ($alter) {
case 'add':
return 'ADD';
case 'remove':
return 'DROP';
case 'rename':
return 'RENAME TO';
default:
throw new BiscuitException('The alter method does not match a valid type: ' . $alter);
}
}
/*
* MYSQL
* Function to convert comparison operators from literal strings to their MySQL values.
*/
public static function getMySQLComparison($operator)
{
switch ($operator) {
case 'eq':
return '=';
case 'gt':
return '>';
case 'lt':
return '<';
case 'gte':
return '>=';
case 'lte':
return '<=';
case 'neq':
return '!=';
default:
throw new BiscuitException('Invalid BISCUIT to MySQL operator given: ' . $operator);
}
}
/* MYSQL
* Function to be able to parse the BISCUIT types into MySQL types
*/
public static function getMySQLTypes($type)
{
switch ($type) {
case 'string':
return 'TEXT';
case 'int':
return 'BIGINT';
case 'dec':
return 'DOUBLE';
case 'date':
return 'DATE';
case 'time':
return 'TIME';
case 'timestamp':
return 'TIMESTAMP';
default:
throw new BiscuitException('Invalid BISCUIT to MySQL Type given: ' . $type);
}
}
/* MYSQL
* Function to be able to parse the BISCUIT attribute extras into MySQL
*/
public static function getMySQLAttributes($attribute)
{
switch ($attribute) {
case 'autoinc':
return 'AUTO_INCREMENT';
case 'notnull':
return 'NOT NULL';
case 'unique':
return 'UNIQUE';
default:
throw new BiscuitException('Invalid MySQL Attribute given: ' . $attribute);
}
}
/*
* MYSQL
* Function that returns the correct function names.
*/
public static function getMySQLFunction($function)
{
switch ($function) {
case 'asc':
return 'ASC';
case'dsc':
return 'DESC';
default:
throw new BiscuitException('Failed to convert to the MySQL function: ' . $function);
}
}
}