db.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. class Db
  3. {
  4. /* @var Db $instance */
  5. private static $instance;
  6. /* @var IDb $adapter */
  7. private $adapter;
  8. private $link;
  9. /* @var PDO $pdo */
  10. private $pdo;
  11. private function __clone() {
  12. //
  13. }
  14. private function legacy_connect() {
  15. user_error("Legacy connect requested to " . DB_TYPE, E_USER_NOTICE);
  16. $er = error_reporting(E_ALL);
  17. switch (DB_TYPE) {
  18. case "mysql":
  19. $this->adapter = new Db_Mysqli();
  20. break;
  21. case "pgsql":
  22. $this->adapter = new Db_Pgsql();
  23. break;
  24. default:
  25. die("Unknown DB_TYPE: " . DB_TYPE);
  26. }
  27. if (!$this->adapter) {
  28. print("Error initializing database adapter for " . DB_TYPE);
  29. exit(100);
  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. // this really shouldn't be used unless a separate PDO connection is needed
  39. // normal usage is Db::pdo()->prepare(...) etc
  40. public function pdo_connect() {
  41. $db_port = defined('DB_PORT') && DB_PORT ? ';port=' . DB_PORT : '';
  42. $db_host = defined('DB_HOST') && DB_HOST ? ';host=' . DB_HOST : '';
  43. try {
  44. $pdo = new PDO(DB_TYPE . ':dbname=' . DB_NAME . $db_host . $db_port,
  45. DB_USER,
  46. DB_PASS);
  47. } catch (Exception $e) {
  48. print "<pre>Exception while creating PDO object:" . $e->getMessage() . "</pre>";
  49. exit(101);
  50. }
  51. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  52. if (DB_TYPE == "pgsql") {
  53. $pdo->query("set client_encoding = 'UTF-8'");
  54. $pdo->query("set datestyle = 'ISO, european'");
  55. $pdo->query("set TIME ZONE 0");
  56. $pdo->query("set cpu_tuple_cost = 0.5");
  57. } else if (DB_TYPE == "mysql") {
  58. $pdo->query("SET time_zone = '+0:0'");
  59. if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
  60. $pdo->query("SET NAMES " . MYSQL_CHARSET);
  61. }
  62. }
  63. return $pdo;
  64. }
  65. public static function instance() {
  66. if (self::$instance == null)
  67. self::$instance = new self();
  68. return self::$instance;
  69. }
  70. public static function get() {
  71. if (self::$instance == null)
  72. self::$instance = new self();
  73. if (!self::$instance->adapter) {
  74. self::$instance->legacy_connect();
  75. }
  76. return self::$instance->adapter;
  77. }
  78. public static function pdo() {
  79. if (self::$instance == null)
  80. self::$instance = new self();
  81. if (!self::$instance->pdo) {
  82. self::$instance->pdo = self::$instance->pdo_connect();
  83. }
  84. return self::$instance->pdo;
  85. }
  86. }