db.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. if (!$this->pdo) {
  28. print("Error connecting via PDO.");
  29. exit(101);
  30. }
  31. $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
  32. if (!$this->link) {
  33. print("Error connecting through adapter: " . $this->adapter->last_error());
  34. exit(101);
  35. }
  36. error_reporting($er);
  37. }
  38. private function __clone() {
  39. //
  40. }
  41. public static function get() {
  42. if (self::$instance == null)
  43. self::$instance = new self();
  44. return self::$instance;
  45. }
  46. public static function pdo() {
  47. if (self::$instance == null)
  48. self::$instance = new self();
  49. return self::$instance->pdo;
  50. }
  51. static function quote($str){
  52. return("'$str'");
  53. }
  54. function reconnect() {
  55. $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
  56. }
  57. function connect($host, $user, $pass, $db, $port) {
  58. //return $this->adapter->connect($host, $user, $pass, $db, $port);
  59. return ;
  60. }
  61. function escape_string($s, $strip_tags = true) {
  62. return $this->adapter->escape_string($s, $strip_tags);
  63. }
  64. function query($query, $die_on_error = true) {
  65. return $this->adapter->query($query, $die_on_error);
  66. }
  67. function fetch_assoc($result) {
  68. return $this->adapter->fetch_assoc($result);
  69. }
  70. function num_rows($result) {
  71. return $this->adapter->num_rows($result);
  72. }
  73. function fetch_result($result, $row, $param) {
  74. return $this->adapter->fetch_result($result, $row, $param);
  75. }
  76. function close() {
  77. return $this->adapter->close();
  78. }
  79. function affected_rows($result) {
  80. return $this->adapter->affected_rows($result);
  81. }
  82. function last_error() {
  83. return $this->adapter->last_error();
  84. }
  85. function last_query_error() {
  86. return $this->adapter->last_query_error();
  87. }
  88. }