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