Passed
Pull Request — master (#245)
by Wilmer
04:02
created

Connection::getSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
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 Yiisoft\Db\Driver\PDO\AbstractConnectionPDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
10
use Yiisoft\Db\Schema\Quoter;
11
use Yiisoft\Db\Schema\QuoterInterface;
12
use Yiisoft\Db\Schema\SchemaInterface;
13
use Yiisoft\Db\Sqlite\QueryBuilder;
14
use Yiisoft\Db\Sqlite\Schema;
15
use Yiisoft\Db\Transaction\TransactionInterface;
16
17
use function str_starts_with;
18
19
/**
20
 * Implements a connection to a database via PDO (PHP Data Objects) for SQLite Server.
21
 *
22
 * @link https://www.php.net/manual/en/ref.pdo-sqlite.php
23
 */
24
final class Connection extends AbstractConnectionPDO
25
{
26
    /**
27
     * Reset the connection after cloning.
28
     */
29 1
    public function __clone()
30
    {
31 1
        $this->transaction = null;
32
33 1
        if (!str_starts_with($this->driver->getDsn(), 'sqlite::memory:')) {
34
            /** Reset PDO connection, unless its sqlite in-memory, which can only have one connection. */
35 1
            $this->pdo = null;
36
        }
37
    }
38
39 286
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
40
    {
41 286
        $command = new Command($this);
42
43 286
        if ($sql !== null) {
44 249
            $command->setSql($sql);
45
        }
46
47 286
        if ($this->logger !== null) {
48 3
            $command->setLogger($this->logger);
49
        }
50
51 286
        if ($this->profiler !== null) {
52 4
            $command->setProfiler($this->profiler);
53
        }
54
55 286
        return $command->bindValues($params);
56
    }
57
58 15
    public function createTransaction(): TransactionInterface
59
    {
60 15
        return new Transaction($this);
61
    }
62
63 508
    public function getQueryBuilder(): QueryBuilderInterface
64
    {
65 508
        if ($this->queryBuilder === null) {
66 508
            $this->queryBuilder = new QueryBuilder(
67 508
                $this->getQuoter(),
68 508
                $this->getSchema(),
69 508
            );
70
        }
71
72 508
        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\QueryBuilder\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75 538
    public function getQuoter(): QuoterInterface
76
    {
77 538
        if ($this->quoter === null) {
78 538
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
79
        }
80
81 538
        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...
82
    }
83
84 536
    public function getSchema(): SchemaInterface
85
    {
86 536
        if ($this->schema === null) {
87 536
            $this->schema = new Schema($this, $this->schemaCache);
88
        }
89
90 536
        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...
91
    }
92
}
93