db.php 2.9 KB

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