Passed
Push — dev ( fdf8fd...812003 )
by Def
28:15 queued 25:26
created

ConnectionPDOMysql::isActive()   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\PDO;
6
7
use PDO;
8
use Yiisoft\Db\Cache\QueryCache;
9
use Yiisoft\Db\Cache\SchemaCache;
10
use Yiisoft\Db\Command\CommandInterface;
11
use Yiisoft\Db\Connection\ConnectionPDO;
12
use Yiisoft\Db\Driver\PDODriver;
13
use Yiisoft\Db\Exception\Exception;
14
use Yiisoft\Db\Exception\InvalidConfigException;
15
use Yiisoft\Db\Mysql\Quoter;
16
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...
17
use Yiisoft\Db\Schema\QuoterInterface;
18
use Yiisoft\Db\Schema\SchemaInterface;
19
use Yiisoft\Db\Transaction\TransactionInterface;
20
21
use function constant;
22
23
/**
24
 * Database connection class prefilled for MySQL Server.
25
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
26
 */
27
final class ConnectionPDOMysql extends ConnectionPDO
28
{
29 412
    public function __construct(
30
        protected PDODriver $driver,
31
        protected QueryCache $queryCache,
32
        protected SchemaCache $schemaCache
33
    ) {
34 412
        parent::__construct($queryCache);
35
    }
36
37 348
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
38
    {
39 348
        $command = new CommandPDOMysql($this, $this->queryCache);
40
41 348
        if ($sql !== null) {
42 173
            $command->setSql($sql);
43
        }
44
45 348
        if ($this->logger !== null) {
46 348
            $command->setLogger($this->logger);
47
        }
48
49 348
        if ($this->profiler !== null) {
50 348
            $command->setProfiler($this->profiler);
51
        }
52
53 348
        return $command->bindValues($params);
54
    }
55
56 8
    public function createTransaction(): TransactionInterface
57
    {
58 8
        return new TransactionPDOMysql($this);
59
    }
60
61 10
    public function getDriverName(): string
62
    {
63 10
        return 'mysql';
64
    }
65
66
    /**
67
     * @throws Exception|InvalidConfigException
68
     */
69 348
    public function getQueryBuilder(): QueryBuilderInterface
70
    {
71 348
        if ($this->queryBuilder === null) {
72 348
            $this->queryBuilder = new QueryBuilderPDOMysql(
73 348
                $this->createCommand(),
74 348
                $this->getQuoter(),
75 348
                $this->getSchema(),
76
            );
77
        }
78
79 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...
80
    }
81
82 365
    public function getQuoter(): QuoterInterface
83
    {
84 365
        if ($this->quoter === null) {
85 365
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
86
        }
87
88 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...
89
    }
90
91 374
    public function getSchema(): SchemaInterface
92
    {
93 374
        if ($this->schema === null) {
94 374
            $this->schema = new SchemaPDOMysql($this, $this->schemaCache);
95
        }
96
97 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...
98
    }
99
100
    /**
101
     * Initializes the DB connection.
102
     *
103
     * This method is invoked right after the DB connection is established.
104
     *
105
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
106
     *
107
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
108
     *
109
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
110
     */
111 189
    protected function initConnection(): void
112
    {
113 189
        $this->pdo = $this->driver->createConnection();
114 189
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
115
116 189
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
117 1
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
118
        }
119
120 189
        $charset = $this->driver->getCharset();
121
122 189
        if ($charset !== null) {
123
            $this->pdo->exec('SET NAMES ' . $this->pdo->quote($charset));
124
        }
125
    }
126
}
127