Passed
Push — master ( 4c27aa...22c6d1 )
by Def
02:16
created

AbstractPDODriver::getPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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