1
|
|
|
<?php
|
2
|
|
|
namespace core;
|
3
|
|
|
class Querybuilder {
|
4
|
|
|
protected $req_beginning;
|
5
|
|
|
protected $champs = [];
|
6
|
|
|
protected $value = [];
|
7
|
|
|
protected $conditions = [];
|
8
|
|
|
protected $table = [];
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
//-------------------------- QUERY BUILDER in construction no test have been done --------------------------------------------//
|
12
|
|
|
/**
|
13
|
|
|
* @param string $champs
|
14
|
|
|
* @return $this
|
15
|
|
|
*
|
16
|
|
|
* pour initialisé une requete avec un select
|
17
|
|
|
*/
|
18
|
|
|
public function select($champs = "*") {
|
19
|
|
|
$this->req_beginning = "SELECT ";
|
20
|
|
|
$this->champs[] = $champs;
|
21
|
|
|
|
22
|
|
|
return $this;
|
23
|
|
|
}
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* @param $champ
|
27
|
|
|
* @param $value
|
28
|
|
|
* @return $this
|
29
|
|
|
*
|
30
|
|
|
* fonction qui permet de préparer les champs et la valeur qui y sera associée
|
31
|
|
|
*/
|
32
|
|
|
public function insert($champ, $value) {
|
33
|
|
|
$this->add($champ, $value);
|
34
|
|
|
|
35
|
|
|
$this->req_beginning = "INSERT INTO ";
|
36
|
|
|
|
37
|
|
|
return $this;
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* @param $champ
|
42
|
|
|
* @param $value
|
43
|
|
|
* @return $this
|
44
|
|
|
*/
|
45
|
|
|
public function update($champ, $value) {
|
46
|
|
|
$this->add($champ, $value);
|
47
|
|
|
|
48
|
|
|
$this->req_beginning = "UPDATE ";
|
49
|
|
|
|
50
|
|
|
return $this;
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* @return $this
|
55
|
|
|
*
|
56
|
|
|
* fonction qui initialise un delete en base de donnée
|
57
|
|
|
*/
|
58
|
|
|
public function delete() {
|
59
|
|
|
$this->req_beginning = "DELETE FROM ";
|
60
|
|
|
|
61
|
|
|
return $this;
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
/**
|
65
|
|
|
* @param $champ
|
66
|
|
|
* @param $value
|
67
|
|
|
*
|
68
|
|
|
* fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs
|
69
|
|
|
* tableaux respectifs (appellée dans this->insert et this->update
|
70
|
|
|
*/
|
71
|
|
|
private function add($champ, $value) {
|
72
|
|
View Code Duplication |
if (($champ !== null) && ($value !== null)) {
|
|
|
|
|
73
|
|
|
$this->champs[] = $champ;
|
74
|
|
|
$this->value[] = $value;
|
75
|
|
|
}
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
/**
|
79
|
|
|
* @param $table
|
80
|
|
|
* @return $this
|
81
|
|
|
*
|
82
|
|
|
* pour initialiser la les listes des tables ou il faudra aler chercher les données
|
83
|
|
|
*/
|
84
|
|
|
public function from($table) {
|
85
|
|
|
$this->table[] = $table;
|
86
|
|
|
|
87
|
|
|
return $this;
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* @param $table
|
92
|
|
|
*
|
93
|
|
|
* pour initialiser la table dans laquelle on va insérer les données
|
94
|
|
|
*/
|
95
|
|
|
public function into($table) {
|
96
|
|
|
$this->table[] = $table;
|
97
|
|
|
|
98
|
|
|
return $this;
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* @param $champ
|
103
|
|
|
* @param $cond
|
104
|
|
|
* @param $champ_test
|
105
|
|
|
* @param null $closure
|
106
|
|
|
* @return $this
|
107
|
|
|
*
|
108
|
|
|
* pour intialiser la ou les clauses where d'une requete
|
109
|
|
|
*/
|
110
|
|
View Code Duplication |
public function where($champ, $cond, $champ_test, $closure = null) {
|
|
|
|
|
111
|
|
|
if ( $closure === null) {
|
112
|
|
|
$this->conditions[] = $champ.$cond.$champ_test;
|
113
|
|
|
}
|
114
|
|
|
else {
|
115
|
|
|
$this->conditions[] = $champ.$cond.$champ_test." ".$closure;
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
return $this;
|
119
|
|
|
}
|
120
|
|
|
|
121
|
|
|
/**
|
122
|
|
|
* @return array
|
123
|
|
|
*
|
124
|
|
|
* fonction qui permet de récupérer un select fait sur une table
|
125
|
|
|
*/
|
126
|
|
View Code Duplication |
public function get() {
|
|
|
|
|
127
|
|
|
$requete = $this->req_beginning . implode(",", $this->champs) . " FROM " . implode(",", $this->table);
|
128
|
|
|
|
129
|
|
|
if (!empty($this->conditions)) {
|
130
|
|
|
$requete .= " WHERE ". implode(" ", $this->conditions);
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
$this->unsetQueryBuilder();
|
134
|
|
|
return $this->query($requete);
|
|
|
|
|
135
|
|
|
}
|
136
|
|
|
|
137
|
|
|
/**
|
138
|
|
|
* fonction utlisée pour terminer un insert ou un update dans la base de données
|
139
|
|
|
*/
|
140
|
|
View Code Duplication |
public function set() {
|
|
|
|
|
141
|
|
|
$values = array_combine($this->champs, $this->value);
|
142
|
|
|
|
143
|
|
|
$datas = [];
|
144
|
|
|
$count = count($this->champs);
|
145
|
|
|
for ($i=0 ; $i<$count ; $i++) {
|
146
|
|
|
$datas[] = $this->champs[$i]."=:".$this->champs[$i];
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
//si on a des conditions alors on sera dans un insert
|
150
|
|
|
$requete = $this->req_beginning . implode(",", $this->table) . " SET " . implode(", ", $datas);
|
151
|
|
|
|
152
|
|
|
if (!empty($this->conditions)) {
|
153
|
|
|
$requete .= " WHERE " . implode(" ", $this->conditions);
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
$this->prepare($requete, $values);
|
|
|
|
|
157
|
|
|
$this->unsetQueryBuilder();
|
158
|
|
|
}
|
159
|
|
|
|
160
|
|
|
/**
|
161
|
|
|
* fonction utilisée pour finir un delete
|
162
|
|
|
*/
|
163
|
|
View Code Duplication |
public function del() {
|
|
|
|
|
164
|
|
|
$requete = $this->req_beginning . implode(",", $this->table);
|
165
|
|
|
|
166
|
|
|
if (!empty($this->conditions)) {
|
167
|
|
|
$requete .= " WHERE " . implode(" ", $this->conditions);
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
$this->query($requete);
|
|
|
|
|
171
|
|
|
$this->unsetQueryBuilder();
|
172
|
|
|
}
|
173
|
|
|
|
174
|
|
|
/**
|
175
|
|
|
* fonction qui détruit toutes les variables utilisées.
|
176
|
|
|
*/
|
177
|
|
View Code Duplication |
private function unsetQueryBuilder() {
|
|
|
|
|
178
|
|
|
$this->req_beginning;
|
179
|
|
|
$this->champs = [];
|
180
|
|
|
$this->value = [];
|
181
|
|
|
$this->conditions = [];
|
182
|
|
|
$this->table = [];
|
183
|
|
|
}
|
184
|
|
|
} |
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.