stuppid_bot

Untitled

May 29th, 2014
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.19 KB | None | 0 0
  1. <?php
  2.  
  3. class DBConnectionFailure extends Exception {}
  4.  
  5. class DB extends PDO {
  6.     function __construct() {
  7.         $dsn = 'mysql:dbname=' . DBConfig::DBNAME . ';host=' . DBConfig::HOST;
  8.         try {
  9.             parent::__construct($dsn, DBConfig::USER, DBConfig::PASSWORD);
  10.         }
  11.         catch (PDOException $e) {
  12.             throw new DBConnectionFailure();
  13.         }
  14.         $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.         $this->exec('SET NAMES ' . DBConfig::CHARSET);
  16.     }
  17.  
  18.     static function i() {
  19.         static $instance;
  20.         if (!$instance) {
  21.             $instance = new self;
  22.         }
  23.         return $instance;
  24.     }
  25.  
  26.     function quoteIndent($str) {
  27.         return '`' . str_replace('`', '``', $str) . '`';
  28.     }
  29.  
  30.     function getFullTableName($table) {
  31.         return $this->quoteIndent(DBConfig::TABLE_PREFIX . $table);
  32.     }
  33.  
  34.     function pQuery($sql, $params = null) {
  35.         // подставляем преффиксы таблицам
  36.         $sql = str_replace('?_', DBConfig::TABLE_PREFIX, $sql);
  37.         $stmt = $this->prepare($sql);
  38.         $stmt->execute($params);
  39.         return $stmt;
  40.     }
  41.  
  42.     function single() {
  43.         $args = func_get_args();
  44.         $stmt = call_user_func_array(array($this, 'pQuery'), $args);
  45.         return $stmt->fetchColumn();
  46.     }
  47.  
  48.     function row() {
  49.         $args = func_get_args();
  50.         $stmt = call_user_func_array(array($this, 'pQuery'), $args);
  51.         return $stmt->fetch();
  52.     }
  53.  
  54.     function rows() {
  55.         $args = func_get_args();
  56.         $stmt = call_user_func_array(array($this, 'pQuery'), $args);
  57.         return $stmt->fetchAll();
  58.     }
  59.  
  60.     function assoc() {
  61.         $args = func_get_args();
  62.         $stmt = call_user_func_array(array($this, 'pQuery'), $args);
  63.         $res = $stmt->fetchAll(PDO::FETCH_NUM);
  64.         if (!is_array($res)) {
  65.             return $res;
  66.         }
  67.         $ret = array();
  68.         $total = count($res);
  69.         for ($i = 0; $i < $total; ++$i) {
  70.             $ret[$res[$i][0]] = $res[$i][1];
  71.         }
  72.         return $ret;
  73.     }
  74.  
  75.     function insert($table, $data) {
  76.         $sql = 'INSERT INTO' . $this->getFullTableName($table);
  77.         $values = array_values($data);
  78.         if ($values != $data) {
  79.             $keys = array_keys($data);
  80.             $sql .= '(' . implode(',', array_map(array($this, 'quoteIndent'), $keys)) . ')';
  81.         }
  82.         $sql .= 'VALUES(' . implode(',', array_fill(0, count($data), '?')) . ')';
  83.         $this->pQuery($sql, $values);
  84.         return $this->lastInsertId();
  85.     }
  86.  
  87.     function update($table, $id, $data) {
  88.         $sql = 'UPDATE' . $this->getFullTableName($table) . ' SET ';
  89.         $temp = array();
  90.         foreach (array_keys($data) as $v) {
  91.             $temp[] = $this->quoteIndent($v) . '=?';
  92.         }
  93.         $sql .= implode(',', $temp) . 'WHERE id=' . intval($id);
  94.  
  95.         $stmt = $this->pQuery($sql, array_values($data));
  96.         return $stmt->rowCount();
  97.     }
  98.  
  99.     function delete($table, $id) {
  100.         $sql = 'DELETE FROM' . $this->getFullTableName($table) . ' WHERE id=' . intval($id);
  101.         $stmt = $this->pQuery($sql);
  102.         return $stmt->rowCount();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment