|
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 $conditions = []; |
|
9
|
|
|
protected $conditions_table = []; |
|
10
|
|
|
protected $closure = []; |
|
11
|
|
|
protected $table = []; |
|
12
|
|
|
protected $order_by; |
|
13
|
|
|
protected $limit; |
|
14
|
|
|
|
|
15
|
|
|
abstract public function query(); |
|
16
|
|
|
abstract public function prepare(); |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
//-------------------------- QUERY BUILDER in construction no test have been done --------------------------------------------// |
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $champs |
|
22
|
|
|
* @return $this |
|
23
|
|
|
* |
|
24
|
|
|
* pour initialisé une requete avec un select |
|
25
|
|
|
*/ |
|
26
|
|
|
public function select($champs = "*") { |
|
27
|
|
|
$this->req_beginning = "SELECT "; |
|
28
|
|
|
$this->select_champ[] = $champs; |
|
29
|
|
|
|
|
30
|
|
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param $champ |
|
35
|
|
|
* @param $value |
|
36
|
|
|
* @return $this |
|
37
|
|
|
* |
|
38
|
|
|
* fonction qui permet de préparer les champs et la valeur qui y sera associée |
|
39
|
|
|
*/ |
|
40
|
|
|
public function insert($champ, $value) { |
|
41
|
|
|
$this->add($champ, $value); |
|
42
|
|
|
|
|
43
|
|
|
$this->req_beginning = "INSERT INTO "; |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param $champ |
|
50
|
|
|
* @param $value |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
|
|
public function update($champ, $value) { |
|
54
|
|
|
$this->add($champ, $value); |
|
55
|
|
|
|
|
56
|
|
|
$this->req_beginning = "UPDATE "; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return $this |
|
63
|
|
|
* |
|
64
|
|
|
* fonction qui initialise un delete en base de donnée |
|
65
|
|
|
*/ |
|
66
|
|
|
public function delete() { |
|
67
|
|
|
$this->req_beginning = "DELETE FROM "; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $champ |
|
74
|
|
|
* @param $value |
|
75
|
|
|
* |
|
76
|
|
|
* fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs |
|
77
|
|
|
* tableaux respectifs (appellée dans this->insert et this->update |
|
78
|
|
|
*/ |
|
79
|
|
|
private function add($champ, $value) { |
|
80
|
|
|
if (($champ !== null) && ($value !== null)) { |
|
81
|
|
|
$this->champs[] = $champ; |
|
82
|
|
|
$this->value[] = $value; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $table |
|
88
|
|
|
* @return $this |
|
89
|
|
|
* |
|
90
|
|
|
* pour initialiser la les listes des tables ou il faudra aler chercher les données |
|
91
|
|
|
*/ |
|
92
|
|
|
public function from($table) { |
|
93
|
|
|
$this->table[] = $table; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $table |
|
100
|
|
|
* |
|
101
|
|
|
* pour initialiser la table dans laquelle on va insérer les données |
|
102
|
|
|
*/ |
|
103
|
|
|
public function into($table) { |
|
104
|
|
|
$this->table[] = $table; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $champ |
|
111
|
|
|
* @param $cond |
|
112
|
|
|
* @param $champ_test |
|
113
|
|
|
* @param string $closure |
|
114
|
|
|
* @param null $no_bind |
|
115
|
|
|
* @return $this |
|
116
|
|
|
* pour intialiser la ou les clauses where d'une requete |
|
117
|
|
|
*/ |
|
118
|
|
|
public function where($champ, $cond, $champ_test, $closure = "", $no_bind = null) { |
|
119
|
|
|
$this->closure[] = $closure; |
|
120
|
|
|
|
|
121
|
|
|
if ($no_bind !== null) { |
|
122
|
|
|
$this->conditions_table[] = $champ.$cond.$champ_test; |
|
123
|
|
|
} |
|
124
|
|
|
else { |
|
125
|
|
|
$this->conditions[] = $cond; |
|
126
|
|
|
$this->add($champ, $champ_test); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $order |
|
134
|
|
|
*/ |
|
135
|
|
|
public function orderBy($order) { |
|
136
|
|
|
$this->order_by = " ORDER BY ".$order." "; |
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function limit($debut, $fin) { |
|
142
|
|
|
$this->limit = " LIMIT ".$debut.", ".$fin." "; |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return array |
|
149
|
|
|
* |
|
150
|
|
|
* fonction qui permet de récupérer un select fait sur une table |
|
151
|
|
|
*/ |
|
152
|
|
|
public function get() { |
|
153
|
|
|
$values = []; |
|
154
|
|
|
$datas = []; |
|
155
|
|
|
$requete = $this->req_beginning . implode(",", $this->select_champ) . " FROM " . implode(",", $this->table); |
|
156
|
|
|
|
|
157
|
|
|
if ((!empty($this->conditions)) || (!empty($this->conditions_table))) { |
|
158
|
|
|
$values = array_combine(str_replace(".", "", $this->champs),$this->value); |
|
159
|
|
|
|
|
160
|
|
|
$count = count($this->champs); |
|
161
|
|
|
|
|
162
|
|
|
for ($i=0 ; $i<$count ; $i++) { |
|
163
|
|
|
$datas[] = $this->champs[$i]." ".$this->conditions[$i]." :".str_replace(".", "", $this->champs[$i])." ".$this->closure[$i]." "; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
foreach ($this->conditions_table as $cond) { |
|
167
|
|
|
$datas[] = $cond; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$requete .= " WHERE ". implode(" ", $datas); |
|
171
|
|
|
} |
|
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
|
|
|
$values = array_combine($this->champs, $this->value); |
|
186
|
|
|
|
|
187
|
|
|
$datas = []; |
|
188
|
|
|
$count = count($this->champs); |
|
189
|
|
|
for ($i=0 ; $i<$count ; $i++) { |
|
190
|
|
|
$datas[] = $this->champs[$i]."=:".$this->champs[$i]; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
//si on a des conditions alors on sera dans un insert |
|
194
|
|
|
$requete = $this->req_beginning . implode(",", $this->table) . " SET " . implode(", ", $datas); |
|
195
|
|
|
|
|
196
|
|
|
if (!empty($this->conditions)) { |
|
197
|
|
|
$requete .= " WHERE " . implode(" ", $this->conditions); |
|
198
|
|
|
} |
|
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
|
|
|
$requete = $this->req_beginning . implode(",", $this->table); |
|
209
|
|
|
|
|
210
|
|
|
if (!empty($this->conditions)) { |
|
211
|
|
|
$requete .= " WHERE " . implode(" ", $this->conditions); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$this->query($requete); |
|
|
|
|
|
|
215
|
|
|
$this->unsetQueryBuilder(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* fonction qui détruit toutes les variables utilisées. |
|
220
|
|
|
*/ |
|
221
|
|
|
private function unsetQueryBuilder() { |
|
222
|
|
|
$this->req_beginning; |
|
223
|
|
|
$this->select_champ = []; |
|
224
|
|
|
$this->champs = []; |
|
225
|
|
|
$this->value = []; |
|
226
|
|
|
$this->conditions = []; |
|
227
|
|
|
$this->conditions_table = []; |
|
228
|
|
|
$this->closure = []; |
|
229
|
|
|
$this->table = []; |
|
230
|
|
|
$this->order_by = ""; |
|
231
|
|
|
$this->limit = ""; |
|
232
|
|
|
} |
|
233
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.