From bb84330234c649fb0a8726e488fca012f5295ce4 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 10 Sep 2018 21:49:31 +0300 Subject: [PATCH] Logger_SQL: use separate PDO connection --- classes/db.php | 31 +++++++++++++++++++++---------- classes/logger/sql.php | 19 ++++++++----------- 2 files changed, 29 insertions(+), 21 deletions(-) mode change 100644 => 100755 classes/db.php mode change 100644 => 100755 classes/logger/sql.php diff --git a/classes/db.php b/classes/db.php old mode 100644 new mode 100755 index e9d9e841..ac493f6d --- a/classes/db.php +++ b/classes/db.php @@ -49,13 +49,15 @@ 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 : ''; $db_host = defined('DB_HOST') && DB_HOST ? ';host=' . DB_HOST : ''; try { - $this->pdo = new PDO(DB_TYPE . ':dbname=' . DB_NAME . $db_host . $db_port, + $pdo = new PDO(DB_TYPE . ':dbname=' . DB_NAME . $db_host . $db_port, DB_USER, DB_PASS); } catch (Exception $e) { @@ -63,22 +65,31 @@ class Db exit(101); } - $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $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() { @@ -97,7 +108,7 @@ 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; diff --git a/classes/logger/sql.php b/classes/logger/sql.php old mode 100644 new mode 100755 index 12058401..73552c14 --- a/classes/logger/sql.php +++ b/classes/logger/sql.php @@ -1,21 +1,18 @@ 117) { - try { - $pdo->rollBack(); - } catch (Exception $e) { - // - } + // separate PDO connection object is used for logging + if (!$this->pdo) $this->pdo = Db::instance()->pdo_connect(); + + if ($this->pdo && get_schema_version() > 117) { $owner_uid = $_SESSION["uid"] ? $_SESSION["uid"] : null; - $sth = $pdo->prepare("INSERT INTO ttrss_error_log + $sth = $this->pdo->prepare("INSERT INTO ttrss_error_log (errno, errstr, filename, lineno, context, owner_uid, created_at) VALUES (?, ?, ?, ?, ?, ?, NOW())"); $sth->execute([$errno, $errstr, $file, $line, $context, $owner_uid]); @@ -26,4 +23,4 @@ class Logger_SQL { return false; } -} \ No newline at end of file +} -- 2.39.5