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