26
26
namespace doganoo \PHPUtil \Storage ;
27
27
28
28
use doganoo \PHPUtil \Exception \InvalidCredentialsException ;
29
+ use PDO ;
30
+ use PDOStatement ;
29
31
30
32
/**
31
33
* Class MySQLConnector
@@ -36,9 +38,9 @@ class PDOConnector implements IStorageConnector {
36
38
37
39
/** @var array $credentials */
38
40
private $ credentials = null ;
39
- /** @var \ PDO $mysqli */
41
+ /** @var PDO $mysqli */
40
42
private $ pdo = null ;
41
- /** @var \ PDOStatement $statement */
43
+ /** @var PDOStatement $statement */
42
44
private $ statement = null ;
43
45
/** @var bool $transactionExists */
44
46
private $ transactionExists = false ;
@@ -64,6 +66,7 @@ public function getSchema(): ?string {
64
66
65
67
/**
66
68
* starts a database transaction
69
+ *
67
70
* @return bool
68
71
*/
69
72
public function startTransaction (): bool {
@@ -77,6 +80,7 @@ public function startTransaction(): bool {
77
80
78
81
/**
79
82
* commits a started database transaction
83
+ *
80
84
* @return bool
81
85
*/
82
86
public function commit (): bool {
@@ -90,6 +94,7 @@ public function commit(): bool {
90
94
91
95
/**
92
96
* rolls a started transaction back
97
+ *
93
98
* @return bool
94
99
*/
95
100
public function rollback (): bool {
@@ -113,12 +118,13 @@ public function connect(): bool {
113
118
}
114
119
$ host = $ this ->credentials ["servername " ];
115
120
$ db = $ this ->credentials ["dbname " ];
116
- $ dsn = "mysql:host= $ host;dbname= $ db;charset=utf8 " ;
117
- $ this ->pdo = new \PDO ($ dsn ,
121
+ $ charSet = $ this ->credentials ["charset " ] ?? 'utf8 ' ;
122
+ $ dsn = "mysql:host= $ host;dbname= $ db;charset= $ charSet " ;
123
+ $ this ->pdo = new PDO ($ dsn ,
118
124
$ this ->credentials ["username " ],
119
125
$ this ->credentials ["password " ]
120
126
);
121
- $ this ->pdo ->setAttribute (\ PDO ::ATTR_ERRMODE , \ PDO ::ERRMODE_EXCEPTION );
127
+ $ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
122
128
return $ this ->pdo !== null ;
123
129
}
124
130
@@ -147,9 +153,10 @@ private function hasMinimumCredentials(): bool {
147
153
* prepares a SQL statement
148
154
*
149
155
* @param string $sql
150
- * @return null|\PDOStatement
156
+ *
157
+ * @return null|PDOStatement
151
158
*/
152
- public function prepare (string $ sql ): ?\ PDOStatement {
159
+ public function prepare (string $ sql ): ?PDOStatement {
153
160
$ statement = $ this ->getConnection ()->prepare ($ sql );
154
161
if ($ statement === false ) {
155
162
return null ;
@@ -161,7 +168,7 @@ public function prepare(string $sql): ?\PDOStatement {
161
168
/**
162
169
* returns the connection
163
170
*
164
- * @return \ PDO|null
171
+ * @return PDO|null
165
172
*/
166
173
public function getConnection () {
167
174
if (!$ this ->hasMinimumCredentials ()) {
@@ -203,6 +210,7 @@ public function disconnect(): bool {
203
210
204
211
/**
205
212
* @param null $name
213
+ *
206
214
* @return string
207
215
*/
208
216
public function getLastInsertId ($ name = null ) {
@@ -218,8 +226,8 @@ public function getLastInsertId($name = null) {
218
226
* @param null $length
219
227
* @param null $driverOptions
220
228
*/
221
- public function bindParam (string $ param , $ value , $ dataType = \ PDO ::PARAM_STR , $ length = null , $ driverOptions = null ) {
229
+ public function bindParam (string $ param , $ value , $ dataType = PDO ::PARAM_STR , $ length = null , $ driverOptions = null ) {
222
230
$ this ->statement ->bindParam ($ param , $ value , $ dataType , $ length , $ driverOptions );
223
231
}
224
232
225
- }
233
+ }
0 commit comments