2013-04-17 09:08:36 +00:00
|
|
|
<?php
|
2017-12-03 11:54:15 +00:00
|
|
|
class Db
|
|
|
|
{
|
2017-12-03 11:49:18 +00:00
|
|
|
/* @var Db $instance */
|
2013-04-17 09:08:36 +00:00
|
|
|
private static $instance;
|
2017-12-03 11:49:18 +00:00
|
|
|
|
2013-04-17 11:36:34 +00:00
|
|
|
private $link;
|
2017-12-03 11:49:18 +00:00
|
|
|
|
|
|
|
/* @var PDO $pdo */
|
2017-11-30 07:47:42 +00:00
|
|
|
private $pdo;
|
2013-04-17 09:08:36 +00:00
|
|
|
|
2017-12-03 11:49:18 +00:00
|
|
|
private function __clone() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:49:31 +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() {
|
2017-12-03 11:49:18 +00:00
|
|
|
|
2017-12-03 11:54:15 +00:00
|
|
|
$db_port = defined('DB_PORT') && DB_PORT ? ';port=' . DB_PORT : '';
|
2017-12-10 06:20:52 +00:00
|
|
|
$db_host = defined('DB_HOST') && DB_HOST ? ';host=' . DB_HOST : '';
|
2017-11-30 07:47:42 +00:00
|
|
|
|
2017-12-10 06:27:07 +00:00
|
|
|
try {
|
2021-02-22 18:47:48 +00:00
|
|
|
$pdo = new PDO(Config::get(Config::DB_TYPE) . ':dbname=' . Config::get(Config::DB_NAME) . $db_host . $db_port,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:49:31 +00:00
|
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
2017-12-03 11:49:18 +00:00
|
|
|
|
2021-02-22 18:47:48 +00:00
|
|
|
if (Config::get(Config::DB_TYPE) == "pgsql") {
|
2017-12-01 19:14:54 +00:00
|
|
|
|
2018-09-10 18:49:31 +00:00
|
|
|
$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");
|
2017-12-01 19:14:54 +00:00
|
|
|
|
2021-02-22 18:47:48 +00:00
|
|
|
} else if (Config::get(Config::DB_TYPE) == "mysql") {
|
2018-09-10 18:49:31 +00:00
|
|
|
$pdo->query("SET time_zone = '+0:0'");
|
2017-12-01 19:14:54 +00:00
|
|
|
|
2021-02-22 19:35:27 +00:00
|
|
|
if (defined('Config::get(Config::MYSQL_CHARSET)') && Config::get(Config::MYSQL_CHARSET)) {
|
|
|
|
$pdo->query("SET NAMES " . Config::get(Config::MYSQL_CHARSET));
|
2017-12-01 19:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 18:49:31 +00:00
|
|
|
|
|
|
|
return $pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function instance() {
|
|
|
|
if (self::$instance == null)
|
|
|
|
self::$instance = new self();
|
|
|
|
|
|
|
|
return self::$instance;
|
2013-04-17 09:08:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 18:11:56 +00:00
|
|
|
public static function pdo() : PDO {
|
2017-12-03 11:54:15 +00:00
|
|
|
if (self::$instance == null)
|
|
|
|
self::$instance = new self();
|
2017-11-30 09:28:02 +00:00
|
|
|
|
2017-12-03 11:54:15 +00:00
|
|
|
if (!self::$instance->pdo) {
|
2018-09-10 18:49:31 +00:00
|
|
|
self::$instance->pdo = self::$instance->pdo_connect();
|
2017-12-03 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 11:54:15 +00:00
|
|
|
return self::$instance->pdo;
|
2016-04-26 17:04:24 +00:00
|
|
|
}
|
2020-09-22 06:04:33 +00:00
|
|
|
|
|
|
|
public static function sql_random_function() {
|
2021-02-22 18:47:48 +00:00
|
|
|
if (Config::get(Config::DB_TYPE) == "mysql") {
|
2020-09-22 06:04:33 +00:00
|
|
|
return "RAND()";
|
|
|
|
} else {
|
|
|
|
return "RANDOM()";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 09:30:26 +00:00
|
|
|
}
|