Passed
Push — dev ( 8cdfb1...f21bf7 )
by Def
06:58 queued 02:32
created

ConnectionPDOSqlite::getDriverName()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 186
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
41
    {
42 186
        $command = new CommandPDOSqlite($this, $this->queryCache);
43
44 186
        if ($sql !== null) {
45 181
            $command->setSql($sql);
46
        }
47
48 186
        if ($this->logger !== null) {
49 186
            $command->setLogger($this->logger);
50
        }
51
52 186
        if ($this->profiler !== null) {
53 186
            $command->setProfiler($this->profiler);
54
        }
55
56 186
        return $command->bindValues($params);
57
    }
58
59 11
    public function createTransaction(): TransactionInterface
60
    {
61 11
        return new TransactionPDOSqlite($this);
62
    }
63
64
    /**
65
     * @throws Exception|InvalidConfigException
66
     */
67 337
    public function getQueryBuilder(): QueryBuilderInterface
68
    {
69 337
        if ($this->queryBuilder === null) {
70 337
            $this->queryBuilder = new QueryBuilderPDOSqlite(
71 337
                $this->getQuoter(),
72 337
                $this->getSchema(),
73
            );
74
        }
75
76 337
        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...
77
    }
78
79 353
    public function getQuoter(): QuoterInterface
80
    {
81 353
        if ($this->quoter === null) {
82 353
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
83
        }
84
85 353
        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...
86
    }
87
88 351
    public function getSchema(): SchemaInterface
89
    {
90 351
        if ($this->schema === null) {
91 351
            $this->schema = new Schema($this, $this->schemaCache);
92
        }
93
94 351
        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...
95
    }
96
97
    /**
98
     * Initializes the DB connection.
99
     *
100
     * This method is invoked right after the DB connection is established.
101
     *
102
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
103
     *
104
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
105
     *
106
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
107
     */
108 186
    protected function initConnection(): void
109
    {
110 186
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
111
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
112
        }
113
114 186
        $this->pdo = $this->driver->createConnection();
115
    }
116
}
117