|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Driver\Facades; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Facade to table functions |
|
10
|
|
|
*/ |
|
11
|
|
|
trait TableTrait |
|
12
|
|
|
{ |
|
13
|
|
|
use AbstractTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Get the facade |
|
17
|
|
|
* |
|
18
|
|
|
* @return TableFacade |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function tableFacade(): TableFacade |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->di()->g(TableFacade::class); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get details about a table or a view |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $table The table name |
|
29
|
|
|
* |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getTableInfo(string $table): array |
|
33
|
|
|
{ |
|
34
|
|
|
$this->connectToSchema(); |
|
35
|
|
|
$this->breadcrumbs(true) |
|
36
|
|
|
->item($this->utils->trans->lang('Tables')) |
|
37
|
|
|
->item("<i><b>$table</b></i>"); |
|
38
|
|
|
$this->utils->input->table = $table; |
|
39
|
|
|
return $this->tableFacade()->getTableInfo($table); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get details about a table or a view |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $table The table name |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
* @throws Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getTableFields(string $table): array |
|
51
|
|
|
{ |
|
52
|
|
|
$this->connectToSchema(); |
|
53
|
|
|
$this->utils->input->table = $table; |
|
54
|
|
|
return $this->tableFacade()->getTableFields($table); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the indexes of a table |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $table The table name |
|
61
|
|
|
* |
|
62
|
|
|
* @return array|null |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getTableIndexes(string $table): ?array |
|
65
|
|
|
{ |
|
66
|
|
|
$this->connectToSchema(); |
|
67
|
|
|
$this->utils->input->table = $table; |
|
68
|
|
|
return $this->tableFacade()->getTableIndexes($table); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the foreign keys of a table |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $table The table name |
|
75
|
|
|
* |
|
76
|
|
|
* @return array|null |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getTableForeignKeys(string $table): ?array |
|
79
|
|
|
{ |
|
80
|
|
|
$this->connectToSchema(); |
|
81
|
|
|
$this->utils->input->table = $table; |
|
82
|
|
|
return $this->tableFacade()->getTableForeignKeys($table); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the triggers of a table |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $table The table name |
|
89
|
|
|
* |
|
90
|
|
|
* @return array|null |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getTableTriggers(string $table): ?array |
|
93
|
|
|
{ |
|
94
|
|
|
$this->connectToSchema(); |
|
95
|
|
|
$this->utils->input->table = $table; |
|
96
|
|
|
return $this->tableFacade()->getTableTriggers($table); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get required data for create/update on tables |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $table The table name |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
* @throws Exception |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getTableData(string $table = ''): array |
|
108
|
|
|
{ |
|
109
|
|
|
$this->connectToSchema(); |
|
110
|
|
|
$this->breadcrumbs(true)->item($this->utils->trans->lang('Tables')); |
|
111
|
|
|
if (!$table) { |
|
112
|
|
|
$this->breadcrumbs()->item($this->utils->trans->lang('Create table')); |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->breadcrumbs()->item("<i><b>$table</b></i>") |
|
115
|
|
|
->item($this->utils->trans->lang('Alter table')); |
|
116
|
|
|
} |
|
117
|
|
|
$this->utils->input->table = $table; |
|
118
|
|
|
return $this->tableFacade()->getTableData($table); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get fields for a new column |
|
123
|
|
|
* |
|
124
|
|
|
* @return TableFieldEntity |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getTableField(): TableFieldEntity |
|
127
|
|
|
{ |
|
128
|
|
|
$this->connectToSchema(); |
|
129
|
|
|
return $this->tableFacade()->getTableField(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get field types |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $type The type name |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getFieldTypes(string $type = ''): array |
|
140
|
|
|
{ |
|
141
|
|
|
// Must be called after the connection to a schema is made. |
|
142
|
|
|
// $this->connectToSchema(); |
|
143
|
|
|
return $this->tableFacade()->getFieldTypes($type); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Create a table |
|
148
|
|
|
* |
|
149
|
|
|
* @param array $values The table values |
|
150
|
|
|
* |
|
151
|
|
|
* @return array|null |
|
152
|
|
|
*/ |
|
153
|
|
|
public function createTable(array $values): ?array |
|
154
|
|
|
{ |
|
155
|
|
|
$this->connectToSchema(); |
|
156
|
|
|
$this->utils->input->table = $values['name']; |
|
157
|
|
|
$this->utils->input->values = $values; |
|
158
|
|
|
return $this->tableFacade()->createTable($values); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Alter a table |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $table The table name |
|
165
|
|
|
* @param array $values The table values |
|
166
|
|
|
* |
|
167
|
|
|
* @return array|null |
|
168
|
|
|
* @throws Exception |
|
169
|
|
|
*/ |
|
170
|
|
|
public function alterTable(string $table, array $values): ?array |
|
171
|
|
|
{ |
|
172
|
|
|
$this->connectToSchema(); |
|
173
|
|
|
$this->utils->input->table = $table; |
|
174
|
|
|
$this->utils->input->values = $values; |
|
175
|
|
|
return $this->tableFacade()->alterTable($table, $values); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Drop a table |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $table The table name |
|
182
|
|
|
* |
|
183
|
|
|
* @return array|null |
|
184
|
|
|
*/ |
|
185
|
|
|
public function dropTable(string $table): ?array |
|
186
|
|
|
{ |
|
187
|
|
|
$this->connectToSchema(); |
|
188
|
|
|
$this->utils->input->table = $table; |
|
189
|
|
|
return $this->tableFacade()->dropTable($table); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|