Passed
Push — master ( a5da8b...336ef8 )
by Wilmer
01:45
created

Connection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 87
ccs 19
cts 21
cp 0.9048
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommand() 0 9 2
A initConnection() 0 11 5
A createPdoInstance() 0 3 1
A getSchema() 0 7 2
A getDriverName() 0 3 1
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 8
    public function getDriverName(): string
104
    {
105 8
        return 'mysql';
106
    }
107
}
108