Passed
Push — master ( e13094...37ac1c )
by Alexander
04:29 queued 03:02
created

Connection::initConnection()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.583

Importance

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