-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySQL.php
239 lines (204 loc) · 8.95 KB
/
MySQL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* Copyright 2023-2024 Christophe LEMOINE
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the “Software”),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
declare(strict_types=1);
namespace components\extended {
use components\core\Route;
/**
* Gestionnaire de données via Php PDO
*
* Requiert:
* Configuration: MYSQL_HOST (impératif)
* Configuration: MYSQL_PORT (impératif)
* Configuration: MYSQL_DATABASE (impératif)
* Configuration: MYSQL_USERNAME (impératif)
* Configuration: MYSQL_PASSWORD (impératif)
*/
class MySQL {
/** Se produit lorsque le composant est chargé
* @return void
*/
public static function __required(): void {
if (!defined('MYSQL_HOST'))
throw new \Exception("MYSQL_HOST parameter not defined in config file.");
if (!defined('MYSQL_PORT'))
throw new \Exception("MYSQL_PORT parameter not defined in config file.");
if (!defined('MYSQL_USERNAME'))
throw new \Exception("MYSQL_USERNAME parameter not defined in config file.");
if (!defined('MYSQL_PASSWORD'))
throw new \Exception("MYSQL_PASSWORD parameter not defined in config file.");
if (!defined('MYSQL_DATABASE'))
throw new \Exception("MYSQL_DATABASE parameter not defined in config file.");
Route::extendWith(MySQL::class);
}
protected ?\PDO $instance = null;
/** Charge une nouvelle connexion à la base de données
* @return void
*/
private function connect(): void {
try {
$cs = sprintf('mysql:host=%s;port=%d;charset=utf8', MYSQL_HOST, MYSQL_PORT);
$opts = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::MYSQL_ATTR_FOUND_ROWS => true,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_STRINGIFY_FETCHES => false,
];
$pdo = new \PDO($cs, MYSQL_USERNAME, MYSQL_PASSWORD, $opts);
$stmt = $pdo->query(sprintf("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '%s';", MYSQL_DATABASE));
$exists = (bool) $stmt->fetchColumn();
if (!$exists) {
$pdo->exec(sprintf("CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;", MYSQL_DATABASE));
$pdo->exec("DEFAULT CHARACTER SET utf8mb4;");
$pdo->exec("DEFAULT COLLATE utf8_general_ci;");
$pdo->exec("SET default_storage_engine = InnoDB;");
}
$pdo->exec(sprintf("USE %s;", MYSQL_DATABASE));
$this->instance = $pdo;
} catch (Exception $ex) {
$this->instance = null;
throw new \Exception("Unable to load new PDO instance.");
}
}
public function __construct() {
$this->connect();
}
public function __destruct() {
$this->instance = null;
}
/** Retourne une instance de l'objet PDO
* @return \PDO
*/
public function getInstance(): \PDO {
return $this->instance;
}
/** Charge un fichier SQL
* @param string $sqlFile Fichier SQL à charger
* @return void
*/
public function loadSqlFile(string $sqlFile): void {
if (file_exists($sqlFile)) {
$sql = file_get_contents($sqlFile);
$this->instance->exec($sql);
}
}
/** Assainie une valeur
* @param mixed $value Valeur à assainir
* @return string
*/
public function quote(mixed $value): string {
return $this->instance->quote(strval($value));
}
/** Execute une requète
* @param string $sql Requète SQL à éxecuter. (eg: INSERT INTO table (id) VALUES (0) )
* @param array|null $args Tableau d'arguments de la requète
* @return int Nombre d'enregistrements concernées par les changements
*/
public function execute(string $sql, ?array $args = null, &$id = false): int {
try {
$stmt = $this->instance->prepare($sql);
$stmt->execute($args);
$id = $this->instance->lastInsertId();
return $stmt->rowCount();
} catch (\Exception $ex) {
return 0;
}
}
/** Retourne le nombre d'enregistrements trouvés
* @param string $sql Requète SQL à éxecuter. (eg: SELECT COUNT(*) AS count FROM ...)
* @param array|null $args Tableau d'arguments de la requète
* @return int Quantité retournée
*/
public function count(string $sql, ?array $args = null): int {
try {
$stmt = $this->instance->prepare($sql);
$stmt->execute($args);
return $stmt->fetchColumn();
} catch (\Exception $ex) {
return 0;
}
}
/** Retourne l'unique enregistrement trouvé
* @param string $sql Requète SQL à éxecuter. (eg: SELECT * FROM ...)
* @param array|null $args Tableau d'arguments de la requète
* @return array|bool false, en cas d'erreur ou si aucun ou plusieurs enregistrements ont été trouvés, sinon, retourne le premier enregistrement trouvé
*/
public function unique(string $sql, ?array $args = null): array|bool {
try {
$stmt = $this->instance->prepare($sql);
$stmt->execute($args);
$result = $stmt->fetchAll() ?? [];
if (count($result) !== 1)
return false;
return $result[0];
} catch (\Exception $ex) {
return false;
}
}
/** Retourne le premier enregistrement trouvé
* @param string $sql Requète SQL à éxecuter. (eg: SELECT * FROM ...)
* @param array|null $args Tableau d'arguments de la requète
* @return array|bool false, en cas d'erreur ou si aucun enregistrement n'a été trouvé, sinon, retourne le premier enregistrement trouvé
*/
public function first(string $sql, ?array $args = null): array|bool {
try {
$stmt = $this->instance->prepare($sql);
$stmt->execute($args);
$result = $stmt->fetchAll() ?? [];
if (count($result) === 0)
return false;
return $result[0];
} catch (\Exception $ex) {
return false;
}
}
/** Retourne tous les enregistrements trouvés
* @param string $sql Requète SQL à éxecuter. (eg: SELECT * FROM ...)
* @param array|null $args Tableau d'arguments de la requète
* @return array Tous les enregistrements trouvés
*/
public function all(string $sql, ?array $args = null): array {
try {
$stmt = $this->instance->prepare($sql);
$stmt->execute($args);
return $stmt->fetchAll() ?? [];
} catch (\Exception $ex) {
return [];
}
}
/** Retourne le nombre d'enregistrements supprimés
* @param string $sql Requète SQL à éxecuter. (eg: SELECT * FROM ...)
* @param array|null $args Tableau d'arguments de la requète
* @return int Nombre d'enregistrements supprimés
*/
public function delete(string $sql, ?array $args = null): int {
try {
$stmt = $this->instance->prepare($sql);
$stmt->execute($args);
return $stmt->rowCount();
} catch (\Exception $ex) {
return 0;
}
}
}
}