| Line 15... |
Line 15... |
| 15 |
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
|
15 |
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
|
| 16 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
16 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
| 17 |
* @copyright Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
|
17 |
* @copyright Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
|
| 18 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
18 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
| 19 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
19 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
| 20 |
* @version $Id: Bdd.php 500 2020-04-23 16:05:33Z killian $
|
20 |
* @version $Id: Bdd.php 501 2020-04-23 16:15:01Z killian $
|
| 21 |
* @link /doc/framework/
|
21 |
* @link /doc/framework/
|
| 22 |
*/
|
22 |
*/
|
| 23 |
class Bdd {
|
23 |
class Bdd {
|
| 24 |
/** Constante stockant le squelette du message en cas d'erreur de requête sql. */
|
24 |
/** Constante stockant le squelette du message en cas d'erreur de requête sql. */
|
| 25 |
const ERREUR_REQUETE_TPL = 'Requête echec.\nFichier : %s.\nLigne : %s.\nMessage : %s.\nRequête : %s';
|
25 |
const ERREUR_REQUETE_TPL = 'Requête echec.\nFichier : %s.\nLigne : %s.\nMessage : %s.\nRequête : %s';
|
| Line 77... |
Line 77... |
| 77 |
|
77 |
|
| 78 |
/** Encodage de la base de données */
|
78 |
/** Encodage de la base de données */
|
| Line 79... |
Line 79... |
| 79 |
protected $encodage = null;
|
79 |
protected $encodage = null;
|
| 80 |
|
80 |
|
| Line 81... |
Line 81... |
| 81 |
/** Connexion à la base de données */
|
81 |
/** Connexion à la base de données */
|
| 82 |
public $connexion = null;
|
82 |
protected $connexion = null;
|
| 83 |
|
83 |
|
| Line 124... |
Line 124... |
| 124 |
$this->ASSOC = 'SQLITE3_ASSOC';
|
124 |
$this->ASSOC = 'SQLITE3_ASSOC';
|
| 125 |
$this->OBJECT = 'SQLITE3_OBJECT';
|
125 |
$this->OBJECT = 'SQLITE3_OBJECT';
|
| 126 |
break;
|
126 |
break;
|
| 127 |
default:
|
127 |
default:
|
| 128 |
$m = "Erreur : l'abstraction '{$this->abstraction}' n'est pas prise en charge";
|
128 |
$m = "Erreur : l'abstraction '{$this->abstraction}' n'est pas prise en charge";
|
| 129 |
trigger_error($m, E_USER_WARNING);
|
129 |
throw new Exception($m);
|
| 130 |
}
|
130 |
}
|
| 131 |
}
|
131 |
}
|
| Line 132... |
Line 132... |
| 132 |
|
132 |
|
| 133 |
/**
|
133 |
/**
|
| Line 144... |
Line 144... |
| 144 |
try {
|
144 |
try {
|
| 145 |
$this->connexion = new PDO($this->dsn, $this->utilisateur, $this->pass);
|
145 |
$this->connexion = new PDO($this->dsn, $this->utilisateur, $this->pass);
|
| 146 |
$this->connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
146 |
$this->connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
| 147 |
} catch (PDOException $e) {
|
147 |
} catch (PDOException $e) {
|
| 148 |
$e = sprintf(self::ERREUR_CONNEXION_TPL, $e->getMessage());
|
148 |
$e = sprintf(self::ERREUR_CONNEXION_TPL, $e->getMessage());
|
| 149 |
trigger_error($e, E_USER_WARNING);
|
149 |
throw new Exception($e);
|
| 150 |
}
|
150 |
}
|
| 151 |
if ($this->encodage != null && $this->type == 'mysql') {
|
151 |
if ($this->encodage != null && $this->type == 'mysql') {
|
| 152 |
$this->connexion->query("SET names '".$this->encodage."'");
|
152 |
$this->connexion->query("SET names '".$this->encodage."'");
|
| 153 |
} else if ($this->type == 'sqlite') {
|
153 |
} else if ($this->type == 'sqlite') {
|
| 154 |
$this->connexion->query("PRAGMA case_sensitive_like = false");
|
154 |
$this->connexion->query("PRAGMA case_sensitive_like = false");
|
| Line 220... |
Line 220... |
| 220 |
case self::ABSTRACTION_PDO :
|
220 |
case self::ABSTRACTION_PDO :
|
| 221 |
try {
|
221 |
try {
|
| 222 |
$retour = $this->connexion->query($requete);
|
222 |
$retour = $this->connexion->query($requete);
|
| 223 |
} catch (PDOException $e) {
|
223 |
} catch (PDOException $e) {
|
| 224 |
$m = sprintf(self::ERREUR_REQUETE_TPL, $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
224 |
$m = sprintf(self::ERREUR_REQUETE_TPL, $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
| 225 |
trigger_error($m, E_USER_WARNING);
|
225 |
throw new Exception($m);
|
| 226 |
}
|
226 |
}
|
| 227 |
break;
|
227 |
break;
|
| 228 |
case self::ABSTRACTION_MYSQL :
|
228 |
case self::ABSTRACTION_MYSQL :
|
| 229 |
$retour = mysql_query($requete, $this->connexion);
|
229 |
$retour = mysql_query($requete, $this->connexion);
|
| 230 |
break;
|
230 |
break;
|
| Line 252... |
Line 252... |
| 252 |
case self::ABSTRACTION_PDO :
|
252 |
case self::ABSTRACTION_PDO :
|
| 253 |
try {
|
253 |
try {
|
| 254 |
$retour = $this->connexion->exec($requete);
|
254 |
$retour = $this->connexion->exec($requete);
|
| 255 |
} catch (PDOException $e) {
|
255 |
} catch (PDOException $e) {
|
| 256 |
$m = sprintf(self::ERREUR_REQUETE_TPL, $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
256 |
$m = sprintf(self::ERREUR_REQUETE_TPL, $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
| 257 |
trigger_error($m, E_USER_WARNING);
|
257 |
throw new Exception($m);
|
| 258 |
}
|
258 |
}
|
| 259 |
break;
|
259 |
break;
|
| 260 |
default:
|
260 |
default:
|
| 261 |
$m = "Cette méthode n'est pas disponible pour l'abstraction de base de données actuellement utilisée.";
|
261 |
$m = "Cette méthode n'est pas disponible pour l'abstraction de base de données actuellement utilisée.";
|
| 262 |
trigger_error($m, E_USER_ERROR);
|
262 |
trigger_error($m, E_USER_ERROR);
|
| Line 288... |
Line 288... |
| 288 |
try {
|
288 |
try {
|
| 289 |
$resultat = $this->connexion->query($requete);
|
289 |
$resultat = $this->connexion->query($requete);
|
| 290 |
$retour = ($resultat !== false) ? $resultat->fetch($this->$mode) : $resultat;
|
290 |
$retour = ($resultat !== false) ? $resultat->fetch($this->$mode) : $resultat;
|
| 291 |
} catch (PDOException $e) {
|
291 |
} catch (PDOException $e) {
|
| 292 |
$m = sprintf(self::ERREUR_REQUETE_TPL, $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
292 |
$m = sprintf(self::ERREUR_REQUETE_TPL, $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
| 293 |
trigger_error($m, E_USER_WARNING);
|
293 |
throw new Exception($m);
|
| 294 |
}
|
294 |
}
|
| 295 |
break;
|
295 |
break;
|
| 296 |
case self::ABSTRACTION_MYSQL :
|
296 |
case self::ABSTRACTION_MYSQL :
|
| 297 |
$res = mysql_query($requete, $this->connexion);
|
297 |
$res = mysql_query($requete, $this->connexion);
|
| 298 |
$fonction_fetch = $this->$mode;
|
298 |
$fonction_fetch = $this->$mode;
|
| Line 336... |
Line 336... |
| 336 |
try {
|
336 |
try {
|
| 337 |
$resultat = $this->connexion->query($requete);
|
337 |
$resultat = $this->connexion->query($requete);
|
| 338 |
$retour = ($resultat !== false) ? $resultat->fetchAll($this->$mode) : $resultat;
|
338 |
$retour = ($resultat !== false) ? $resultat->fetchAll($this->$mode) : $resultat;
|
| 339 |
} catch (PDOException $e) {
|
339 |
} catch (PDOException $e) {
|
| 340 |
$m = sprintf(self::ERREUR_REQUETE_TPL, $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
340 |
$m = sprintf(self::ERREUR_REQUETE_TPL, $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
| 341 |
trigger_error($m, E_USER_WARNING);
|
341 |
throw new Exception($m);
|
| 342 |
}
|
342 |
}
|
| 343 |
break;
|
343 |
break;
|
| 344 |
case self::ABSTRACTION_MYSQL :
|
344 |
case self::ABSTRACTION_MYSQL :
|
| 345 |
$resultat = mysql_query($requete, $this->connexion);
|
345 |
$resultat = mysql_query($requete, $this->connexion);
|
| 346 |
$fonction_fetch = $this->$mode;
|
346 |
$fonction_fetch = $this->$mode;
|