1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Repositories\Database; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Cria comandos SQL |
7
|
|
|
*/ |
8
|
|
|
class Sql |
9
|
|
|
{ |
10
|
|
|
/** @var string[] */ |
11
|
|
|
public $columns = ['*']; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $table; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
private $values = []; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
private $join = []; |
21
|
|
|
|
22
|
|
|
/** @var array */ |
23
|
|
|
private $where = []; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
private $whereValues = []; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
private $orderBy = []; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $limit = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Prepara a query |
36
|
|
|
* @param string $table |
37
|
|
|
* @param mixed $values |
38
|
|
|
* @param string[] $columns |
39
|
|
|
*/ |
40
|
|
|
public function __construct($table, $values = []) |
41
|
|
|
{ |
42
|
|
|
$this->table = $table; |
43
|
|
|
$this->values = $values; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @param mixed[] */ |
47
|
|
|
public function setValues($values) |
48
|
|
|
{ |
49
|
|
|
return $this->values = $values; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @return mixed[] */ |
53
|
|
|
public function values() |
54
|
|
|
{ |
55
|
|
|
return $this->values + $this->whereValues; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* SELECT * FROM ... |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function select() |
63
|
|
|
{ |
64
|
|
|
return 'SELECT ' . implode(', ', $this->columns) |
65
|
|
|
. ' FROM ' . $this->table |
66
|
|
|
. $this->join() |
67
|
|
|
. $this->where() |
68
|
|
|
. $this->orderBy() |
69
|
|
|
. $this->limit(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* SELECT COUNT(*) FROM |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function selectCount() |
77
|
|
|
{ |
78
|
|
|
return 'SELECT COUNT(*) FROM ' . $this->table |
79
|
|
|
. $this->join() |
80
|
|
|
. $this->where(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* INSERT INTO ... VALUES |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function insert() |
88
|
|
|
{ |
89
|
|
|
$params = str_split(str_repeat('?', count($this->values))); |
90
|
|
|
return 'INSERT INTO ' . $this->table |
91
|
|
|
. ' (' . implode(',', array_keys($this->values)) . ')' |
92
|
|
|
. ' VALUES (' . implode(', ', $params) . ')'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* UPDATE ... SET |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function update() |
100
|
|
|
{ |
101
|
|
|
$columns = array_map(function ($column) { |
102
|
|
|
return $column . ' = ?'; |
103
|
|
|
}, array_keys($this->values)); |
104
|
|
|
|
105
|
|
|
return 'UPDATE ' . $this->table |
106
|
|
|
. ' SET ' . implode(', ', $columns) |
107
|
|
|
. $this->where() |
108
|
|
|
. $this->limit(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* DELETE FROM ... |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function delete() |
116
|
|
|
{ |
117
|
|
|
return 'DELETE FROM ' . $this->table |
118
|
|
|
. $this->where() |
119
|
|
|
. $this->limit(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* WHERE ... |
124
|
|
|
* @param string $comparator |
125
|
|
|
* @param mixed $values |
126
|
|
|
*/ |
127
|
|
|
public function addWhere($comparator, $values) |
128
|
|
|
{ |
129
|
|
|
$hasBindParams = preg_match('/[:\?]/', $comparator); |
130
|
|
|
if ($values && !$hasBindParams) { |
131
|
|
|
$comparator .= ' = ?'; |
132
|
|
|
} |
133
|
|
|
$this->where[] = '(' . $comparator . ')'; |
134
|
|
|
$this->whereValues = array_merge($this->whereValues, $values); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* JOIN, LEFT JOIN ... |
139
|
|
|
* @param string $join |
140
|
|
|
*/ |
141
|
|
|
public function addJoin($join) |
142
|
|
|
{ |
143
|
|
|
$this->join[] = $join; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Define o limit |
148
|
|
|
* @param int $offset |
149
|
|
|
* @param int $limit |
150
|
|
|
*/ |
151
|
|
|
public function setLimit($offset, $limit) |
152
|
|
|
{ |
153
|
|
|
$this->limit = $offset . ',' . $limit; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function join() |
157
|
|
|
{ |
158
|
|
|
if ($this->join) { |
|
|
|
|
159
|
|
|
return ' ' . implode(' ', $this->join); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return ''; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Retorna o SQL |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
private function where() |
170
|
|
|
{ |
171
|
|
|
if ($this->where) { |
|
|
|
|
172
|
|
|
return ' WHERE ' . implode(' AND ', $this->where); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return ''; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Define a ordenação principal |
181
|
|
|
* @param string $orderBy |
182
|
|
|
*/ |
183
|
|
|
public function setOrderBy($orderBy) |
184
|
|
|
{ |
185
|
|
|
$this->orderBy = [$orderBy]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Adiciona uma ordenação |
190
|
|
|
* @param string $orderBy |
191
|
|
|
* @param int $priority |
192
|
|
|
*/ |
193
|
|
|
public function addOrderBy($orderBy, $priority = 0) |
194
|
|
|
{ |
195
|
|
|
$this->orderBy[$priority] = $orderBy; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* LIMIT ... |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
private function limit() |
203
|
|
|
{ |
204
|
|
|
if ($this->limit) { |
205
|
|
|
return ' LIMIT ' . $this->limit; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return ''; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* ORDER BY... |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
private function orderBy() |
217
|
|
|
{ |
218
|
|
|
if ($this->orderBy) { |
|
|
|
|
219
|
|
|
ksort($this->orderBy); |
220
|
|
|
return ' ORDER BY ' . implode(', ', $this->orderBy); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return ''; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.