2 class Db implements IDb {
3 private static $instance;
8 private function __construct() {
10 $er = error_reporting(E_ALL);
14 $this->adapter = new Db_Mysqli();
17 $this->adapter = new Db_Pgsql();
20 die("Unknown DB_TYPE: " . DB_TYPE);
23 if (!$this->adapter) {
24 print("Error initializing database adapter for " . DB_TYPE);
28 $db_port = defined(DB_PORT) ? ';port='.DB_PORT : '';
30 $this->pdo = new PDO(DB_TYPE . ':dbname='.DB_NAME.';host='.DB_HOST.$db_port,
35 print("Error connecting via PDO.");
39 $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
42 print("Error connecting through adapter: " . $this->adapter->last_error());
49 private function __clone() {
53 public static function get() {
54 if (self::$instance == null)
55 self::$instance = new self();
57 return self::$instance;
60 public static function pdo() {
61 if (self::$instance == null)
62 self::$instance = new self();
64 return self::$instance->pdo;
67 static function quote($str){
71 function reconnect() {
72 $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
75 function connect($host, $user, $pass, $db, $port) {
76 //return $this->adapter->connect($host, $user, $pass, $db, $port);
80 function escape_string($s, $strip_tags = true) {
81 return $this->adapter->escape_string($s, $strip_tags);
84 function query($query, $die_on_error = true) {
85 return $this->adapter->query($query, $die_on_error);
88 function fetch_assoc($result) {
89 return $this->adapter->fetch_assoc($result);
92 function num_rows($result) {
93 return $this->adapter->num_rows($result);
96 function fetch_result($result, $row, $param) {
97 return $this->adapter->fetch_result($result, $row, $param);
101 return $this->adapter->close();
104 function affected_rows($result) {
105 return $this->adapter->affected_rows($result);
108 function last_error() {
109 return $this->adapter->last_error();
112 function last_query_error() {
113 return $this->adapter->last_query_error();