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