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 $order_by; |
15
|
|
|
protected $group_by; |
16
|
|
|
protected $limit; |
17
|
|
|
|
18
|
|
|
abstract public function query(); |
19
|
|
|
abstract public function prepare(); |
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
|
|
|
$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
|
|
|
|
195
|
|
|
$values = array_merge($values, $this->getWhereConditions()[1]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$requete .= $this->limit; |
199
|
|
|
|
200
|
|
|
$this->prepare($requete, $values); |
|
|
|
|
201
|
|
|
$this->unsetQueryBuilder(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* fonction utilisée pour finir un delete |
206
|
|
|
*/ |
207
|
|
|
public function del() { |
208
|
|
|
$values = []; |
209
|
|
|
$requete = $this->req_beginning.implode(",", $this->table); |
210
|
|
|
|
211
|
|
|
if (!empty($this->conditions)) { |
|
|
|
|
212
|
|
|
$requete .= $this->getWhereConditions()[0]; |
213
|
|
|
|
214
|
|
|
$values = array_merge($values, $this->getWhereConditions()[1]); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$requete .= $this->order_by; |
218
|
|
|
|
219
|
|
|
$requete .= $this->limit; |
220
|
|
|
|
221
|
|
|
$this->prepare($requete, $values); |
|
|
|
|
222
|
|
|
$this->unsetQueryBuilder(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
|
227
|
|
|
//-------------------------- PRIVATE FUNCTIONS --------------------------------------------// |
228
|
|
|
/** |
229
|
|
|
* @param $champ |
230
|
|
|
* @param $value |
231
|
|
|
* |
232
|
|
|
* fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs |
233
|
|
|
* tableaux respectifs (appellée dans this->insert et this->update |
234
|
|
|
*/ |
235
|
|
|
private function add($champ, $value) { |
236
|
|
|
if (($champ !== null) && ($value !== null)) { |
237
|
|
|
$this->champs[] = $champ; |
238
|
|
|
$this->value[] = $value; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param $champ |
244
|
|
|
* @param $value |
245
|
|
|
* |
246
|
|
|
* fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs |
247
|
|
|
* tableaux respectifs (appellée dans this->insert et this->update |
248
|
|
|
*/ |
249
|
|
|
private function addWhere($champ, $value) { |
250
|
|
|
if (($champ !== null) && ($value !== null)) { |
251
|
|
|
$this->champs_where[] = $champ; |
252
|
|
|
$this->value_where[] = $value; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return array |
258
|
|
|
* crée les tableau et renvoi la clause where |
259
|
|
|
*/ |
260
|
|
|
private function getWhereConditions() { |
261
|
|
|
$values = []; |
262
|
|
|
$datas = []; |
263
|
|
|
|
264
|
|
|
if ((!empty($this->conditions))) { |
265
|
|
|
$values = array_combine(str_replace(".", "", $this->champs_where), $this->value_where); |
266
|
|
|
|
267
|
|
|
$count = count($this->champs_where); |
268
|
|
|
|
269
|
|
|
for ($i = 0; $i < $count; $i++) { |
270
|
|
|
$datas[] = $this->champs_where[$i]." ".$this->conditions[$i]." :".str_replace(".", "", $this->champs_where[$i])." ".$this->closure[$i]." "; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if ((!empty($this->conditions_table))) { |
275
|
|
|
foreach ($this->conditions_table as $cond) { |
276
|
|
|
$datas[] = $cond; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return [" WHERE ".implode(" ", $datas), $values]; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* fonction qui détruit toutes les variables utilisées. |
285
|
|
|
*/ |
286
|
|
|
private function unsetQueryBuilder() { |
287
|
|
|
$this->req_beginning; |
288
|
|
|
$this->select_champ = []; |
289
|
|
|
$this->champs = []; |
290
|
|
|
$this->value = []; |
291
|
|
|
$this->champs_where = []; |
292
|
|
|
$this->value_where = []; |
293
|
|
|
$this->conditions = []; |
294
|
|
|
$this->conditions_table = []; |
295
|
|
|
$this->closure = []; |
296
|
|
|
$this->table = []; |
297
|
|
|
$this->order_by = ""; |
298
|
|
|
$this->group_by = ""; |
299
|
|
|
$this->limit = ""; |
300
|
|
|
} |
301
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.