|
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
|
|
|
public static function all() |
|
21
|
|
|
{ |
|
22
|
|
|
// MongoDb |
|
23
|
|
|
if (Driver::isMongoDB()) { |
|
|
|
|
|
|
24
|
|
|
return MongoDB::getCollectionNames(); |
|
25
|
|
|
} |
|
26
|
|
|
return SchemaManager::getInstance()->listTableNames(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function getTable($tableName) |
|
30
|
|
|
{ |
|
31
|
|
|
if (Driver::isMongoDB()) { |
|
32
|
|
|
return MongoDB::getCollection($tableName); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
Type::registerCustomTypes(); |
|
36
|
|
|
|
|
37
|
|
|
$table = SchemaManager::getInstance()->listTableDetails($tableName); |
|
38
|
|
|
$primaryKeyName = $table->getPrimaryKey(); |
|
39
|
|
|
$options = $table->getOptions(); |
|
40
|
|
|
|
|
41
|
|
|
return [ |
|
42
|
|
|
'name' => $table->getName(), |
|
43
|
|
|
'oldName' => $table->getName(), |
|
44
|
|
|
'columns' => static::getColumns($table), |
|
45
|
|
|
'indexes' => static::getIndexes($table), |
|
46
|
|
|
'foreignKeys' => static::getForeignKeys($table), |
|
47
|
|
|
'primaryKeyName' => $primaryKeyName, |
|
48
|
|
|
'options' => $options, |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function getColumnsName($tableName) |
|
53
|
|
|
{ |
|
54
|
|
|
$columns = static::getTable($tableName)['columns']; |
|
55
|
|
|
|
|
56
|
|
|
$columnsName = []; |
|
57
|
|
|
|
|
58
|
|
|
foreach ($columns as $column) { |
|
59
|
|
|
$columnsName[] = $column->name; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $columnsName; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public static function create($table = []) |
|
66
|
|
|
{ |
|
67
|
|
|
if (!is_array($table)) { |
|
68
|
|
|
$table = json_decode($table, true); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// MongoDB |
|
72
|
|
|
if (Driver::isMongoDB()) { |
|
73
|
|
|
return MongoDB::createCollection($table['name']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$newTable = self::prepareTable($table); |
|
77
|
|
|
|
|
78
|
|
|
$schema = SchemaManager::getInstance(); |
|
79
|
|
|
return $schema->createTable($newTable); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public static function update($table = []) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!is_array($table)) { |
|
85
|
|
|
$table = json_decode($table, true); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (Driver::isMongoDB()) { |
|
89
|
|
|
return MongoDB::updateCollection($table); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
(new UpdateManager())->update($table); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public static function drop($tableName) |
|
96
|
|
|
{ |
|
97
|
|
|
if (Driver::isMongoDB()) { |
|
98
|
|
|
return MongoDB::dropCollection($tableName); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return SchemaManager::getInstance()->dropTable($tableName); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function prepareTable($table = []) |
|
105
|
|
|
{ |
|
106
|
|
|
|
|
107
|
|
|
if (!is_array($table)) { |
|
108
|
|
|
$table = json_decode($table, true); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
Type::registerCustomTypes(); |
|
112
|
|
|
|
|
113
|
|
|
// DoctrineType::addType('varchar', 'App\CodexShaper\Database\Types\Common\VarCharType'); |
|
114
|
|
|
// SchemaManager::getInstance()->getDatabasePlatform()->registerDoctrineTypeMapping('db_varchar', 'varchar'); |
|
115
|
|
|
|
|
116
|
|
|
$conn = 'database.connections.' . config('database.default'); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
$table['options']['collate'] = config($conn . '.collation', 'utf8mb4_unicode_ci'); |
|
119
|
|
|
$table['options']['charset'] = config($conn . '.charset', 'utf8mb4'); |
|
120
|
|
|
|
|
121
|
|
|
$tableName = $table['name']; |
|
122
|
|
|
$columns = $table['columns']; |
|
123
|
|
|
$indexes = $table['indexes']; |
|
124
|
|
|
$foreignKeys = $table['foreignKeys']; |
|
125
|
|
|
|
|
126
|
|
|
// Make Doctrain columns |
|
127
|
|
|
$DoctrineColumns = []; |
|
128
|
|
|
foreach ($columns as $column) { |
|
129
|
|
|
$DoctrineColumn = Column::create($column); |
|
130
|
|
|
$DoctrineColumns[$DoctrineColumn->getName()] = $DoctrineColumn; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Make Doctrain indexes |
|
134
|
|
|
$DoctrineIndexes = []; |
|
135
|
|
|
foreach ($indexes as $index) { |
|
136
|
|
|
$DoctrineIndex = Index::create($index); |
|
137
|
|
|
$DoctrineIndexes[$DoctrineIndex->getName()] = $DoctrineIndex; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Make Doctrain Foreign Keys |
|
141
|
|
|
$DoctrineForeignKeys = []; |
|
142
|
|
|
foreach ($foreignKeys as $foreignKey) { |
|
143
|
|
|
|
|
144
|
|
|
$DoctrineForeignKey = ForeignKey::create($foreignKey); |
|
145
|
|
|
|
|
146
|
|
|
$DoctrineForeignKeys[$DoctrineForeignKey->getName()] = $DoctrineForeignKey; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$options = $table['options']; |
|
150
|
|
|
|
|
151
|
|
|
return new DoctrineTable($tableName, $DoctrineColumns, $DoctrineIndexes, $DoctrineForeignKeys, false, $options); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public static function getColumns(DoctrineTable $table) |
|
155
|
|
|
{ |
|
156
|
|
|
$columns = []; |
|
157
|
|
|
|
|
158
|
|
|
$order = 1; |
|
159
|
|
|
foreach ($table->getColumns() as $column) { |
|
160
|
|
|
$columns[] = (object) array_merge( |
|
161
|
|
|
Column::toArray($column), |
|
162
|
|
|
array('order' => $order) |
|
163
|
|
|
); |
|
164
|
|
|
$order++; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $columns; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public static function getIndexes(DoctrineTable $table) |
|
171
|
|
|
{ |
|
172
|
|
|
$indexes = []; |
|
173
|
|
|
|
|
174
|
|
|
foreach ($table->getIndexes() as $index) { |
|
175
|
|
|
$indexes[] = (object) array_merge( |
|
176
|
|
|
Index::toArray($index), |
|
177
|
|
|
array('table' => $table->getName()) |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $indexes; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public static function getForeignKeys(DoctrineTable $table) |
|
185
|
|
|
{ |
|
186
|
|
|
$foreignKeys = []; |
|
187
|
|
|
|
|
188
|
|
|
foreach ($table->getForeignKeys() as $name => $foreignKey) { |
|
189
|
|
|
$foreignKeys[$name] = ForeignKey::toArray($foreignKey); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $foreignKeys; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public static function exists($tableName) |
|
196
|
|
|
{ |
|
197
|
|
|
if (Driver::isMongoDB()) { |
|
198
|
|
|
return MongoDB::hasCollection($tableName); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if (!SchemaManager::getInstance()->tablesExist($tableName)) { |
|
202
|
|
|
throw SchemaException::tableDoesNotExist($tableName); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return true; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public static function paginate($perPage = 15, $page = null, $options = [], $query = "") |
|
209
|
|
|
{ |
|
210
|
|
|
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1); |
|
211
|
|
|
$options['path'] = Paginator::resolveCurrentPath(); |
|
212
|
|
|
$items = static::all(); |
|
213
|
|
|
$collection = $items instanceof Collection ? $items : Collection::make($items); |
|
214
|
|
|
if (!empty($query)) { |
|
215
|
|
|
$collection = $collection->filter(function ($value, $key) use ($query) { |
|
|
|
|
|
|
216
|
|
|
return false !== stristr($value, $query); |
|
217
|
|
|
}); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return new LengthAwarePaginator($collection->forPage($page, $perPage), $collection->count(), $perPage, $page, $options); |
|
221
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths