Passed
Push — dev ( d79752...59496e )
by Def
20:02 queued 16:27
created

ConnectionPDOSqlite::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 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\Command\CommandInterface;
9
use Yiisoft\Db\Connection\ConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
use function constant;
19
use function strncmp;
20
21
/**
22
 * Database connection class prefilled for SQLite Server.
23
 */
24
final class ConnectionPDOSqlite extends ConnectionPDO
25
{
26
    /**
27
     * Reset the connection after cloning.
28
     */
29 1
    public function __clone()
30
    {
31 1
        $this->transaction = null;
32
33 1
        if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) {
34
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
35 1
            $this->pdo = null;
36
        }
37
    }
38
39 328
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
40
    {
41 328
        $command = new CommandPDOSqlite($this, $this->queryCache);
42
43 328
        if ($sql !== null) {
44 172
            $command->setSql($sql);
45
        }
46
47 328
        if ($this->logger !== null) {
48 328
            $command->setLogger($this->logger);
49
        }
50
51 328
        if ($this->profiler !== null) {
52 328
            $command->setProfiler($this->profiler);
53
        }
54
55 328
        return $command->bindValues($params);
56
    }
57
58 8
    public function createTransaction(): TransactionInterface
59
    {
60 8
        return new TransactionPDOSqlite($this);
61
    }
62
63 9
    public function getDriverName(): string
64
    {
65 9
        return 'sqlite';
66
    }
67
68
    /**
69
     * @throws Exception|InvalidConfigException
70
     */
71 328
    public function getQueryBuilder(): QueryBuilderInterface
72
    {
73 328
        if ($this->queryBuilder === null) {
74 328
            $this->queryBuilder = new QueryBuilderPDOSqlite(
75 328
                $this->createCommand(),
76 328
                $this->getQuoter(),
77 328
                $this->getSchema(),
78
            );
79
        }
80
81 328
        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 344
    public function getQuoter(): QuoterInterface
85
    {
86 344
        if ($this->quoter === null) {
87 344
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
88
        }
89
90 344
        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 342
    public function getSchema(): SchemaInterface
94
    {
95 342
        if ($this->schema === null) {
96 342
            $this->schema = new SchemaPDOSqlite($this, $this->schemaCache);
97
        }
98
99 342
        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 176
    protected function initConnection(): void
114
    {
115 176
        $this->pdo = $this->driver->createConnection();
116 176
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
117
118 176
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
119
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
120
        }
121
    }
122
}
123