Passed
Push — master ( 1d9b86...df2d47 )
by Anthony
02:56
created

Querybuilder::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
	namespace core\database;
3
	trait Querybuilder {
4
		protected $req_beginning;
5
		protected $champs = [];
6
		protected $value = [];
7
		protected $conditions = [];
8
		protected $table = [];
9
10
		abstract public function query();
11
		abstract public function prepare();
12
13
		
14
		//-------------------------- QUERY BUILDER in construction no test have been done --------------------------------------------//
15
		/**
16
		 * @param string $champs
17
		 * @return $this
18
		 *
19
		 * pour initialisé une requete avec un select
20
		 */
21
		public function select($champs = "*") {
22
			$this->req_beginning = "SELECT ";
23
			$this->champs[] = $champs;
24
25
			return $this;
26
		}
27
28
		/**
29
		 * @param $champ
30
		 * @param $value
31
		 * @return $this
32
		 *
33
		 * fonction qui permet de préparer les champs et la valeur qui y sera associée
34
		 */
35
		public function insert($champ, $value) {
36
			$this->add($champ, $value);
37
38
			$this->req_beginning = "INSERT INTO ";
39
40
			return $this;
41
		}
42
43
		/**
44
		 * @param $champ
45
		 * @param $value
46
		 * @return $this
47
		 */
48
		public function update($champ, $value) {
49
			$this->add($champ, $value);
50
51
			$this->req_beginning = "UPDATE ";
52
53
			return $this;
54
		}
55
56
		/**
57
		 * @return $this
58
		 *
59
		 * fonction qui initialise un delete en base de donnée
60
		 */
61
		public function delete() {
62
			$this->req_beginning = "DELETE FROM ";
63
64
			return $this;
65
		}
66
67
		/**
68
		 * @param $champ
69
		 * @param $value
70
		 *
71
		 * fonction qui se cahrge d'ajouter les valeurs et les champs si non null dans leurs
72
		 * tableaux respectifs (appellée dans this->insert et this->update
73
		 */
74
		private function add($champ, $value) {
75
			if (($champ !== null) && ($value !== null)) {
76
				$this->champs[] = $champ;
77
				$this->value[] = $value;
78
			}
79
		}
80
81
		/**
82
		 * @param $table
83
		 * @return $this
84
		 *
85
		 * pour initialiser la les listes des tables ou il faudra aler chercher les données
86
		 */
87
		public function from($table) {
88
			$this->table[] = $table;
89
90
			return $this;
91
		}
92
93
		/**
94
		 * @param $table
95
		 *
96
		 * pour initialiser la table dans laquelle on va insérer les données
97
		 */
98
		public function into($table) {
99
			$this->table[] = $table;
100
101
			return $this;
102
		}
103
104
		/**
105
		 * @param $champ
106
		 * @param $cond
107
		 * @param $champ_test
108
		 * @param null $closure
109
		 * @return $this
110
		 *
111
		 * pour intialiser la ou les clauses where d'une requete
112
		 */
113
		public function where($champ, $cond, $champ_test, $closure = null) {
114
			if ( $closure === null) {
115
				$this->conditions[] = $champ.$cond.$champ_test;
116
			}
117
			else {
118
				$this->conditions[] = $champ.$cond.$champ_test." ".$closure;
119
			}
120
121
			return $this;
122
		}
123
124
		/**
125
		 * @return array
126
		 *
127
		 * fonction qui permet de récupérer un select fait sur une table
128
		 */
129
		public function get() {
130
			$requete = $this->req_beginning . implode(",", $this->champs) . " FROM " . implode(",", $this->table);
131
132
			if (!empty($this->conditions)) {
133
				$requete .= " WHERE ". implode(" ", $this->conditions);
134
			}
135
136
			$this->unsetQueryBuilder();
137
			return $this->query($requete);
0 ignored issues
show
Unused Code introduced by
The call to Querybuilder::query() has too many arguments starting with $requete.

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 @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
138
		}
139
140
		/**
141
		 * fonction utlisée pour terminer un insert ou un update dans la base de données
142
		 */
143
		public function set() {
144
			$values = array_combine($this->champs, $this->value);
145
146
			$datas = [];
147
			$count = count($this->champs);
148
			for ($i=0 ; $i<$count ; $i++) {
149
				$datas[] = $this->champs[$i]."=:".$this->champs[$i];
150
			}
151
152
			//si on a des conditions alors on sera dans un insert
153
			$requete = $this->req_beginning . implode(",", $this->table) . " SET " . implode(", ", $datas);
154
155
			if (!empty($this->conditions)) {
156
				$requete .= " WHERE " . implode(" ", $this->conditions);
157
			}
158
159
			$this->prepare($requete, $values);
0 ignored issues
show
Unused Code introduced by
The call to Querybuilder::prepare() has too many arguments starting with $requete.

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 @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
160
			$this->unsetQueryBuilder();
161
		}
162
163
		/**
164
		 * fonction utilisée pour finir un delete
165
		 */
166
		public function del() {
167
			$requete = $this->req_beginning . implode(",", $this->table);
168
169
			if (!empty($this->conditions)) {
170
				$requete .= " WHERE " . implode(" ", $this->conditions);
171
			}
172
173
			$this->query($requete);
0 ignored issues
show
Unused Code introduced by
The call to Querybuilder::query() has too many arguments starting with $requete.

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 @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
174
			$this->unsetQueryBuilder();
175
		}
176
177
		/**
178
		 * fonction qui détruit toutes les variables utilisées.
179
		 */
180
		private function unsetQueryBuilder() {
181
			$this->req_beginning;
182
			$this->champs = [];
183
			$this->value = [];
184
			$this->conditions = [];
185
			$this->table = [];
186
		}
187
	}