]> git.wh0rd.org Git - tt-rss.git/blob - classes/db.php
PDO: switch error reporting to exceptions
[tt-rss.git] / classes / db.php
1 <?php
2 class Db
3 {
4
5         /* @var Db $instance */
6         private static $instance;
7
8         /* @var IDb $adapter */
9         private $adapter;
10
11         private $link;
12
13         /* @var PDO $pdo */
14         private $pdo;
15
16         private function __clone() {
17                 //
18         }
19
20         private function legacy_connect() {
21
22                 user_error("Legacy connect requested to " . DB_TYPE, E_USER_NOTICE);
23
24                 $er = error_reporting(E_ALL);
25
26                 switch (DB_TYPE) {
27                         case "mysql":
28                                 $this->adapter = new Db_Mysqli();
29                                 break;
30                         case "pgsql":
31                                 $this->adapter = new Db_Pgsql();
32                                 break;
33                         default:
34                                 die("Unknown DB_TYPE: " . DB_TYPE);
35                 }
36
37                 if (!$this->adapter) {
38                         print("Error initializing database adapter for " . DB_TYPE);
39                         exit(100);
40                 }
41
42                 $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
43
44                 if (!$this->link) {
45                         print("Error connecting through adapter: " . $this->adapter->last_error());
46                         exit(101);
47                 }
48
49                 error_reporting($er);
50         }
51
52         private function pdo_connect() {
53
54                 $db_port = defined('DB_PORT') && DB_PORT ? ';port=' . DB_PORT : '';
55                 $db_host = defined('DB_HOST') && DB_HOST ? ';host=' . DB_HOST : '';
56
57                 try {
58                         $this->pdo = new PDO(DB_TYPE . ':dbname=' . DB_NAME . $db_host . $db_port,
59                                 DB_USER,
60                                 DB_PASS);
61                 } catch (Exception $e) {
62                         print "<pre>Exception while creating PDO object:" . $e->getMessage() . "</pre>";
63                         exit(101);
64                 }
65
66                 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
67
68                 if (DB_TYPE == "pgsql") {
69
70                         $this->pdo->query("set client_encoding = 'UTF-8'");
71                         $this->pdo->query("set datestyle = 'ISO, european'");
72                         $this->pdo->query("set TIME ZONE 0");
73                         $this->pdo->query("set cpu_tuple_cost = 0.5");
74
75                 } else if (DB_TYPE == "mysql") {
76                         $this->pdo->query("SET time_zone = '+0:0'");
77
78                         if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
79                                 $this->pdo->query("SET NAMES " . MYSQL_CHARSET);
80                         }
81                 }
82         }
83
84         public static function get() {
85                 if (self::$instance == null)
86                         self::$instance = new self();
87
88                 if (!self::$instance->adapter) {
89                         self::$instance->legacy_connect();
90                 }
91
92                 return self::$instance->adapter;
93         }
94
95         public static function pdo() {
96                 if (self::$instance == null)
97                         self::$instance = new self();
98
99                 if (!self::$instance->pdo) {
100                         self::$instance->pdo_connect();
101                 }
102
103                 return self::$instance->pdo;
104         }
105 }