Passed
Push — dev ( c872e6...d79752 )
by Def
24:43 queued 20:57
created

ConnectionPDOSqlite::getPDO()   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\Cache\QueryCache;
9
use Yiisoft\Db\Cache\SchemaCache;
10
use Yiisoft\Db\Command\CommandInterface;
11
use Yiisoft\Db\Connection\ConnectionPDO;
12
use Yiisoft\Db\Driver\PDODriver;
13
use Yiisoft\Db\Exception\Exception;
14
use Yiisoft\Db\Exception\InvalidConfigException;
15
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...
16
use Yiisoft\Db\Schema\Quoter;
17
use Yiisoft\Db\Schema\QuoterInterface;
18
use Yiisoft\Db\Schema\SchemaInterface;
19
use Yiisoft\Db\Transaction\TransactionInterface;
20
21
use function constant;
22
23
/**
24
 * Database connection class prefilled for SQLite Server.
25
 */
26
final class ConnectionPDOSqlite extends ConnectionPDO
27
{
28 380
    public function __construct(
29
        protected PDODriver $driver,
30
        protected QueryCache $queryCache,
31
        protected SchemaCache $schemaCache
32
    ) {
33 380
        parent::__construct($queryCache);
34
    }
35
36 328
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
37
    {
38 328
        $command = new CommandPDOSqlite($this, $this->queryCache);
39
40 328
        if ($sql !== null) {
41 172
            $command->setSql($sql);
42
        }
43
44 328
        if ($this->logger !== null) {
45 328
            $command->setLogger($this->logger);
46
        }
47
48 328
        if ($this->profiler !== null) {
49 328
            $command->setProfiler($this->profiler);
50
        }
51
52 328
        return $command->bindValues($params);
53
    }
54
55 8
    public function createTransaction(): TransactionInterface
56
    {
57 8
        return new TransactionPDOSqlite($this);
58
    }
59
60 9
    public function getDriverName(): string
61
    {
62 9
        return 'sqlite';
63
    }
64
65
    /**
66
     * @throws Exception|InvalidConfigException
67
     */
68 328
    public function getQueryBuilder(): QueryBuilderInterface
69
    {
70 328
        if ($this->queryBuilder === null) {
71 328
            $this->queryBuilder = new QueryBuilderPDOSqlite(
72 328
                $this->createCommand(),
73 328
                $this->getQuoter(),
74 328
                $this->getSchema(),
75
            );
76
        }
77
78 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...
79
    }
80
81 344
    public function getQuoter(): QuoterInterface
82
    {
83 344
        if ($this->quoter === null) {
84 344
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
85
        }
86
87 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...
88
    }
89
90 342
    public function getSchema(): SchemaInterface
91
    {
92 342
        if ($this->schema === null) {
93 342
            $this->schema = new SchemaPDOSqlite($this, $this->schemaCache);
94
        }
95
96 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...
97
    }
98
99
    /**
100
     * Initializes the DB connection.
101
     *
102
     * This method is invoked right after the DB connection is established.
103
     *
104
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
105
     *
106
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
107
     *
108
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
109
     */
110 176
    protected function initConnection(): void
111
    {
112 176
        $this->pdo = $this->driver->createConnection();
113 176
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
114
115 176
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
116
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
117
        }
118
    }
119
}
120