X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=classes%2Fdb.php;h=ac493f6d6a66c65ee27929a1a18ec7d0920bdfd8;hb=bb84330234c649fb0a8726e488fca012f5295ce4;hp=3f3c12680751c39703ff99e15fe54841405af40a;hpb=2f0623c9a5045eee4c7eddbb4e2b2bdfe533eb9a;p=tt-rss.git diff --git a/classes/db.php b/classes/db.php old mode 100644 new mode 100755 index 3f3c1268..ac493f6d --- a/classes/db.php +++ b/classes/db.php @@ -49,35 +49,47 @@ class Db error_reporting($er); } - private function pdo_connect() { + // this really shouldn't be used unless a separate PDO connection is needed + // normal usage is Db::pdo()->prepare(...) etc + public function pdo_connect() { $db_port = defined('DB_PORT') && 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."); + $db_host = defined('DB_HOST') && DB_HOST ? ';host=' . DB_HOST : ''; + + try { + $pdo = new PDO(DB_TYPE . ':dbname=' . DB_NAME . $db_host . $db_port, + DB_USER, + DB_PASS); + } catch (Exception $e) { + print "
Exception while creating PDO object:" . $e->getMessage() . "
"; exit(101); } - $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if (DB_TYPE == "pgsql") { - $this->pdo->query("set client_encoding = 'UTF-8'"); - $this->pdo->query("set datestyle = 'ISO, european'"); - $this->pdo->query("set TIME ZONE 0"); - $this->pdo->query("set cpu_tuple_cost = 0.5"); + $pdo->query("set client_encoding = 'UTF-8'"); + $pdo->query("set datestyle = 'ISO, european'"); + $pdo->query("set TIME ZONE 0"); + $pdo->query("set cpu_tuple_cost = 0.5"); } else if (DB_TYPE == "mysql") { - $this->pdo->query("SET time_zone = '+0:0'"); + $pdo->query("SET time_zone = '+0:0'"); if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - $this->pdo->query("SET NAMES " . MYSQL_CHARSET); + $pdo->query("SET NAMES " . MYSQL_CHARSET); } } + + return $pdo; + } + + public static function instance() { + if (self::$instance == null) + self::$instance = new self(); + + return self::$instance; } public static function get() { @@ -96,9 +108,9 @@ class Db self::$instance = new self(); if (!self::$instance->pdo) { - self::$instance->pdo_connect(); + self::$instance->pdo = self::$instance->pdo_connect(); } return self::$instance->pdo; } -} \ No newline at end of file +}