]> git.wh0rd.org - tt-rss.git/blob - classes/db.php
pluginhost: do not connect via legacy DB api until requested
[tt-rss.git] / classes / db.php
1 <?php
2 class Db implements IDb {
3
4 /* @var Db $instance */
5 private static $instance;
6
7 /* @var IDb $adapter */
8 private $adapter;
9
10 private $link;
11
12 /* @var PDO $pdo */
13 private $pdo;
14
15 private function __construct() {
16
17 }
18
19 private function __clone() {
20 //
21 }
22
23 private function legacy_connect() {
24
25 user_error("Legacy connect requested to " . DB_TYPE, E_USER_NOTICE);
26
27 $er = error_reporting(E_ALL);
28
29 switch (DB_TYPE) {
30 case "mysql":
31 $this->adapter = new Db_Mysqli();
32 break;
33 case "pgsql":
34 $this->adapter = new Db_Pgsql();
35 break;
36 default:
37 die("Unknown DB_TYPE: " . DB_TYPE);
38 }
39
40 if (!$this->adapter) {
41 print("Error initializing database adapter for " . DB_TYPE);
42 exit(100);
43 }
44
45 $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
46
47 if (!$this->link) {
48 print("Error connecting through adapter: " . $this->adapter->last_error());
49 exit(101);
50 }
51
52 error_reporting($er);
53 }
54
55 private function pdo_connect() {
56
57 $db_port = defined('DB_PORT') && DB_PORT ? ';port='.DB_PORT : '';
58
59 $this->pdo = new PDO(DB_TYPE . ':dbname='.DB_NAME.';host='.DB_HOST.$db_port,
60 DB_USER,
61 DB_PASS);
62
63 if (!$this->pdo) {
64 print("Error connecting via PDO.");
65 exit(101);
66 }
67
68 $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
69
70 if (DB_TYPE == "pgsql") {
71
72 $this->pdo->query("set client_encoding = 'UTF-8'");
73 $this->pdo->query("set datestyle = 'ISO, european'");
74 $this->pdo->query("set TIME ZONE 0");
75 $this->pdo->query("set cpu_tuple_cost = 0.5");
76
77 } else if (DB_TYPE == "mysql") {
78 $this->pdo->query("SET time_zone = '+0:0'");
79
80 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
81 $this->pdo->query("SET NAMES " . MYSQL_CHARSET);
82 }
83 }
84 }
85
86 public static function get() {
87 if (self::$instance == null)
88 self::$instance = new self();
89
90 if (!self::$instance->link) {
91 self::$instance->legacy_connect();
92 }
93
94 return self::$instance;
95 }
96
97 public static function pdo() {
98 if (self::$instance == null)
99 self::$instance = new self();
100
101 if (!self::$instance->pdo) {
102 self::$instance->pdo_connect();
103 }
104
105 return self::$instance->pdo;
106 }
107
108 static function quote($str){
109 return("'$str'");
110 }
111
112 function reconnect() {
113 $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
114 }
115
116 function connect($host, $user, $pass, $db, $port) {
117 //return $this->adapter->connect($host, $user, $pass, $db, $port);
118 return ;
119 }
120
121 function escape_string($s, $strip_tags = true) {
122 return $this->adapter->escape_string($s, $strip_tags);
123 }
124
125 function query($query, $die_on_error = true) {
126 return $this->adapter->query($query, $die_on_error);
127 }
128
129 function fetch_assoc($result) {
130 return $this->adapter->fetch_assoc($result);
131 }
132
133 function num_rows($result) {
134 return $this->adapter->num_rows($result);
135 }
136
137 function fetch_result($result, $row, $param) {
138 return $this->adapter->fetch_result($result, $row, $param);
139 }
140
141 function close() {
142 return $this->adapter->close();
143 }
144
145 function affected_rows($result) {
146 return $this->adapter->affected_rows($result);
147 }
148
149 function last_error() {
150 return $this->adapter->last_error();
151 }
152
153 function last_query_error() {
154 return $this->adapter->last_query_error();
155 }
156 }