Passed
Push — dev ( c3a781...f9d2dd )
by Def
08:22 queued 05:42
created

ConnectionPDOMysql::createCommand()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 2
b 0
f 0
nc 8
nop 2
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\PDO;
6
7
use PDO;
8
use Yiisoft\Db\Command\CommandInterface;
9
use Yiisoft\Db\Connection\ConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Mysql\Quoter;
13
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Db\Schema\QuoterInterface;
15
use Yiisoft\Db\Schema\SchemaInterface;
16
use Yiisoft\Db\Transaction\TransactionInterface;
17
18
use function constant;
19
20
/**
21
 * Database connection class prefilled for MySQL Server.
22
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
23
 */
24
final class ConnectionPDOMysql extends ConnectionPDO
25
{
26 348
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
27
    {
28 348
        $command = new CommandPDOMysql($this, $this->queryCache);
29
30 348
        if ($sql !== null) {
31 173
            $command->setSql($sql);
32
        }
33
34 348
        if ($this->logger !== null) {
35 348
            $command->setLogger($this->logger);
36
        }
37
38 348
        if ($this->profiler !== null) {
39 348
            $command->setProfiler($this->profiler);
40
        }
41
42 348
        return $command->bindValues($params);
43
    }
44
45 8
    public function createTransaction(): TransactionInterface
46
    {
47 8
        return new TransactionPDOMysql($this);
48
    }
49
50 10
    public function getDriverName(): string
51
    {
52 10
        return 'mysql';
53
    }
54
55
    /**
56
     * @throws Exception|InvalidConfigException
57
     */
58 348
    public function getQueryBuilder(): QueryBuilderInterface
59
    {
60 348
        if ($this->queryBuilder === null) {
61 348
            $this->queryBuilder = new QueryBuilderPDOMysql(
62 348
                $this->createCommand(),
63 348
                $this->getQuoter(),
64 348
                $this->getSchema(),
65
            );
66
        }
67
68 348
        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 365
    public function getQuoter(): QuoterInterface
72
    {
73 365
        if ($this->quoter === null) {
74 365
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
75
        }
76
77 365
        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 374
    public function getSchema(): SchemaInterface
81
    {
82 374
        if ($this->schema === null) {
83 374
            $this->schema = new SchemaPDOMysql($this, $this->schemaCache);
84
        }
85
86 374
        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 189
    protected function initConnection(): void
101
    {
102 189
        $this->pdo = $this->driver->createConnection();
103 189
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
104
105 189
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
106 1
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
107
        }
108
109 189
        $charset = $this->driver->getCharset();
110
111 189
        if ($charset !== null) {
112
            $this->pdo->exec('SET NAMES ' . $this->pdo->quote($charset));
113
        }
114
    }
115
}
116