Passed
Pull Request — master (#133)
by Wilmer
17:16 queued 12:38
created

ConnectionPDO::initConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use PDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
13
use Yiisoft\Db\Schema\Quoter;
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 SQLite Server.
20
 */
21
final class ConnectionPDO extends AbstractConnectionPDO
22
{
23
    /**
24
     * Reset the connection after cloning.
25
     */
26 1
    public function __clone()
27
    {
28 1
        $this->transaction = null;
29
30 1
        if (!str_starts_with($this->driver->getDsn(), 'sqlite::memory:')) {
31
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
32 1
            $this->pdo = null;
33
        }
34
    }
35
36 213
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
37
    {
38 213
        $command = new CommandPDO($this, $this->queryCache);
39
40 213
        if ($sql !== null) {
41 159
            $command->setSql($sql);
42
        }
43
44 213
        if ($this->logger !== null) {
45 3
            $command->setLogger($this->logger);
46
        }
47
48 213
        if ($this->profiler !== null) {
49 3
            $command->setProfiler($this->profiler);
50
        }
51
52 213
        return $command->bindValues($params);
53
    }
54
55 9
    public function createTransaction(): TransactionInterface
56
    {
57 9
        return new TransactionPDO($this);
58
    }
59
60
    /**
61
     * @throws Exception|InvalidConfigException
62
     */
63 455
    public function getQueryBuilder(): QueryBuilderInterface
64
    {
65 455
        if ($this->queryBuilder === null) {
66 455
            $this->queryBuilder = new QueryBuilder(
67 455
                $this->getQuoter(),
68 455
                $this->getSchema(),
69
            );
70
        }
71
72 455
        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...
73
    }
74
75 488
    public function getQuoter(): QuoterInterface
76
    {
77 488
        if ($this->quoter === null) {
78 488
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
79
        }
80
81 488
        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...
82
    }
83
84 494
    public function getSchema(): SchemaInterface
85
    {
86 494
        if ($this->schema === null) {
87 494
            $this->schema = new Schema($this, $this->schemaCache);
88
        }
89
90 494
        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...
91
    }
92
93
    /**
94
     * Initializes the DB connection.
95
     *
96
     * This method is invoked right after the DB connection is established.
97
     *
98
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
99
     *
100
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
101
     *
102
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
103
     */
104 214
    protected function initConnection(): void
105
    {
106 214
        if ($this->getEmulatePrepare() !== null) {
107 1
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
108
        }
109
110 214
        $this->pdo = $this->driver->createConnection();
111
    }
112
}
113