ttrss/classes/db.php

100 lines
2.6 KiB
PHP
Raw Normal View History

<?php
class Db
{
2021-03-01 15:36:47 +00:00
/** @var Db $instance */
private static $instance;
/** @var PDO|null $pdo */
2017-11-30 07:47:42 +00:00
private $pdo;
2021-03-01 15:36:47 +00:00
function __construct() {
ORM::configure(self::get_dsn());
ORM::configure('username', Config::get(Config::DB_USER));
ORM::configure('password', Config::get(Config::DB_PASS));
ORM::configure('return_result_sets', true);
if (Config::get(Config::DB_TYPE) == "mysql" && Config::get(Config::MYSQL_CHARSET)) {
ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . Config::get(Config::MYSQL_CHARSET)));
}
2021-03-01 15:36:47 +00:00
}
static function NOW() {
return date("Y-m-d H:i:s", time());
}
private function __clone() {
//
}
2021-03-01 15:36:47 +00:00
public static function get_dsn() {
$db_port = Config::get(Config::DB_PORT) ? ';port=' . Config::get(Config::DB_PORT) : '';
$db_host = Config::get(Config::DB_HOST) ? ';host=' . Config::get(Config::DB_HOST) : '';
if (Config::get(Config::DB_TYPE) == "mysql" && Config::get(Config::MYSQL_CHARSET)) {
$db_charset = ';charset=' . Config::get(Config::MYSQL_CHARSET);
} else {
$db_charset = '';
}
2017-11-30 07:47:42 +00:00
return Config::get(Config::DB_TYPE) . ':dbname=' . Config::get(Config::DB_NAME) . $db_host . $db_port . $db_charset;
2021-03-01 15:36:47 +00:00
}
// this really shouldn't be used unless a separate PDO connection is needed
// normal usage is Db::pdo()->prepare(...) etc
public function pdo_connect() : PDO {
2017-12-10 06:27:07 +00:00
try {
2021-03-01 15:36:47 +00:00
$pdo = new PDO(self::get_dsn(),
2021-02-22 18:47:48 +00:00
Config::get(Config::DB_USER),
Config::get(Config::DB_PASS));
2017-12-10 06:27:07 +00:00
} catch (Exception $e) {
print "<pre>Exception while creating PDO object:" . $e->getMessage() . "</pre>";
2017-11-30 07:47:42 +00:00
exit(101);
}
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2021-02-22 18:47:48 +00:00
if (Config::get(Config::DB_TYPE) == "pgsql") {
$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");
2021-02-22 18:47:48 +00:00
} else if (Config::get(Config::DB_TYPE) == "mysql") {
$pdo->query("SET time_zone = '+0:0'");
2021-02-23 06:01:27 +00:00
if (Config::get(Config::MYSQL_CHARSET)) {
2021-02-22 19:35:27 +00:00
$pdo->query("SET NAMES " . Config::get(Config::MYSQL_CHARSET));
}
}
return $pdo;
}
2021-03-01 15:36:47 +00:00
public static function instance() : Db {
if (self::$instance == null)
self::$instance = new self();
return self::$instance;
}
2021-02-08 18:11:56 +00:00
public static function pdo() : PDO {
if (self::$instance == null)
self::$instance = new self();
2017-11-30 09:28:02 +00:00
2021-03-01 15:36:47 +00:00
if (empty(self::$instance->pdo)) {
self::$instance->pdo = self::$instance->pdo_connect();
}
return self::$instance->pdo;
}
public static function sql_random_function() {
2021-02-22 18:47:48 +00:00
if (Config::get(Config::DB_TYPE) == "mysql") {
return "RAND()";
} else {
return "RANDOM()";
}
}
}