]> git.wh0rd.org - tt-rss.git/blame - classes/db.php
pluginhost: do not connect via legacy DB api until requested
[tt-rss.git] / classes / db.php
CommitLineData
95947917
AD
1<?php
2class Db implements IDb {
df5d2a06
AD
3
4 /* @var Db $instance */
95947917 5 private static $instance;
df5d2a06
AD
6
7 /* @var IDb $adapter */
95947917 8 private $adapter;
df5d2a06 9
404e2e36 10 private $link;
df5d2a06
AD
11
12 /* @var PDO $pdo */
99bda9cc 13 private $pdo;
95947917
AD
14
15 private function __construct() {
e441b583 16
df5d2a06
AD
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
e441b583
AD
27 $er = error_reporting(E_ALL);
28
99bda9cc 29 switch (DB_TYPE) {
e441b583 30 case "mysql":
e54eb40a 31 $this->adapter = new Db_Mysqli();
e441b583
AD
32 break;
33 case "pgsql":
34 $this->adapter = new Db_Pgsql();
35 break;
36 default:
37 die("Unknown DB_TYPE: " . DB_TYPE);
95947917
AD
38 }
39
3c111597
AD
40 if (!$this->adapter) {
41 print("Error initializing database adapter for " . DB_TYPE);
42 exit(100);
43 }
e441b583 44
df5d2a06
AD
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
28040002 57 $db_port = defined('DB_PORT') && DB_PORT ? ';port='.DB_PORT : '';
99bda9cc
AD
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
df5d2a06
AD
68 $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
69
dd90eefa
AD
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 }
95947917
AD
84 }
85
86 public static function get() {
87 if (self::$instance == null)
88 self::$instance = new self();
89
df5d2a06
AD
90 if (!self::$instance->link) {
91 self::$instance->legacy_connect();
92 }
93
95947917
AD
94 return self::$instance;
95 }
96
df5d2a06 97 public static function pdo() {
8adb3ec4
AD
98 if (self::$instance == null)
99 self::$instance = new self();
100
df5d2a06
AD
101 if (!self::$instance->pdo) {
102 self::$instance->pdo_connect();
103 }
104
8adb3ec4
AD
105 return self::$instance->pdo;
106 }
107
95947917
AD
108 static function quote($str){
109 return("'$str'");
110 }
111
e2cf81e2
AD
112 function reconnect() {
113 $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
114 }
115
95947917
AD
116 function connect($host, $user, $pass, $db, $port) {
117 //return $this->adapter->connect($host, $user, $pass, $db, $port);
6322ac79 118 return ;
95947917
AD
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
977cea14
AD
153 function last_query_error() {
154 return $this->adapter->last_query_error();
155 }
ea79a0e0 156}