Passed
Push — dev ( c92037...8f8fc2 )
by Def
16:17 queued 13:40
created

ConnectionPDOMysql   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
eloc 28
c 2
b 0
f 0
dl 0
loc 88
ccs 34
cts 35
cp 0.9714
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initConnection() 0 13 4
A getSchema() 0 7 2
A getQuoter() 0 7 2
A createCommand() 0 17 4
A getDriverName() 0 3 1
A createTransaction() 0 3 1
A getQueryBuilder() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\PDO;
6
7
use PDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Mysql\Quoter;
13
use Yiisoft\Db\Mysql\Schema;
14
use Yiisoft\Db\Query\QueryBuilderInterface;
15
use Yiisoft\Db\Schema\QuoterInterface;
16
use Yiisoft\Db\Schema\SchemaInterface;
17
use Yiisoft\Db\Transaction\TransactionInterface;
18
19
use function constant;
20
21
/**
22
 * Database connection class prefilled for MySQL Server.
23
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
24
 */
25
final class ConnectionPDOMysql extends ConnectionPDO
26
{
27 183
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
28
    {
29 183
        $command = new CommandPDOMysql($this, $this->queryCache);
30
31 183
        if ($sql !== null) {
32 182
            $command->setSql($sql);
33
        }
34
35 183
        if ($this->logger !== null) {
36 183
            $command->setLogger($this->logger);
37
        }
38
39 183
        if ($this->profiler !== null) {
40 183
            $command->setProfiler($this->profiler);
41
        }
42
43 183
        return $command->bindValues($params);
44
    }
45
46 11
    public function createTransaction(): TransactionInterface
47
    {
48 11
        return new TransactionPDOMysql($this);
49
    }
50
51 10
    public function getDriverName(): string
52
    {
53 10
        return 'mysql';
54
    }
55
56
    /**
57
     * @throws Exception|InvalidConfigException
58
     */
59 354
    public function getQueryBuilder(): QueryBuilderInterface
60
    {
61 354
        if ($this->queryBuilder === null) {
62 354
            $this->queryBuilder = new QueryBuilderPDOMysql(
63 354
                $this->getQuoter(),
64 354
                $this->getSchema(),
65
            );
66
        }
67
68 354
        return $this->queryBuilder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryBuilder could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Query\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
71 371
    public function getQuoter(): QuoterInterface
72
    {
73 371
        if ($this->quoter === null) {
74 371
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
75
        }
76
77 371
        return $this->quoter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->quoter could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\QuoterInterface. Consider adding an additional type-check to rule them out.
Loading history...
78
    }
79
80 380
    public function getSchema(): SchemaInterface
81
    {
82 380
        if ($this->schema === null) {
83 380
            $this->schema = new Schema($this, $this->schemaCache);
84
        }
85
86 380
        return $this->schema;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\SchemaInterface. Consider adding an additional type-check to rule them out.
Loading history...
87
    }
88
89
    /**
90
     * Initializes the DB connection.
91
     *
92
     * This method is invoked right after the DB connection is established.
93
     *
94
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
95
     *
96
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
97
     *
98
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
99
     */
100 195
    protected function initConnection(): void
101
    {
102 195
        $this->pdo = $this->driver->createConnection();
103 195
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
104
105 195
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
106 1
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
107
        }
108
109 195
        $charset = $this->driver->getCharset();
110
111 195
        if ($charset !== null) {
112
            $this->pdo->exec('SET NAMES ' . $this->pdo->quote($charset));
113
        }
114
    }
115
}
116