Passed
Push — master ( 508d6c...b217d5 )
by Anthony
02:52
created

Querybuilder::groupBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 $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) {
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 = $this->getConditions();
262
			$this->getConditionsTable();
263
			
264
			return [" WHERE ".implode(" ", $this->datas), $values];
265
		}
266
		
267
		/**
268
		 * @return array
269
		 */
270
		private function getConditions() {
271
			if ((!empty($this->conditions))) {
272
				$values = array_combine(str_replace(".", "", $this->champs_where), $this->value_where);
273
				
274
				$count = count($this->champs_where);
275
				
276
				for ($i = 0; $i < $count; $i++) {
277
					$this->datas[] = $this->champs_where[$i]." ".$this->conditions[$i]." :".str_replace(".", "", $this->champs_where[$i])." ".$this->closure[$i]." ";
278
				}
279
				
280
				return $values;
281
			}
282
		}
283
		
284
		/**
285
		 *
286
		 */
287
		private function getConditionsTable() {
288
			if ((!empty($this->conditions_table))) {
289
				foreach ($this->conditions_table as $cond) {
290
					$this->datas[] = $cond;
291
				}
292
			}
293
		}
294
		
295
		/**
296
		 * function that set values for insert update and delete
297
		 */
298
		private function setValues() {
299
			$this->values = array_merge($this->values, $this->getWhereConditions()[1]);
300
		}
301
		
302
		/**
303
		 * fonction qui détruit toutes les variables utilisées.
304
		 */
305
		private function unsetQueryBuilder() {
306
			$this->req_beginning;
307
			$this->select_champ = [];
308
			$this->champs = [];
309
			$this->value = [];
310
			$this->datas = [];
311
			$this->champs_where = [];
312
			$this->value_where = [];
313
			$this->conditions = [];
314
			$this->conditions_table = [];
315
			$this->closure = [];
316
			$this->table = [];
317
			$this->order_by = "";
318
			$this->group_by = "";
319
			$this->limit = "";
320
		}
321
	}