Advertisement
annukaka

dbClass

May 31st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. <?php
  2. namespace app\libs;
  3.  
  4. use app\libs\config;
  5. use app\libs\log;
  6. use PDO;
  7. /**
  8. *
  9. */
  10. class db
  11. {
  12.  
  13.     private $pipe = ["driver"=>"mysql","host"=>null,"database"=>null,"port"=>null,"username"=>null,"password"=>null];
  14.    
  15.     private $xdb = null;
  16.     public $stmt = null;
  17.     private $dbconfig = null;
  18.     private $defaultdbconfig = null;
  19.  
  20.     private $query = null;
  21.     # select
  22.  
  23.     private $wherequery = null;
  24.     private $limitquery = null;
  25.  
  26.     static private $log = [];
  27.  
  28.     # edit
  29.    
  30.     public function __construct($setupinput = null)
  31.     {
  32.         $this->null();
  33.         try {
  34.             $stringconfig = $this->setup($setupinput);
  35.             $attribute = array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION);
  36.             $this->xdb = new PDO($stringconfig,$this->dbconfig["username"],$this->dbconfig["password"],$attribute);
  37.         } catch (PDOException $e) {
  38.             $this->setlog($e->getMessage());
  39.         }
  40.     }
  41.     private function setup($setupinput=[])
  42.     {
  43.         # set pass
  44.         $pass = true;
  45.         # get db config
  46.         $config = new config;
  47.         $defaultdbconfig = $config->get("db");
  48.  
  49.         $config = null;
  50.         $configstring = null;
  51.  
  52.         # merging
  53.         $config = array_replace($this->pipe,$defaultdbconfig);
  54.  
  55.         if (isset($setupinput[0]))
  56.             $config = array_replace($this->pipe,$setupinput);
  57.  
  58.         if ($config["driver"]!=null AND $config["host"]!=null) {
  59.             $configstring = $config["driver"].":";
  60.             $configstring .= "host=" . $config["host"].";";
  61.  
  62.             if ($config["database"]!=null)
  63.                 $configstring .= "dbname=" . $config["database"].";";
  64.             if ($config["port"]!=null)
  65.                 $configstring .= "port=" . $config["port"] . ";";
  66.         }
  67.        
  68.         $this->defaultdbconfig = $defaultdbconfig;
  69.         $this->dbconfig = $config;
  70.         return $configstring;
  71.     }
  72.     private function setlog($log=null)
  73.     {
  74.         self::$log[] = $log;
  75.     }
  76.     public function getlogs()
  77.     {
  78.         return self::$log;
  79.     }
  80.     public function getlog()
  81.     {
  82.         if (isset(self::$log[0])) {
  83.             end(self::$log);
  84.             $key = key(self::$log);
  85.             return self::$log[$key];
  86.         }
  87.     }
  88.  
  89.     public function prepare($query=null)
  90.     {
  91.         try {
  92.             $stmt = $this->xdb->prepare($query);
  93.             $this->stmt = $stmt;
  94.             return $stmt;
  95.            
  96.         } catch (PDOException $e) {
  97.             $this->setlog($e->getMessage());
  98.         }
  99.     }
  100.     public function execute()
  101.     {
  102.         try {
  103.             if ($this->stmt == null) {
  104.                 $this->prepare($this->generatequery());
  105.             }
  106.             return $this->stmt->execute();
  107.            
  108.         } catch (PDOException $e) {
  109.             $this->setlog($e->getMessage());
  110.         }
  111.  
  112.     }
  113.     public function fetchall()
  114.     {
  115.         try {
  116.             $result = $this->stmt->setFetchMode(PDO::FETCH_ASSOC);
  117.             $fetchall = $this->stmt->fetchALL();
  118.             return $fetchall;
  119.            
  120.         } catch (PDOException $e) {
  121.             $this->setlog($e->getMessage());
  122.         }
  123.     }
  124.     public function fetch()
  125.     {
  126.         try {
  127.             $result = $this->stmt->setFetchMode(PDO::FETCH_ASSOC);
  128.             $fetch = $this->stmt->fetch();
  129.             return $fetch;
  130.            
  131.         } catch (PDOException $e) {
  132.             $this->setlog($e->getMessage());
  133.         }
  134.     }
  135.     public function generatequery()
  136.     {
  137.         $query = $this->query . $this->wherequery . $this->limitquery;
  138.         return $query;
  139.     }
  140.  
  141.     public function query($query='')
  142.     {
  143.         $this->query = $query;
  144.         return $this;
  145.     }
  146.     public function where($array=null)
  147.     {
  148.         $where = " WHERE ";
  149.         $i = 0;
  150.         foreach ($array as $key => $value) {
  151.             $prefix = null;
  152.             if ($i > 0) {
  153.                 $prefix = " AND ";
  154.             }
  155.             $where .= $prefix . $key ." = \"" . $value ."\" ";
  156.             $i++;
  157.         }
  158.         $this->wherequery = $where;
  159.         return $this;
  160.     }
  161.     public function page($page=null,$limit=null)
  162.     {
  163.         $offset = $page * $limit;
  164.         $this->limitquery = " LIMIT " . $limit . " OFFSET " . $offset . " ";
  165.         return $this;
  166.     }
  167.     public function limit($offset=null,$limit=null)
  168.     {
  169.         $this->limitquery = " LIMIT " . $limit . " OFFSET " . $offset . " ";
  170.         return $this;
  171.     }
  172.     public function null()
  173.     {
  174.         $this->stmt = null;
  175.         $this->xdb = null;
  176.         $this->query = null;
  177.         $this->dbconfig = null;
  178.         $this->defaultdbconfig = null;
  179.         $this->wherequery = null;
  180.         $this->pagequery = null;
  181.     }
  182.    
  183.     function __destruct()
  184.     {
  185.         log::error("DB","error",$this->getlogs());
  186.     }
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement