Passed
Pull Request — master (#184)
by Wilmer
03:44
created

ConnectionPDO::createTransaction()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use PDO;
8
use Psr\Log\LogLevel;
9
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
10
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
11
use Yiisoft\Db\Exception\Exception;
12
use Yiisoft\Db\Exception\InvalidConfigException;
13
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
14
use Yiisoft\Db\Schema\QuoterInterface;
15
use Yiisoft\Db\Schema\SchemaInterface;
16
use Yiisoft\Db\Transaction\TransactionInterface;
17
18
/**
19
 * Database connection class prefilled for MySQL Server.
20
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
21
 */
22
final class ConnectionPDO extends AbstractConnectionPDO
23
{
24 44
    public function close(): void
25
    {
26 44
        if ($this->pdo !== null) {
27 44
            $this->logger?->log(
28 44
                LogLevel::DEBUG,
29 44
                'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__,
30 44
            );
31
32
            // Solution for close connections from https://stackoverflow.com/questions/18277233/pdo-closing-connection
33
            try {
34 44
                $this->pdo->query('KILL CONNECTION_ID()');
35 44
            } catch (\Exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
36
            }
37
38 44
            $this->pdo = null;
39 44
            $this->transaction = null;
40
        }
41
    }
42
43 240
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
44
    {
45 240
        $command = new CommandPDO($this, $this->queryCache);
46
47 240
        if ($sql !== null) {
48 220
            $command->setSql($sql);
49
        }
50
51 240
        if ($this->logger !== null) {
52
            $command->setLogger($this->logger);
53
        }
54
55 240
        if ($this->profiler !== null) {
56
            $command->setProfiler($this->profiler);
57
        }
58
59 240
        return $command->bindValues($params);
60
    }
61
62 9
    public function createTransaction(): TransactionInterface
63
    {
64 9
        return new TransactionPDO($this);
65
    }
66
67
    /**
68
     * @throws Exception|InvalidConfigException
69
     */
70 465
    public function getQueryBuilder(): QueryBuilderInterface
71
    {
72 465
        if ($this->queryBuilder === null) {
73 465
            $this->queryBuilder = new QueryBuilder(
74 465
                $this->getQuoter(),
75 465
                $this->getSchema(),
76 465
            );
77
        }
78
79 465
        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\QueryBuilder\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82 527
    public function getQuoter(): QuoterInterface
83
    {
84 527
        if ($this->quoter === null) {
85 527
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
86
        }
87
88 527
        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 504
    public function getSchema(): SchemaInterface
92
    {
93 504
        if ($this->schema === null) {
94 504
            $this->schema = new Schema($this, $this->schemaCache);
95
        }
96
97 504
        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