Passed
Branch master (6554fc)
by Wilmer
30:37 queued 16:38
created

ConnectionPDO::initConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use PDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
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
/**
19
 * Database connection class prefilled for SQLite Server.
20
 */
21
final class ConnectionPDO extends AbstractConnectionPDO
22
{
23
    /**
24
     * Reset the connection after cloning.
25
     */
26 1
    public function __clone()
27
    {
28 1
        $this->transaction = null;
29
30 1
        if (!str_starts_with($this->driver->getDsn(), 'sqlite::memory:')) {
31
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
32 1
            $this->pdo = null;
33
        }
34
    }
35
36 231
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
37
    {
38 231
        $command = new CommandPDO($this, $this->queryCache);
39
40 231
        if ($sql !== null) {
41 199
            $command->setSql($sql);
42
        }
43
44 231
        if ($this->logger !== null) {
45 97
            $command->setLogger($this->logger);
46
        }
47
48 231
        if ($this->profiler !== null) {
49 97
            $command->setProfiler($this->profiler);
50
        }
51
52 231
        return $command->bindValues($params);
53
    }
54
55 12
    public function createTransaction(): TransactionInterface
56
    {
57 12
        return new TransactionPDO($this);
58
    }
59
60
    /**
61
     * @throws Exception|InvalidConfigException
62
     */
63 449
    public function getQueryBuilder(): QueryBuilderInterface
64
    {
65 449
        if ($this->queryBuilder === null) {
66 449
            $this->queryBuilder = new QueryBuilder(
67 449
                $this->getQuoter(),
68 449
                $this->getSchema(),
69
            );
70
        }
71
72 449
        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 487
    public function getQuoter(): QuoterInterface
76
    {
77 487
        if ($this->quoter === null) {
78 487
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
79
        }
80
81 487
        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 470
    public function getSchema(): SchemaInterface
85
    {
86 470
        if ($this->schema === null) {
87 470
            $this->schema = new Schema($this, $this->schemaCache);
88
        }
89
90 470
        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