stuppid_bot

PHP DB Wrapper

Jun 26th, 2013
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.85 KB | None | 0 0
  1. <?php
  2.  
  3. class DB {
  4.     protected $_db, $_table_prefix;
  5.  
  6.     public function __construct($config) {
  7.         extract($config);
  8.         @$this->_db = new mysqli($server, $username, $password, $database);
  9.         if ($this->_db->connect_error) {
  10.             die("Cant't connect to db.");
  11.         }
  12.         $this->_table_prefix = $table_prefix;
  13.         $this->_db->set_charset($charset);
  14.     }
  15.  
  16.     public function rawQuery($sql) {
  17.         // echo $sql;
  18.         if (!(@$result = $this->_db->query($sql))) {
  19.             die('Bad query.');
  20.         }
  21.         return $result;
  22.     }
  23.  
  24.     public function quote($str) {
  25.         return "'" . $this->_db->real_escape_string($str) . "'";
  26.     }
  27.  
  28.     public function sanitize($data) {
  29.         $ret = array();
  30.         foreach ($data as $k => $v) {
  31.             switch (gettype($v)) {
  32.                 case 'boolean':
  33.                     $v = $v ? 'TRUE' : 'FALSE';
  34.                     break;
  35.                 case 'string':
  36.                     $v = $this->quote($v);
  37.                     break;
  38.                 case 'NULL':
  39.                     $v = 'NULL';
  40.             }
  41.             $ret[$k] = $v;
  42.         }
  43.         return $ret;
  44.     }
  45.  
  46.     public function a($data) {
  47.         $data = $this->sanitize($data);
  48.         $temp = array();
  49.         foreach ($data as $k => $v) {
  50.             $temp[] = "`$k`=$v";
  51.         }
  52.         return implode(',', $temp);
  53.     }
  54.  
  55.     public function v($data) {
  56.         return implode(',', $this->sanitize($data));
  57.     }
  58.  
  59.     protected function _query($params) {
  60.         $parts = preg_split('/\?([adfvj_]?)/', $params[0], -1, PREG_SPLIT_DELIM_CAPTURE);
  61.         $p = 1;
  62.         $count = count($parts);
  63.         for ($i = 1; $i < $count; $i += 2) {
  64.             $a = &$parts[$i];
  65.             if ($a == '_') {
  66.                 $a = $this->_table_prefix;
  67.             }
  68.             else {
  69.                 $b = $params[$p++];
  70.                 switch ($a) {
  71.                     case 'a':
  72.                         $a = $this->a($b);
  73.                         break;
  74.                     case 'd':
  75.                         $a = (int) $b;
  76.                         break;
  77.                     case 'f':
  78.                         $a = (float) $b;
  79.                         break;
  80.                     case 'v':
  81.                         $a = $this->v($b);
  82.                         break;
  83.                     case 'j':
  84.                         $a = implode(',', $b);
  85.                         break;
  86.                     default:
  87.                         $a = $this->quote($b);
  88.                 }
  89.             }
  90.         }
  91.         return $this->rawQuery(implode('', $parts));
  92.     }
  93.  
  94.     /**
  95.      * $db->query($sql, $arg1, $arg2, ...);
  96.      * плейсхолдеры:
  97.      * ? - экранированная строка в кавычках
  98.      * ?d - целое число
  99.      * ?f - плавающее число
  100.      * ?v - значения через запятую(фильтруются)
  101.      * ?a - col1 = val1, col2 = val2, ...(фильтруются)
  102.      * ?j - значения через запятую
  103.      * ?_ - преффикс для таблиц
  104.      *
  105.      * @return mysqli_result
  106.      */
  107.     public function query() {
  108.         return $this->_query(func_get_args());
  109.     }
  110.  
  111.     public function single() {
  112.         $result = $this->_query(func_get_args());
  113.         $row = $result->fetch_row();
  114.         return $row[0];
  115.     }
  116.  
  117.     public function get() {
  118.         $result = $this->_query(func_get_args());
  119.         return $result->fetch_assoc();
  120.     }
  121.  
  122.     public function getAll() {
  123.         $result = $this->_query(func_get_args());
  124.         return $result->fetch_all(MYSQLI_ASSOC);
  125.     }
  126.  
  127.     /**
  128.      * Возвращает ассоциативный массив, где первое поле является ключом, а второе значением
  129.      *
  130.      * @return array
  131.      */
  132.     public function assoc() {
  133.         $result = $this->_query(func_get_args());
  134.         $ret = array();
  135.         $data = $result->fetch_all(MYSQLI_NUM);
  136.         for ($i = 0, $n = count($data); $i < $n; ++$i) {
  137.             $ret[$data[$i][0]] = $data[$i][1];
  138.         }
  139.         return $ret;
  140.     }
  141.  
  142.     public function insert($table, $data) {
  143.         $this->rawQuery('INSERT INTO `' . $this->_table_prefix . $table . '` (`' . implode('`,`', array_keys($data)) . '`) VALUES(' . $this->v($data) . ');');
  144.         return $this->_db->insert_id;
  145.     }
  146.  
  147.     public function update($table, $id, $data) {
  148.         $this->rawQuery('UPDATE `' . $this->_table_prefix . $table . '` SET ' . $this->a($data) . ' WHERE `id`=' . intval($id) . ';');
  149.         return $this->_db->affected_rows;
  150.     }
  151.  
  152.     public function delete($table, $id) {
  153.         $this->rawQuery('DELETE FROM `' . $this->_table_prefix . $table . '` WHERE `id`=' . intval($id) . ';');
  154.         return $this->_db->affected_rows;
  155.     }
  156. }
  157.  
  158. ?>
Advertisement
Add Comment
Please, Sign In to add comment