1
|
|
|
<?php
|
2
|
|
|
namespace core;
|
3
|
|
|
|
4
|
|
|
use core\HTML\flashmessage\FlashMessage;
|
5
|
|
|
use PDO;
|
6
|
|
|
|
7
|
|
|
class Database {
|
8
|
|
|
private $db_type;
|
9
|
|
|
private $db_name;
|
10
|
|
|
private $db_user;
|
11
|
|
|
private $db_pass;
|
12
|
|
|
private $db_host;
|
13
|
|
|
private $dbc;
|
14
|
|
|
|
15
|
|
|
//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------//
|
16
|
|
|
public function __construct($db_type, $db_name, $db_user, $db_pass, $db_host) {
|
17
|
|
|
$this->db_type = $db_type;
|
18
|
|
|
$this->db_name = $db_name;
|
19
|
|
|
$this->db_user = $db_user;
|
20
|
|
|
$this->db_pass = $db_pass;
|
21
|
|
|
$this->db_host = $db_host;
|
22
|
|
|
}
|
23
|
|
|
//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------//
|
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------//
|
28
|
|
|
/**
|
29
|
|
|
* function qui fait la connexion a la bdd ne peu etre appelee que dans la classe
|
30
|
|
|
* @return PDO
|
31
|
|
|
*/
|
32
|
|
|
private function getPdo() {
|
33
|
|
|
if ($this->dbc === null) {
|
34
|
|
|
$dbc = new PDO($this->db_type.':host='.$this->db_host.';dbname='.$this->db_name, $this->db_user, $this->db_pass);
|
35
|
|
|
$dbc->exec("set names utf8");
|
36
|
|
|
$this->dbc = $dbc;
|
37
|
|
|
}
|
38
|
|
|
return $this->dbc;
|
39
|
|
|
}
|
40
|
|
|
//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
|
41
|
|
|
|
42
|
|
|
//-------------------------- FUNCTION QUI FONT DES REQUETES SUR LA BDD --------------------------------------------//
|
43
|
|
|
/**
|
44
|
|
|
* effectue une requete en selectr dans la BDD, si ok on renvoit les donnees sinon on renvoi une erreur
|
45
|
|
|
* @param $req
|
46
|
|
|
* @return array
|
47
|
|
|
*/
|
48
|
|
|
public function query($req) {
|
49
|
|
|
$query = $this->getPdo()->query($req);
|
50
|
|
|
|
51
|
|
|
if ($query) {
|
52
|
|
|
$obj = $query->fetchAll(PDO::FETCH_OBJ);
|
53
|
|
|
return $obj;
|
54
|
|
|
}
|
55
|
|
|
else {
|
56
|
|
|
FlashMessage::setFlash("Une erreur est survenue en executant cette requette : ".$req);
|
57
|
|
|
}
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
/**
|
61
|
|
|
* fonction qui prepare une requete et qui l'envoi, marche pour insert et update et delete
|
62
|
|
|
* @param $req -> la req a executer
|
63
|
|
|
* @param $value -> le ou les tableaux de valeurs
|
64
|
|
|
*/
|
65
|
|
|
public function prepare($req, $value) {
|
66
|
|
|
$query = $this->getPdo()->prepare($req);
|
67
|
|
|
//si on a plusieurs tableaux
|
68
|
|
|
if (array_key_exists(0, $value)) {
|
69
|
|
|
foreach ($value as $val) {
|
70
|
|
|
if (!$query->execute($val)) {
|
71
|
|
|
$err = true;
|
72
|
|
|
}
|
73
|
|
|
}
|
74
|
|
|
}
|
75
|
|
|
else {
|
76
|
|
|
if (!$query->execute($value)) {
|
77
|
|
|
$err = true;
|
78
|
|
|
}
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
//si on a une erreur on renvoi un message
|
82
|
|
|
if (isset($err)) {
|
83
|
|
|
FlashMessage::setFlash("Une erreur est survenue en executant cette requette : ".$req);
|
84
|
|
|
}
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* pour savoir si une valeur sur un champ précis existe deja en bdd, renvoi true si vrai
|
89
|
|
|
* @param $table
|
90
|
|
|
* @param $champ
|
91
|
|
|
* @param $value
|
92
|
|
|
* @return boolean|null
|
93
|
|
|
*/
|
94
|
|
|
public function rechercherEgalite($table, $champ, $value, $id_table = null, $id = null) {
|
95
|
|
|
if ($id == null) {
|
96
|
|
|
$query = $this->getPdo()->query("SELECT COUNT($champ) as nb FROM $table WHERE $champ LIKE '$value'");
|
97
|
|
|
}
|
98
|
|
|
else {
|
99
|
|
|
$query = $this->getPdo()->query("SELECT COUNT($champ) as nb FROM $table WHERE $champ LIKE '$value' AND $id_table != $id");
|
100
|
|
|
}
|
101
|
|
|
|
102
|
|
|
if (count($query) > 0) {
|
103
|
|
|
foreach ($query as $obj) {
|
104
|
|
|
$nb = $obj["nb"];
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
if ((isset($nb)) && ($nb != 0)) return true;
|
108
|
|
|
}
|
109
|
|
|
else {
|
110
|
|
|
return false;
|
111
|
|
|
}
|
112
|
|
|
}
|
113
|
|
|
//-------------------------- FIN FUNCTION QUI FONT DES REQUETES SUR LA BDD --------------------------------------------//
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/**
|
117
|
|
|
* tester si une table dans la base donnee existe
|
118
|
|
|
* @param string $table definit la table pour laquelle on doit tester l'existance
|
119
|
|
|
* @return boolean
|
120
|
|
|
*/
|
121
|
|
|
public function TestTableExist($table) {
|
122
|
|
|
$query = $this->getPdo()->query("SHOW TABLES LIKE '$table'");
|
123
|
|
|
|
124
|
|
|
if ($query->rowCount() > 0) {
|
125
|
|
|
return true;
|
126
|
|
|
}
|
127
|
|
|
else {
|
128
|
|
|
return false;
|
129
|
|
|
}
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
public function quote($quote) {
|
133
|
|
|
return $this->getPdo()->quote($quote);
|
134
|
|
|
}
|
135
|
|
|
|
136
|
|
|
public function lastInsertId() {
|
137
|
|
|
return $this->getPdo()->lastInsertId();
|
138
|
|
|
}
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
//-------------------------- QUERY BUILDER in construction no test have been done --------------------------------------------//
|
143
|
|
|
private $fiels = [];
|
144
|
|
|
private $conditions = [];
|
145
|
|
|
private $from = [];
|
146
|
|
|
|
147
|
|
|
public function select() {
|
148
|
|
|
$this->fiels = func_get_args();
|
149
|
|
|
|
150
|
|
|
return $this;
|
151
|
|
|
}
|
152
|
|
|
|
153
|
|
|
public function where() {
|
154
|
|
|
foreach (func_get_args() as $arg) {
|
155
|
|
|
$this->conditions[] = $arg;
|
156
|
|
|
}
|
157
|
|
|
|
158
|
|
|
return $this;
|
159
|
|
|
}
|
160
|
|
|
|
161
|
|
|
public function from($table, $alias) {
|
162
|
|
|
if (is_null($alias)) {
|
163
|
|
|
$this->from[] = $table;
|
164
|
|
|
}
|
165
|
|
|
else {
|
166
|
|
|
$this->from[] = "$table as $alias";
|
167
|
|
|
}
|
168
|
|
|
|
169
|
|
|
return $this;
|
170
|
|
|
}
|
171
|
|
|
|
172
|
|
|
public function getQuery() {
|
173
|
|
|
return "SELECT ". implode(", ", $this->fiels) . " FROM " . implode(",", $this->from) . " WHERE " . implode(" AND ", $this->conditions);
|
174
|
|
|
}
|
175
|
|
|
} |