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