|
1
|
|
|
<?php |
|
2
|
|
|
namespace core\database; |
|
3
|
|
|
trait Querybuilder { |
|
4
|
|
|
protected $req_beginning; |
|
5
|
|
|
protected $select_champ = []; |
|
6
|
|
|
protected $champs = []; |
|
7
|
|
|
protected $value = []; |
|
8
|
|
|
protected $champs_where = []; |
|
9
|
|
|
protected $value_where = []; |
|
10
|
|
|
protected $conditions = []; |
|
11
|
|
|
protected $conditions_table = []; |
|
12
|
|
|
protected $closure = []; |
|
13
|
|
|
protected $table = []; |
|
14
|
|
|
protected $values = []; |
|
15
|
|
|
protected $order_by; |
|
16
|
|
|
protected $group_by; |
|
17
|
|
|
protected $limit; |
|
18
|
|
|
|
|
19
|
|
|
abstract public function prepare($req, $value); |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
//-------------------------- QUERY BUILDER --------------------------------------------// |
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $champs |
|
25
|
|
|
* @return $this |
|
26
|
|
|
* |
|
27
|
|
|
* pour initialisé une requete avec un select |
|
28
|
|
|
*/ |
|
29
|
|
|
public function select($champs = "*") { |
|
30
|
|
|
$this->req_beginning = "SELECT "; |
|
31
|
|
|
$this->select_champ[] = $champs; |
|
32
|
|
|
|
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $champ |
|
38
|
|
|
* @param $value |
|
39
|
|
|
* @return $this |
|
40
|
|
|
* |
|
41
|
|
|
* fonction qui permet de préparer les champs et la valeur qui y sera associée |
|
42
|
|
|
*/ |
|
43
|
|
|
public function insert($champ, $value) { |
|
44
|
|
|
$this->add($champ, $value); |
|
45
|
|
|
|
|
46
|
|
|
$this->req_beginning = "INSERT INTO "; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $champ |
|
53
|
|
|
* @param $value |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function update($champ, $value) { |
|
57
|
|
|
$this->add($champ, $value); |
|
58
|
|
|
|
|
59
|
|
|
$this->req_beginning = "UPDATE "; |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return $this |
|
66
|
|
|
* |
|
67
|
|
|
* fonction qui initialise un delete en base de donnée |
|
68
|
|
|
*/ |
|
69
|
|
|
public function delete() { |
|
70
|
|
|
$this->req_beginning = "DELETE FROM "; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $table |
|
77
|
|
|
* @return $this |
|
78
|
|
|
* |
|
79
|
|
|
* pour initialiser la les listes des tables ou il faudra aler chercher les données |
|
80
|
|
|
*/ |
|
81
|
|
|
public function from($table) { |
|
82
|
|
|
$this->table[] = $table; |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $table |
|
89
|
|
|
* |
|
90
|
|
|
* pour initialiser la table dans laquelle on va insérer les données |
|
91
|
|
|
*/ |
|
92
|
|
|
public function into($table) { |
|
93
|
|
|
$this->table[] = $table; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $champ |
|
100
|
|
|
* @param string $cond |
|
101
|
|
|
* @param $champ_test |
|
102
|
|
|
* @param string $closure |
|
103
|
|
|
* @param bool $no_bind |
|
104
|
|
|
* @return $this |
|
105
|
|
|
* pour intialiser la ou les clauses where d'une requete |
|
106
|
|
|
*/ |
|
107
|
|
|
public function where($champ, $cond, $champ_test, $closure = "", $no_bind = false) { |
|
108
|
|
|
$this->closure[] = $closure; |
|
109
|
|
|
|
|
110
|
|
|
if ($no_bind === true) { |
|
111
|
|
|
$this->conditions_table[] = $champ.$cond.$champ_test." ".$closure; |
|
112
|
|
|
} |
|
113
|
|
|
else { |
|
114
|
|
|
$this->conditions[] = $cond; |
|
115
|
|
|
$this->addWhere($champ, $champ_test); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $order |
|
123
|
|
|
* @param string $type |
|
124
|
|
|
*/ |
|
125
|
|
|
public function orderBy($order, $type = null) { |
|
126
|
|
|
if ($type === null) $type = "ASC"; |
|
127
|
|
|
|
|
128
|
|
|
$this->order_by = " ORDER BY ".$order." ".$type." "; |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param integer $debut |
|
135
|
|
|
* @param integer $fin |
|
136
|
|
|
*/ |
|
137
|
|
|
public function limit($debut, $fin = "no") { |
|
138
|
|
|
if ($fin == "no") { |
|
139
|
|
|
$this->limit = " LIMIT ".$debut." "; |
|
140
|
|
|
} |
|
141
|
|
|
else { |
|
142
|
|
|
$this->limit = " LIMIT ".$debut.", ".$fin." "; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function groupBy($name) { |
|
150
|
|
|
$this->group_by = " GROUP BY ".$name." "; |
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return array |
|
157
|
|
|
* |
|
158
|
|
|
* fonction qui permet de récupérer un select fait sur une table |
|
159
|
|
|
*/ |
|
160
|
|
|
public function get() { |
|
161
|
|
|
$values = []; |
|
162
|
|
|
$requete = $this->req_beginning.implode(",", $this->select_champ)." FROM ".implode(",", $this->table); |
|
163
|
|
|
if ((!empty($this->conditions)) || (!empty($this->conditions_table))) { |
|
164
|
|
|
$requete .= $this->getWhereConditions()[0]; |
|
165
|
|
|
$values = $this->getWhereConditions()[1]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$requete .= $this->group_by; |
|
169
|
|
|
|
|
170
|
|
|
$requete .= $this->order_by; |
|
171
|
|
|
|
|
172
|
|
|
$requete .= $this->limit; |
|
173
|
|
|
|
|
174
|
|
|
$this->unsetQueryBuilder(); |
|
175
|
|
|
return $this->prepare($requete, $values); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* fonction utlisée pour terminer un insert ou un update dans la base de données |
|
180
|
|
|
*/ |
|
181
|
|
|
public function set() { |
|
182
|
|
|
$this->values = array_combine($this->champs, $this->value); |
|
183
|
|
|
$datas = []; |
|
184
|
|
|
$count = count($this->champs); |
|
185
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
186
|
|
|
$datas[] = $this->champs[$i]."=:".$this->champs[$i]; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
//si on a des conditions alors on sera dans un insert |
|
190
|
|
|
$requete = $this->req_beginning.implode(",", $this->table)." SET ".implode(", ", $datas); |
|
191
|
|
|
|
|
192
|
|
|
if ((!empty($this->conditions)) || (!empty($this->conditions_table))) { |
|
193
|
|
|
$requete .= $this->getWhereConditions()[0]; |
|
194
|
|
|
$this->setValues(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$requete .= $this->limit; |
|
198
|
|
|
|
|
199
|
|
|
$this->prepare($requete, $this->values); |
|
200
|
|
|
$this->unsetQueryBuilder(); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* fonction utilisée pour finir un delete |
|
205
|
|
|
*/ |
|
206
|
|
|
public function del() { |
|
207
|
|
|
$requete = $this->req_beginning.implode(",", $this->table); |
|
208
|
|
|
|
|
209
|
|
|
if (!empty($this->conditions)) { |
|
210
|
|
|
$requete .= $this->getWhereConditions()[0]; |
|
211
|
|
|
$this->setValues(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$requete .= $this->order_by; |
|
215
|
|
|
|
|
216
|
|
|
$requete .= $this->limit; |
|
217
|
|
|
|
|
218
|
|
|
$this->prepare($requete, $this->values); |
|
219
|
|
|
$this->unsetQueryBuilder(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
//-------------------------- PRIVATE FUNCTIONS --------------------------------------------// |
|
225
|
|
|
/** |
|
226
|
|
|
* @param $champ |
|
227
|
|
|
* @param $value |
|
228
|
|
|
* |
|
229
|
|
|
* fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs |
|
230
|
|
|
* tableaux respectifs (appellée dans this->insert et this->update |
|
231
|
|
|
*/ |
|
232
|
|
|
private function add($champ, $value) { |
|
233
|
|
|
if (($champ !== null) && ($value !== null)) { |
|
234
|
|
|
$this->champs[] = $champ; |
|
235
|
|
|
$this->value[] = $value; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param $champ |
|
241
|
|
|
* @param $value |
|
242
|
|
|
* |
|
243
|
|
|
* fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs |
|
244
|
|
|
* tableaux respectifs (appellée dans this->insert et this->update |
|
245
|
|
|
*/ |
|
246
|
|
|
private function addWhere($champ, $value) { |
|
247
|
|
|
if (($champ !== null) && ($value !== null)) { |
|
248
|
|
|
$this->champs_where[] = $champ; |
|
249
|
|
|
$this->value_where[] = $value; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return array |
|
255
|
|
|
* crée les tableau et renvoi la clause where |
|
256
|
|
|
*/ |
|
257
|
|
|
private function getWhereConditions() { |
|
258
|
|
|
$values = []; |
|
259
|
|
|
$datas = []; |
|
260
|
|
|
|
|
261
|
|
|
if ((!empty($this->conditions))) { |
|
262
|
|
|
$values = array_combine(str_replace(".", "", $this->champs_where), $this->value_where); |
|
263
|
|
|
|
|
264
|
|
|
$count = count($this->champs_where); |
|
265
|
|
|
|
|
266
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
267
|
|
|
$datas[] = $this->champs_where[$i]." ".$this->conditions[$i]." :".str_replace(".", "", $this->champs_where[$i])." ".$this->closure[$i]." "; |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
if ((!empty($this->conditions_table))) { |
|
272
|
|
|
foreach ($this->conditions_table as $cond) { |
|
273
|
|
|
$datas[] = $cond; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
return [" WHERE ".implode(" ", $datas), $values]; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* function that set values for insert update and delete |
|
282
|
|
|
*/ |
|
283
|
|
|
private function setValues() { |
|
284
|
|
|
$this->values = array_merge($this->values, $this->getWhereConditions()[1]); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* fonction qui détruit toutes les variables utilisées. |
|
289
|
|
|
*/ |
|
290
|
|
|
private function unsetQueryBuilder() { |
|
291
|
|
|
$this->req_beginning; |
|
292
|
|
|
$this->select_champ = []; |
|
293
|
|
|
$this->champs = []; |
|
294
|
|
|
$this->value = []; |
|
295
|
|
|
$this->champs_where = []; |
|
296
|
|
|
$this->value_where = []; |
|
297
|
|
|
$this->conditions = []; |
|
298
|
|
|
$this->conditions_table = []; |
|
299
|
|
|
$this->closure = []; |
|
300
|
|
|
$this->table = []; |
|
301
|
|
|
$this->order_by = ""; |
|
302
|
|
|
$this->group_by = ""; |
|
303
|
|
|
$this->limit = ""; |
|
304
|
|
|
} |
|
305
|
|
|
} |