codysnider /
tt-rss
| 1 | <?php |
||
| 2 | class Db |
||
| 3 | { |
||
| 4 | |||
| 5 | /* @var Db $instance */ |
||
| 6 | private static $instance; |
||
| 7 | |||
| 8 | /* @var IDb $adapter */ |
||
| 9 | private $adapter; |
||
| 10 | |||
| 11 | private $link; |
||
| 12 | |||
| 13 | /* @var PDO $pdo */ |
||
| 14 | private $pdo; |
||
| 15 | |||
| 16 | private function __clone() { |
||
| 17 | // |
||
| 18 | } |
||
| 19 | |||
| 20 | private function legacy_connect() { |
||
| 21 | |||
| 22 | user_error("Legacy connect requested to ".DB_TYPE, E_USER_NOTICE); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 23 | |||
| 24 | $er = error_reporting(E_ALL); |
||
| 25 | |||
| 26 | switch (DB_TYPE) { |
||
| 27 | case "mysql": |
||
| 28 | $this->adapter = new Db_Mysqli(); |
||
| 29 | break; |
||
| 30 | case "pgsql": |
||
| 31 | $this->adapter = new Db_Pgsql(); |
||
| 32 | break; |
||
| 33 | default: |
||
| 34 | die("Unknown DB_TYPE: ".DB_TYPE); |
||
|
0 ignored issues
–
show
|
|||
| 35 | } |
||
| 36 | |||
| 37 | if (!$this->adapter) { |
||
| 38 | print("Error initializing database adapter for ".DB_TYPE); |
||
| 39 | exit(100); |
||
|
0 ignored issues
–
show
|
|||
| 40 | } |
||
| 41 | |||
| 42 | $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : ""); |
||
|
0 ignored issues
–
show
|
|||
| 43 | |||
| 44 | if (!$this->link) { |
||
| 45 | print("Error connecting through adapter: ".$this->adapter->last_error()); |
||
| 46 | exit(101); |
||
|
0 ignored issues
–
show
|
|||
| 47 | } |
||
| 48 | |||
| 49 | error_reporting($er); |
||
| 50 | } |
||
| 51 | |||
| 52 | // this really shouldn't be used unless a separate PDO connection is needed |
||
| 53 | // normal usage is Db::pdo()->prepare(...) etc |
||
| 54 | public function pdo_connect() { |
||
| 55 | |||
| 56 | $db_port = defined('DB_PORT') && DB_PORT ? ';port='.DB_PORT : ''; |
||
|
0 ignored issues
–
show
|
|||
| 57 | $db_host = defined('DB_HOST') && DB_HOST ? ';host='.DB_HOST : ''; |
||
|
0 ignored issues
–
show
|
|||
| 58 | |||
| 59 | try { |
||
| 60 | $pdo = new PDO(DB_TYPE.':dbname='.DB_NAME.$db_host.$db_port, |
||
|
0 ignored issues
–
show
|
|||
| 61 | DB_USER, |
||
|
0 ignored issues
–
show
|
|||
| 62 | DB_PASS); |
||
|
0 ignored issues
–
show
|
|||
| 63 | } catch (Exception $e) { |
||
| 64 | print "<pre>Exception while creating PDO object:".$e->getMessage()."</pre>"; |
||
| 65 | exit(101); |
||
|
0 ignored issues
–
show
|
|||
| 66 | } |
||
| 67 | |||
| 68 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 69 | |||
| 70 | if (DB_TYPE == "pgsql") { |
||
| 71 | |||
| 72 | $pdo->query("set client_encoding = 'UTF-8'"); |
||
| 73 | $pdo->query("set datestyle = 'ISO, european'"); |
||
| 74 | $pdo->query("set TIME ZONE 0"); |
||
| 75 | $pdo->query("set cpu_tuple_cost = 0.5"); |
||
| 76 | |||
| 77 | } else if (DB_TYPE == "mysql") { |
||
| 78 | $pdo->query("SET time_zone = '+0:0'"); |
||
| 79 | |||
| 80 | if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { |
||
|
0 ignored issues
–
show
|
|||
| 81 | $pdo->query("SET NAMES ".MYSQL_CHARSET); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | return $pdo; |
||
| 86 | } |
||
| 87 | |||
| 88 | public static function instance() { |
||
| 89 | if (self::$instance == null) { |
||
| 90 | self::$instance = new self(); |
||
| 91 | } |
||
| 92 | |||
| 93 | return self::$instance; |
||
| 94 | } |
||
| 95 | |||
| 96 | public static function get() { |
||
| 97 | if (self::$instance == null) { |
||
| 98 | self::$instance = new self(); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!self::$instance->adapter) { |
||
| 102 | self::$instance->legacy_connect(); |
||
| 103 | } |
||
| 104 | |||
| 105 | return self::$instance->adapter; |
||
| 106 | } |
||
| 107 | |||
| 108 | public static function pdo() { |
||
| 109 | if (self::$instance == null) { |
||
| 110 | self::$instance = new self(); |
||
| 111 | } |
||
| 112 | |||
| 113 | if (!self::$instance->pdo) { |
||
| 114 | self::$instance->pdo = self::$instance->pdo_connect(); |
||
| 115 | } |
||
| 116 | |||
| 117 | return self::$instance->pdo; |
||
| 118 | } |
||
| 119 | } |
||
| 120 |