Passed
Push — dev ( bae50f...820ac9 )
by Def
33:33 queued 29:55
created

ConnectionPDOSqlite   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
eloc 28
c 2
b 0
f 0
dl 0
loc 95
ccs 35
cts 36
cp 0.9722
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuoter() 0 7 2
A getSchema() 0 7 2
A initConnection() 0 7 3
A getDriverName() 0 3 1
A createTransaction() 0 3 1
A createCommand() 0 17 4
A __clone() 0 7 2
A getQueryBuilder() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite\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\Query\QueryBuilderInterface;
13
use Yiisoft\Db\Schema\Quoter;
14
use Yiisoft\Db\Schema\QuoterInterface;
15
use Yiisoft\Db\Schema\SchemaInterface;
16
use Yiisoft\Db\Sqlite\Schema;
17
use Yiisoft\Db\Transaction\TransactionInterface;
18
19
use function constant;
20
use function strncmp;
21
22
/**
23
 * Database connection class prefilled for SQLite Server.
24
 */
25
final class ConnectionPDOSqlite extends ConnectionPDO
26
{
27
    /**
28
     * Reset the connection after cloning.
29
     */
30 1
    public function __clone()
31
    {
32 1
        $this->transaction = null;
33
34 1
        if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) {
35
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
36 1
            $this->pdo = null;
37
        }
38
    }
39
40 185
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
41
    {
42 185
        $command = new CommandPDOSqlite($this, $this->queryCache);
43
44 185
        if ($sql !== null) {
45 180
            $command->setSql($sql);
46
        }
47
48 185
        if ($this->logger !== null) {
49 185
            $command->setLogger($this->logger);
50
        }
51
52 185
        if ($this->profiler !== null) {
53 185
            $command->setProfiler($this->profiler);
54
        }
55
56 185
        return $command->bindValues($params);
57
    }
58
59 11
    public function createTransaction(): TransactionInterface
60
    {
61 11
        return new TransactionPDOSqlite($this);
62
    }
63
64 9
    public function getDriverName(): string
65
    {
66 9
        return 'sqlite';
67
    }
68
69
    /**
70
     * @throws Exception|InvalidConfigException
71
     */
72 336
    public function getQueryBuilder(): QueryBuilderInterface
73
    {
74 336
        if ($this->queryBuilder === null) {
75 336
            $this->queryBuilder = new QueryBuilderPDOSqlite(
76 336
                $this->getQuoter(),
77 336
                $this->getSchema(),
78
            );
79
        }
80
81 336
        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...
82
    }
83
84 352
    public function getQuoter(): QuoterInterface
85
    {
86 352
        if ($this->quoter === null) {
87 352
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
88
        }
89
90 352
        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...
91
    }
92
93 350
    public function getSchema(): SchemaInterface
94
    {
95 350
        if ($this->schema === null) {
96 350
            $this->schema = new Schema($this, $this->schemaCache);
97
        }
98
99 350
        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...
100
    }
101
102
    /**
103
     * Initializes the DB connection.
104
     *
105
     * This method is invoked right after the DB connection is established.
106
     *
107
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
108
     *
109
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
110
     *
111
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
112
     */
113 184
    protected function initConnection(): void
114
    {
115 184
        $this->pdo = $this->driver->createConnection();
116 184
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
117
118 184
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
119
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
120
        }
121
    }
122
}
123