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 {
|
2018-09-10 18:49:31 +00:00
|
|
|
$pdo = new PDO(DB_TYPE . ':dbname=' . DB_NAME . $db_host . $db_port,
|
2017-12-10 06:27:07 +00:00
|
|
|
DB_USER,
|
|
|
|
DB_PASS);
|
|
|
|
} 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
|
|
|
|
2017-12-01 19:14:54 +00:00
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
|
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
|
|
|
|
|
|
|
} else if (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
|
|
|
|
|
|
|
if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
|
2018-09-10 18:49:31 +00:00
|
|
|
$pdo->query("SET NAMES " . 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() {
|
|
|
|
if (DB_TYPE == "mysql") {
|
|
|
|
return "RAND()";
|
|
|
|
} else {
|
|
|
|
return "RANDOM()";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 09:30:26 +00:00
|
|
|
}
|