Skip to content

Commit 3fc42fe

Browse files
committed
update class schema
1 parent c2a8218 commit 3fc42fe

File tree

3 files changed

+131
-85
lines changed

3 files changed

+131
-85
lines changed

src/Controllers/BadasoTableController.php

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public function read(Request $request)
4949
]);
5050
$table = $request->table;
5151
$table_fields = SchemaManager::describeTable($table);
52-
53-
// $table_fields = Schema::getIndexes($table);
5452
$fields = [];
5553
foreach ($table_fields as $key => $column) {
5654
$column = collect($column)->toArray();

src/Database/Schema/SchemaManager.php

+63-59
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,65 @@
22

33
namespace Uasoft\Badaso\Database\Schema;
44

5-
use Doctrine\DBAL\Schema\SchemaException;
6-
use Doctrine\DBAL\Schema\Table as DoctrineTable;
5+
use Doctrine\DBAL\Schema\Table;
6+
use Illuminate\Database\Schema\Blueprint;
77
use Illuminate\Support\Facades\DB;
8-
use Uasoft\Badaso\Database\Types\Type;
98
use Illuminate\Support\Facades\Schema;
9+
use Uasoft\Badaso\Database\Types\Type;
1010

1111
abstract class SchemaManager
1212
{
13-
// todo: trim parameters
14-
1513
public static function __callStatic($method, $args)
1614
{
17-
return static::manager()->$method(...$args);
18-
}
19-
20-
public static function manager()
21-
{
22-
// return Schema::connection();
23-
return DB::connection()->getDoctrineSchemaManager();
24-
}
25-
26-
public static function getDatabaseConnection()
27-
{
28-
return DB::connection()->getDoctrineConnection();
15+
return Schema::$method(...$args);
2916
}
3017

3118
public static function tableExists($table)
3219
{
33-
if (! is_array($table)) {
20+
if (!is_array($table)) {
3421
$table = [$table];
3522
}
3623

37-
return static::manager()->tablesExist($table);
24+
foreach ($table as $tbl) {
25+
if (!Schema::hasTable($tbl)) {
26+
return false;
27+
}
28+
}
29+
30+
return true;
3831
}
3932

4033
public static function listTables()
4134
{
42-
$tables = [];
43-
44-
foreach (static::manager()->listTableNames() as $table_name) {
45-
$tables[$table_name] = static::listTableDetails($table_name);
35+
$tables = DB::select('SHOW TABLES');
36+
$database_name = DB::getDatabaseName();
37+
$tables_key = "Tables_in_$database_name";
38+
39+
$result = [];
40+
foreach ($tables as $table) {
41+
$table_name = $table->$tables_key;
42+
$result[$table_name] = static::listTableDetails($table_name);
4643
}
4744

48-
return $tables;
45+
return $result;
4946
}
5047

51-
/**
52-
* @param string $table_name
53-
* @return \Uasoft\Badaso\Database\Schema\Table
54-
*/
5548
public static function listTableDetails($table_name)
5649
{
57-
// $columns = static::manager()->listTableColumns($table_name);
58-
$columns = Schema::getColumns($table_name);
59-
60-
$foreign_keys = [];
61-
// if (static::manager()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
62-
// $foreign_keys = Schema::getForeignKeys($table_name);
63-
// }
64-
$foreign_keys = Schema::getForeignKeys($table_name);
50+
$columns = Schema::getColumnListing($table_name);
6551

66-
$indexes = Schema::getIndexes($table_name);
52+
// $foreign_keys = static::listTableForeignKeys($table_name);
53+
$foreign_keys[] = Schema::getForeignKeys($table_name);
54+
// $indexes = Schema::listTableIndexes($table_name);
55+
$indexes[] = Schema::getIndexes($table_name);
56+
return new Table($table_name, $columns, $indexes, $foreign_keys, []);
57+
}
6758

68-
return new Table($table_name, $columns, $indexes, $foreign_keys, false, []);
59+
public static function getColumnDetails($table_name, $column_name)
60+
{
61+
return DB::select(DB::raw("SHOW COLUMNS FROM `$table_name` LIKE '$column_name'"))[0];
6962
}
7063

71-
/**
72-
* Describes given table.
73-
*
74-
* @param string $table_name
75-
* @return \Illuminate\Support\Collection
76-
*/
7764
public static function describeTable($table_name)
7865
{
7966
// Type::registerCustomPlatformTypes();
@@ -109,42 +96,59 @@ public static function listTableColumnNames($table_name)
10996
{
11097
Type::registerCustomPlatformTypes();
11198

112-
$column_names = [];
113-
114-
foreach (static::manager()->listTableColumns($table_name) as $column) {
115-
$column_names[] = $column->getName();
116-
}
117-
118-
return $column_names;
99+
return Schema::getColumnListing($table_name);
119100
}
120101

121102
public static function createTable($table)
122103
{
123-
if (! ($table instanceof DoctrineTable)) {
104+
if (!($table instanceof Blueprint)) {
124105
$table = Table::make($table);
125106
}
126107

127-
static::manager()->createTable($table);
108+
Schema::create($table->getTable(), function (Blueprint $blueprint) use ($table) {
109+
foreach ($table->getColumns() as $column) {
110+
$blueprint->addColumn($column->getType(), $column->getName(), $column->getOptions());
111+
}
112+
113+
foreach ($table->getIndexes() as $index) {
114+
$blueprint->index($index->getColumns(), $index->getName(), $index->getOptions());
115+
}
116+
117+
foreach ($table->getForeignKeys() as $foreignKey) {
118+
$blueprint->foreign($foreignKey->getLocalColumns())
119+
->references($foreignKey->getForeignColumns())
120+
->on($foreignKey->getForeignTable())
121+
->onDelete($foreignKey->onDelete())
122+
->onUpdate($foreignKey->onUpdate());
123+
}
124+
});
128125
}
129126

130127
public static function getDoctrineTable($table)
131128
{
132129
$table = trim($table);
133130

134-
if (! static::tableExists($table)) {
135-
throw SchemaException::tableDoesNotExist($table);
131+
if (!static::tableExists($table)) {
132+
throw new \Exception("Table $table does not exist.");
136133
}
137134

138-
return static::manager()->listTableDetails($table);
135+
return static::listTableDetails($table);
139136
}
140137

141138
public static function getDoctrineColumn($table, $column)
142139
{
143-
return static::getDoctrineTable($table)->getColumn($column);
140+
return static::getColumnDetails($table, $column);
141+
}
142+
143+
public static function listTableForeignKeys($table_name)
144+
{
145+
$foreignKeys = DB::select(DB::raw("SELECT * FROM information_schema.key_column_usage WHERE TABLE_NAME = '$table_name' AND TABLE_SCHEMA = '" . DB::getDatabaseName() . "' AND REFERENCED_COLUMN_NAME IS NOT NULL"));
146+
return $foreignKeys;
144147
}
145148

146-
public static function getDoctrineForeignKeys($table)
149+
public static function listTableIndexes($table_name)
147150
{
148-
return static::manager()->listTableForeignKeys($table);
151+
$indexes = DB::select(DB::raw("SHOW INDEX FROM `$table_name`"));
152+
return $indexes;
149153
}
150154
}

src/Database/Types/Type.php

+68-24
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Uasoft\Badaso\Database\Types;
44

5-
use Doctrine\DBAL\Platforms\AbstractPlatform as DoctrineAbstractPlatform;
6-
use Doctrine\DBAL\Types\Type as DoctrineType;
5+
use Illuminate\Support\Facades\DB;
76
use Uasoft\Badaso\Database\Platforms\Platform;
8-
use Uasoft\Badaso\Database\Schema\SchemaManager;
7+
use Doctrine\DBAL\Types\Type as DoctrineType;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform as DoctrineAbstractPlatform;
99

1010
abstract class Type extends DoctrineType
1111
{
@@ -15,6 +15,7 @@ abstract class Type extends DoctrineType
1515
protected static $platform_types = [];
1616
protected static $custom_type_options = [];
1717
protected static $type_categories = [];
18+
protected static $types = [];
1819

1920
const NAME = 'UNDEFINED_TYPE_NAME';
2021
const NOT_SUPPORTED = 'notSupported';
@@ -29,7 +30,7 @@ public function getName()
2930
return static::NAME;
3031
}
3132

32-
public static function toArray(DoctrineType $type)
33+
public static function toArray($type)
3334
{
3435
$custom_type_options = $type->customOptions ?? [];
3536

@@ -44,15 +45,15 @@ public static function getPlatformTypes()
4445
return static::$platform_types;
4546
}
4647

47-
if (! static::$custom_type_registered) {
48+
if (!static::$custom_type_registered) {
4849
static::registerCustomPlatformTypes();
4950
}
5051

51-
$platform = SchemaManager::getDatabasePlatform();
52+
$platform_name = DB::getDriverName();
5253

5354
static::$platform_types = Platform::getPlatformTypes(
54-
$platform->getName(),
55-
static::getPlatformTypeMapping($platform)
55+
$platform_name,
56+
static::getPlatformTypeMapping($platform_name)
5657
);
5758

5859
static::$platform_types = static::$platform_types->map(function ($type) {
@@ -62,52 +63,75 @@ public static function getPlatformTypes()
6263
return static::$platform_types;
6364
}
6465

65-
public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform)
66+
public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform_name)
6667
{
6768
if (static::$platform_type_mapping) {
6869
return static::$platform_type_mapping;
6970
}
7071

72+
// Anda perlu mendefinisikan cara mengambil pemetaan tipe khusus untuk platform tertentu
7173
static::$platform_type_mapping = collect(
72-
get_protected_property($platform, 'doctrineTypeMapping')
74+
get_protected_property($platform_name, 'doctrineTypeMapping')
7375
);
7476

7577
return static::$platform_type_mapping;
7678
}
7779

7880
public static function registerCustomPlatformTypes($force = false)
7981
{
80-
if (static::$custom_type_registered && ! $force) {
82+
if (static::$custom_type_registered && !$force) {
8183
return;
8284
}
8385

84-
$platform = SchemaManager::getDatabasePlatform();
85-
$platform_name = ucfirst($platform->getName());
86-
86+
$platform_name = DB::getDriverName();
87+
8788
$custom_types = array_merge(
8889
static::getPlatformCustomTypes('Common'),
8990
static::getPlatformCustomTypes($platform_name)
9091
);
91-
92+
9293
foreach ($custom_types as $type) {
9394
$name = $type::NAME;
9495

95-
if (static::hasType($name)) {
96-
static::overrideType($name, $type);
97-
} else {
96+
if (!static::hasType($name)) {
9897
static::addType($name, $type);
98+
} else {
99+
static::overrideType($name, $type);
99100
}
100101

101102
$db_type = defined("{$type}::DBTYPE") ? $type::DBTYPE : $name;
102-
103-
$platform->registerDoctrineTypeMapping($db_type, $name);
103+
104+
static::registerDoctrineTypeMapping($db_type, $name);
104105
}
105106

106107
static::addCustomTypeOptions($platform_name);
107108

108109
static::$custom_type_registered = true;
109110
}
110111

112+
protected static function registerDoctrineTypeMapping($db_type, $name)
113+
{
114+
$databaseConfig = config('database.connections.' . config('database.default'));
115+
116+
$connectionParams = [
117+
'dbname' => $databaseConfig['database'],
118+
'user' => $databaseConfig['username'],
119+
'password' => $databaseConfig['password'],
120+
'host' => $databaseConfig['host'],
121+
'driver' => 'pdo_mysql', // Sesuaikan dengan driver yang digunakan, misal: 'pdo_pgsql' untuk PostgreSQL
122+
'port' => $databaseConfig['port'],
123+
];
124+
125+
$doctrine_connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
126+
$platform = $doctrine_connection->getDatabasePlatform();
127+
128+
if ($platform->hasDoctrineTypeMappingFor($db_type)) {
129+
$platform->registerDoctrineTypeMapping($db_type, $name);
130+
} else {
131+
throw new \Doctrine\DBAL\Exception("Type to be overwritten $db_type does not exist.");
132+
}
133+
}
134+
111135
protected static function addCustomTypeOptions($platform_name)
112136
{
113137
static::registerCommonCustomTypeOptions();
@@ -126,12 +150,12 @@ protected static function addCustomTypeOptions($platform_name)
126150

127151
protected static function getPlatformCustomTypes($platform_name)
128152
{
129-
$types_path = __DIR__.DIRECTORY_SEPARATOR.$platform_name.DIRECTORY_SEPARATOR;
130-
$namespace = __NAMESPACE__.'\\'.$platform_name.'\\';
153+
$types_path = __DIR__ . DIRECTORY_SEPARATOR . $platform_name . DIRECTORY_SEPARATOR;
154+
$namespace = __NAMESPACE__ . '\\' . $platform_name . '\\';
131155
$types = [];
132156

133-
foreach (glob($types_path.'*.php') as $class_file) {
134-
$types[] = $namespace.str_replace(
157+
foreach (glob($types_path . '*.php') as $class_file) {
158+
$types[] = $namespace . str_replace(
135159
'.php',
136160
'',
137161
str_replace($types_path, '', $class_file)
@@ -330,4 +354,24 @@ public static function getTypeCategories()
330354

331355
return static::$type_categories;
332356
}
357+
358+
public static function hasType($name)
359+
{
360+
return DoctrineType::hasType($name);
361+
}
362+
363+
public static function getType($name)
364+
{
365+
return static::$types[$name] ?? null;
366+
}
367+
368+
public static function addType($name, $type)
369+
{
370+
static::$types[$name] = new $type();
371+
}
372+
373+
public static function overrideType($name, $type)
374+
{
375+
static::$types[$name] = new $type();
376+
}
333377
}

0 commit comments

Comments
 (0)