diff options
| author | 2013-12-23 13:22:50 +0100 | |
|---|---|---|
| committer | 2013-12-23 13:22:50 +0100 | |
| commit | 24d9d1628d673f68fa0cc7b98a3ef9a8d021b070 (patch) | |
| tree | 2d794dc100d707e53b82a2de7d84b3d13103e461 /lib/Minz/ModelPdo.php | |
| parent | 75096e6a39fe5d34d3951991f296f616e62a9fd8 (diff) | |
| parent | 9e46c1ee7fc7f9ad9e2c07f0cf826573dd4c9766 (diff) | |
Fusion 0.7-dev
Diffstat (limited to 'lib/Minz/ModelPdo.php')
| -rw-r--r-- | lib/Minz/ModelPdo.php | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php new file mode 100644 index 000000000..9655539b2 --- /dev/null +++ b/lib/Minz/ModelPdo.php @@ -0,0 +1,111 @@ +<?php +/** + * MINZ - Copyright 2011 Marien Fressinaud + * Sous licence AGPL3 <http://www.gnu.org/licenses/> +*/ + +/** + * La classe Model_sql représente le modèle interragissant avec les bases de données + * Seul la connexion MySQL est prise en charge pour le moment + */ +class Minz_ModelPdo { + + /** + * Partage la connexion à la base de données entre toutes les instances. + */ + public static $useSharedBd = true; + private static $sharedBd = null; + private static $sharedPrefix; + + /** + * $bd variable représentant la base de données + */ + protected $bd; + + protected $prefix; + + /** + * Créé la connexion à la base de données à l'aide des variables + * HOST, BASE, USER et PASS définies dans le fichier de configuration + */ + public function __construct () { + if (self::$useSharedBd && self::$sharedBd != null) { + $this->bd = self::$sharedBd; + $this->prefix = self::$sharedPrefix; + return; + } + + $db = Minz_Configuration::dataBase (); + $driver_options = null; + + try { + $type = $db['type']; + if($type == 'mysql') { + $string = $type + . ':host=' . $db['host'] + . ';dbname=' . $db['base'] + . ';charset=utf8'; + $driver_options = array( + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' + ); + } elseif($type == 'sqlite') { + $string = $type . ':/' . DATA_PATH . $db['base'] . '.sqlite'; //TODO: DEBUG UTF-8 http://www.siteduzero.com/forum/sujet/sqlite-connexion-utf-8-18797 + } + + $this->bd = new FreshPDO ( + $string, + $db['user'], + $db['password'], + $driver_options + ); + self::$sharedBd = $this->bd; + + $userPrefix = Minz_Configuration::currentUser (); + $this->prefix = $db['prefix'] . (empty($userPrefix) ? '' : ($userPrefix . '_')); + self::$sharedPrefix = $this->prefix; + } catch (Exception $e) { + throw new Minz_PDOConnectionException ( + $string, + $db['user'], Minz_Exception::ERROR + ); + } + } + + public function beginTransaction() { + $this->bd->beginTransaction(); + } + public function commit() { + $this->bd->commit(); + } + public function rollBack() { + $this->bd->rollBack(); + } + + public function size() { + $db = Minz_Configuration::dataBase (); + $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = ?'; + $stm = $this->bd->prepare ($sql); + $values = array ($db['base']); + $stm->execute ($values); + $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0); + return $res[0]; + } +} + +class FreshPDO extends PDO { + private static function check($statement) { + if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) { + invalidateHttpCache(); + } + } + + public function prepare ($statement, $driver_options = array()) { + FreshPDO::check($statement); + return parent::prepare($statement, $driver_options); + } + + public function exec ($statement) { + FreshPDO::check($statement); + return parent::exec($statement); + } +} |
