Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.86 KB | None | 0 0
  1. <?php
  2. class DB {
  3.     public function __construct() {
  4.         $dbdriver = 'mysql';
  5.         $dbhost = Config::get('mysql.host');
  6.         $dbuser = Config::get('mysql.username');
  7.         $dbpass = Config::get('mysql.password');
  8.         $dbname = Config::get('mysql.db');
  9.  
  10.         # $sqlitedb = $this->config['sqlitedb'];
  11.  
  12.         $options = [
  13.             PDO::ATTR_PERSISTENT => true,
  14.             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  15.         ];
  16.  
  17.         try {
  18.             switch($dbdriver) {
  19.                 case 'sqlite':
  20.                     $conn = "sqlite:{$sqlitedb}";
  21.                     break;
  22.                 case 'mysql':
  23.                     $conn = "mysql:host={$dbhost};dbname={$dbname}";
  24.                     break;
  25.                 default:
  26.                     echo "Unsuportted DB Driver! Check the configuration.";
  27.                     exit(1);
  28.             }
  29.  
  30.             $this->db = new PDO($conn, $dbuser, $dbpass, $options);
  31.            
  32.         } catch(PDOException $e) {
  33.             echo $e->getMessage(); exit(1);
  34.         }
  35.     }
  36.  
  37.     public function run($sql, $bind=array()) {
  38.         $sql = trim($sql);
  39.        
  40.         try {
  41.  
  42.             $result = $this->db->prepare($sql);
  43.             $result->execute($bind);
  44.             return $result;
  45.  
  46.         } catch (PDOException $e) {
  47.             echo $e->getMessage();
  48.             exit(1);
  49.         }
  50.     }
  51.  
  52.     public function create($table, $data) {
  53.         $fields = $this->filter($table, $data);
  54.  
  55.         $sql = "INSERT INTO " . $table . " (" . implode($fields, ", ") . ") VALUES (:" . implode($fields, ", :") . ");";
  56.  
  57.         $bind = array();
  58.         foreach($fields as $field)
  59.             $bind[":$field"] = $data[$field];
  60.  
  61.         $result = $this->run($sql, $bind);
  62.         return $this->db->lastInsertId();
  63.     }
  64.  
  65.     public function read($table, $where="", $bind=array(), $fields="*") {
  66.         $sql = "SELECT " . $fields . " FROM " . $table;
  67.         if(!empty($where))
  68.             $sql .= " WHERE " . $where;
  69.         $sql .= ";";
  70.  
  71.         $result = $this->run($sql, $bind);
  72.         $result->setFetchMode(PDO::FETCH_ASSOC);
  73.  
  74.         $rows = array();
  75.         while($row = $result->fetch()) {
  76.             $rows[] = $row;
  77.         }
  78.  
  79.         return $rows;
  80.     }
  81.  
  82.     public function update($table, $data, $where, $bind=array()) {
  83.         $fields = $this->filter($table, $data);
  84.         $fieldSize = sizeof($fields);
  85.  
  86.         $sql = "UPDATE " . $table . " SET ";
  87.         for($f = 0; $f < $fieldSize; ++$f) {
  88.             if($f > 0)
  89.                 $sql .= ", ";
  90.             $sql .= $fields[$f] . " = :update_" . $fields[$f];
  91.         }
  92.         $sql .= " WHERE " . $where . ";";
  93.  
  94.         foreach($fields as $field)
  95.             $bind[":update_$field"] = $data[$field];
  96.        
  97.         $result = $this->run($sql, $bind);
  98.         return $result->rowCount();
  99.     }
  100.  
  101.     function delete($table, $where, $bind="") {
  102.         $sql = "DELETE FROM " . $table . " WHERE " . $where . ";";
  103.         $result = $this->run($sql, $bind);
  104.         return $result->rowCount();
  105.     }
  106.  
  107.     private function filter($table, $data) {
  108.         $driver = $this->config['dbdriver'];
  109.  
  110.         if($driver == 'sqlite') {
  111.             $sql = "PRAGMA table_info('" . $table . "');";
  112.             $key = "name";
  113.         } elseif($driver == 'mysql') {
  114.             $sql = "DESCRIBE " . $table . ";";
  115.             $key = "Field";
  116.         } else {    
  117.             $sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '" . $table . "';";
  118.             $key = "column_name";
  119.         }  
  120.  
  121.         if(false !== ($list = $this->run($sql))) {
  122.             $fields = array();
  123.             foreach($list as $record)
  124.                 $fields[] = $record[$key];
  125.             return array_values(array_intersect($fields, array_keys($data)));
  126.         }
  127.  
  128.         return array();
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement