1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Database\Query\Grammar; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Database\Query\Query; |
5
|
|
|
use Stringy\Stringy; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class GrammarMySql |
9
|
|
|
* @package JayaCode\Framework\Core\Database\Query\Grammar |
10
|
|
|
*/ |
11
|
|
|
class GrammarMySql extends Grammar |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
60 |
|
public function build() |
18
|
|
|
{ |
19
|
60 |
|
switch ($this->query->getType()) { |
20
|
60 |
|
case Query::TYPE_SELECT: |
21
|
51 |
|
return $this->select(); |
22
|
|
|
|
23
|
9 |
|
case Query::TYPE_INSERT: |
24
|
3 |
|
return $this->insert(); |
25
|
|
|
|
26
|
6 |
|
case Query::TYPE_UPDATE: |
27
|
3 |
|
return $this->update(); |
28
|
|
|
|
29
|
3 |
|
case Query::TYPE_DELETE: |
30
|
3 |
|
return $this->delete(); |
31
|
|
|
|
32
|
|
|
case Query::TYPE_QUERY: |
33
|
|
|
return $this->query->query; |
34
|
|
|
} |
35
|
|
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
51 |
|
private function select() |
42
|
|
|
{ |
43
|
51 |
|
$this->queryString = "SELECT "; |
44
|
|
|
|
45
|
51 |
|
$this->queryString .= $this->selectColumn(); |
46
|
|
|
|
47
|
51 |
|
$table = $this->query->table; |
48
|
51 |
|
$this->queryString .= " FROM {$this->getFormattedTableOrColumn($table)}"; |
49
|
|
|
|
50
|
51 |
|
$this->queryString .= $this->where(); |
51
|
|
|
|
52
|
51 |
|
$this->queryString .= $this->sort(); |
53
|
51 |
|
$this->queryString .= $this->limit(); |
54
|
|
|
|
55
|
51 |
|
return $this->queryString; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
54 |
|
private function selectColumn() |
62
|
|
|
{ |
63
|
54 |
|
$columns = $this->query->columns; |
64
|
54 |
|
if (is_null($columns)) { |
65
|
42 |
|
$columns = [Query::sql("*")]; |
66
|
42 |
|
} |
67
|
|
|
|
68
|
54 |
|
if (is_string($columns)) { |
69
|
3 |
|
$columns = array($columns); |
70
|
3 |
|
} |
71
|
|
|
|
72
|
54 |
|
foreach ($columns as $key => $val) { |
73
|
54 |
|
if ($columns[$key] instanceof Query) { |
74
|
45 |
|
$columns[$key] = $columns[$key]->query; |
75
|
45 |
|
} else { |
76
|
12 |
|
$columns[$key] = $this->getFormattedTableOrColumn($val); |
77
|
|
|
} |
78
|
54 |
|
} |
79
|
|
|
|
80
|
54 |
|
return implode(', ', $columns); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
57 |
|
private function where() |
87
|
|
|
{ |
88
|
57 |
|
if (count($this->query->where) <= 0) { |
89
|
12 |
|
return ""; |
90
|
|
|
} |
91
|
|
|
|
92
|
45 |
|
$q = Stringy::create(""); |
93
|
|
|
|
94
|
45 |
|
foreach ($this->query->where as $where) { |
95
|
45 |
|
$type = $where[0]; |
96
|
45 |
|
$arr = $where[1]; |
97
|
|
|
|
98
|
45 |
|
if ($q->count() > 1) { |
99
|
18 |
|
$q = $q->append(" {$type} "); |
100
|
18 |
|
} |
101
|
|
|
|
102
|
45 |
|
if (is_string($arr)) { |
103
|
3 |
|
$q = $q->append($arr); |
104
|
3 |
|
} |
105
|
|
|
|
106
|
45 |
|
if (is_array($arr)) { |
107
|
45 |
|
$q = $q->append($this->buildArrWhere($arr)); |
108
|
|
|
|
109
|
45 |
|
$this->params[] = $arr[2]; |
110
|
45 |
|
} |
111
|
45 |
|
} |
112
|
|
|
|
113
|
45 |
|
return $q->count()?$q->prepend(" WHERE ")->__toString():""; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
51 |
|
private function sort() |
120
|
|
|
{ |
121
|
|
|
|
122
|
51 |
|
$sort = $this->query->sort; |
123
|
|
|
|
124
|
51 |
|
if (!isset($sort['column']) || !isset($sort['order'])) { |
125
|
45 |
|
return ""; |
126
|
|
|
} |
127
|
|
|
|
128
|
6 |
|
return " ORDER BY {$this->getFormattedTableOrColumn($sort['column'])} {$sort['order']}"; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
51 |
|
private function limit() |
135
|
|
|
{ |
136
|
51 |
|
if (!is_numeric($this->query->limit)) { |
137
|
45 |
|
return ""; |
138
|
|
|
} |
139
|
|
|
|
140
|
6 |
|
$str = " LIMIT {$this->query->limit}"; |
141
|
|
|
|
142
|
6 |
|
if (is_numeric($this->query->offset)) { |
143
|
3 |
|
$str .= " OFFSET {$this->query->offset}"; |
144
|
3 |
|
} |
145
|
6 |
|
return $str; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $arr |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
45 |
|
private function buildArrWhere($arr) |
153
|
|
|
{ |
154
|
45 |
|
if (count($arr) != 3) { |
155
|
|
|
throw new \OutOfBoundsException(); |
156
|
|
|
} |
157
|
|
|
|
158
|
45 |
|
switch ($arr[1]) { |
159
|
45 |
|
case "BETWEEN": |
160
|
|
|
return "`{$arr[0]}` {$arr[1]} ? AND ?"; |
161
|
45 |
|
default: |
162
|
45 |
|
return "`{$arr[0]}` {$arr[1]} ?"; |
163
|
45 |
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
3 |
|
private function insert() |
170
|
|
|
{ |
171
|
|
|
|
172
|
3 |
|
$this->params = $this->query->values; |
173
|
|
|
|
174
|
3 |
|
$table = $this->query->table; |
175
|
3 |
|
$this->queryString = "INSERT INTO {$this->getFormattedTableOrColumn($table)}"; |
176
|
|
|
|
177
|
3 |
|
$this->queryString .= "({$this->selectColumn()})"; |
178
|
|
|
|
179
|
3 |
|
$this->queryString .= " VALUES("; |
180
|
|
|
|
181
|
3 |
|
$params = array(); |
182
|
|
|
|
183
|
3 |
|
for ($i=0, $c=count($this->query->columns); $i<$c; $i++) { |
184
|
3 |
|
$params[$i] = "?"; |
185
|
3 |
|
} |
186
|
|
|
|
187
|
3 |
|
$this->queryString .= join(', ', $params).")"; |
188
|
|
|
|
189
|
3 |
|
return $this->queryString; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
3 |
|
private function update() |
196
|
|
|
{ |
197
|
3 |
|
$this->params = $this->query->values; |
198
|
|
|
|
199
|
3 |
|
$table = $this->query->table; |
200
|
3 |
|
$this->queryString = "UPDATE {$this->getFormattedTableOrColumn($table)} SET "; |
201
|
|
|
|
202
|
3 |
|
$arrSet = array(); |
203
|
3 |
|
foreach ($this->query->columns as $column) { |
204
|
3 |
|
$arrSet[] = "{$this->getFormattedTableOrColumn($column)} = ?"; |
205
|
3 |
|
} |
206
|
3 |
|
$this->queryString .= join(", ", $arrSet); |
207
|
|
|
|
208
|
3 |
|
$this->queryString .= $this->where(); |
209
|
|
|
|
210
|
3 |
|
return $this->queryString; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
3 |
|
private function delete() |
217
|
|
|
{ |
218
|
3 |
|
$table = $this->query->table; |
219
|
3 |
|
$this->queryString = "DELETE FROM {$this->getFormattedTableOrColumn($table)}"; |
220
|
3 |
|
$this->queryString .= $this->where(); |
221
|
|
|
|
222
|
3 |
|
return $this->queryString; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param null $str |
227
|
|
|
* @return null|string |
228
|
|
|
*/ |
229
|
60 |
|
private function getFormattedTableOrColumn($str = null) |
230
|
|
|
{ |
231
|
60 |
|
$tmpStr = $str?$str:$this->query->table; |
232
|
60 |
|
$strArr = explode(".", $tmpStr); |
233
|
|
|
|
234
|
60 |
|
foreach ($strArr as $key => $val) { |
235
|
60 |
|
$strArr[$key] = "`{$val}`"; |
236
|
60 |
|
} |
237
|
|
|
|
238
|
60 |
|
return join(".", $strArr); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|