Passed
Branch master (deb69d)
by Wilmer
14:01 queued 01:23
created

ConnectionPDO::__clone()   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;
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
use function strncmp;
19
20
/**
21
 * Database connection class prefilled for SQLite Server.
22
 */
23
final class ConnectionPDO extends AbstractConnectionPDO
24
{
25
    /**
26
     * Reset the connection after cloning.
27
     */
28 1
    public function __clone()
29
    {
30 1
        $this->transaction = null;
31
32 1
        if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) {
33
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
34 1
            $this->pdo = null;
35
        }
36
    }
37
38 188
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
39
    {
40 188
        $command = new CommandPDO($this, $this->queryCache);
41
42 188
        if ($sql !== null) {
43 182
            $command->setSql($sql);
44
        }
45
46 188
        if ($this->logger !== null) {
47 188
            $command->setLogger($this->logger);
48
        }
49
50 188
        if ($this->profiler !== null) {
51 188
            $command->setProfiler($this->profiler);
52
        }
53
54 188
        return $command->bindValues($params);
55
    }
56
57 11
    public function createTransaction(): TransactionInterface
58
    {
59 11
        return new TransactionPDO($this);
60
    }
61
62
    /**
63
     * @throws Exception|InvalidConfigException
64
     */
65 338
    public function getQueryBuilder(): QueryBuilderInterface
66
    {
67 338
        if ($this->queryBuilder === null) {
68 338
            $this->queryBuilder = new QueryBuilder(
69 338
                $this->getQuoter(),
70 338
                $this->getSchema(),
71
            );
72
        }
73
74 338
        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...
75
    }
76
77 354
    public function getQuoter(): QuoterInterface
78
    {
79 354
        if ($this->quoter === null) {
80 354
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
81
        }
82
83 354
        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...
84
    }
85
86 353
    public function getSchema(): SchemaInterface
87
    {
88 353
        if ($this->schema === null) {
89 353
            $this->schema = new Schema($this, $this->schemaCache);
90
        }
91
92 353
        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...
93
    }
94
95
    /**
96
     * Initializes the DB connection.
97
     *
98
     * This method is invoked right after the DB connection is established.
99
     *
100
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
101
     *
102
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
103
     *
104
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
105
     */
106 187
    protected function initConnection(): void
107
    {
108 187
        if ($this->getEmulatePrepare() !== null) {
109
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
110
        }
111
112 187
        $this->pdo = $this->driver->createConnection();
113
    }
114
}
115