22 private $driver_options;
34 public function __construct($dsn, $username =
'', $password =
'', $driver_options = array())
37 if (strpos($dsn,
"mysql:") === 0) {
38 $this->dsn .=
";charset=utf8mb4";
40 $this->username = $username;
41 $this->password = $password;
43 if (isset($driver_options[
'prefix'])) {
44 $this->prefix = $driver_options[
'prefix'];
45 unset($driver_options[
'prefix']);
47 $this->driver_options = $driver_options;
60 private function lateInitialize()
62 $this->pdo = new \PDO($this->dsn, $this->username, $this->password, $this->driver_options);
65 $this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
69 $this->setAttribute(\PDO::ATTR_EMULATE_PREPARES,
false);
74 if (is_null($this->pdo)) {
75 $this->lateInitialize();
83 public function __set($name, $value)
85 if (is_null($this->pdo)) {
86 $this->lateInitialize();
88 $this->$name = $value;
94 if (is_null($this->pdo)) {
95 $this->lateInitialize();
102 public function __call($name, $arguments)
104 if (is_null($this->pdo)) {
105 $this->lateInitialize();
109 return call_user_func_array(array($this->pdo, $name), $arguments);
110 }
catch (\PDOException $e) {
112 if (in_array($name, [
"prepare",
"exec",
"query"])) {
113 $message =
"\non the following query: \n" . $arguments[0];
115 throw new \Depage\Db\Exceptions\PdoException($e->getMessage() . $message, (
int) $e->getCode(), $e);
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);
162 list($info[
'protocol'], $rest) = explode(
":", $dsn, 2);
164 $parts = explode(
";", $rest);
166 foreach ($parts as $part) {
167 list($name, $value) = explode(
"=", $part, 2);
168 $info[$name] = $value;
__wakeup()
allows Depage\Db\Pdo-object to be unserialized
__construct($dsn, $username='', $password='', $driver_options=array())
constructor for PDO object with an additional prefix-parameter in driver-options
__call($name, $arguments)
__destruct()
removes the pdo object which closes the connection to the database
static __callStatic($name, $arguments)
static parse_dsn($dsn)
parses dsn intro its parts
__sleep()
allows Depage\Db\Pdo-object to be serialized