Advertisement
stuppid_bot

Untitled

Jul 14th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.63 KB | None | 0 0
  1. <?php
  2.  
  3. class dal {
  4.     public $pdo;
  5.     protected $queryCache = [];
  6.     const FETCH_ALL = 1;
  7.     const FETCH_ROW = 2;
  8.     const FETCH_COLUMN = 4;
  9.     // const D = 8;
  10.     // const E = 16;
  11.     // const F = 32;
  12.     // const G = 64;
  13.     // const H = 128;
  14.     // const I = 256;
  15.     // const J = 512;
  16.     // const K = 1024;
  17.     // const L = 2048;
  18.     const FETCH_NUM = 4096;
  19.     const FETCH_ASSOC = 8192;
  20.     // const O = 16384;
  21.     // const P = 32768;
  22.     // const Q = 65536;
  23.     // const R = 131072;
  24.     // const S = 262144;
  25.     // const T = 524288;
  26.     // const U = 1048576;
  27.     // const V = 2097152;
  28.     // const W = 4194304;
  29.     // const X = 8388608;
  30.     // const Y = 16777216;
  31.     // const Z = 33554432;
  32.     const FETCH_BOTH = 12288; // 4096 | 8192
  33.  
  34.     public function __construct($dsn, $usr=null, $pswd=null, $args=null,
  35.                                 $charset='utf8')
  36.     {
  37.         try {
  38.             $this->pdo = new PDO($dsn, $usr, $pswd, $args);
  39.         }
  40.         catch (PDOException $e) {
  41.             die('Connection failed: ' . $e->getMessage());
  42.         }
  43.  
  44.         $this->query("SET NAMES $charset");
  45.     }
  46.  
  47.     public function query($query, array $params=null)
  48.     {
  49.         echo dump($query);
  50.         $h = md5($query);
  51.         // кешируем подготовленные выражения
  52.         if (isset($this->queryCache[$h])) {
  53.             echo 'Используем подготовленное выражение из кэша.<br>';
  54.             $s = $this->queryCache[$h];
  55.         }
  56.         else {
  57.             $s = $this->pdo->prepare($query);
  58.             $this->queryCache[$h] = $s;
  59.         }
  60.         $s->execute($params);
  61.         preg_match('/^\s*(\w+)/', $query, $m);
  62.         $action = strtolower($m[1]);
  63.         // var_dump($action);
  64.         if ($action == 'insert') {
  65.             return $this->lastInsertId();
  66.         }
  67.         if ($action == 'update' or $action == 'delete') {
  68.             return $s->rowCount();
  69.         }
  70.         return $s;
  71.     }
  72.  
  73.     public function get($query, array $params=null, $flags=self::FETCH_ALL)
  74.     {
  75.         $r = $this->query($query, $params);
  76.         if ($flags & self::FETCH_ROW) {
  77.             return $r->fetch();
  78.         }
  79.         if ($flags & self::FETCH_COLUMN) {
  80.             return $r->fetchColumn();
  81.         }
  82.         return $r->fetchAll();
  83.     }
  84.  
  85.     public function row($query, array $params=null, $add_flags=0)
  86.     {
  87.         return $this->get($query, $params, self::FETCH_ROW | $add_flagsPar);
  88.     }
  89.  
  90.     public function column($query, array $params=null)
  91.     {
  92.         return $this->get($query, $params, self::FETCH_COLUMN);
  93.     }
  94.  
  95.     public function count($table, $where=1, array $params=null)
  96.     {
  97.         return $this->column('SELECT COUNT(*) FROM ' .
  98.                              $this->quoteid($table) . " WHERE $where",
  99.                              $params);
  100.     }
  101.  
  102.     public function insert($table, array $data)
  103.     {    
  104.         $q = 'INSERT INTO ' . $this->quoteid($table);
  105.         $fields = array_keys($data);
  106.         $q .= ' (' .
  107.               implode( ',', array_map([$this, 'quoteid'], $fields) ) .
  108.               ') VALUES (';
  109.         for ($i = 0, $c = count($fields); $i < $c; ++$i) {
  110.             $q .= ($i ? ',' : '') . ':' . $fields[$i];
  111.         }
  112.         $q .= ')';
  113.         return $this->query($q, $data);
  114.     }
  115.  
  116.     public function update($table, array $data, $where=1, array $params=null)
  117.     {
  118.         is_null($params) and $params = [];
  119.         $is_list_params = array_values($params) == $params;
  120.         // var_dump($is_list_params);
  121.         $params = array_merge($is_list_params ? array_values($data) : $data,
  122.                               $params);
  123.         $s = '';
  124.         foreach ($data as $k => $v) {
  125.             $s .= (strlen($s) ? ',' : '') . $this->quoteid($k) . '=' .
  126.                   ($is_list_params ? '?' : ":$k");
  127.         }
  128.         // echo dump($params);
  129.         return $this->query('UPDATE ' . $this->quoteid($table) .
  130.                             " SET $s WHERE $where", $params);
  131.     }
  132.  
  133.     public function delete($table, $where=1, array $params=null) {
  134.         return $this->query('DELETE FROM ' . $this->quoteid($table) .
  135.                             " WHERE $where", $params);
  136.     }
  137.  
  138.     public function describe($table)
  139.     {
  140.         return $this->get( 'DESCRIBE ' . $this->quoteid($table) );
  141.     }
  142.  
  143.     public function quoteid($s)
  144.     {
  145.         return '`' . str_replace('`', '``', $s) . '`';
  146.     }
  147.  
  148.     public function lastInsertId()
  149.     {
  150.         return $this->pdo->lastInsertId();
  151.     }
  152. }
  153.  
  154. /* ---------------------------- 78 characters ----------------------------- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement