|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2017. Este código foi feito por @marciioluucas, sob licença MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Phiber\ORM\Queries; |
|
7
|
|
|
|
|
8
|
|
|
use Phiber\ORM\Exceptions\PhiberException; |
|
9
|
|
|
use Phiber\ORM\Interfaces\IPhiberQueryBuilder; |
|
10
|
|
|
use Phiber\Util\Internationalization; |
|
11
|
|
|
use ReflectionMethod; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Classe responsável por escrever os SQLs |
|
15
|
|
|
* |
|
16
|
|
|
* @package bin |
|
17
|
|
|
*/ |
|
18
|
|
|
class PhiberQueryWriter implements IPhiberQueryBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $sql; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* PhiberQueryWriter constructor. |
|
27
|
|
|
* @param $method |
|
28
|
|
|
* @param $infos |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($method, $infos) |
|
31
|
|
|
{ |
|
32
|
|
|
$reflectionMethod = new ReflectionMethod(get_class($this), $method); |
|
33
|
|
|
$reflectionMethod->invoke($this, $infos); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
function __toString() |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
return $this->sql; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Responsável por criar a quesry string. |
|
46
|
|
|
* |
|
47
|
|
|
* @param $infos |
|
48
|
|
|
* @return string |
|
49
|
|
|
* @throws PhiberException |
|
50
|
|
|
* @internal param $object |
|
51
|
|
|
*/ |
|
52
|
|
|
public function create($infos) |
|
53
|
|
|
{ |
|
54
|
|
|
$tabela = $infos['table']; |
|
55
|
|
|
$campos = $infos['fields']; |
|
56
|
|
|
$camposV = $infos['values']; |
|
57
|
|
|
try { |
|
58
|
|
|
$camposNome = []; |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
for ($i = 0; $i < count($campos); $i++) { |
|
|
|
|
|
|
61
|
|
|
if ($camposV[$i] != null) { |
|
62
|
|
|
$camposNome[$i] = $campos[$i]; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$camposNome = array_values($camposNome); |
|
67
|
|
|
$this->sql = "INSERT INTO $tabela (" . implode(", ", $camposNome) . ") VALUES ("; |
|
68
|
|
|
|
|
69
|
|
|
for ($j = 0; $j < count($camposNome); $j++) { |
|
|
|
|
|
|
70
|
|
|
if ($j != count($camposNome) - 1) { |
|
71
|
|
|
$this->sql .= ":" . $camposNome[$j] . ", "; |
|
72
|
|
|
} else if ($j == count($camposNome) - 1) { |
|
73
|
|
|
$this->sql .= ":" . $camposNome[$j] . ")"; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $this->sql; |
|
77
|
|
|
|
|
78
|
|
|
} catch (PhiberException $e) { |
|
79
|
|
|
throw new PhiberException(new Internationalization("query_processor_error")); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Faz a query de update de um registro no banco com os dados. |
|
85
|
|
|
* |
|
86
|
|
|
* @param $infos |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
* @throws PhiberException |
|
89
|
|
|
* @internal param $object |
|
90
|
|
|
* @internal param $id |
|
91
|
|
|
*/ |
|
92
|
|
|
public function update($infos) |
|
93
|
|
|
{ |
|
94
|
|
|
$tabela = $infos['table']; |
|
95
|
|
|
$campos = $infos['fields']; |
|
96
|
|
|
$camposV = $infos['values']; |
|
97
|
|
|
$whereCriteria = $infos['where']; |
|
98
|
|
|
|
|
99
|
|
|
try { |
|
100
|
|
|
$camposNome = []; |
|
101
|
|
View Code Duplication |
for ($i = 0; $i < count($campos); $i++) { |
|
|
|
|
|
|
102
|
|
|
if ($camposV[$i] != null) { |
|
103
|
|
|
$camposNome[$i] = $campos[$i]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$camposNome = array_values($camposNome); |
|
108
|
|
|
$this->sql = "UPDATE $tabela SET "; |
|
109
|
|
|
|
|
110
|
|
|
for ($i = 0; $i < count($camposNome); $i++) { |
|
|
|
|
|
|
111
|
|
|
if (!empty($camposNome[$i])) { |
|
112
|
|
|
if ($i != count($camposNome) - 1) { |
|
113
|
|
|
$this->sql .= $camposNome[$i] . " = :" . $camposNome[$i] . ", "; |
|
114
|
|
|
} else { |
|
115
|
|
|
$this->sql .= $camposNome[$i] . " = :" . $camposNome[$i]; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (!empty($whereCriteria)) { |
|
121
|
|
|
$this->sql .= " WHERE " . $whereCriteria . " "; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->sql . ";"; |
|
125
|
|
|
|
|
126
|
|
|
} catch (PhiberException $e) { |
|
127
|
|
|
throw new PhiberException(new Internationalization("query_processor_error")); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Faz a query de delete de um registro no banco com os dados. |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $infos |
|
135
|
|
|
* @return bool|string |
|
136
|
|
|
* @throws PhiberException |
|
137
|
|
|
* @internal param $object |
|
138
|
|
|
* @internal param array $conditions |
|
139
|
|
|
* @internal param array $conjunctions |
|
140
|
|
|
*/ |
|
141
|
|
|
public function delete(array $infos) |
|
142
|
|
|
{ |
|
143
|
|
|
$tabela = $infos['table']; |
|
144
|
|
|
$whereCriteria = $infos['where']; |
|
145
|
|
|
|
|
146
|
|
|
try { |
|
147
|
|
|
$this->sql = "DELETE FROM $tabela "; |
|
148
|
|
|
if (!empty($whereCriteria)) { |
|
149
|
|
|
$this->sql .= " WHERE " . $whereCriteria . " "; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this->sql . ";"; |
|
153
|
|
|
|
|
154
|
|
|
} catch (PhiberException $e) { |
|
|
|
|
|
|
155
|
|
|
throw new PhiberException(new Internationalization("query_processor_error")); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Faz a query de select de um registro no banco com os dados. |
|
161
|
|
|
* |
|
162
|
|
|
* @param array $infos |
|
163
|
|
|
* @return string |
|
164
|
|
|
* @throws PhiberException |
|
165
|
|
|
*/ |
|
166
|
|
|
public function select(array $infos) |
|
167
|
|
|
{ |
|
168
|
|
|
try { |
|
169
|
|
|
|
|
170
|
|
|
$tabela = $infos['table']; |
|
171
|
|
|
$campos = $infos['fields']; |
|
172
|
|
|
|
|
173
|
|
|
$whereCriteria = $infos['where']; |
|
174
|
|
|
$joins = $infos['join']; |
|
175
|
|
|
$limitCriteria = $infos['limit']; |
|
176
|
|
|
$offsetCriteria = $infos['offset']; |
|
177
|
|
|
$orderByCriteria = $infos['orderby']; |
|
178
|
|
|
|
|
179
|
|
|
$campos = gettype($campos) == "array" ? implode(", ", $campos) : $campos; |
|
180
|
|
|
|
|
181
|
|
|
$this->sql = "SELECT " . $campos . " FROM $tabela"; |
|
182
|
|
|
|
|
183
|
|
|
// responsável por montar JOIN |
|
184
|
|
|
if (!empty($joins)) { |
|
185
|
|
|
for ($i = 0; $i < count($joins); $i++) { |
|
|
|
|
|
|
186
|
|
|
$this->sql .= " " . $joins[$i] . " "; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
// responsável por montar o WHERE. |
|
191
|
|
|
if (!empty($whereCriteria)) { |
|
192
|
|
|
$this->sql .= " WHERE " . $whereCriteria; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// responsável por montar o order by. |
|
196
|
|
|
if (!is_null($orderByCriteria)) { |
|
197
|
|
|
$orderBy = gettype($orderByCriteria) == "array" ? implode(", ", $orderByCriteria) : $orderByCriteria; |
|
198
|
|
|
|
|
199
|
|
|
$this->sql .= " ORDER BY " . $orderBy; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// responsável por montar o limit. |
|
203
|
|
|
if (!empty($limitCriteria)) { |
|
204
|
|
|
$this->sql .= " LIMIT " . $limitCriteria; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
// responsável por montar OFFSET |
|
208
|
|
|
if (!empty($offsetCriteria)) { |
|
209
|
|
|
|
|
210
|
|
|
$this->sql .= " OFFSET " . $offsetCriteria; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$this->sql .= ";"; |
|
214
|
|
|
|
|
215
|
|
|
return $this->sql; |
|
216
|
|
|
|
|
217
|
|
|
} catch (PhiberException $phiberException) { |
|
218
|
|
|
throw new PhiberException(new Internationalization("query_processor_error")); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.