Test Failed
Pull Request — dev (#106)
by Def
23:40 queued 20:10
created

ConnectionPDOSqlite::getQuoter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
ccs 2
cts 2
cp 1
cc 2
nc 2
nop 0
crap 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
    /**
65
     * @throws Exception|InvalidConfigException
66 9
     */
67
    public function getQueryBuilder(): QueryBuilderInterface
68
    {
69
        if ($this->queryBuilder === null) {
70
            $this->queryBuilder = new QueryBuilderPDOSqlite(
71
                $this->getQuoter(),
72 336
                $this->getSchema(),
73
            );
74 336
        }
75 336
76 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...
77 336
    }
78
79
    public function getQuoter(): QuoterInterface
80
    {
81 336
        if ($this->quoter === null) {
82
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
83
        }
84 352
85
        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 352
    }
87 352
88
    public function getSchema(): SchemaInterface
89
    {
90 352
        if ($this->schema === null) {
91
            $this->schema = new Schema($this, $this->schemaCache);
92
        }
93 350
94
        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 350
    }
96 350
97
    /**
98
     * Initializes the DB connection.
99 350
     *
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
    protected function initConnection(): void
109
    {
110
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
111
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
112
        }
113 184
114
        $this->pdo = $this->driver->createConnection();
115 184
    }
116
}
117