Advertisement
HwapX

DB Nickc

Mar 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2. //defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. define('DB_HOST', 'localhost');
  5. define('DB_NAME', 'db');
  6. define('DB_USER', 'root');
  7. define('DB_PASS', 'pass');
  8.  
  9. class DB {
  10.     protected static $instance = null;
  11.  
  12.     final private function __construct() {}
  13.     final private function __clone() {}
  14.  
  15.     /**
  16.      * @return PDO
  17.      */
  18.     public static function instance() {
  19.         if (self::$instance === null) {
  20.             try {
  21.                 self::$instance = new PDO(
  22.                     'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME,
  23.                     DB_USER,
  24.                     DB_PASS
  25.                 );
  26.                 self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  27.             } catch (PDOException $e) {
  28.                 die('Database connection could not be established.');
  29.             }
  30.         }
  31.  
  32.         return self::$instance;
  33.     }
  34.  
  35.     /**
  36.      * @return PDOStatement
  37.      */
  38.     public static function q($query) {
  39.         if (func_num_args() == 1) {
  40.             return self::instance()->query($query);
  41.         }
  42.  
  43.         $args = func_get_args();
  44.         return self::instance()->query(self::autoQuote(array_shift($args), $args));
  45.     }
  46.  
  47.     public static function x($query) {
  48.         if (func_num_args() == 1) {
  49.             return self::instance()->exec($query);
  50.         }
  51.  
  52.         $args = func_get_args();
  53.         return self::instance()->exec(self::autoQuote(array_shift($args), $args));
  54.     }
  55.  
  56.     public static function autoQuote($query, array $args) {
  57.         $i = strlen($query) - 1;
  58.         $c = count($args);
  59.  
  60.         while ($i--) {
  61.             if ('?' === $query[$i] && false !== $type = strpos('siaf', $query[$i + 1])) {
  62.                 if (--$c < 0) {
  63.                     throw new InvalidArgumentException('Too little parameters.');
  64.                 }
  65.  
  66.                 if (0 === $type) {
  67.                     $replace = self::instance()->quote($args[$c]);
  68.                 } elseif (1 === $type) {
  69.                     $replace = intval($args[$c]);
  70.                 } elseif (2 === $type) {
  71.                     foreach ($args[$c] as &$value) {
  72.                         $value = self::instance()->quote($value);
  73.                     }
  74.                     $replace = '(' . implode(',', $args[$c]) . ')';
  75.                 } elseif (3 === $type) {
  76.                     $replace = floatval($args[$c]);
  77.                 }
  78.  
  79.                 $query = substr_replace($query, $replace, $i, 2);
  80.             }
  81.         }
  82.  
  83.         if ($c > 0) {
  84.             throw new InvalidArgumentException('Too many parameters.');
  85.         }
  86.  
  87.         return $query;
  88.     }
  89.  
  90.     public static function __callStatic($method, $args) {
  91.         return call_user_func_array(array(self::instance(), $method), $args);
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement