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