-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathSqliteDriver.php
More file actions
252 lines (201 loc) · 6.9 KB
/
SqliteDriver.php
File metadata and controls
252 lines (201 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Database\Drivers;
use Nette;
/**
* Supplemental SQLite3 database driver.
*/
class SqliteDriver implements Nette\Database\ISupplementalDriver
{
use Nette\SmartObject;
/** @var Nette\Database\Connection */
private $connection;
/** @var string Datetime format */
private $fmtDateTime;
public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
$this->fmtDateTime = $options['formatDateTime'] ?? 'U';
}
public function convertException(\PDOException $e): Nette\Database\DriverException
{
$code = $e->errorInfo[1] ?? null;
$msg = $e->getMessage();
if ($code !== 19) {
return Nette\Database\DriverException::from($e);
} elseif (
strpos($msg, 'must be unique') !== false
|| strpos($msg, 'is not unique') !== false
|| strpos($msg, 'UNIQUE constraint failed') !== false
) {
return Nette\Database\UniqueConstraintViolationException::from($e);
} elseif (
strpos($msg, 'may not be null') !== false
|| strpos($msg, 'NOT NULL constraint failed') !== false
) {
return Nette\Database\NotNullConstraintViolationException::from($e);
} elseif (
strpos($msg, 'foreign key constraint failed') !== false
|| strpos($msg, 'FOREIGN KEY constraint failed') !== false
) {
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
} else {
return Nette\Database\ConstraintViolationException::from($e);
}
}
/********************* SQL ****************d*g**/
public function delimite(string $name): string
{
return '[' . strtr($name, '[]', ' ') . ']';
}
public function undelimite(string $name): string
{
$name = preg_replace('#(?:^|\.)\[#', '', $name);
// Can not be fully undelimited due to ambigious delimite() - was delimited "[x x]" name originally "x x" or "x[]x"?
return $name;
}
public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format($this->fmtDateTime);
}
public function formatDateInterval(\DateInterval $value): string
{
throw new Nette\NotSupportedException;
}
public function formatLike(string $value, int $pos): string
{
$value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_\\');
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
}
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{
if ($limit < 0 || $offset < 0) {
throw new Nette\InvalidArgumentException('Negative offset or limit.');
} elseif ($limit !== null || $offset) {
$sql .= ' LIMIT ' . ($limit === null ? '-1' : $limit)
. ($offset ? ' OFFSET ' . $offset : '');
}
}
/********************* reflection ****************d*g**/
public function getTables(): array
{
$tables = [];
foreach ($this->connection->query("
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
ORDER BY name
") as $row) {
$tables[] = [
'name' => $row->name,
'view' => (bool) $row->view,
];
}
return $tables;
}
public function getColumns(string $table): array
{
$meta = $this->connection->query("
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = {$this->connection->quote($table)}
UNION ALL
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = {$this->connection->quote($table)}
")->fetch();
$columns = [];
foreach ($this->connection->query("PRAGMA table_info({$this->delimite($table)})") as $row) {
$column = $row['name'];
$pattern = "/(\"$column\"|`$column`|\[$column\]|$column)\\s+[^,]+\\s+PRIMARY\\s+KEY\\s+AUTOINCREMENT/Ui";
$type = explode('(', $row['type']);
$columns[] = [
'name' => $column,
'table' => $table,
'nativetype' => strtoupper($type[0]),
'size' => isset($type[1]) ? (int) $type[1] : null,
'nullable' => $row['notnull'] == '0',
'default' => $row['dflt_value'],
'autoincrement' => $meta && preg_match($pattern, (string) $meta['sql']),
'primary' => $row['pk'] > 0,
'vendor' => (array) $row,
];
}
return $columns;
}
public function getIndexes(string $table): array
{
$indexes = [];
foreach ($this->connection->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
$indexes[$row['name']]['name'] = $row['name'];
$indexes[$row['name']]['unique'] = (bool) $row['unique'];
$indexes[$row['name']]['primary'] = false;
}
foreach ($indexes as $index => $values) {
$res = $this->connection->query("PRAGMA index_info({$this->delimite($index)})");
while ($row = $res->fetch()) {
$indexes[$index]['columns'][$row['seqno']] = $row['name'];
}
}
$columns = $this->getColumns($table);
foreach ($indexes as $index => $values) {
$column = $indexes[$index]['columns'][0];
foreach ($columns as $info) {
if ($column == $info['name']) {
$indexes[$index]['primary'] = (bool) $info['primary'];
break;
}
}
}
if (!$indexes) { // @see http://www.sqlite.org/lang_createtable.html#rowid
foreach ($columns as $column) {
if ($column['vendor']['pk']) {
$indexes[] = [
'name' => 'ROWID',
'unique' => true,
'primary' => true,
'columns' => [$column['name']],
];
break;
}
}
}
return array_values($indexes);
}
public function getForeignKeys(string $table): array
{
$keys = [];
foreach ($this->connection->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
$keys[$row['id']]['name'] = $row['id']; // foreign key name
$keys[$row['id']]['local'] = $row['from']; // local columns
$keys[$row['id']]['table'] = $row['table']; // referenced table
$keys[$row['id']]['foreign'] = $row['to']; // referenced columns
if ($keys[$row['id']]['foreign'][0] == null) {
$keys[$row['id']]['foreign'] = null;
}
}
return array_values($keys);
}
public function getColumnTypes(\PDOStatement $statement): array
{
$types = [];
$count = $statement->columnCount();
for ($col = 0; $col < $count; $col++) {
$meta = $statement->getColumnMeta($col);
if (isset($meta['sqlite:decl_type'])) {
if (in_array($meta['sqlite:decl_type'], ['DATE', 'DATETIME'], true)) {
$types[$meta['name']] = Nette\Database\IStructure::FIELD_UNIX_TIMESTAMP;
} else {
$types[$meta['name']] = Nette\Database\Helpers::detectType($meta['sqlite:decl_type']);
}
} elseif (isset($meta['native_type'])) {
$types[$meta['name']] = Nette\Database\Helpers::detectType($meta['native_type']);
}
}
return $types;
}
public function isSupported(string $item): bool
{
return $item === self::SUPPORT_MULTI_INSERT_AS_SELECT || $item === self::SUPPORT_SUBSELECT || $item === self::SUPPORT_MULTI_COLUMN_AS_OR_COND;
}
}