Passed
Pull Request — master (#48)
by Wilmer
27:32 queued 12:35
created

Connection::createPdoInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use PDO;
8
use Yiisoft\Db\Command\Command;
9
use Yiisoft\Db\Connection\Connection as AbstractConnection;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Exception\NotSupportedException;
13
14
use function constant;
15
16
/**
17
 * Database connection class prefilled for MYSQL Server.
18
 */
19
final class Connection extends AbstractConnection
20
{
21
    private ?Schema $schema = null;
22
23
    /**
24
     * Creates a command for execution.
25
     *
26
     * @param string|null $sql the SQL statement to be executed
27
     * @param array $params the parameters to be bound to the SQL statement
28
     *
29
     * @return Command the DB command
30
     * @throws Exception
31
     * @throws InvalidConfigException
32
     * @throws NotSupportedException
33
     */
34 154
    public function createCommand(?string $sql = null, array $params = []): Command
35
    {
36 154
        if ($sql !== null) {
37 154
            $sql = $this->quoteSql($sql);
38
        }
39
40 154
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
41
42 154
        return $command->bindValues($params);
43
    }
44
45
    /**
46
     * Returns the schema information for the database opened by this connection.
47
     *
48
     * @return Schema the schema information for the database opened by this connection.
49
     */
50 310
    public function getSchema(): Schema
51
    {
52 310
        if ($this->schema !== null) {
53 202
            return $this->schema;
54
        }
55
56 310
        return $this->schema = new Schema($this);
57
    }
58
59
    /**
60
     * Creates the PDO instance.
61
     *
62
     * This method is called by {@see open} to establish a DB connection. The default implementation will create a PHP
63
     * PDO instance. You may override this method if the default PDO needs to be adapted for certain DBMS.
64
     *
65
     * @return PDO the pdo instance
66
     */
67 174
    protected function createPdoInstance(): PDO
68
    {
69 174
        return new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
70
    }
71
72
    /**
73
     * Initializes the DB connection.
74
     *
75
     * This method is invoked right after the DB connection is established.
76
     *
77
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
78
     *
79
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
80
     *
81
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
82
     */
83 174
    protected function initConnection(): void
84
    {
85 174
        if ($this->getPDO() !== null) {
86 174
            $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
87
88 174
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
89
                $this->getPDO()->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
90
            }
91
92 174
            if ($this->getCharset() !== null) {
93
                $this->getPDO()->exec('SET NAMES ' . $this->getPDO()->quote($this->getCharset()));
94
            }
95
        }
96 174
    }
97
98
    /**
99
     * Returns the name of the DB driver.
100
     *
101
     * @return string name of the DB driver
102
     */
103
    public function getDriverName(): string
104
    {
105
        return 'mysql';
106
    }
107
}
108