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) {
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() {
self::$instance = new self();
if (!self::$instance->pdo) {
- self::$instance->pdo_connect();
+ self::$instance->pdo = self::$instance->pdo_connect();
}
return self::$instance->pdo;
<?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]);
return false;
}
-}
\ No newline at end of file
+}