1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\Sqlite\Db; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableEntity; |
6
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TriggerEntity; |
7
|
|
|
|
8
|
|
|
use function strtoupper; |
9
|
|
|
use function preg_match; |
10
|
|
|
use function implode; |
11
|
|
|
|
12
|
|
|
trait TableTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string $table |
16
|
|
|
* |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
|
|
private function queryStatus(string $table = '') |
20
|
|
|
{ |
21
|
|
|
$query = "SELECT name AS Name, type AS Engine, 'rowid' AS Oid, '' AS Auto_increment " . |
22
|
|
|
"FROM sqlite_master WHERE type IN ('table', 'view') " . |
23
|
|
|
($table != "" ? "AND name = " . $this->driver->quote($table) : "ORDER BY name"); |
24
|
|
|
return $this->driver->rows($query); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $row |
29
|
|
|
* |
30
|
|
|
* @return TableEntity |
31
|
|
|
*/ |
32
|
|
|
private function makeStatus(array $row) |
33
|
|
|
{ |
34
|
|
|
$status = new TableEntity($row['Name']); |
35
|
|
|
$status->engine = $row['Engine']; |
36
|
|
|
$status->oid = $row['Oid']; |
37
|
|
|
// $status->Auto_increment = $row['Auto_increment']; |
38
|
|
|
$query = 'SELECT COUNT(*) FROM ' . $this->driver->escapeId($row['Name']); |
39
|
|
|
$status->rows = $this->connection->result($query); |
40
|
|
|
|
41
|
|
|
return $status; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function tableStatus(string $table, bool $fast = false) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$rows = $this->queryStatus($table); |
50
|
|
|
if (!($row = reset($rows))) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
return $this->makeStatus($row); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
|
|
public function tableStatuses(bool $fast = false) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$tables = []; |
62
|
|
|
$rows = $this->queryStatus(); |
63
|
|
|
foreach ($rows as $row) { |
64
|
|
|
$tables[$row['Name']] = $this->makeStatus($row); |
65
|
|
|
} |
66
|
|
|
return $tables; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function tableNames() |
73
|
|
|
{ |
74
|
|
|
$tables = []; |
75
|
|
|
$rows = $this->queryStatus(); |
76
|
|
|
foreach ($rows as $row) { |
77
|
|
|
$tables[] = $row['Name']; |
78
|
|
|
} |
79
|
|
|
return $tables; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function triggerOptions() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
"Timing" => ["BEFORE", "AFTER", "INSTEAD OF"], |
89
|
|
|
"Event" => ["INSERT", "UPDATE", "UPDATE OF", "DELETE"], |
90
|
|
|
"Type" => ["FOR EACH ROW"], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
|
|
public function trigger(string $name, string $table = '') |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if ($name == "") { |
100
|
|
|
return new TriggerEntity('', '', "BEGIN\n\t;\nEND"); |
101
|
|
|
} |
102
|
|
|
$idf = '(?:[^`"\s]+|`[^`]*`|"[^"]*")+'; |
103
|
|
|
$options = $this->triggerOptions(); |
104
|
|
|
preg_match("~^CREATE\\s+TRIGGER\\s*$idf\\s*(" . implode("|", $options["Timing"]) . |
105
|
|
|
")\\s+([a-z]+)(?:\\s+OF\\s+($idf))?\\s+ON\\s*$idf\\s*(?:FOR\\s+EACH\\s+ROW\\s)?(.*)~is", |
106
|
|
|
$this->connection->result("SELECT sql FROM sqlite_master WHERE type = 'trigger' AND name = " . |
107
|
|
|
$this->driver->quote($name)), $match); |
108
|
|
|
$of = $match[3]; |
109
|
|
|
return new TriggerEntity(strtoupper($match[1]), strtoupper($match[2]), $match[4], |
110
|
|
|
($of[0] == '`' || $of[0] == '"' ? $this->driver->unescapeId($of) : $of), $name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
public function triggers(string $table) |
117
|
|
|
{ |
118
|
|
|
$triggers = []; |
119
|
|
|
$options = $this->triggerOptions(); |
120
|
|
|
$query = "SELECT * FROM sqlite_master WHERE type = 'trigger' AND tbl_name = " . $this->driver->quote($table); |
121
|
|
|
foreach ($this->driver->rows($query) as $row) { |
122
|
|
|
preg_match('~^CREATE\s+TRIGGER\s*(?:[^`"\s]+|`[^`]*`|"[^"]*")+\s*(' . |
123
|
|
|
implode("|", $options["Timing"]) . ')\s*(.*?)\s+ON\b~i', $row["sql"], $match); |
124
|
|
|
$triggers[$row["name"]] = new TriggerEntity($match[1], $match[2], '', '', $row["name"]); |
125
|
|
|
} |
126
|
|
|
return $triggers; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
|
|
public function tableHelp(string $name) |
133
|
|
|
{ |
134
|
|
|
if ($name == "sqlite_sequence") { |
135
|
|
|
return "fileformat2.html#seqtab"; |
136
|
|
|
} |
137
|
|
|
if ($name == "sqlite_master") { |
138
|
|
|
return "fileformat2.html#$name"; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.