]>
Commit | Line | Data |
---|---|---|
95947917 AD |
1 | <?php |
2 | class Db implements IDb { | |
3 | private static $instance; | |
4 | private $adapter; | |
404e2e36 | 5 | private $link; |
99bda9cc | 6 | private $pdo; |
95947917 AD |
7 | |
8 | private function __construct() { | |
e441b583 AD |
9 | |
10 | $er = error_reporting(E_ALL); | |
11 | ||
99bda9cc | 12 | switch (DB_TYPE) { |
e441b583 | 13 | case "mysql": |
e54eb40a | 14 | $this->adapter = new Db_Mysqli(); |
e441b583 AD |
15 | break; |
16 | case "pgsql": | |
17 | $this->adapter = new Db_Pgsql(); | |
18 | break; | |
19 | default: | |
20 | die("Unknown DB_TYPE: " . DB_TYPE); | |
95947917 AD |
21 | } |
22 | ||
3c111597 AD |
23 | if (!$this->adapter) { |
24 | print("Error initializing database adapter for " . DB_TYPE); | |
25 | exit(100); | |
26 | } | |
e441b583 | 27 | |
99bda9cc AD |
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 | ||
2e46b434 AD |
34 | $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); |
35 | ||
99bda9cc AD |
36 | if (!$this->pdo) { |
37 | print("Error connecting via PDO."); | |
38 | exit(101); | |
39 | } | |
40 | ||
dd90eefa AD |
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 | ||
1daf0f75 | 56 | $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : ""); |
e441b583 AD |
57 | |
58 | if (!$this->link) { | |
3c111597 AD |
59 | print("Error connecting through adapter: " . $this->adapter->last_error()); |
60 | exit(101); | |
e441b583 AD |
61 | } |
62 | ||
63 | error_reporting($er); | |
95947917 AD |
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 | ||
8adb3ec4 AD |
77 | public static function pdo() { |
78 | if (self::$instance == null) | |
79 | self::$instance = new self(); | |
80 | ||
81 | return self::$instance->pdo; | |
82 | } | |
83 | ||
95947917 AD |
84 | static function quote($str){ |
85 | return("'$str'"); | |
86 | } | |
87 | ||
e2cf81e2 AD |
88 | function reconnect() { |
89 | $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : ""); | |
90 | } | |
91 | ||
95947917 AD |
92 | function connect($host, $user, $pass, $db, $port) { |
93 | //return $this->adapter->connect($host, $user, $pass, $db, $port); | |
6322ac79 | 94 | return ; |
95947917 AD |
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 | ||
977cea14 AD |
129 | function last_query_error() { |
130 | return $this->adapter->last_query_error(); | |
131 | } | |
ea79a0e0 | 132 | } |