]> git.wh0rd.org Git - tt-rss.git/commitdiff
Logger_SQL: use separate PDO connection
authorAndrew Dolgov <noreply@fakecake.org>
Mon, 10 Sep 2018 18:49:31 +0000 (21:49 +0300)
committerAndrew Dolgov <noreply@fakecake.org>
Mon, 10 Sep 2018 18:49:31 +0000 (21:49 +0300)
classes/db.php [changed mode: 0644->0755]
classes/logger/sql.php [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index e9d9e84..ac493f6
@@ -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;
old mode 100644 (file)
new mode 100755 (executable)
index 1205840..73552c1
@@ -1,21 +1,18 @@
 <?php
 class Logger_SQL {
 
+       private $pdo;
+
        function log_error($errno, $errstr, $file, $line, $context) {
-               
-               $pdo = Db::pdo();
-               
-               if ($pdo && get_schema_version() > 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
+}