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 $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 = "*") { |
||
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) { |
||
50 | |||
51 | /** |
||
52 | * @param $champ |
||
53 | * @param $value |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function update($champ, $value) { |
||
63 | |||
64 | /** |
||
65 | * @return $this |
||
66 | * |
||
67 | * fonction qui initialise un delete en base de donnée |
||
68 | */ |
||
69 | public function delete() { |
||
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) { |
||
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) { |
||
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) { |
||
120 | |||
121 | /** |
||
122 | * @param string $order |
||
123 | * @param string $type |
||
124 | */ |
||
125 | public function orderBy($order, $type = null) { |
||
126 | if ($type === null) { |
||
127 | $type = "ASC"; |
||
128 | } |
||
129 | |||
130 | $this->order_by = " ORDER BY ".$order." ".$type." "; |
||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param integer $debut |
||
137 | * @param integer $fin |
||
138 | */ |
||
139 | public function limit($debut, $fin = "no") { |
||
140 | if ($fin == "no") { |
||
141 | $this->limit = " LIMIT ".$debut." "; |
||
142 | } |
||
143 | else { |
||
144 | $this->limit = " LIMIT ".$debut.", ".$fin." "; |
||
145 | } |
||
146 | |||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | public function groupBy($name) { |
||
152 | $this->group_by = " GROUP BY ".$name." "; |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return array |
||
159 | * |
||
160 | * fonction qui permet de récupérer un select fait sur une table |
||
161 | */ |
||
162 | public function get() { |
||
163 | $values = []; |
||
164 | $requete = $this->req_beginning.implode(",", $this->select_champ)." FROM ".implode(",", $this->table); |
||
165 | if ((!empty($this->conditions)) || (!empty($this->conditions_table))) { |
||
166 | $requete .= $this->getWhereConditions()[0]; |
||
167 | $values = $this->getWhereConditions()[1]; |
||
168 | } |
||
169 | |||
170 | $requete .= $this->group_by; |
||
171 | |||
172 | $requete .= $this->order_by; |
||
173 | |||
174 | $requete .= $this->limit; |
||
175 | |||
176 | $this->unsetQueryBuilder(); |
||
177 | return $this->prepare($requete, $values); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * fonction utlisée pour terminer un insert ou un update dans la base de données |
||
182 | */ |
||
183 | public function set() { |
||
184 | $this->values = array_combine($this->champs, $this->value); |
||
185 | $datas = []; |
||
186 | $count = count($this->champs); |
||
187 | for ($i = 0; $i < $count; $i++) { |
||
188 | $datas[] = $this->champs[$i]."=:".$this->champs[$i]; |
||
189 | } |
||
190 | |||
191 | //si on a des conditions alors on sera dans un insert |
||
192 | $requete = $this->req_beginning.implode(",", $this->table)." SET ".implode(", ", $datas); |
||
193 | |||
194 | if ((!empty($this->conditions)) || (!empty($this->conditions_table))) { |
||
195 | $requete .= $this->getWhereConditions()[0]; |
||
196 | $this->setValues(); |
||
197 | } |
||
198 | |||
199 | $requete .= $this->limit; |
||
200 | |||
201 | $this->prepare($requete, $this->values); |
||
202 | $this->unsetQueryBuilder(); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * fonction utilisée pour finir un delete |
||
207 | */ |
||
208 | public function del() { |
||
209 | $requete = $this->req_beginning.implode(",", $this->table); |
||
210 | |||
211 | if (!empty($this->conditions)) { |
||
212 | $requete .= $this->getWhereConditions()[0]; |
||
213 | $this->setValues(); |
||
214 | } |
||
215 | |||
216 | $requete .= $this->order_by; |
||
217 | |||
218 | $requete .= $this->limit; |
||
219 | |||
220 | $this->prepare($requete, $this->values); |
||
221 | $this->unsetQueryBuilder(); |
||
222 | } |
||
223 | |||
224 | |||
225 | |||
226 | //-------------------------- PRIVATE FUNCTIONS --------------------------------------------// |
||
227 | /** |
||
228 | * @param $champ |
||
229 | * @param $value |
||
230 | * |
||
231 | * fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs |
||
232 | * tableaux respectifs (appellée dans this->insert et this->update |
||
233 | */ |
||
234 | private function add($champ, $value) { |
||
240 | |||
241 | /** |
||
242 | * @param $champ |
||
243 | * @param $value |
||
244 | * |
||
245 | * fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs |
||
246 | * tableaux respectifs (appellée dans this->insert et this->update |
||
247 | */ |
||
248 | private function addWhere($champ, $value) { |
||
254 | |||
255 | /** |
||
256 | * @return array |
||
257 | * crée les tableau et renvoi la clause where |
||
258 | */ |
||
259 | private function getWhereConditions() { |
||
281 | |||
282 | /** |
||
283 | * function that set values for insert update and delete |
||
284 | */ |
||
285 | private function setValues() { |
||
286 | $this->values = array_merge($this->values, $this->getWhereConditions()[1]); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * fonction qui détruit toutes les variables utilisées. |
||
291 | */ |
||
292 | private function unsetQueryBuilder() { |
||
307 | } |