]> git.wh0rd.org - tt-rss.git/blob - classes/db.php
add some starting pdo glue
[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 if (!$this->pdo) {
35 print("Error connecting via PDO.");
36 exit(101);
37 }
38
39 $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
40
41 if (!$this->link) {
42 print("Error connecting through adapter: " . $this->adapter->last_error());
43 exit(101);
44 }
45
46 error_reporting($er);
47 }
48
49 private function __clone() {
50 //
51 }
52
53 public static function get() {
54 if (self::$instance == null)
55 self::$instance = new self();
56
57 return self::$instance;
58 }
59
60 static function quote($str){
61 return("'$str'");
62 }
63
64 function reconnect() {
65 $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
66 }
67
68 function connect($host, $user, $pass, $db, $port) {
69 //return $this->adapter->connect($host, $user, $pass, $db, $port);
70 return ;
71 }
72
73 function escape_string($s, $strip_tags = true) {
74 return $this->adapter->escape_string($s, $strip_tags);
75 }
76
77 function query($query, $die_on_error = true) {
78 return $this->adapter->query($query, $die_on_error);
79 }
80
81 function fetch_assoc($result) {
82 return $this->adapter->fetch_assoc($result);
83 }
84
85 function num_rows($result) {
86 return $this->adapter->num_rows($result);
87 }
88
89 function fetch_result($result, $row, $param) {
90 return $this->adapter->fetch_result($result, $row, $param);
91 }
92
93 function close() {
94 return $this->adapter->close();
95 }
96
97 function affected_rows($result) {
98 return $this->adapter->affected_rows($result);
99 }
100
101 function last_error() {
102 return $this->adapter->last_error();
103 }
104
105 function last_query_error() {
106 return $this->adapter->last_query_error();
107 }
108 }