1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Facades; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
use function compact; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Facade to view functions |
11
|
|
|
*/ |
12
|
|
|
class ViewFacade extends AbstractFacade |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The current table status |
16
|
|
|
* |
17
|
|
|
* @var mixed |
18
|
|
|
*/ |
19
|
|
|
protected $viewStatus = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the current table status |
23
|
|
|
* |
24
|
|
|
* @param string $table |
25
|
|
|
* |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
protected function status(string $table) |
29
|
|
|
{ |
30
|
|
|
if (!$this->viewStatus) { |
31
|
|
|
$this->viewStatus = $this->driver->tableStatusOrName($table, true); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
return $this->viewStatus; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get details about a view |
38
|
|
|
* |
39
|
|
|
* @param string $view The view name |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getViewInfo(string $view): array |
44
|
|
|
{ |
45
|
|
|
// From table.inc.php |
46
|
|
|
$status = $this->status($view); |
47
|
|
|
$name = $this->admin->tableName($status); |
48
|
|
|
$title = ($status->engine == 'materialized view' ? $this->utils->trans->lang('Materialized view') : |
49
|
|
|
$this->utils->trans->lang('View')) . ': ' . ($name != '' ? $name : $this->utils->str->html($view)); |
50
|
|
|
|
51
|
|
|
$comment = $status->comment; |
52
|
|
|
|
53
|
|
|
$tabs = [ |
54
|
|
|
'fields' => $this->utils->trans->lang('Columns'), |
55
|
|
|
// 'indexes' => $this->utils->trans->lang('Indexes'), |
56
|
|
|
// 'foreign-keys' => $this->utils->trans->lang('Foreign keys'), |
57
|
|
|
// 'triggers' => $this->utils->trans->lang('Triggers'), |
58
|
|
|
]; |
59
|
|
|
if ($this->driver->support('view_trigger')) { |
60
|
|
|
$tabs['triggers'] = $this->utils->trans->lang('Triggers'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return compact('title', 'comment', 'tabs'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the fields of a table or a view |
68
|
|
|
* |
69
|
|
|
* @param string $view The view name |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
* @throws Exception |
73
|
|
|
*/ |
74
|
|
|
public function getViewFields(string $view): array |
75
|
|
|
{ |
76
|
|
|
// From table.inc.php |
77
|
|
|
$fields = $this->driver->fields($view); |
78
|
|
|
if (empty($fields)) { |
79
|
|
|
throw new Exception($this->driver->error()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$tabs = [ |
83
|
|
|
'fields' => $this->utils->trans->lang('Columns'), |
84
|
|
|
// 'triggers' => $this->utils->trans->lang('Triggers'), |
85
|
|
|
]; |
86
|
|
|
if ($this->driver->support('view_trigger')) { |
87
|
|
|
$tabs['triggers'] = $this->utils->trans->lang('Triggers'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$headers = [ |
91
|
|
|
$this->utils->trans->lang('Name'), |
92
|
|
|
$this->utils->trans->lang('Type'), |
93
|
|
|
$this->utils->trans->lang('Collation'), |
94
|
|
|
]; |
95
|
|
|
$hasComment = $this->driver->support('comment'); |
96
|
|
|
if ($hasComment) { |
97
|
|
|
$headers[] = $this->utils->trans->lang('Comment'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$details = []; |
101
|
|
|
foreach ($fields as $field) { |
102
|
|
|
$type = $this->utils->str->html($field->fullType); |
103
|
|
|
if ($field->null) { |
104
|
|
|
$type .= ' <i>nullable</i>'; // ' <i>NULL</i>'; |
105
|
|
|
} |
106
|
|
|
if ($field->autoIncrement) { |
107
|
|
|
$type .= ' <i>' . $this->utils->trans->lang('Auto Increment') . '</i>'; |
108
|
|
|
} |
109
|
|
|
if ($field->hasDefault) { |
110
|
|
|
$type .= /*' ' . $this->utils->trans->lang('Default value') .*/ ' [<b>' . $this->utils->str->html($field->default) . '</b>]'; |
111
|
|
|
} |
112
|
|
|
$detail = [ |
113
|
|
|
'name' => $this->utils->str->html($field->name), |
114
|
|
|
'type' => $type, |
115
|
|
|
'collation' => $this->utils->str->html($field->collation), |
116
|
|
|
]; |
117
|
|
|
if ($hasComment) { |
118
|
|
|
$detail['comment'] = $this->utils->str->html($field->comment); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$details[] = $detail; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return compact('headers', 'details'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the triggers of a table |
129
|
|
|
* |
130
|
|
|
* @param string $view The view name |
131
|
|
|
* |
132
|
|
|
* @return array|null |
133
|
|
|
*/ |
134
|
|
|
public function getViewTriggers(string $view): ?array |
135
|
|
|
{ |
136
|
|
|
if (!$this->driver->support('view_trigger')) { |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$headers = [ |
141
|
|
|
$this->utils->trans->lang('Name'), |
142
|
|
|
' ', |
143
|
|
|
' ', |
144
|
|
|
' ', |
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
$details = []; |
148
|
|
|
// From table.inc.php |
149
|
|
|
$triggers = $this->driver->triggers($view); |
150
|
|
|
foreach ($triggers as $name => $trigger) { |
151
|
|
|
$details[] = [ |
152
|
|
|
$this->utils->str->html($trigger->timing), |
153
|
|
|
$this->utils->str->html($trigger->event), |
154
|
|
|
$this->utils->str->html($name), |
155
|
|
|
$this->utils->trans->lang('Alter'), |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return compact('headers', 'details'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get a view |
164
|
|
|
* |
165
|
|
|
* @param string $view The view name |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
* @throws Exception |
169
|
|
|
*/ |
170
|
|
|
public function getView(string $view): array |
171
|
|
|
{ |
172
|
|
|
$values = $this->driver->view($view); |
173
|
|
|
$error = $this->driver->error(); |
174
|
|
|
if (($error)) { |
175
|
|
|
throw new Exception($error); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return [ |
179
|
|
|
'view' => $values, |
180
|
|
|
'materialized' => $this->driver->support('materializedview'), |
181
|
|
|
]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Create a view |
186
|
|
|
* |
187
|
|
|
* @param array $values The view values |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
* @throws Exception |
191
|
|
|
*/ |
192
|
|
|
public function createView(array $values): array |
193
|
|
|
{ |
194
|
|
|
$success = $this->driver->createView($values); |
195
|
|
|
$message = $this->utils->trans->lang('View has been created.'); |
196
|
|
|
$error = $this->driver->error(); |
197
|
|
|
|
198
|
|
|
return compact('success', 'message', 'error'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Update a view |
203
|
|
|
* |
204
|
|
|
* @param string $view The view name |
205
|
|
|
* @param array $values The view values |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
* @throws Exception |
209
|
|
|
*/ |
210
|
|
|
public function updateView(string $view, array $values): array |
211
|
|
|
{ |
212
|
|
|
$result = $this->driver->updateView($view, $values); |
213
|
|
|
$message = $this->utils->trans->lang("View has been $result."); |
214
|
|
|
$error = $this->driver->error(); |
215
|
|
|
$success = !$error; |
216
|
|
|
|
217
|
|
|
return compact('success', 'message', 'error'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Drop a view |
222
|
|
|
* |
223
|
|
|
* @param string $view The view name |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
* @throws Exception |
227
|
|
|
*/ |
228
|
|
|
public function dropView(string $view): array |
229
|
|
|
{ |
230
|
|
|
$success = $this->driver->dropView($view); |
231
|
|
|
$message = $this->utils->trans->lang('View has been dropped.'); |
232
|
|
|
$error = $this->driver->error(); |
233
|
|
|
|
234
|
|
|
return compact('success', 'message', 'error'); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.