Last active
August 29, 2015 14:14
-
-
Save makamo/5559df3730a8cffeb0b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Core\Database; | |
use \PDO; | |
class MsAcessDatabase extends Database{ | |
private $db_dbq; | |
private $db_user; | |
private $db_pass; | |
private $db_driver; | |
private $pdo; | |
/** | |
* @param $db_dbq | |
* @param string $db_user | |
* @param string $db_pass | |
* @param string $db_driver | |
*/ | |
public function __construct($db_dbq, $db_user = '', $db_pass = '',$db_driver="{Microsoft Access Driver (*.mdb, *.accdb)}"){ | |
$this->db_dbq = $db_dbq; | |
$this->db_user = $db_user; | |
$this->db_pass = $db_pass; | |
$this->db_driver = $db_driver; | |
} | |
/** | |
* @return PDO | |
*/ | |
private function getPDO(){ | |
if($this->pdo === null){ | |
try{ | |
$pdo = new PDO("odbc:Driver={$this->db_driver}; Dbq=$this->db_dbq; charset=UTF-8; Uid=' . $this->db_user . '; Pwd=' . $this->db_pass . ';'"); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
}catch (\PDOException $e){ | |
var_dump($e); | |
die(); | |
} | |
$this->pdo = $pdo; | |
} | |
return $this->pdo; | |
} | |
/** | |
* @param $statement | |
* @param null $class_name | |
* @param bool $one | |
* @return array|mixed|\PDOStatement | |
*/ | |
public function query($statement, $class_name = null, $one = false){ | |
try{ | |
$req = $this->getPDO()->query($statement); | |
}catch (\PDOException $e){ | |
var_dump($e); | |
die(); | |
} | |
if( | |
strpos($statement, 'UPDATE') === 0 || | |
strpos($statement, 'INSERT') === 0 || | |
strpos($statement, 'DELETE') === 0 | |
) { | |
return $req; | |
} | |
if($class_name === null){ | |
$req->setFetchMode(PDO::FETCH_OBJ); | |
} else { | |
$req->setFetchMode(PDO::FETCH_CLASS, $class_name); | |
} | |
if($one) { | |
$datas = $req->fetch(); | |
} else { | |
$datas = $req->fetchAll(); | |
} | |
return $datas; | |
} | |
/** | |
* @param $statement | |
* @param $attributes | |
* @param null $class_name | |
* @param bool $one | |
* @return array|bool|mixed | |
*/ | |
public function prepare($statement, $attributes, $class_name = null, $one = false){ | |
try{ | |
$req = $this->getPDO()->prepare($statement); | |
$res = $req->execute($attributes); | |
}catch (\PDOException $e){ | |
var_dump($e); | |
die(); | |
} | |
if( | |
strpos($statement, 'UPDATE') === 0 || | |
strpos($statement, 'INSERT') === 0 || | |
strpos($statement, 'DELETE') === 0 | |
) { | |
return $res; | |
} | |
if($class_name === null){ | |
$req->setFetchMode(PDO::FETCH_OBJ); | |
} else { | |
$req->setFetchMode(PDO::FETCH_CLASS, $class_name); | |
} | |
if($one) { | |
$datas = $req->fetch(); | |
} else { | |
$datas = $req->fetchAll(); | |
} | |
return $datas; | |
} | |
/** | |
* @return string | |
*/ | |
public function lastInsertId(){ | |
return $this->getPDO()->lastInsertId(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment