Passed
Push — master ( 2b2b59...ef2215 )
by Wilmer
04:20
created

Connection::createCommand()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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