1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Database\Schema; |
4
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Database\Schema\Column; |
6
|
|
|
use CodexShaper\DBM\Database\Schema\ForeignKey; |
7
|
|
|
use CodexShaper\DBM\Database\Schema\Index; |
8
|
|
|
use CodexShaper\DBM\Database\Schema\UpdateManager; |
9
|
|
|
use CodexShaper\DBM\Database\Types\Type; |
10
|
|
|
use CodexShaper\DBM\Facades\Driver; |
11
|
|
|
use CodexShaper\DBM\Facades\MongoDB; |
12
|
|
|
use Doctrine\DBAL\Schema\SchemaException; |
13
|
|
|
use Doctrine\DBAL\Schema\Table as DoctrineTable; |
14
|
|
|
use Illuminate\Database\Eloquent\Collection; |
15
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
16
|
|
|
use Illuminate\Pagination\Paginator; |
17
|
|
|
|
18
|
|
|
class Table |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Returns a list of all tables in the current database. |
22
|
|
|
* |
23
|
|
|
* @return array|string[] |
24
|
|
|
*/ |
25
|
|
|
public static function all() |
26
|
|
|
{ |
27
|
|
|
// MongoDb |
28
|
|
|
if (Driver::isMongoDB()) { |
29
|
|
|
return MongoDB::getCollectionNames(); |
30
|
|
|
} |
31
|
|
|
return SchemaManager::getInstance()->listTableNames(); |
32
|
|
|
} |
33
|
|
|
/** |
34
|
|
|
* Get table details |
35
|
|
|
* |
36
|
|
|
* @param string $tableName |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public static function getTable($tableName) |
41
|
|
|
{ |
42
|
|
|
if (Driver::isMongoDB()) { |
43
|
|
|
return MongoDB::getCollection($tableName); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
Type::registerCustomTypes(); |
47
|
|
|
|
48
|
|
|
$table = SchemaManager::getInstance()->listTableDetails($tableName); |
49
|
|
|
$primaryKeyName = $table->getPrimaryKey(); |
50
|
|
|
$options = $table->getOptions(); |
51
|
|
|
|
52
|
|
|
return [ |
53
|
|
|
'name' => $table->getName(), |
54
|
|
|
'oldName' => $table->getName(), |
55
|
|
|
'columns' => static::getColumns($table), |
56
|
|
|
'indexes' => static::getIndexes($table), |
57
|
|
|
'foreignKeys' => static::getForeignKeys($table), |
58
|
|
|
'primaryKeyName' => $primaryKeyName, |
59
|
|
|
'options' => $options, |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* Get column names |
64
|
|
|
* |
65
|
|
|
* @param string $tableName |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public static function getColumnsName($tableName) |
70
|
|
|
{ |
71
|
|
|
$columns = static::getTable($tableName)['columns']; |
72
|
|
|
|
73
|
|
|
$columnsName = []; |
74
|
|
|
|
75
|
|
|
foreach ($columns as $column) { |
76
|
|
|
$columnsName[] = $column->name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $columnsName; |
80
|
|
|
} |
81
|
|
|
/** |
82
|
|
|
* Create new table |
83
|
|
|
* |
84
|
|
|
* @param array $table |
85
|
|
|
* |
86
|
|
|
* @return array|object|void |
87
|
|
|
*/ |
88
|
|
|
public static function create($table) |
89
|
|
|
{ |
90
|
|
|
if (!is_array($table)) { |
|
|
|
|
91
|
|
|
$table = json_decode($table, true); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// MongoDB |
95
|
|
|
if (Driver::isMongoDB()) { |
96
|
|
|
return MongoDB::createCollection($table['name']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$newTable = self::prepareTable($table); |
100
|
|
|
|
101
|
|
|
$schema = SchemaManager::getInstance(); |
102
|
|
|
$schema->createTable($newTable); |
103
|
|
|
} |
104
|
|
|
/** |
105
|
|
|
* Update table |
106
|
|
|
* |
107
|
|
|
* @param array $table |
108
|
|
|
* |
109
|
|
|
* @return true|void |
110
|
|
|
*/ |
111
|
|
|
public static function update($table) |
112
|
|
|
{ |
113
|
|
|
if (!is_array($table)) { |
|
|
|
|
114
|
|
|
$table = json_decode($table, true); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (Driver::isMongoDB()) { |
118
|
|
|
return MongoDB::updateCollection($table); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
(new UpdateManager())->update($table); |
122
|
|
|
} |
123
|
|
|
/** |
124
|
|
|
* Drop table |
125
|
|
|
* |
126
|
|
|
* @param string $tableName |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public static function drop($tableName) |
131
|
|
|
{ |
132
|
|
|
if (Driver::isMongoDB()) { |
133
|
|
|
return MongoDB::dropCollection($tableName); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return SchemaManager::getInstance()->dropTable($tableName); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
/** |
139
|
|
|
* Prepare table |
140
|
|
|
* |
141
|
|
|
* @param array $table |
142
|
|
|
* |
143
|
|
|
* @return \Doctrine\DBAL\Schema\Table |
144
|
|
|
*/ |
145
|
|
|
public static function prepareTable($table) |
146
|
|
|
{ |
147
|
|
|
|
148
|
|
|
if (!is_array($table)) { |
|
|
|
|
149
|
|
|
$table = json_decode($table, true); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
Type::registerCustomTypes(); |
153
|
|
|
|
154
|
|
|
$conn = 'database.connections.' . config('database.default'); |
155
|
|
|
|
156
|
|
|
$table['options']['collate'] = $table['options']['collation'] ?? config($conn . '.collation', 'utf8mb4_unicode_ci'); |
157
|
|
|
if (Driver::isMysql()) { |
158
|
|
|
$table['options']['charset'] = $table['options']['charset'] ?? config($conn . '.charset', 'utf8mb4'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$tableName = $table['name']; |
162
|
|
|
$columns = $table['columns']; |
163
|
|
|
$indexes = $table['indexes']; |
164
|
|
|
$foreignKeys = $table['foreignKeys']; |
165
|
|
|
|
166
|
|
|
// Make Doctrain columns |
167
|
|
|
$DoctrineColumns = []; |
168
|
|
|
foreach ($columns as $column) { |
169
|
|
|
$DoctrineColumn = Column::create($column); |
170
|
|
|
$DoctrineColumns[$DoctrineColumn->getName()] = $DoctrineColumn; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// Make Doctrain indexes |
174
|
|
|
$DoctrineIndexes = []; |
175
|
|
|
foreach ($indexes as $index) { |
176
|
|
|
$DoctrineIndex = Index::create($index); |
177
|
|
|
$DoctrineIndexes[$DoctrineIndex->getName()] = $DoctrineIndex; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// Make Doctrain Foreign Keys |
181
|
|
|
$DoctrineForeignKeys = []; |
182
|
|
|
foreach ($foreignKeys as $foreignKey) { |
183
|
|
|
|
184
|
|
|
$DoctrineForeignKey = ForeignKey::create($foreignKey); |
185
|
|
|
|
186
|
|
|
$DoctrineForeignKeys[$DoctrineForeignKey->getName()] = $DoctrineForeignKey; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$options = $table['options']; |
190
|
|
|
|
191
|
|
|
return new DoctrineTable($tableName, $DoctrineColumns, $DoctrineIndexes, $DoctrineForeignKeys, false, $options); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
/** |
194
|
|
|
* Get all columns |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public static function getColumns(DoctrineTable $table) |
199
|
|
|
{ |
200
|
|
|
$columns = []; |
201
|
|
|
|
202
|
|
|
$order = 1; |
203
|
|
|
foreach ($table->getColumns() as $column) { |
204
|
|
|
$columns[] = (object) array_merge( |
205
|
|
|
Column::toArray($column), |
206
|
|
|
array('order' => $order) |
207
|
|
|
); |
208
|
|
|
$order++; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $columns; |
212
|
|
|
} |
213
|
|
|
/** |
214
|
|
|
* Get all indexes |
215
|
|
|
* |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
|
|
public static function getIndexes(DoctrineTable $table) |
219
|
|
|
{ |
220
|
|
|
$indexes = []; |
221
|
|
|
|
222
|
|
|
foreach ($table->getIndexes() as $index) { |
223
|
|
|
$indexes[] = (object) array_merge( |
224
|
|
|
Index::toArray($index), |
225
|
|
|
array('table' => $table->getName()) |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $indexes; |
230
|
|
|
} |
231
|
|
|
/** |
232
|
|
|
* Get all foreign keys |
233
|
|
|
* |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
|
|
public static function getForeignKeys(DoctrineTable $table) |
237
|
|
|
{ |
238
|
|
|
$foreignKeys = []; |
239
|
|
|
|
240
|
|
|
foreach ($table->getForeignKeys() as $name => $foreignKey) { |
241
|
|
|
$foreignKeys[$name] = ForeignKey::toArray($foreignKey); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $foreignKeys; |
245
|
|
|
} |
246
|
|
|
/** |
247
|
|
|
* Check table exists or not |
248
|
|
|
* |
249
|
|
|
* @param string $tableName |
250
|
|
|
* |
251
|
|
|
* @return boolean |
252
|
|
|
*/ |
253
|
|
|
public static function exists($tableName) |
254
|
|
|
{ |
255
|
|
|
if (Driver::isMongoDB()) { |
256
|
|
|
return MongoDB::hasCollection($tableName); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if (!SchemaManager::getInstance()->tablesExist($tableName)) { |
260
|
|
|
throw SchemaException::tableDoesNotExist($tableName); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return true; |
264
|
|
|
} |
265
|
|
|
/** |
266
|
|
|
* Get tables with pagination |
267
|
|
|
* |
268
|
|
|
* @param int $perPage |
269
|
|
|
* @param int|null $page |
270
|
|
|
* @param array $options |
271
|
|
|
* @param string $query |
272
|
|
|
* |
273
|
|
|
* @return \Illuminate\Pagination\LengthAwarePaginator |
274
|
|
|
*/ |
275
|
|
|
public static function paginate($perPage = 15, $page = null, $options = [], $query = "") |
276
|
|
|
{ |
277
|
|
|
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1); |
278
|
|
|
$options['path'] = Paginator::resolveCurrentPath(); |
279
|
|
|
$items = static::all(); |
280
|
|
|
$collection = $items instanceof Collection ? $items : Collection::make($items); |
|
|
|
|
281
|
|
|
if (!empty($query)) { |
282
|
|
|
$collection = $collection->filter(function ($value, $key) use ($query) { |
|
|
|
|
283
|
|
|
return false !== stristr($value, $query); |
284
|
|
|
}); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return new LengthAwarePaginator($collection->forPage($page, $perPage), $collection->count(), $perPage, $page, $options); |
288
|
|
|
|
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|