Created
September 8, 2012 12:03
-
-
Save psykzz/3674232 to your computer and use it in GitHub Desktop.
Method chaining PDO abstract DB layer
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 | |
class dbLayer { | |
private $hostname; | |
private $username; | |
private $password; | |
private $dbname; | |
private $current_query; | |
private $validQuery; // while not perfect should be good enough to say if we have chosen a starter and finisher atleast. | |
private $param; | |
private $link; // DB link | |
/* | |
* @param string $hostname -- server db resides on | |
* @param string $username -- username to for server & database | |
* @param string $password -- password for the user | |
* @param string $dbname -- database to connect to. | |
* | |
* @return null -- returns nothing. | |
*/ | |
function __construct ($hostname,$username,$password,$dbname) { | |
//Connection details | |
$this->hostname = $hostname; | |
$this->username = $username; | |
$this->password = $password; | |
$this->dbname = $dbname; | |
//Default members | |
$this->_initQuery(); | |
} | |
function _initQuery() { | |
$this->current_query = ""; | |
$this->params = NULL; | |
$this->validQuery = false; | |
$this->param = NULL; | |
} | |
function _printQuery() { | |
echo "{$this->current_query} <br />Params: ";print_r($this->param); | |
} | |
function connect() { | |
$this->link = new PDO("mysql:host={$this->hostname};dbname={$this->dbname}", $this->username, $this->password); | |
return $this; | |
} | |
#Query Starters | |
function select() { | |
$this->_initQuery(); | |
$tblColumns = func_get_args(); | |
if (is_array($tblColumns)) | |
$args = implode("`, `", $tblColumns); | |
else | |
$args = $tblColumns; | |
$this->current_query = "SELECT `{$args}` "; | |
return $this; | |
} | |
#Query Finishers | |
function from($dbTable) { | |
if (empty($this->current_query)) { | |
$this->last_error = "Error with ".__FUNCTION__." : Check your starters before using finishers.<br />Current Query: {$this->current_query}"; | |
return false; | |
} | |
$this->current_query .= "FROM `{$dbTable}` "; | |
return $this; | |
} | |
function where ($data) { | |
if (empty($this->current_query)) { | |
$this->last_error = "Error with ".__FUNCTION__." : Check your starters and finishers.<br />Current Query: {$this->current_query}"; | |
return false; | |
} | |
//$params = array(':username' => 'test', ':email' => $mail, ':last_login' => time() - 3600); | |
$this->current_query .= "WHERE "; | |
foreach ($data as $k=>$v) { | |
$firstchar = substr($v, 0, 1); | |
if (in_array($firstchar, array("=","<",">","<>","=>","=<"))) { | |
$this->current_query .= "`{$k}` {$firstchar} :{$k}"; | |
$this->param[":{$k}"] = $v; | |
} else{ // default to equal to | |
$this->current_query .= "`{$k}` = :{$k}"; | |
$this->param[":{$k}"] = $v; | |
} | |
} | |
return $this; | |
} | |
function results ( $type='array' ) { | |
if (!$this->validQuery || empty($this->current_query)) { | |
$this->last_error = "Error with ".__FUNCTION__." : Check your starters and finishers.<br />Current Query: {$this->current_query}"; | |
return false; | |
} | |
$this->link->prepare($this->current_query); | |
$this->link->execute($this->params); | |
switch ($type) { | |
case "array": | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment