db.php 3.3 KB

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