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