Codexshaper /
laravel-database-manager
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace CodexShaper\DBM\Database\Schema; |
||||
| 4 | |||||
| 5 | use CodexShaper\DBM\Database\Types\Type; |
||||
| 6 | use CodexShaper\DBM\Facades\Driver; |
||||
| 7 | use CodexShaper\DBM\Facades\MongoDB; |
||||
| 8 | use Doctrine\DBAL\Schema\SchemaException; |
||||
| 9 | use Doctrine\DBAL\Schema\Table as DoctrineTable; |
||||
| 10 | use Illuminate\Database\Eloquent\Collection; |
||||
| 11 | use Illuminate\Pagination\LengthAwarePaginator; |
||||
| 12 | use Illuminate\Pagination\Paginator; |
||||
| 13 | |||||
| 14 | class Table |
||||
| 15 | { |
||||
| 16 | /** |
||||
| 17 | * Returns a list of all tables in the current database. |
||||
| 18 | * |
||||
| 19 | * @return array|string[] |
||||
| 20 | */ |
||||
| 21 | public static function all() |
||||
| 22 | { |
||||
| 23 | // MongoDb |
||||
| 24 | if (Driver::isMongoDB()) { |
||||
| 25 | return MongoDB::getCollectionNames(); |
||||
| 26 | } |
||||
| 27 | |||||
| 28 | return SchemaManager::getInstance()->listTableNames(); |
||||
| 29 | } |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * Get table details. |
||||
| 33 | * |
||||
| 34 | * @param string $tableName |
||||
| 35 | * |
||||
| 36 | * @return array |
||||
| 37 | */ |
||||
| 38 | public static function getTable($tableName) |
||||
| 39 | { |
||||
| 40 | if (Driver::isMongoDB()) { |
||||
| 41 | return MongoDB::getCollection($tableName); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | Type::registerCustomTypes(); |
||||
| 45 | |||||
| 46 | $table = SchemaManager::getInstance()->listTableDetails($tableName); |
||||
| 47 | $primaryKeyName = $table->getPrimaryKey(); |
||||
| 48 | $options = $table->getOptions(); |
||||
| 49 | |||||
| 50 | return [ |
||||
| 51 | 'name' => $table->getName(), |
||||
| 52 | 'oldName' => $table->getName(), |
||||
| 53 | 'columns' => static::getColumns($table), |
||||
| 54 | 'indexes' => static::getIndexes($table), |
||||
| 55 | 'foreignKeys' => static::getForeignKeys($table), |
||||
| 56 | 'primaryKeyName' => $primaryKeyName, |
||||
| 57 | 'options' => $options, |
||||
| 58 | ]; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Get column names. |
||||
| 63 | * |
||||
| 64 | * @param string $tableName |
||||
| 65 | * |
||||
| 66 | * @return array |
||||
| 67 | */ |
||||
| 68 | public static function getColumnsName($tableName) |
||||
| 69 | { |
||||
| 70 | $columns = static::getTable($tableName)['columns']; |
||||
| 71 | |||||
| 72 | $columnsName = []; |
||||
| 73 | |||||
| 74 | foreach ($columns as $column) { |
||||
| 75 | $columnsName[] = $column->name; |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | return $columnsName; |
||||
| 79 | } |
||||
| 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)) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 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 | /** |
||||
| 106 | * Update table. |
||||
| 107 | * |
||||
| 108 | * @param array $table |
||||
| 109 | * |
||||
| 110 | * @return true|void |
||||
| 111 | */ |
||||
| 112 | public static function update($table) |
||||
| 113 | { |
||||
| 114 | if (! is_array($table)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 115 | $table = json_decode($table, true); |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | if (Driver::isMongoDB()) { |
||||
| 119 | return MongoDB::updateCollection($table); |
||||
| 120 | } |
||||
| 121 | |||||
| 122 | (new UpdateManager())->update($table); |
||||
| 123 | } |
||||
| 124 | |||||
| 125 | /** |
||||
| 126 | * Drop table. |
||||
| 127 | * |
||||
| 128 | * @param string $tableName |
||||
| 129 | * |
||||
| 130 | * @return void |
||||
| 131 | */ |
||||
| 132 | public static function drop($tableName) |
||||
| 133 | { |
||||
| 134 | if (Driver::isMongoDB()) { |
||||
| 135 | return MongoDB::dropCollection($tableName); |
||||
|
0 ignored issues
–
show
Are you sure the usage of
CodexShaper\DBM\Facades\...pCollection($tableName) targeting CodexShaper\DBM\Database...ngoDB::dropCollection() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 136 | } |
||||
| 137 | |||||
| 138 | return SchemaManager::getInstance()->dropTable($tableName); |
||||
|
0 ignored issues
–
show
Are you sure the usage of
CodexShaper\DBM\Database...->dropTable($tableName) targeting Doctrine\DBAL\Schema\Abs...emaManager::dropTable() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 139 | } |
||||
| 140 | |||||
| 141 | /** |
||||
| 142 | * Prepare table. |
||||
| 143 | * |
||||
| 144 | * @param array $table |
||||
| 145 | * |
||||
| 146 | * @return \Doctrine\DBAL\Schema\Table |
||||
| 147 | */ |
||||
| 148 | public static function prepareTable($table) |
||||
| 149 | { |
||||
| 150 | if (! is_array($table)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 151 | $table = json_decode($table, true); |
||||
| 152 | } |
||||
| 153 | |||||
| 154 | Type::registerCustomTypes(); |
||||
| 155 | |||||
| 156 | $conn = 'database.connections.'.config('database.default'); |
||||
| 157 | |||||
| 158 | $table['options']['collate'] = $table['options']['collation'] ?? config($conn.'.collation', 'utf8mb4_unicode_ci'); |
||||
| 159 | if (Driver::isMysql()) { |
||||
| 160 | $table['options']['charset'] = $table['options']['charset'] ?? config($conn.'.charset', 'utf8mb4'); |
||||
| 161 | } |
||||
| 162 | |||||
| 163 | $tableName = $table['name']; |
||||
| 164 | $columns = $table['columns']; |
||||
| 165 | $indexes = $table['indexes']; |
||||
| 166 | $foreignKeys = $table['foreignKeys']; |
||||
| 167 | |||||
| 168 | // Make Doctrain columns |
||||
| 169 | $DoctrineColumns = []; |
||||
| 170 | foreach ($columns as $column) { |
||||
| 171 | $DoctrineColumn = Column::create($column); |
||||
| 172 | $DoctrineColumns[$DoctrineColumn->getName()] = $DoctrineColumn; |
||||
| 173 | } |
||||
| 174 | |||||
| 175 | // Make Doctrain indexes |
||||
| 176 | $DoctrineIndexes = []; |
||||
| 177 | foreach ($indexes as $index) { |
||||
| 178 | $DoctrineIndex = Index::create($index); |
||||
| 179 | $DoctrineIndexes[$DoctrineIndex->getName()] = $DoctrineIndex; |
||||
| 180 | } |
||||
| 181 | |||||
| 182 | // Make Doctrain Foreign Keys |
||||
| 183 | $DoctrineForeignKeys = []; |
||||
| 184 | foreach ($foreignKeys as $foreignKey) { |
||||
| 185 | $DoctrineForeignKey = ForeignKey::create($foreignKey); |
||||
| 186 | |||||
| 187 | $DoctrineForeignKeys[$DoctrineForeignKey->getName()] = $DoctrineForeignKey; |
||||
| 188 | } |
||||
| 189 | |||||
| 190 | $options = $table['options']; |
||||
| 191 | |||||
| 192 | return new DoctrineTable($tableName, $DoctrineColumns, $DoctrineIndexes, $DoctrineForeignKeys, false, $options); |
||||
|
0 ignored issues
–
show
false of type false is incompatible with the type integer expected by parameter $idGeneratorType of Doctrine\DBAL\Schema\Table::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 193 | } |
||||
| 194 | |||||
| 195 | /** |
||||
| 196 | * Get all columns. |
||||
| 197 | * |
||||
| 198 | * @return array |
||||
| 199 | */ |
||||
| 200 | public static function getColumns(DoctrineTable $table) |
||||
| 201 | { |
||||
| 202 | $columns = []; |
||||
| 203 | |||||
| 204 | $order = 1; |
||||
| 205 | foreach ($table->getColumns() as $column) { |
||||
| 206 | $columns[] = (object) array_merge( |
||||
| 207 | Column::toArray($column), |
||||
| 208 | ['order' => $order] |
||||
| 209 | ); |
||||
| 210 | $order++; |
||||
| 211 | } |
||||
| 212 | |||||
| 213 | return $columns; |
||||
| 214 | } |
||||
| 215 | |||||
| 216 | /** |
||||
| 217 | * Get all indexes. |
||||
| 218 | * |
||||
| 219 | * @return array |
||||
| 220 | */ |
||||
| 221 | public static function getIndexes(DoctrineTable $table) |
||||
| 222 | { |
||||
| 223 | $indexes = []; |
||||
| 224 | |||||
| 225 | foreach ($table->getIndexes() as $index) { |
||||
| 226 | $indexes[] = (object) array_merge( |
||||
| 227 | Index::toArray($index), |
||||
| 228 | ['table' => $table->getName()] |
||||
| 229 | ); |
||||
| 230 | } |
||||
| 231 | |||||
| 232 | return $indexes; |
||||
| 233 | } |
||||
| 234 | |||||
| 235 | /** |
||||
| 236 | * Get all foreign keys. |
||||
| 237 | * |
||||
| 238 | * @return array |
||||
| 239 | */ |
||||
| 240 | public static function getForeignKeys(DoctrineTable $table) |
||||
| 241 | { |
||||
| 242 | $foreignKeys = []; |
||||
| 243 | |||||
| 244 | foreach ($table->getForeignKeys() as $name => $foreignKey) { |
||||
| 245 | $foreignKeys[$name] = ForeignKey::toArray($foreignKey); |
||||
| 246 | } |
||||
| 247 | |||||
| 248 | return $foreignKeys; |
||||
| 249 | } |
||||
| 250 | |||||
| 251 | /** |
||||
| 252 | * Check table exists or not. |
||||
| 253 | * |
||||
| 254 | * @param string $tableName |
||||
| 255 | * |
||||
| 256 | * @return bool |
||||
| 257 | */ |
||||
| 258 | public static function exists($tableName) |
||||
| 259 | { |
||||
| 260 | if (Driver::isMongoDB()) { |
||||
| 261 | return MongoDB::hasCollection($tableName); |
||||
| 262 | } |
||||
| 263 | |||||
| 264 | if (! SchemaManager::getInstance()->tablesExist($tableName)) { |
||||
| 265 | throw SchemaException::tableDoesNotExist($tableName); |
||||
| 266 | } |
||||
| 267 | |||||
| 268 | return true; |
||||
| 269 | } |
||||
| 270 | |||||
| 271 | /** |
||||
| 272 | * Get tables with pagination. |
||||
| 273 | * |
||||
| 274 | * @param int $perPage |
||||
| 275 | * @param int|null $page |
||||
| 276 | * @param array $options |
||||
| 277 | * @param string $query |
||||
| 278 | * |
||||
| 279 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||||
| 280 | */ |
||||
| 281 | public static function paginate($perPage = 15, $page = null, $options = [], $query = '') |
||||
| 282 | { |
||||
| 283 | $page = $page ?: (Paginator::resolveCurrentPage() ?: 1); |
||||
| 284 | $options['path'] = Paginator::resolveCurrentPath(); |
||||
| 285 | $items = static::all(); |
||||
| 286 | $collection = $items instanceof Collection ? $items : Collection::make($items); |
||||
|
0 ignored issues
–
show
|
|||||
| 287 | if (! empty($query)) { |
||||
| 288 | $collection = $collection->filter(function ($value, $key) use ($query) { |
||||
|
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 289 | return false !== stristr($value, $query); |
||||
| 290 | }); |
||||
| 291 | } |
||||
| 292 | |||||
| 293 | return new LengthAwarePaginator($collection->forPage($page, $perPage), $collection->count(), $perPage, $page, $options); |
||||
| 294 | } |
||||
| 295 | } |
||||
| 296 |