Update DB class to PSR-2

This commit is contained in:
pxgamer
2018-05-29 15:37:05 +01:00
parent 44c9eb5ce6
commit d8f1f4ea57

View File

@@ -1,20 +1,25 @@
<?php <?php
// a simple wrapper for pdo
class db extends PDO {
/**
* Class DB
*
* A simple wrapper for PDO.
*/
class DB extends PDO
{
private $error; private $error;
private $sql; private $sql;
private $bind; private $bind;
private $debugger = 0; private $debugger = 0;
public $working = "yes"; public $working = "yes";
public function __construct($dsn, $user="", $passwd="",$debug_level=0) { public function __construct($dsn, $user = "", $passwd = "", $debug_level = 0)
$options = array( {
$options = [
PDO::ATTR_PERSISTENT => true, PDO::ATTR_PERSISTENT => true,
PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
); ];
$this->debugger = $debug_level; $this->debugger = $debug_level;
try { try {
parent::__construct($dsn, $user, $passwd, $options); parent::__construct($dsn, $user, $passwd, $options);
@@ -24,51 +29,58 @@ class db extends PDO {
} }
} }
private function debug() { private function debug()
if(!$this->debugger) return; {
$error = array("Error" => $this->error); if (!$this->debugger) {
if(!empty($this->sql)) return;
}
$error = ["Error" => $this->error];
if (!empty($this->sql)) {
$error["SQL Statement"] = $this->sql; $error["SQL Statement"] = $this->sql;
if(!empty($this->bind)) }
if (!empty($this->bind)) {
$error["Bind Parameters"] = trim(print_r($this->bind, true)); $error["Bind Parameters"] = trim(print_r($this->bind, true));
}
$backtrace = debug_backtrace(); $backtrace = debug_backtrace();
if (!empty($backtrace)) { if (!empty($backtrace)) {
foreach ($backtrace as $info) { foreach ($backtrace as $info) {
if($info["file"] != __FILE__) if ($info["file"] != __FILE__) {
$error["Backtrace"] = $info["file"]." at line ".$info["line"]; $error["Backtrace"] = $info["file"]." at line ".$info["line"];
} }
} }
}
$msg = ""; $msg = "";
$msg .= "SQL Error\n".str_repeat("-", 50); $msg .= "SQL Error\n".str_repeat("-", 50);
foreach($error as $key => $val) foreach ($error as $key => $val) {
$msg .= "\n\n$key:\n$val"; $msg .= "\n\n$key:\n$val";
}
if ($this->debugger) { if ($this->debugger) {
echo nl2br($msg); echo nl2br($msg);
} }
} }
private function cleanup($bind,$sql="") { private function cleanup($bind, $sql = "")
{
if (!is_array($bind)) { if (!is_array($bind)) {
if(!empty($bind)) if (!empty($bind)) {
$bind = array($bind); $bind = [$bind];
else } else {
$bind = array(); $bind = [];
}
} }
foreach ($bind as $key => $val) { foreach ($bind as $key => $val) {
if(str_replace($key,"",$sql)==$sql) unset($bind[$key]); if (str_replace($key, "", $sql) == $sql) {
unset($bind[$key]);
}
} }
return $bind; return $bind;
} }
public function single($sql, $bind = "")
{
public function single($sql,$bind="") {
$this->sql = trim($sql); $this->sql = trim($sql);
$this->bind = $this->cleanup($bind, $sql); $this->bind = $this->cleanup($bind, $sql);
$this->error = ""; $this->error = "";
@@ -84,8 +96,8 @@ class db extends PDO {
} }
} }
public function run($sql, $bind = "")
public function run($sql, $bind="") { {
$this->sql = trim($sql); $this->sql = trim($sql);
$this->bind = $this->cleanup($bind, $sql); $this->bind = $this->cleanup($bind, $sql);
$this->error = ""; $this->error = "";
@@ -93,11 +105,12 @@ class db extends PDO {
try { try {
$pdostmt = $this->prepare($this->sql); $pdostmt = $this->prepare($this->sql);
if ($pdostmt->execute($this->bind) !== false) { if ($pdostmt->execute($this->bind) !== false) {
if(preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql)) if (preg_match("/^(".implode("|", ["select", "describe", "pragma"]).") /i", $this->sql)) {
return $pdostmt->fetchAll(PDO::FETCH_ASSOC); return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
elseif(preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this->sql)) } elseif (preg_match("/^(".implode("|", ["delete", "insert", "update"]).") /i", $this->sql)) {
return $pdostmt->rowCount(); return $pdostmt->rowCount();
} }
}
} catch (PDOException $e) { } catch (PDOException $e) {
$this->error = $e->getMessage(); $this->error = $e->getMessage();
$this->debug(); $this->debug();
@@ -105,18 +118,20 @@ class db extends PDO {
} }
} }
public function row($sql,$bind=""){ public function row($sql, $bind = "")
{
$query = $this->run($sql, $bind); $query = $this->run($sql, $bind);
if(count($query)==0) return false; if (count($query) == 0) {
if(count($query)>1) return $query; return false;
}
if (count($query) > 1) {
return $query;
}
if (count($query) == 1) { if (count($query) == 1) {
foreach($query as $row) $result=$row; foreach ($query as $row) {
$result = $row;
}
return $result; return $result;
} }
} }
} }
?>