1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Driver\PDO; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
|
9
|
|
|
abstract class AbstractPDODriver implements PDODriverInterface |
10
|
|
|
{ |
11
|
|
|
protected string|null $charset = null; |
12
|
|
|
|
13
|
|
|
public function __construct( |
14
|
|
|
protected string $dsn, |
15
|
|
|
protected string $username = '', |
16
|
|
|
protected string $password = '', |
17
|
|
|
protected array $attributes = [] |
18
|
|
|
) { |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* PDO attributes (name => value) that should be set when calling {@see open()} to establish a DB connection. |
23
|
|
|
* Please refer to the [PHP manual](http://php.net/manual/en/pdo.setattribute.php) for details about available |
24
|
|
|
* attributes. |
25
|
|
|
* |
26
|
|
|
* @param array $attributes the attributes (name => value) to be set on the DB connection. |
27
|
|
|
*/ |
28
|
|
|
public function attributes(array $attributes): void |
29
|
|
|
{ |
30
|
|
|
$this->attributes = $attributes; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function createConnection(): PDO |
34
|
|
|
{ |
35
|
|
|
return new PDO($this->dsn, $this->username, $this->password, $this->attributes); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The charset used for database connection. The property is only used for MySQL, PostgresSQL databases. Defaults to |
40
|
|
|
* null, meaning using default charset as configured by the database. |
41
|
|
|
* |
42
|
|
|
* For Oracle Database, the charset must be specified in the {@see dsn}, for example for UTF-8 by appending |
43
|
|
|
* `;charset=UTF-8` to the DSN string. |
44
|
|
|
* |
45
|
|
|
* The same applies for if you're using GBK or BIG5 charset with MySQL, then it's highly recommended specifying |
46
|
|
|
* charset via {@see dsn} like `'mysql:dbname=database;host=127.0.0.1;charset=GBK;'`. |
47
|
|
|
* |
48
|
|
|
* @param string|null $charset |
49
|
|
|
*/ |
50
|
|
|
public function setCharset(string|null $charset): void |
51
|
|
|
{ |
52
|
|
|
$this->charset = $charset; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the charset currently used for database connection. The returned charset is only applicable for MySQL, |
57
|
|
|
* PostgresSQL databases. |
58
|
|
|
* |
59
|
|
|
* @return string|null the charset of the pdo instance. Null is returned if the charset is not set yet or not |
60
|
|
|
* supported by the pdo driver |
61
|
|
|
*/ |
62
|
|
|
public function getCharset(): string|null |
63
|
|
|
{ |
64
|
|
|
return $this->charset; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
abstract public function getDriverName(): string; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return dsn string for current driver. |
71
|
|
|
*/ |
72
|
|
|
public function getDsn(): string |
73
|
|
|
{ |
74
|
|
|
return $this->dsn; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the password for establishing DB connection. |
79
|
|
|
* |
80
|
|
|
* @return string the password for establishing DB connection. |
81
|
|
|
*/ |
82
|
|
|
public function getPassword(): string |
83
|
|
|
{ |
84
|
|
|
return $this->password; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the username for establishing DB connection. |
89
|
|
|
* |
90
|
|
|
* @return string the username for establishing DB connection. |
91
|
|
|
*/ |
92
|
|
|
public function getUsername(): string |
93
|
|
|
{ |
94
|
|
|
return $this->username; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The password for establishing DB connection. Defaults to `null` meaning no password to use. |
99
|
|
|
* |
100
|
|
|
* @param string $password the password for establishing DB connection. |
101
|
|
|
*/ |
102
|
|
|
public function password(string $password): void |
103
|
|
|
{ |
104
|
|
|
$this->password = $password; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The username for establishing DB connection. Defaults to `null` meaning no username to use. |
109
|
|
|
* |
110
|
|
|
* @param string $username the username for establishing DB connection. |
111
|
|
|
*/ |
112
|
|
|
public function username(string $username): void |
113
|
|
|
{ |
114
|
|
|
$this->username = $username; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|