Advertisement
stuppid_bot

Untitled

Jul 19th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.14 KB | None | 0 0
  1. <?php
  2.  
  3. class DataBase {
  4.     public $pdo;
  5.     protected $prepared_statements = [];
  6.  
  7.     public function __construct($dsn, $usr=null, $pwd=null, $args=null) {
  8.         try {
  9.             $pdo = new PDO($dsn, $usr, $pwd, $args);
  10.         }
  11.         catch (PDOException $e) {
  12.             throw new RuntimeException('Connection failed: ' . $e->getMessage());
  13.         }
  14.         $this->pdo = $pdo;
  15.     }  
  16.  
  17.     public function quote_name($name) {
  18.         return '`' . str_replace('`', '``', $name) .  '`';
  19.     }
  20.  
  21.     public function execute($query, array $bindings=[]) {
  22.         $t = $this;
  23.         $h = md5($query);
  24.         if (isset($t->prepared_statements[$h])) {
  25.             $stm = $t->prepared_statements[$h];
  26.         }
  27.         else {
  28.             $stm = $t->pdo->prepare($query);
  29.             $t->prepared_statements[$h] = $stm;
  30.         }
  31.         $stm->execute($bindings);
  32.         preg_match('/^\s*(\w+)/', $query, $m);
  33.         $action = strtolower($m[1]);
  34.         if ($action == 'update' or $action == 'delete') {
  35.             return $stm->rowCount();
  36.         }
  37.         if ($action == 'insert') {
  38.             return $t->last_insert_id();
  39.         }
  40.         return $stm;
  41.     }
  42.  
  43.     public function row($query, array $bindings=[]) {
  44.         $stm = $this->execute($query, $bindings);
  45.         return $stm->fetch(PDO::FETCH_ASSOC);
  46.     }
  47.  
  48.     public function rows($query, array $bindings=[]) {
  49.         $stm = $this->execute($query, $bindings);
  50.         return $stm->fetchAll(PDO::FETCH_ASSOC);
  51.     }
  52.  
  53.     public function column($query, array $bindings=[]) {
  54.         $stm = $this->execute($query, $bindings);
  55.         return $stm->fetchColumn();
  56.     }
  57.  
  58.     public function find($table, $id, $field='id') {
  59.         $t = $this;
  60.         $table = $t->quote_name($table);
  61.         $field = $t->quote_name($field);
  62.         return $t->row("SELECT * FROM $table WHERE $field=? LIMIT 1", [$id]);
  63.     }
  64.  
  65.     public function count($table, $where=1, array $bindings=[]) {
  66.         $t = $this;
  67.         $table = $t->quote_name($table);
  68.         list($where, $bindings) = $t->get_where($where, $bindings);
  69.         $q = "SELECT COUNT(*) FROM $table WHERE $where";
  70.         return (int) $t->column($q, $bindings);
  71.     }
  72.    
  73.     public function contains($table, $field, $value) {
  74.         return $this->count($table, [$field => $value]) > 0;
  75.     }
  76.  
  77.     public function add($table, array $data) {
  78.         $t = $this;
  79.         $table = $t->quote_name($table);
  80.         $q = "INSERT INTO $table ";
  81.         if (is_list($data)) {
  82.             $q .= 'VALUES (' . implode(',', array_fill(0, count($data), '?')) . ')';
  83.         }
  84.         else {
  85.             $columns = [];
  86.             $values = [];
  87.             $keys = array_keys($data);
  88.             foreach ($keys as $v) {
  89.                 $columns[] = $t->quote_name($v);
  90.                 $values[] = ":$v";
  91.             }
  92.             $columns = implode(',', $columns);
  93.             $values = implode(',', $values);
  94.             $q .= "($columns) VALUES ($values)";
  95.         }
  96.         return $t->execute($q, $data);
  97.     }
  98.  
  99.     public function update($table, array $data, $where=1, array $bindings=[]) {
  100.         $t = $this;
  101.         $set = [];
  102.         list($where, $bindings) = $t->get_where($where, $bindings);
  103.         $is_list = is_list($bindings);
  104.         $bindings = array_merge($is_list ? array_values($data) : $data, $bindings);
  105.         $keys = array_keys($data);
  106.         foreach ($keys as $v) {
  107.             // подстраитваемся под стиль плейсхолдеров
  108.             $set[] = $t->quote_name($v) . '=' . ($is_list ? '?' : ":$v");
  109.         }
  110.         $set = implode(',', $set);
  111.         $table = $t->quote_name($table);
  112.         return $t->execute("UPDATE $table SET $set WHERE $where", $bindings);
  113.     }
  114.  
  115.     public function delete($table, $where=1, array $bindings=[]) {
  116.         $t = $this;
  117.         $table = $t->quote_name($table);
  118.         list($where, $bindings) = $t->get_where($where, $bindings);
  119.         return $t->execute("DELETE FROM $table WHERE $where", $bindings);
  120.     }
  121.  
  122.     protected function get_where($criteria, $bindings) {
  123.         if (is_array($criteria)) {
  124.             $bindings = [];
  125.             $parts = [];
  126.             foreach ($criteria as $k => $v) {
  127.                 $parts[] = $this->quote_name($k) . '=?';
  128.                 $bindings[] = $v;
  129.             }
  130.             $criteria = implode($parts, ' AND ');
  131.         }
  132.         return [$criteria, $bindings];
  133.     }
  134.  
  135.     public function query($query) {
  136.         return $this->pdo->query($query);
  137.     }
  138.  
  139.     public function exec($query) {
  140.         return $this->pdo->exec($query);
  141.     }
  142.  
  143.     public function quote($s) {
  144.         return $this->pdo->quote($s);
  145.     }
  146.  
  147.     public function commit() {
  148.         return $this->pdo->commit();
  149.     }
  150.  
  151.     public function rollback() {
  152.         return $this->pdo->rollback();
  153.     }
  154.  
  155.     public function last_insert_id() {
  156.         return $this->pdo->lastInsertId();
  157.     }
  158.  
  159.     public function clear_cache() {
  160.         $this->prepared_statements = [];
  161.     }
  162. }
  163.  
  164. if (!function_exists('is_list')) {
  165.     function is_list($arr) {
  166.         return $arr === array_values($arr);
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement