Created
May 10, 2017 22:16
-
-
Save benphelps/8187ca4f3e6e2a39c6d7f4a60af58638 to your computer and use it in GitHub Desktop.
PDO Abstraction
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 PDG\Database; | |
use PDO; | |
/** | |
* An actually simple PDO wrapper. Woah, dude. | |
*/ | |
class Abstraction | |
{ | |
public $pdo; | |
private $qCount; | |
private $qLog; | |
public function __construct($adapter, ...$args) | |
{ | |
$adapter_class = 'PDG\Database\Adapters\\' . $adapter; | |
$this->pdo = (new $adapter_class(...$args))->pdo; | |
} | |
private function stringToType($type) | |
{ | |
return [ | |
'string' => PDO::PARAM_STR , 'str' => PDO::PARAM_STR, | |
'integer' => PDO::PARAM_INT , 'int' => PDO::PARAM_INT, | |
'bool' => PDO::PARAM_BOOL, 'null' => PDO::PARAM_NULL | |
][$type]; | |
} | |
public function query($sql, ...$params) | |
{ | |
$index = 0; | |
$binding = false; | |
$stmt = $this->pdo->prepare($sql); | |
foreach ($params as $param) { | |
$index += 1; | |
if (is_array($param)) { | |
$binding = true; | |
$stmt->bindParam($index, $param[1], $this->stringToType($param[0])); | |
} | |
} | |
try { | |
$status = $stmt->execute(( $binding ? null : $params )); | |
} catch (PDOException $e) { | |
echo "Query Error: " . $e->getMessage(); | |
} | |
$this->qCount++; | |
$this->qLog[] = ['sql' => $sql, 'params' => $params]; | |
return $stmt; | |
} | |
public function queryCount() | |
{ | |
return $this->qCount; | |
} | |
public function queryLog() | |
{ | |
return $this->qLog; | |
} | |
} |
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 PDG\Database\Adapters; | |
use PDO; | |
/** | |
* An actually simple MSSQL adapter for our actually simple PDO wrapper. | |
*/ | |
class MicrosoftSQL | |
{ | |
public $pdo; | |
public function __construct($server, $username, $password, $database) | |
{ | |
try { | |
$options = array( | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, | |
PDO::ATTR_EMULATE_PREPARES => true, | |
); | |
$this->pdo = new PDO("dblib:host={$server};dbname={$database}", $username, $password, $options); | |
} catch (PDOException $e) { | |
echo "There was a problem connecting. " . $e->getMessage(); | |
} | |
} | |
} |
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 PDG\Database\Adapters; | |
use PDO; | |
/** | |
* An actually simple MySQL adapter for our actually simple PDO wrapper. | |
*/ | |
class MySQL | |
{ | |
public $pdo; | |
public function __construct($server, $username, $password, $database) | |
{ | |
try { | |
$options = array( | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ | |
); | |
$this->pdo = new PDO("mysql:host={$server};dbname={$database}", $username, $password, $options); | |
} catch (PDOException $e) { | |
echo "There was a problem connecting. " . $e->getMessage(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment