Skip to content

Commit

Permalink
update class schema
Browse files Browse the repository at this point in the history
  • Loading branch information
miftahurrahmi committed Jun 7, 2024
1 parent c2a8218 commit 3fc42fe
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 85 deletions.
2 changes: 0 additions & 2 deletions src/Controllers/BadasoTableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public function read(Request $request)
]);
$table = $request->table;
$table_fields = SchemaManager::describeTable($table);

// $table_fields = Schema::getIndexes($table);
$fields = [];
foreach ($table_fields as $key => $column) {
$column = collect($column)->toArray();
Expand Down
122 changes: 63 additions & 59 deletions src/Database/Schema/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,65 @@

namespace Uasoft\Badaso\Database\Schema;

use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Table as DoctrineTable;
use Doctrine\DBAL\Schema\Table;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Uasoft\Badaso\Database\Types\Type;
use Illuminate\Support\Facades\Schema;
use Uasoft\Badaso\Database\Types\Type;

abstract class SchemaManager
{
// todo: trim parameters

public static function __callStatic($method, $args)
{
return static::manager()->$method(...$args);
}

public static function manager()
{
// return Schema::connection();
return DB::connection()->getDoctrineSchemaManager();
}

public static function getDatabaseConnection()
{
return DB::connection()->getDoctrineConnection();
return Schema::$method(...$args);
}

public static function tableExists($table)
{
if (! is_array($table)) {
if (!is_array($table)) {
$table = [$table];
}

return static::manager()->tablesExist($table);
foreach ($table as $tbl) {
if (!Schema::hasTable($tbl)) {
return false;
}
}

return true;
}

public static function listTables()
{
$tables = [];

foreach (static::manager()->listTableNames() as $table_name) {
$tables[$table_name] = static::listTableDetails($table_name);
$tables = DB::select('SHOW TABLES');
$database_name = DB::getDatabaseName();
$tables_key = "Tables_in_$database_name";

$result = [];
foreach ($tables as $table) {
$table_name = $table->$tables_key;
$result[$table_name] = static::listTableDetails($table_name);
}

return $tables;
return $result;
}

/**
* @param string $table_name
* @return \Uasoft\Badaso\Database\Schema\Table
*/
public static function listTableDetails($table_name)
{
// $columns = static::manager()->listTableColumns($table_name);
$columns = Schema::getColumns($table_name);

$foreign_keys = [];
// if (static::manager()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
// $foreign_keys = Schema::getForeignKeys($table_name);
// }
$foreign_keys = Schema::getForeignKeys($table_name);
$columns = Schema::getColumnListing($table_name);

$indexes = Schema::getIndexes($table_name);
// $foreign_keys = static::listTableForeignKeys($table_name);
$foreign_keys[] = Schema::getForeignKeys($table_name);
// $indexes = Schema::listTableIndexes($table_name);
$indexes[] = Schema::getIndexes($table_name);
return new Table($table_name, $columns, $indexes, $foreign_keys, []);
}

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

/**
* Describes given table.
*
* @param string $table_name
* @return \Illuminate\Support\Collection
*/
public static function describeTable($table_name)
{
// Type::registerCustomPlatformTypes();
Expand Down Expand Up @@ -109,42 +96,59 @@ public static function listTableColumnNames($table_name)
{
Type::registerCustomPlatformTypes();

$column_names = [];

foreach (static::manager()->listTableColumns($table_name) as $column) {
$column_names[] = $column->getName();
}

return $column_names;
return Schema::getColumnListing($table_name);
}

public static function createTable($table)
{
if (! ($table instanceof DoctrineTable)) {
if (!($table instanceof Blueprint)) {
$table = Table::make($table);
}

static::manager()->createTable($table);
Schema::create($table->getTable(), function (Blueprint $blueprint) use ($table) {
foreach ($table->getColumns() as $column) {
$blueprint->addColumn($column->getType(), $column->getName(), $column->getOptions());
}

foreach ($table->getIndexes() as $index) {
$blueprint->index($index->getColumns(), $index->getName(), $index->getOptions());
}

foreach ($table->getForeignKeys() as $foreignKey) {
$blueprint->foreign($foreignKey->getLocalColumns())
->references($foreignKey->getForeignColumns())
->on($foreignKey->getForeignTable())
->onDelete($foreignKey->onDelete())
->onUpdate($foreignKey->onUpdate());
}
});
}

public static function getDoctrineTable($table)
{
$table = trim($table);

if (! static::tableExists($table)) {
throw SchemaException::tableDoesNotExist($table);
if (!static::tableExists($table)) {
throw new \Exception("Table $table does not exist.");
}

return static::manager()->listTableDetails($table);
return static::listTableDetails($table);
}

public static function getDoctrineColumn($table, $column)
{
return static::getDoctrineTable($table)->getColumn($column);
return static::getColumnDetails($table, $column);
}

public static function listTableForeignKeys($table_name)
{
$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"));
return $foreignKeys;
}

public static function getDoctrineForeignKeys($table)
public static function listTableIndexes($table_name)
{
return static::manager()->listTableForeignKeys($table);
$indexes = DB::select(DB::raw("SHOW INDEX FROM `$table_name`"));
return $indexes;
}
}
92 changes: 68 additions & 24 deletions src/Database/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Uasoft\Badaso\Database\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform as DoctrineAbstractPlatform;
use Doctrine\DBAL\Types\Type as DoctrineType;
use Illuminate\Support\Facades\DB;
use Uasoft\Badaso\Database\Platforms\Platform;
use Uasoft\Badaso\Database\Schema\SchemaManager;
use Doctrine\DBAL\Types\Type as DoctrineType;
use Doctrine\DBAL\Platforms\AbstractPlatform as DoctrineAbstractPlatform;

abstract class Type extends DoctrineType
{
Expand All @@ -15,6 +15,7 @@ abstract class Type extends DoctrineType
protected static $platform_types = [];
protected static $custom_type_options = [];
protected static $type_categories = [];
protected static $types = [];

const NAME = 'UNDEFINED_TYPE_NAME';
const NOT_SUPPORTED = 'notSupported';
Expand All @@ -29,7 +30,7 @@ public function getName()
return static::NAME;
}

public static function toArray(DoctrineType $type)
public static function toArray($type)
{
$custom_type_options = $type->customOptions ?? [];

Expand All @@ -44,15 +45,15 @@ public static function getPlatformTypes()
return static::$platform_types;
}

if (! static::$custom_type_registered) {
if (!static::$custom_type_registered) {
static::registerCustomPlatformTypes();
}

$platform = SchemaManager::getDatabasePlatform();
$platform_name = DB::getDriverName();

static::$platform_types = Platform::getPlatformTypes(
$platform->getName(),
static::getPlatformTypeMapping($platform)
$platform_name,
static::getPlatformTypeMapping($platform_name)
);

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

public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform)
public static function getPlatformTypeMapping(DoctrineAbstractPlatform $platform_name)
{
if (static::$platform_type_mapping) {
return static::$platform_type_mapping;
}

// Anda perlu mendefinisikan cara mengambil pemetaan tipe khusus untuk platform tertentu
static::$platform_type_mapping = collect(
get_protected_property($platform, 'doctrineTypeMapping')
get_protected_property($platform_name, 'doctrineTypeMapping')
);

return static::$platform_type_mapping;
}

public static function registerCustomPlatformTypes($force = false)
{
if (static::$custom_type_registered && ! $force) {
if (static::$custom_type_registered && !$force) {
return;
}

$platform = SchemaManager::getDatabasePlatform();
$platform_name = ucfirst($platform->getName());

$platform_name = DB::getDriverName();

$custom_types = array_merge(
static::getPlatformCustomTypes('Common'),
static::getPlatformCustomTypes($platform_name)
);

foreach ($custom_types as $type) {
$name = $type::NAME;

if (static::hasType($name)) {
static::overrideType($name, $type);
} else {
if (!static::hasType($name)) {
static::addType($name, $type);
} else {
static::overrideType($name, $type);
}

$db_type = defined("{$type}::DBTYPE") ? $type::DBTYPE : $name;

$platform->registerDoctrineTypeMapping($db_type, $name);
static::registerDoctrineTypeMapping($db_type, $name);
}

static::addCustomTypeOptions($platform_name);

static::$custom_type_registered = true;
}

protected static function registerDoctrineTypeMapping($db_type, $name)
{
$databaseConfig = config('database.connections.' . config('database.default'));

$connectionParams = [
'dbname' => $databaseConfig['database'],
'user' => $databaseConfig['username'],
'password' => $databaseConfig['password'],
'host' => $databaseConfig['host'],
'driver' => 'pdo_mysql', // Sesuaikan dengan driver yang digunakan, misal: 'pdo_pgsql' untuk PostgreSQL
'port' => $databaseConfig['port'],
];

$doctrine_connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
$platform = $doctrine_connection->getDatabasePlatform();

if ($platform->hasDoctrineTypeMappingFor($db_type)) {
$platform->registerDoctrineTypeMapping($db_type, $name);
} else {
throw new \Doctrine\DBAL\Exception("Type to be overwritten $db_type does not exist.");
}
}

protected static function addCustomTypeOptions($platform_name)
{
static::registerCommonCustomTypeOptions();
Expand All @@ -126,12 +150,12 @@ protected static function addCustomTypeOptions($platform_name)

protected static function getPlatformCustomTypes($platform_name)
{
$types_path = __DIR__.DIRECTORY_SEPARATOR.$platform_name.DIRECTORY_SEPARATOR;
$namespace = __NAMESPACE__.'\\'.$platform_name.'\\';
$types_path = __DIR__ . DIRECTORY_SEPARATOR . $platform_name . DIRECTORY_SEPARATOR;
$namespace = __NAMESPACE__ . '\\' . $platform_name . '\\';
$types = [];

foreach (glob($types_path.'*.php') as $class_file) {
$types[] = $namespace.str_replace(
foreach (glob($types_path . '*.php') as $class_file) {
$types[] = $namespace . str_replace(
'.php',
'',
str_replace($types_path, '', $class_file)
Expand Down Expand Up @@ -330,4 +354,24 @@ public static function getTypeCategories()

return static::$type_categories;
}

public static function hasType($name)
{
return DoctrineType::hasType($name);
}

public static function getType($name)
{
return static::$types[$name] ?? null;
}

public static function addType($name, $type)
{
static::$types[$name] = new $type();
}

public static function overrideType($name, $type)
{
static::$types[$name] = new $type();
}
}

0 comments on commit 3fc42fe

Please sign in to comment.