23 private $driver_options;
35 public function __construct($dsn, $username =
'', $password =
'', $driver_options = [])
38 if (strpos($dsn,
"mysql:") === 0) {
39 $this->dsn .=
";charset=utf8mb4";
41 $this->username = $username;
42 $this->password = $password;
44 if (isset($driver_options[
'prefix'])) {
45 $this->prefix = $driver_options[
'prefix'];
46 unset($driver_options[
'prefix']);
48 $this->driver_options = $driver_options;
61 private function lateInitialize()
63 $this->pdo = new \PDO($this->dsn, $this->username, $this->password, $this->driver_options);
66 $this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
70 $this->setAttribute(\PDO::ATTR_EMULATE_PREPARES,
false);
75 if (is_null($this->pdo)) {
76 $this->lateInitialize();
88 if (defined(
"Pdo\Mysql::ATTR_USE_BUFFERED_QUERY")) {
89 return [\Pdo\Mysql::ATTR_USE_BUFFERED_QUERY =>
false];
91 return [\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY =>
false];
97 public function __set($name, $value)
99 if (is_null($this->pdo)) {
100 $this->lateInitialize();
102 $this->$name = $value;
108 if (is_null($this->pdo)) {
109 $this->lateInitialize();
116 public function __call($name, $arguments)
118 if (is_null($this->pdo)) {
119 $this->lateInitialize();
123 return call_user_func_array([$this->pdo, $name], $arguments);
124 }
catch (\PDOException $e) {
126 if (in_array($name, [
"prepare",
"exec",
"query"])) {
127 $message =
"\non the following query: \n" . $arguments[0];
129 throw new \Depage\Db\Exceptions\PdoException($e->getMessage() . $message, (
int) $e->getCode(), $e);
137 return call_user_func_array(
"pdo::$name", $arguments);
138 }
catch (\PDOException $e) {
139 throw new \Depage\Db\Exceptions\PdoException($e->getMessage(), (
int) $e->getCode(), $e);
174 list($info[
'protocol'], $rest) = explode(
":", $dsn, 2);
176 $parts = explode(
";", $rest);
178 foreach ($parts as $part) {
179 list($name, $value) = explode(
"=", $part, 2);
180 $info[$name] = $value;
__wakeup()
allows Depage\Db\Pdo-object to be unserialized
__call($name, $arguments)
__destruct()
removes the pdo object which closes the connection to the database
static __callStatic($name, $arguments)
static unbuffered()
helper function to set unbuffered query options for mysql
static parse_dsn($dsn)
parses dsn intro its parts
__sleep()
allows Depage\Db\Pdo-object to be serialized
__construct($dsn, $username='', $password='', $driver_options=[])
constructor for PDO object with an additional prefix-parameter in driver-options