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
|
|
|
public function build() |
18
|
|
|
{ |
19
|
|
|
switch ($this->query->getType()) { |
20
|
|
|
case Query::TYPE_SELECT: |
21
|
|
|
return $this->select(); |
22
|
|
|
|
23
|
|
|
case Query::TYPE_INSERT: |
24
|
|
|
return $this->insert(); |
25
|
|
|
|
26
|
|
|
case Query::TYPE_UPDATE: |
27
|
|
|
return $this->update(); |
28
|
|
|
|
29
|
|
|
case Query::TYPE_DELETE: |
30
|
|
|
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
|
|
|
private function select() |
42
|
|
|
{ |
43
|
|
|
$this->queryString = "SELECT "; |
44
|
|
|
|
45
|
|
|
$this->queryString .= $this->selectColumn(); |
46
|
|
|
|
47
|
|
|
$table = $this->query->table; |
48
|
|
|
$this->queryString .= " FROM {$this->getFormattedTableOrColumn($table)}"; |
49
|
|
|
|
50
|
|
|
$this->queryString .= $this->where(); |
51
|
|
|
|
52
|
|
|
return $this->queryString; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
private function selectColumn() |
59
|
|
|
{ |
60
|
|
|
$columns = $this->query->columns; |
61
|
|
|
if (is_null($columns)) { |
62
|
|
|
$columns = [Query::sql("*")]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (is_string($columns)) { |
66
|
|
|
$columns = array($columns); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($columns as $key => $val) { |
70
|
|
|
if ($columns[$key] instanceof Query) { |
71
|
|
|
$columns[$key] = $columns[$key]->query; |
72
|
|
|
} else { |
73
|
|
|
$columns[$key] = $this->getFormattedTableOrColumn($val); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return implode(', ', $columns); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
private function where() |
84
|
|
|
{ |
85
|
|
|
if (count($this->query->where) <= 0) { |
86
|
|
|
return ""; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$q = Stringy::create(""); |
90
|
|
|
|
91
|
|
|
foreach ($this->query->where as $where) { |
92
|
|
|
$type = $where[0]; |
93
|
|
|
$arr = $where[1]; |
94
|
|
|
|
95
|
|
|
if ($q->count() > 1) { |
96
|
|
|
$q = $q->append(" {$type} "); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (is_string($arr)) { |
100
|
|
|
$q = $q->append($arr); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (is_array($arr)) { |
104
|
|
|
$q = $q->append($this->buildArrWhere($arr)); |
105
|
|
|
|
106
|
|
|
$this->params[] = $arr[2]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $q->count()?$q->prepend(" WHERE ")->__toString():""; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $arr |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
private function buildArrWhere($arr) |
118
|
|
|
{ |
119
|
|
|
if (count($arr) != 3) { |
120
|
|
|
throw new \OutOfBoundsException(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
switch ($arr[1]) { |
124
|
|
|
case "BETWEEN": |
125
|
|
|
return "`{$arr[0]}` {$arr[1]} ? AND ?"; |
126
|
|
|
default: |
127
|
|
|
return "`{$arr[0]}` {$arr[1]} ?"; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
private function insert() |
135
|
|
|
{ |
136
|
|
|
|
137
|
|
|
$this->params = $this->query->values; |
138
|
|
|
|
139
|
|
|
$table = $this->query->table; |
140
|
|
|
$this->queryString = "INSERT INTO {$this->getFormattedTableOrColumn($table)}"; |
141
|
|
|
|
142
|
|
|
$this->queryString .= "({$this->selectColumn()})"; |
143
|
|
|
|
144
|
|
|
$this->queryString .= " VALUES("; |
145
|
|
|
|
146
|
|
|
$params = array(); |
147
|
|
|
|
148
|
|
|
for ($i=0, $c=count($this->query->columns); $i<$c; $i++) { |
149
|
|
|
$params[$i] = "?"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->queryString .= join(', ', $params).")"; |
153
|
|
|
|
154
|
|
|
return $this->queryString; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
private function update() |
161
|
|
|
{ |
162
|
|
|
$this->params = $this->query->values; |
163
|
|
|
|
164
|
|
|
$table = $this->query->table; |
165
|
|
|
$this->queryString = "UPDATE {$this->getFormattedTableOrColumn($table)} SET "; |
166
|
|
|
|
167
|
|
|
$arrSet = array(); |
168
|
|
|
foreach ($this->query->columns as $column) { |
169
|
|
|
$arrSet[] = "{$this->getFormattedTableOrColumn($column)} = ?"; |
170
|
|
|
} |
171
|
|
|
$this->queryString .= join(", ", $arrSet); |
172
|
|
|
|
173
|
|
|
$this->queryString .= $this->where(); |
174
|
|
|
|
175
|
|
|
return $this->queryString; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
private function delete() |
182
|
|
|
{ |
183
|
|
|
$table = $this->query->table; |
184
|
|
|
$this->queryString = "DELETE FROM {$this->getFormattedTableOrColumn($table)}"; |
185
|
|
|
$this->queryString .= $this->where(); |
186
|
|
|
|
187
|
|
|
return $this->queryString; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param null $str |
192
|
|
|
* @return null|string |
193
|
|
|
*/ |
194
|
|
|
private function getFormattedTableOrColumn($str = null) |
195
|
|
|
{ |
196
|
|
|
$tmpStr = $str?$str:$this->query->table; |
197
|
|
|
$strArr = explode(".", $tmpStr); |
198
|
|
|
|
199
|
|
|
foreach ($strArr as $key => $val) { |
200
|
|
|
$strArr[$key] = "`{$val}`"; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return join(".", $strArr); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|