Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('DEBUG', true);
- class Database{
- /**
- * Connection Informations.
- * @var string
- */
- private $host, $user, $pass, $db;
- /**
- * The PDO layer.
- * @var PDO
- */
- public $pdo;
- /**
- * If the class is initiated, pdo included.
- * @var bool
- */
- private $_init = false;
- /**
- * Le numero de requêtes que cette classe a executer.
- * @var int
- */
- private $queries = 0;
- public function __construct($host, $user, $db, $pass = '')
- {
- $this->host = $host;
- $this->user = $user;
- $this->db = $db;
- $this->pass = $pass;
- $this->construct_pdo();
- }
- /**
- * Construit PDO.
- * @return void
- */
- private function construct_pdo(){
- try{
- $this->pdo = new PDO("mysql:host=".$this->host.";dbname=".$this->db, $this->user, $this->pass);
- $this->_init = true;
- }
- catch(PDOException $e){
- exit("Erreur SQL: Impossible de se connecter au serveur.");
- $this->_init = false;
- }
- }
- /**
- * Execute une requête SELECT.
- * @see Database::genSelect
- * @return array
- */
- public function doSelect(array $fields, $from, array $conds = null){
- $stmt = $this->pdo->prepare($this->genSelect($fields, $from, $conds));
- if(!$stmt){
- if(DEBUG)
- exit('Erreur SQL !');
- return false;
- }
- if(!$stmt->execute()){
- if(DEBUG)
- exit('Erreur SQL !');
- return false;
- }
- ++$this->queries;
- if($stmt->rowCount() == 1)
- {
- return $stmt->fetch(PDO::FETCH_ASSOC);
- }
- if($stmt->rowCount() > 1)
- {
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- if($stmt->rowCount() < 1)
- {
- return array();
- }
- }
- /**
- * Génère une requête SELECT.
- * @param array $fields Les noms des colonnes à récuperer.
- * @param string $from Le nom de la table.
- * @param array $conds [optional] Les conditions
- * @return string La requête généré.
- */
- private function genSelect(array $fields, $from, array $conds = null){
- // Initiation de la requête
- $query = 'SELECT ';
- // Premièrement parsing des fields
- $f = '';
- foreach($fields as $key => $field)
- {
- $field = $from.'.'.$field;
- if($key != (count($fields)-1))
- $field .= ',';
- $field .= ' ';
- // On ajoute
- $f .= $field;
- }
- // On ajoute les fields à la requête
- $query .= $f;
- // puis la clause from
- $query .= 'FROM '.$from;
- if(isset($conds))
- {
- $query .= ' WHERE ';
- $c = '';
- foreach($conds as $field => $val)
- {
- $cond = $from.".".$field." = '".$val."'";
- if(key($conds) != (count($conds)-1))
- $cond .= ' AND ';
- $c .= $cond;
- }
- $query .= $c;
- }
- return $query;
- }
- /**
- * Execute une requête INSERT.
- * @see Database::genInsert
- * @param array $values Les valeurs à être inséré.
- * @return bool Résultat de l'opération.
- */
- public function doInsert(array $fields, $to, array $values){
- // Si le nombre de valeurs fournies sont pas conformes :/
- if(count($fields) !== count($values))
- throw new Exception("Le nombre de valeurs n'est pas conforme aux nombres de colonnes.");
- // On prépare la requête
- $query = $this->genInsert($fields, $to);
- $stmt = $this->pdo->prepare($query);
- if(!$stmt){
- if(DEBUG)
- exit('Erreur SQL !');
- return false;
- }
- $this->bindValues($stmt, $fields, $values);
- $this->pdo->beginTransaction();
- if(!$stmt->execute())
- {
- $this->pdo->rollBack();
- throw new Exception("Impossible d'éxecuter la requête: ".$query);
- }
- ++$this->queries;
- return $this->pdo->commit();
- }
- /**
- * Génère une requête insert.
- * @param array $fields Un tableau contenant les colonnes et leurs valeurs.
- * @param string $to La table concerné.
- * @return string
- */
- private function genInsert(array $fields, $to){
- // Init the query
- $query = 'INSERT INTO ';
- // Now parse the fields and the params
- $f = '';
- $v = '';
- foreach($fields as $k => $name)
- {
- $field = $name;
- if($k != (count($fields)-1))
- $field .= ', ';
- $f .= $field;
- // Now values
- $value = ":".$name;
- if($k != (count($fields)-1))
- $value .= ', ';
- $v .= $value;
- }
- $query .= $to.'('.$f.') ';
- $query .= 'VALUES('.$v.')';
- return $query;
- }
- /**
- * Ajoute les valeurs au statement.
- * @see Database::doInsert
- * @see Database::doUpdate
- * @return void
- */
- private function bindValues(PDOStatement &$stmt, $fields, $vals){
- foreach($fields as $k => $name)
- {
- $type = is_int($vals[$k]) ? PDO::PARAM_INT : PDO::PARAM_STR;
- $stmt->bindValue($name, $vals[$k], $type);
- }
- return ;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement