stuppid_bot

Шедевральная хуйня

Jun 9th, 2014
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. <?php
  2.  
  3. class DB {
  4.     protected $pdo;
  5.  
  6.     protected function __construct() {
  7.         $info = config('db');
  8.         foreach (array('dsn', 'user', 'password', 'args') as $key) {
  9.             if (!isset($info[$key])) {
  10.                 $info[$key] = null;
  11.             }
  12.         }
  13.         try {
  14.             $this->pdo = new PDO($info['dsn'], $info['user'], $info['password'], $info['args']);
  15.         }
  16.         catch (PDOException $e) {
  17.             die('Connection failed: ' . $e->getMessage());
  18.         }
  19.         $this->query('SET NAMES utf8');
  20.     }
  21.  
  22.     public static function i() {
  23.         static $db;
  24.         if (!$db) {
  25.             $db = new self;
  26.         }
  27.         return $db;
  28.     }
  29.  
  30.     public function query($sql, $bindings = null) {
  31.         print dump($sql);
  32.         $stmt = $this->pdo->prepare($sql);
  33.         $stmt->execute($bindings);
  34.         return $stmt;
  35.     }
  36.  
  37.     public function insert($table, $data) {
  38.         $sql = "INSERT INTO $table";
  39.         $values = array_values($data);
  40.         if ($values != $data) {
  41.             $fields = array_keys($data);
  42.             $sql .= ' (' . implode(',', $fields) . ')';
  43.         }
  44.         $sql .= ' VALUES (' . rtrim(str_repeat('?,', count($data)), ',') . ')';
  45.         $this->query($sql, $values);
  46.         $id = $this->pdo->lastInsertId();
  47.         return $id;
  48.     }
  49.  
  50.     public function select($from, $what = '*', $criteria = 0, $limit = 0, $offset = 0, $order = 0, $bindings = null) {
  51.         $sql = "SELECT $what FROM $from";
  52.         $sql .= $this->where($criteria, $bindings);
  53.         if ($order) {
  54.             $sql .= " ORDER BY $order";
  55.         }
  56.         if ($limit) {
  57.             $sql .= " LIMIT $offset,$limit";
  58.         }
  59.         $stmt = $this->query($sql, $bindings);
  60.         return $stmt;
  61.     }
  62.  
  63.     public function selectRow($from, $what = '*', $criteria = 0, $order = 0, $bindings = null) {
  64.         return $this->select($from, $what, $criteria, 0, 0, $order, $bindings);
  65.     }
  66.  
  67.     public function get($from, $what = '*', $criteria = 0, $limit = 0, $offset = 0, $order = 0, $bindings = null) {
  68.         $stmt = $this->select($from, $what, $criteria, $limit, $offset, $order, $bindings);
  69.         return $stmt->fetchAll(PDO::FETCH_ASSOC);
  70.     }
  71.  
  72.     public function getRow($from, $what = '*', $criteria = 0, $order = 0, $bindings = null) {
  73.         $stmt = $this->selectRow($from, $what, $criteria, $order, $bindings);
  74.         return $stmt->fetch(PDO::FETCH_ASSOC);
  75.     }
  76.  
  77.     public function getValue($from, $field, $criteria = 0, $order = 0, $bindings = null) {
  78.         $stmt = $this->selectRow($from, $field, $criteria, $order, $bindings);
  79.         return $stmt->fetchColumn();
  80.     }
  81.  
  82.     public function count($table, $criteria = 0, $bindings = null) {
  83.         return (int) $this->getValue($table, 'COUNT(*)', $criteria, 0, $bindings);
  84.     }
  85.  
  86.     // можно использовать только именованные плейсхолдеры
  87.     public function update($what, $data, $criteria = 0, $bindings = array()) {
  88.         $sql = "UPDATE $what SET ";
  89.         $parts = array();
  90.         foreach ($data as $k => $v) {
  91.             $bindings[":$k"] = $v;
  92.             $parts[] = "$k=:$k";
  93.         }
  94.         $sql .= implode(',', $parts) . $this->where($criteria, $bindings);
  95.         $stmt = $this->query($sql, $bindings);
  96.         return $stmt->rowCount();
  97.     }
  98.  
  99.     public function delete($from, $criteria, $bindings = null) {
  100.         $sql = "DELETE FROM $from" . $this->where($criteria, $bindings);
  101.         $stmt = $this->query($sql, $bindings);
  102.         return $stmt->rowCount();
  103.     }
  104.  
  105.     protected function where($criteria, &$bindings) {
  106.         if (is_array($criteria)) {
  107.             // array('foo' => $foo, 'bar' => $bar);
  108.             // WHERE foo=:foo AND bar=:bar
  109.             $bindings = (array) $bindings;
  110.             $parts = array();
  111.             foreach ($criteria as $k => $v) {
  112.                 $bindings[":$k"] = $v;
  113.                 $parts[] = "$k=:$k";
  114.             }
  115.             $criteria = implode(' AND ', $parts);
  116.         }
  117.         return $criteria ? " WHERE $criteria" : "";
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment