Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\libs;
- use app\libs\config;
- use app\libs\log;
- use PDO;
- /**
- *
- */
- class db
- {
- private $pipe = ["driver"=>"mysql","host"=>null,"database"=>null,"port"=>null,"username"=>null,"password"=>null];
- private $xdb = null;
- public $stmt = null;
- private $dbconfig = null;
- private $defaultdbconfig = null;
- private $query = null;
- # select
- private $wherequery = null;
- private $limitquery = null;
- static private $log = [];
- # edit
- public function __construct($setupinput = null)
- {
- $this->null();
- try {
- $stringconfig = $this->setup($setupinput);
- $attribute = array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION);
- $this->xdb = new PDO($stringconfig,$this->dbconfig["username"],$this->dbconfig["password"],$attribute);
- } catch (PDOException $e) {
- $this->setlog($e->getMessage());
- }
- }
- private function setup($setupinput=[])
- {
- # set pass
- $pass = true;
- # get db config
- $config = new config;
- $defaultdbconfig = $config->get("db");
- $config = null;
- $configstring = null;
- # merging
- $config = array_replace($this->pipe,$defaultdbconfig);
- if (isset($setupinput[0]))
- $config = array_replace($this->pipe,$setupinput);
- if ($config["driver"]!=null AND $config["host"]!=null) {
- $configstring = $config["driver"].":";
- $configstring .= "host=" . $config["host"].";";
- if ($config["database"]!=null)
- $configstring .= "dbname=" . $config["database"].";";
- if ($config["port"]!=null)
- $configstring .= "port=" . $config["port"] . ";";
- }
- $this->defaultdbconfig = $defaultdbconfig;
- $this->dbconfig = $config;
- return $configstring;
- }
- private function setlog($log=null)
- {
- self::$log[] = $log;
- }
- public function getlogs()
- {
- return self::$log;
- }
- public function getlog()
- {
- if (isset(self::$log[0])) {
- end(self::$log);
- $key = key(self::$log);
- return self::$log[$key];
- }
- }
- public function prepare($query=null)
- {
- try {
- $stmt = $this->xdb->prepare($query);
- $this->stmt = $stmt;
- return $stmt;
- } catch (PDOException $e) {
- $this->setlog($e->getMessage());
- }
- }
- public function execute()
- {
- try {
- if ($this->stmt == null) {
- $this->prepare($this->generatequery());
- }
- return $this->stmt->execute();
- } catch (PDOException $e) {
- $this->setlog($e->getMessage());
- }
- }
- public function fetchall()
- {
- try {
- $result = $this->stmt->setFetchMode(PDO::FETCH_ASSOC);
- $fetchall = $this->stmt->fetchALL();
- return $fetchall;
- } catch (PDOException $e) {
- $this->setlog($e->getMessage());
- }
- }
- public function fetch()
- {
- try {
- $result = $this->stmt->setFetchMode(PDO::FETCH_ASSOC);
- $fetch = $this->stmt->fetch();
- return $fetch;
- } catch (PDOException $e) {
- $this->setlog($e->getMessage());
- }
- }
- public function generatequery()
- {
- $query = $this->query . $this->wherequery . $this->limitquery;
- return $query;
- }
- public function query($query='')
- {
- $this->query = $query;
- return $this;
- }
- public function where($array=null)
- {
- $where = " WHERE ";
- $i = 0;
- foreach ($array as $key => $value) {
- $prefix = null;
- if ($i > 0) {
- $prefix = " AND ";
- }
- $where .= $prefix . $key ." = \"" . $value ."\" ";
- $i++;
- }
- $this->wherequery = $where;
- return $this;
- }
- public function page($page=null,$limit=null)
- {
- $offset = $page * $limit;
- $this->limitquery = " LIMIT " . $limit . " OFFSET " . $offset . " ";
- return $this;
- }
- public function limit($offset=null,$limit=null)
- {
- $this->limitquery = " LIMIT " . $limit . " OFFSET " . $offset . " ";
- return $this;
- }
- public function null()
- {
- $this->stmt = null;
- $this->xdb = null;
- $this->query = null;
- $this->dbconfig = null;
- $this->defaultdbconfig = null;
- $this->wherequery = null;
- $this->pagequery = null;
- }
- function __destruct()
- {
- log::error("DB","error",$this->getlogs());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement