]>
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 | ||
1daf0f75 | 41 | $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : ""); |
e441b583 AD |
42 | |
43 | if (!$this->link) { | |
3c111597 AD |
44 | print("Error connecting through adapter: " . $this->adapter->last_error()); |
45 | exit(101); | |
e441b583 AD |
46 | } |
47 | ||
48 | error_reporting($er); | |
95947917 AD |
49 | } |
50 | ||
51 | private function __clone() { | |
52 | // | |
53 | } | |
54 | ||
55 | public static function get() { | |
56 | if (self::$instance == null) | |
57 | self::$instance = new self(); | |
58 | ||
59 | return self::$instance; | |
60 | } | |
61 | ||
8adb3ec4 AD |
62 | public static function pdo() { |
63 | if (self::$instance == null) | |
64 | self::$instance = new self(); | |
65 | ||
66 | return self::$instance->pdo; | |
67 | } | |
68 | ||
95947917 AD |
69 | static function quote($str){ |
70 | return("'$str'"); | |
71 | } | |
72 | ||
e2cf81e2 AD |
73 | function reconnect() { |
74 | $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : ""); | |
75 | } | |
76 | ||
95947917 AD |
77 | function connect($host, $user, $pass, $db, $port) { |
78 | //return $this->adapter->connect($host, $user, $pass, $db, $port); | |
6322ac79 | 79 | return ; |
95947917 AD |
80 | } |
81 | ||
82 | function escape_string($s, $strip_tags = true) { | |
83 | return $this->adapter->escape_string($s, $strip_tags); | |
84 | } | |
85 | ||
86 | function query($query, $die_on_error = true) { | |
87 | return $this->adapter->query($query, $die_on_error); | |
88 | } | |
89 | ||
90 | function fetch_assoc($result) { | |
91 | return $this->adapter->fetch_assoc($result); | |
92 | } | |
93 | ||
94 | function num_rows($result) { | |
95 | return $this->adapter->num_rows($result); | |
96 | } | |
97 | ||
98 | function fetch_result($result, $row, $param) { | |
99 | return $this->adapter->fetch_result($result, $row, $param); | |
100 | } | |
101 | ||
102 | function close() { | |
103 | return $this->adapter->close(); | |
104 | } | |
105 | ||
106 | function affected_rows($result) { | |
107 | return $this->adapter->affected_rows($result); | |
108 | } | |
109 | ||
110 | function last_error() { | |
111 | return $this->adapter->last_error(); | |
112 | } | |
113 | ||
977cea14 AD |
114 | function last_query_error() { |
115 | return $this->adapter->last_query_error(); | |
116 | } | |
ea79a0e0 | 117 | } |