1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\Db; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Lagdo\DbAdmin\Driver\DriverInterface; |
7
|
|
|
use Lagdo\DbAdmin\Driver\Utils\Utils; |
8
|
|
|
|
9
|
|
|
use function trim; |
10
|
|
|
use function strtoupper; |
11
|
|
|
use function uniqid; |
12
|
|
|
|
13
|
|
|
abstract class Database implements DatabaseInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var DriverInterface |
17
|
|
|
*/ |
18
|
|
|
protected $driver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Utils |
22
|
|
|
*/ |
23
|
|
|
protected $utils; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The constructor |
27
|
|
|
* |
28
|
|
|
* @param DriverInterface $driver |
29
|
|
|
* @param Utils $utils |
30
|
|
|
*/ |
31
|
|
|
public function __construct(DriverInterface $driver, Utils $utils) |
32
|
|
|
{ |
33
|
|
|
$this->driver = $driver; |
34
|
|
|
$this->utils = $utils; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
|
|
public function dropViews(array $views) |
41
|
|
|
{ |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
|
|
public function moveTables(array $tables, array $views, string $target) |
49
|
|
|
{ |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function copyTables(array $tables, array $views, string $target) |
57
|
|
|
{ |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function truncateTables(array $tables) |
65
|
|
|
{ |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function createView(array $values) |
73
|
|
|
{ |
74
|
|
|
// From view.inc.php |
75
|
|
|
$name = trim($values['name']); |
76
|
|
|
$type = $values['materialized'] ? ' MATERIALIZED VIEW ' : ' VIEW '; |
77
|
|
|
|
78
|
|
|
$sql = ($this->driver->jush() === 'mssql' ? 'ALTER' : 'CREATE OR REPLACE') . |
79
|
|
|
$type . $this->driver->escapeTableName($name) . " AS\n" . $values['select']; |
80
|
|
|
return $this->driver->executeQuery($sql); |
81
|
|
|
} |
82
|
|
|
/** |
83
|
|
|
* Execute remembered queries |
84
|
|
|
* |
85
|
|
|
* @param bool $failed |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
* @throws Exception |
89
|
|
|
*/ |
90
|
|
|
private function executeSavedQuery(bool $failed): bool |
91
|
|
|
{ |
92
|
|
|
list($queries/*, $time*/) = $this->driver->queries(); |
93
|
|
|
return $this->driver->executeQuery($queries, false, $failed/*, $time*/); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Drop old object and create a new one |
98
|
|
|
* |
99
|
|
|
* @param string $drop Drop old object query |
100
|
|
|
* @param string $create Create new object query |
101
|
|
|
* @param string $dropCreated Drop new object query |
102
|
|
|
* @param string $test Create test object query |
103
|
|
|
* @param string $dropTest Drop test object query |
104
|
|
|
* @param string $oldName |
105
|
|
|
* @param string $newName |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
* @throws Exception |
109
|
|
|
*/ |
110
|
|
|
private function dropAndCreate(string $drop, string $create, string $dropCreated, |
111
|
|
|
string $test, string $dropTest, string $oldName, string $newName): string |
112
|
|
|
{ |
113
|
|
|
if ($oldName == '' && $newName == '') { |
114
|
|
|
$this->driver->executeQuery($drop); |
115
|
|
|
return 'dropped'; |
116
|
|
|
} |
117
|
|
|
if ($oldName == '') { |
118
|
|
|
$this->driver->executeQuery($create); |
119
|
|
|
return 'created'; |
120
|
|
|
} |
121
|
|
|
if ($oldName != $newName) { |
122
|
|
|
$created = $this->driver->execute($create); |
123
|
|
|
$dropped = $this->driver->execute($drop); |
124
|
|
|
// $this->executeSavedQuery(!($created && $this->driver->execute($drop))); |
125
|
|
|
if (!$dropped && $created) { |
126
|
|
|
$this->driver->execute($dropCreated); |
127
|
|
|
} |
128
|
|
|
return 'altered'; |
129
|
|
|
} |
130
|
|
|
$this->executeSavedQuery(!($this->driver->execute($test) && |
131
|
|
|
$this->driver->execute($dropTest) && |
132
|
|
|
$this->driver->execute($drop) && $this->driver->execute($create))); |
133
|
|
|
return 'altered'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
139
|
|
|
public function updateView(string $view, array $values): string |
140
|
|
|
{ |
141
|
|
|
// From view.inc.php |
142
|
|
|
$origType = 'VIEW'; |
143
|
|
|
if ($this->driver->jush() === 'pgsql') { |
144
|
|
|
$status = $this->driver->tableStatus($view); |
145
|
|
|
$origType = strtoupper($status->engine); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$name = trim($values['name']); |
149
|
|
|
$type = $values['materialized'] ? 'MATERIALIZED VIEW' : 'VIEW'; |
150
|
|
|
$tempName = $name . '_adminer_' . uniqid(); |
151
|
|
|
|
152
|
|
|
return $this->dropAndCreate("DROP $origType " . $this->driver->escapeTableName($view), |
153
|
|
|
"CREATE $type " . $this->driver->escapeTableName($name) . " AS\n" . $values['select'], |
154
|
|
|
"DROP $type " . $this->driver->escapeTableName($name), |
155
|
|
|
"CREATE $type " . $this->driver->escapeTableName($tempName) . " AS\n" . $values['select'], |
156
|
|
|
"DROP $type " . $this->driver->escapeTableName($tempName), $view, $name); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Drop a view |
161
|
|
|
* |
162
|
|
|
* @param string $view The view name |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
* @throws Exception |
166
|
|
|
*/ |
167
|
|
|
public function dropView(string $view): bool |
168
|
|
|
{ |
169
|
|
|
// From view.inc.php |
170
|
|
|
$origType = 'VIEW'; |
171
|
|
|
if ($this->driver->jush() == 'pgsql') { |
172
|
|
|
$status = $this->driver->tableStatus($view); |
173
|
|
|
$origType = strtoupper($status->engine); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$sql = "DROP $origType " . $this->driver->escapeTableName($view); |
177
|
|
|
return $this->driver->executeQuery($sql); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritDoc |
182
|
|
|
*/ |
183
|
|
|
public function sequences() |
184
|
|
|
{ |
185
|
|
|
return []; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @inheritDoc |
190
|
|
|
*/ |
191
|
|
|
public function userTypes() |
192
|
|
|
{ |
193
|
|
|
return []; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @inheritDoc |
198
|
|
|
*/ |
199
|
|
|
public function schemas() |
200
|
|
|
{ |
201
|
|
|
return []; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @inheritDoc |
206
|
|
|
*/ |
207
|
|
|
public function events() |
208
|
|
|
{ |
209
|
|
|
return []; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritDoc |
214
|
|
|
*/ |
215
|
|
|
public function routine(string $name, string $type) |
216
|
|
|
{ |
217
|
|
|
return null; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @inheritDoc |
222
|
|
|
*/ |
223
|
|
|
public function routines() |
224
|
|
|
{ |
225
|
|
|
return []; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritDoc |
230
|
|
|
*/ |
231
|
|
|
public function routineId(string $name, array $row) |
232
|
|
|
{ |
233
|
|
|
return ''; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|