From 99bda9cc12cd39907adc6b76dc3bee4e9241b52b Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 30 Nov 2017 10:47:42 +0300 Subject: [PATCH] add some starting pdo glue --- classes/db.php | 18 +++++--- classes/db/pdo.php | 100 --------------------------------------------- 2 files changed, 13 insertions(+), 105 deletions(-) delete mode 100644 classes/db/pdo.php diff --git a/classes/db.php b/classes/db.php index 3b71f3c8..22449250 100644 --- a/classes/db.php +++ b/classes/db.php @@ -3,15 +3,13 @@ class Db implements IDb { private static $instance; private $adapter; private $link; + private $pdo; private function __construct() { $er = error_reporting(E_ALL); - if (defined('_ENABLE_PDO') && _ENABLE_PDO && class_exists("PDO")) { - $this->adapter = new Db_PDO(); - } else { - switch (DB_TYPE) { + switch (DB_TYPE) { case "mysql": $this->adapter = new Db_Mysqli(); break; @@ -20,7 +18,6 @@ class Db implements IDb { break; default: die("Unknown DB_TYPE: " . DB_TYPE); - } } if (!$this->adapter) { @@ -28,6 +25,17 @@ class Db implements IDb { exit(100); } + $db_port = defined(DB_PORT) ? ';port='.DB_PORT : ''; + + $this->pdo = new PDO(DB_TYPE . ':dbname='.DB_NAME.';host='.DB_HOST.$db_port, + DB_USER, + DB_PASS); + + if (!$this->pdo) { + print("Error connecting via PDO."); + exit(101); + } + $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : ""); if (!$this->link) { diff --git a/classes/db/pdo.php b/classes/db/pdo.php deleted file mode 100644 index d3070fac..00000000 --- a/classes/db/pdo.php +++ /dev/null @@ -1,100 +0,0 @@ -pdo = new PDO($connstr, $user, $pass); - $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $this->init(); - } catch (PDOException $e) { - die($e->getMessage()); - } - - return $this->pdo; - } - - function escape_string($s, $strip_tags = true) { - if ($strip_tags) $s = strip_tags($s); - - $qs = $this->pdo->quote($s); - - return mb_substr($qs, 1, mb_strlen($qs)-2); - } - - function query($query, $die_on_error = true) { - try { - return new Db_Stmt($this->pdo->query($query)); - } catch (PDOException $e) { - user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING); - } - } - - function fetch_assoc($result) { - try { - if ($result) { - return $result->fetch(); - } else { - return null; - } - } catch (PDOException $e) { - user_error($e->getMessage(), E_USER_WARNING); - } - } - - function num_rows($result) { - try { - if ($result) { - return $result->rowCount(); - } else { - return false; - } - } catch (PDOException $e) { - user_error($e->getMessage(), E_USER_WARNING); - } - } - - function fetch_result($result, $row, $param) { - return $result->fetch_result($row, $param); - } - - function close() { - $this->pdo = null; - } - - function affected_rows($result) { - try { - if ($result) { - return $result->rowCount(); - } else { - return null; - } - } catch (PDOException $e) { - user_error($e->getMessage(), E_USER_WARNING); - } - } - - function last_error() { - return join(" ", $this->pdo->errorInfo()); - } - - function init() { - switch (DB_TYPE) { - case "pgsql": - $this->query("set client_encoding = 'UTF-8'"); - $this->query("set datestyle = 'ISO, european'"); - $this->query("set TIME ZONE 0"); - return; - case "mysql": - $this->query("SET time_zone = '+0:0'"); - return; - } - - return true; - } - -} \ No newline at end of file -- 2.39.2