]>
git.wh0rd.org - tt-rss.git/blob - classes/db.php
5 /* @var Db $instance */
6 private static $instance;
8 /* @var IDb $adapter */
16 private function __clone() {
20 private function legacy_connect() {
22 user_error("Legacy connect requested to " . DB_TYPE
, E_USER_NOTICE
);
24 $er = error_reporting(E_ALL
);
28 $this->adapter
= new Db_Mysqli();
31 $this->adapter
= new Db_Pgsql();
34 die("Unknown DB_TYPE: " . DB_TYPE
);
37 if (!$this->adapter
) {
38 print("Error initializing database adapter for " . DB_TYPE
);
42 $this->link
= $this->adapter
->connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
, defined('DB_PORT') ? DB_PORT
: "");
45 print("Error connecting through adapter: " . $this->adapter
->last_error());
52 private function pdo_connect() {
54 $db_port = defined('DB_PORT') && DB_PORT ?
';port=' . DB_PORT
: '';
56 $this->pdo
= new PDO(DB_TYPE
. ':dbname=' . DB_NAME
. ';host=' . DB_HOST
. $db_port,
61 print("Error connecting via PDO.");
65 $this->pdo
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_WARNING
);
67 if (DB_TYPE
== "pgsql") {
69 $this->pdo
->query("set client_encoding = 'UTF-8'");
70 $this->pdo
->query("set datestyle = 'ISO, european'");
71 $this->pdo
->query("set TIME ZONE 0");
72 $this->pdo
->query("set cpu_tuple_cost = 0.5");
74 } else if (DB_TYPE
== "mysql") {
75 $this->pdo
->query("SET time_zone = '+0:0'");
77 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
78 $this->pdo
->query("SET NAMES " . MYSQL_CHARSET
);
83 public static function get() {
84 if (self
::$instance == null)
85 self
::$instance = new self();
87 if (!self
::$instance->adapter
) {
88 self
::$instance->legacy_connect();
91 return self
::$instance->adapter
;
94 public static function pdo() {
95 if (self
::$instance == null)
96 self
::$instance = new self();
98 if (!self
::$instance->pdo
) {
99 self
::$instance->pdo_connect();
102 return self
::$instance->pdo
;