]>
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 // this really shouldn't be used unless a separate PDO connection is needed
53 // normal usage is Db::pdo()->prepare(...) etc
54 public function pdo_connect() {
56 $db_port = defined('DB_PORT') && DB_PORT ?
';port=' . DB_PORT
: '';
57 $db_host = defined('DB_HOST') && DB_HOST ?
';host=' . DB_HOST
: '';
60 $pdo = new PDO(DB_TYPE
. ':dbname=' . DB_NAME
. $db_host . $db_port,
63 } catch (Exception
$e) {
64 print "<pre>Exception while creating PDO object:" . $e->getMessage() . "</pre>";
68 $pdo->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
70 if (DB_TYPE
== "pgsql") {
72 $pdo->query("set client_encoding = 'UTF-8'");
73 $pdo->query("set datestyle = 'ISO, european'");
74 $pdo->query("set TIME ZONE 0");
75 $pdo->query("set cpu_tuple_cost = 0.5");
77 } else if (DB_TYPE
== "mysql") {
78 $pdo->query("SET time_zone = '+0:0'");
80 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
81 $pdo->query("SET NAMES " . MYSQL_CHARSET
);
88 public static function instance() {
89 if (self
::$instance == null)
90 self
::$instance = new self();
92 return self
::$instance;
95 public static function get() {
96 if (self
::$instance == null)
97 self
::$instance = new self();
99 if (!self
::$instance->adapter
) {
100 self
::$instance->legacy_connect();
103 return self
::$instance->adapter
;
106 public static function pdo() {
107 if (self
::$instance == null)
108 self
::$instance = new self();
110 if (!self
::$instance->pdo
) {
111 self
::$instance->pdo
= self
::$instance->pdo_connect();
114 return self
::$instance->pdo
;