depage-db
v1.4.0
Toggle main menu visibility
Loading...
Searching...
No Matches
Pdo.php
Go to the documentation of this file.
1
<?php
2
13
14
namespace
Depage\Db;
15
16
class
Pdo
17
{
18
public
$prefix
=
""
;
19
private
$pdo =
null
;
20
private
$dsn;
21
private
$username;
22
private
$password;
23
private
$driver_options;
24
35
public
function
__construct
($dsn, $username =
''
, $password =
''
, $driver_options = [])
36
{
37
$this->dsn = $dsn;
38
if
(strpos($dsn,
"mysql:"
) === 0) {
39
$this->dsn .=
";charset=utf8mb4"
;
40
}
41
$this->username = $username;
42
$this->password = $password;
43
44
if
(isset($driver_options[
'prefix'
])) {
45
$this->prefix = $driver_options[
'prefix'
];
46
unset($driver_options[
'prefix'
]);
47
}
48
$this->driver_options = $driver_options;
49
}
50
55
public
function
__destruct
()
56
{
57
$this->pdo =
null
;
58
}
59
61
private
function
lateInitialize()
62
{
63
$this->pdo = new \PDO($this->dsn, $this->username, $this->password, $this->driver_options);
64
65
// set error mode to exception by default
66
$this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
67
68
// disable emulated prepares
69
// @todo check why this does not work with some queries
70
$this->setAttribute(\PDO::ATTR_EMULATE_PREPARES,
false
);
71
}
72
73
public
function
getPdoObject
()
74
{
75
if
(is_null($this->pdo)) {
76
$this->lateInitialize();
77
}
78
79
return
$this->pdo;
80
}
81
86
public
static
function
unbuffered
(): array
87
{
88
if
(defined(
"Pdo\Mysql::ATTR_USE_BUFFERED_QUERY"
)) {
89
return
[\Pdo\Mysql::ATTR_USE_BUFFERED_QUERY =>
false
];
90
}
else
{
91
return
[\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY =>
false
];
92
}
93
}
94
97
public
function
__set
($name, $value)
98
{
99
if
(is_null($this->pdo)) {
100
$this->lateInitialize();
101
}
102
$this->$name = $value;
103
}
104
106
public
function
__get
($name)
107
{
108
if
(is_null($this->pdo)) {
109
$this->lateInitialize();
110
}
111
112
return
$this->$name;
113
}
114
116
public
function
__call
($name, $arguments)
117
{
118
if
(is_null($this->pdo)) {
119
$this->lateInitialize();
120
}
121
122
try
{
123
return
call_user_func_array([$this->pdo, $name], $arguments);
124
}
catch
(\PDOException $e) {
125
$message =
""
;
126
if
(in_array($name, [
"prepare"
,
"exec"
,
"query"
])) {
127
$message =
"\non the following query: \n"
. $arguments[0];
128
}
129
throw
new \Depage\Db\Exceptions\PdoException($e->getMessage() . $message, (
int
) $e->getCode(), $e);
130
}
131
}
132
134
public
static
function
__callStatic
($name, $arguments)
135
{
136
try
{
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);
140
}
141
}
142
146
public
function
__sleep
(): array
147
{
148
return
[
149
'dsn'
,
150
'username'
,
151
'password'
,
152
'driver_options'
,
153
'prefix'
,
154
];
155
}
156
161
public
function
__wakeup
() {}
162
170
public
static
function
parse_dsn
($dsn)
171
{
172
$info = [];
173
174
list($info[
'protocol'
], $rest) = explode(
":"
, $dsn, 2);
175
176
$parts = explode(
";"
, $rest);
177
178
foreach
($parts as $part) {
179
list($name, $value) = explode(
"="
, $part, 2);
180
$info[$name] = $value;
181
}
182
183
return
$info;
184
}
185
}
186
187
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
Depage\Db\Pdo
Definition
Pdo.php:17
Depage\Db\Pdo\$prefix
$prefix
Definition
Pdo.php:18
Depage\Db\Pdo\__wakeup
__wakeup()
allows Depage\Db\Pdo-object to be unserialized
Definition
Pdo.php:161
Depage\Db\Pdo\__call
__call($name, $arguments)
Definition
Pdo.php:116
Depage\Db\Pdo\__destruct
__destruct()
removes the pdo object which closes the connection to the database
Definition
Pdo.php:55
Depage\Db\Pdo\__callStatic
static __callStatic($name, $arguments)
Definition
Pdo.php:134
Depage\Db\Pdo\unbuffered
static unbuffered()
helper function to set unbuffered query options for mysql
Definition
Pdo.php:86
Depage\Db\Pdo\__set
__set($name, $value)
Definition
Pdo.php:97
Depage\Db\Pdo\getPdoObject
getPdoObject()
Definition
Pdo.php:73
Depage\Db\Pdo\parse_dsn
static parse_dsn($dsn)
parses dsn intro its parts
Definition
Pdo.php:170
Depage\Db\Pdo\__sleep
__sleep()
allows Depage\Db\Pdo-object to be serialized
Definition
Pdo.php:146
Depage\Db\Pdo\__get
__get($name)
Definition
Pdo.php:106
Depage\Db\Pdo\__construct
__construct($dsn, $username='', $password='', $driver_options=[])
constructor for PDO object with an additional prefix-parameter in driver-options
Definition
Pdo.php:35
Pdo.php
Generated on
for depage-db by
doxygen
1.18.0
For an overview of all docs, go to
docs.depage.net