depage-db v1.4.0
Loading...
Searching...
No Matches
Pdo.php
Go to the documentation of this file.
1<?php
12
13namespace Depage\Db;
14
15class Pdo
16{
17 public $prefix = "";
18 private $pdo = null;
19 private $dsn;
20 private $username;
21 private $password;
22 private $driver_options;
23
34 public function __construct($dsn, $username = '', $password = '', $driver_options = array())
35 {
36 $this->dsn = $dsn;
37 if (strpos($dsn, "mysql:") === 0) {
38 $this->dsn .= ";charset=utf8mb4";
39 }
40 $this->username = $username;
41 $this->password = $password;
42
43 if (isset($driver_options['prefix'])) {
44 $this->prefix = $driver_options['prefix'];
45 unset($driver_options['prefix']);
46 }
47 $this->driver_options = $driver_options;
48 }
49
54 public function __destruct()
55 {
56 $this->pdo = null;
57 }
58
60 private function lateInitialize()
61 {
62 $this->pdo = new \PDO($this->dsn, $this->username, $this->password, $this->driver_options);
63
64 // set error mode to exception by default
65 $this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
66
67 // disable emulated prepares
68 // @todo check why this does not work with some queries
69 $this->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
70 }
71
72 public function getPdoObject()
73 {
74 if (is_null($this->pdo)) {
75 $this->lateInitialize();
76 }
77
78 return $this->pdo;
79 }
80
83 public function __set($name, $value)
84 {
85 if (is_null($this->pdo)) {
86 $this->lateInitialize();
87 }
88 $this->$name = $value;
89 }
90
92 public function __get($name)
93 {
94 if (is_null($this->pdo)) {
95 $this->lateInitialize();
96 }
97
98 return $this->$name;
99 }
100
102 public function __call($name, $arguments)
103 {
104 if (is_null($this->pdo)) {
105 $this->lateInitialize();
106 }
107
108 try {
109 return call_user_func_array(array($this->pdo, $name), $arguments);
110 } catch (\PDOException $e) {
111 $message = "";
112 if (in_array($name, ["prepare", "exec", "query"])) {
113 $message = "\non the following query: \n" . $arguments[0];
114 }
115 throw new \Depage\Db\Exceptions\PdoException($e->getMessage() . $message, (int) $e->getCode(), $e);
116 }
117 }
118
120 public static function __callStatic($name, $arguments)
121 {
122 try {
123 return call_user_func_array("pdo::$name", $arguments);
124 } catch (\PDOException $e) {
125 throw new \Depage\Db\Exceptions\PdoException($e->getMessage(), (int) $e->getCode(), $e);
126 }
127 }
128
132 public function __sleep()
133 {
134 return array(
135 'dsn',
136 'username',
137 'password',
138 'driver_options',
139 'prefix',
140 );
141 }
142
147 public function __wakeup()
148 {
149 }
150
158 public static function parse_dsn($dsn)
159 {
160 $info = array();
161
162 list($info['protocol'], $rest) = explode(":", $dsn, 2);
163
164 $parts = explode(";", $rest);
165
166 foreach ($parts as $part) {
167 list($name, $value) = explode("=", $part, 2);
168 $info[$name] = $value;
169 }
170
171 return $info;
172 }
173}
174
175/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
__wakeup()
allows Depage\Db\Pdo-object to be unserialized
Definition Pdo.php:147
__construct($dsn, $username='', $password='', $driver_options=array())
constructor for PDO object with an additional prefix-parameter in driver-options
Definition Pdo.php:34
__call($name, $arguments)
Definition Pdo.php:102
__destruct()
removes the pdo object which closes the connection to the database
Definition Pdo.php:54
static __callStatic($name, $arguments)
Definition Pdo.php:120
__set($name, $value)
Definition Pdo.php:83
getPdoObject()
Definition Pdo.php:72
static parse_dsn($dsn)
parses dsn intro its parts
Definition Pdo.php:158
__sleep()
allows Depage\Db\Pdo-object to be serialized
Definition Pdo.php:132
__get($name)
Definition Pdo.php:92