Advertisement
Guest User

Untitled

a guest
Jun 8th, 2013
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Обертка над БД
  5.  *
  6.  * @author Fragster
  7.  */
  8. class DB extends PDO {
  9.  
  10.     /**
  11.      *
  12.      * @var DB
  13.      */
  14.     private static $singleton;
  15.     private $engine;
  16.     private $host;
  17.     private $database;
  18.     private $user;
  19.     private $pass;
  20.  
  21.     public function __construct() {
  22.         $this->engine = 'mysql';
  23.         $this->host = Config::$db_host;
  24.         $this->database = Config::$db_database;
  25.         $this->user = Config::$db_user;
  26.         $this->pass = Config::$db_password;
  27.         $dns = $this->engine . ':dbname=' . $this->database . ";host=" . $this->host;
  28.         parent::__construct($dns, $this->user, $this->pass);
  29.         $this->exec("SET NAMES utf8");
  30.         $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  31.     }
  32.  
  33.     /**
  34.      *
  35.      * @return DB
  36.      */
  37.     static function Init() {
  38.         if (!self::$singleton) {
  39.             self::$singleton = new DB;
  40.         }
  41.         return self::$singleton;
  42.     }
  43.  
  44.     public function arrayPrepare($query, array $input_parameters) {
  45.         $query = str_replace("\n", "", $query);
  46.  
  47.         $params = array();
  48.         foreach ($input_parameters as $key => $value) {
  49.             if (is_array($value)) {
  50.                 $implode = '';
  51.                 $delim = '';
  52.                 foreach ($value as $valueKey => $valueValue) {
  53.                     $params[$key . '_' . $valueKey] = $valueValue;
  54.                     $implode.= $delim . $key . '_' . $valueKey;
  55.                     $delim = ', ';
  56.                 }
  57.                 $query = str_replace($key, $implode, $query);
  58.             } else {
  59.                 $params[$key] = $value;
  60.             }
  61.         }
  62.  
  63.         try {
  64.             $statement = $this->prepare($query);
  65.         } catch (Exception $exc) {
  66.             throw new Exception($query."\n".$this->errorInfo());
  67.         }
  68.  
  69.         //echo "$query\n";
  70.         foreach ($params as $key => $value) {
  71.             //echo "bind $key => $value\n";
  72.             $statement->bindValue($key, $value)."\n";
  73.         }
  74.         return $statement;
  75.     }
  76.  
  77.     public function multiInsert($query, array $insert_array) {
  78.         $query = str_replace("\n", "", $query);
  79.        
  80.         $query1 = array();
  81.         preg_match('/.*values\s*/im', $query, $query1);
  82.         $query1 = $query1[0];
  83.  
  84.         $placeholders = array();
  85.         preg_match_all('/:\w+/im', $query, $placeholders);
  86.         $placeholders = $placeholders[0];
  87.         $rowDelim = '';
  88.         foreach ($insert_array as $key => $value) {
  89.             $query1.= $rowDelim . '(';
  90.             $rowDelim = ', ';
  91.             $fieldDelim = '';
  92.             foreach ($placeholders as $placeholder) {
  93.                 $query1.= $fieldDelim . $placeholder . '_' . $key;
  94.                 $fieldDelim = ', ';
  95.             }
  96.             $query1.= ')';
  97.         }
  98.        
  99.         try {
  100.             $statement = $this->prepare($query1);
  101.         } catch (Exception $exc) {
  102.             throw new Exception($query1."\n".$this->errorInfo());
  103.         }
  104.        
  105.         foreach ($insert_array as $key => $value) {
  106.             foreach ($placeholders as $placeholder) {
  107.                 $var = $value[$placeholder];
  108.                 $statement->bindValue($placeholder . '_' . $key, $var);
  109.             }
  110.         }
  111.        
  112.         $statement->execute();
  113.     }
  114.  
  115. }
  116.  
  117. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement