diff --git a/composer.json b/composer.json index d7f4874..a385f85 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,8 @@ } ], "require": { - "illuminate/support": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", - "doctrine/dbal": ">=2.10" + "php": "^8.2", + "illuminate/support": "^10.37|^11.0" }, "autoload": { "psr-4": { @@ -37,6 +37,6 @@ } }, "require-dev": { - "orchestra/testbench": "^6.0" + "orchestra/testbench": "^8.0|^9.0" } } diff --git a/src/Controller/DBMLController.php b/src/Controller/DBMLController.php index 09e0418..c4ad74e 100644 --- a/src/Controller/DBMLController.php +++ b/src/Controller/DBMLController.php @@ -4,62 +4,58 @@ use Aphisitworachorch\Kacher\Traits\DBMLSyntaxTraits; use App\Http\Controllers\Controller; -use Doctrine\DBAL\Exception; -use Doctrine\DBAL\Schema\AbstractSchemaManager; -use Illuminate\Support\Facades\DB; +use Exception; +use Illuminate\Support\Facades\Schema; class DBMLController extends Controller { use DBMLSyntaxTraits; /** - * @var AbstractSchemaManager - */ - private $doctrine_instance; - - /** - * @throws Exception + * */ public function __construct ($custom_type=null) { - if ($custom_type != null){ + /*if ($custom_type != null){ foreach($custom_type as $ct => $key) { DB::connection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping($ct, $key); } - } - DB::connection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); - $this->doctrine_instance = DB::connection()->getDoctrineSchemaManager(); + }*/ } /** - * @throws Exception + * */ private function getColumns($table_name, $type) { $columnInfo = []; if (!empty($table_name)) { - $instance = $this->doctrine_instance->listTableColumns ($table_name); + $instance = Schema::getColumns ($table_name); foreach($instance as $tableColumn){ if($type === "artisan"){ - $columnInfo[] = "name : {$tableColumn->getName ()}\n" . "type : {$tableColumn->getType ()->getName ()}\n"; + $columnInfo[] = "name : {$tableColumn['name']}\n" . "type : {$tableColumn['type']}\n"; } if($type === "array"){ $special = []; - if($this->isPrimaryKey ($tableColumn->getName (),$table_name) === "yes"){ + if($this->isPrimaryKey ($tableColumn['name'],$table_name) === "yes"){ $special[] = "pk"; } - if($this->isUniqueKey ($tableColumn->getName (),$table_name) === "yes"){ + if($this->isUniqueKey ($tableColumn['name'],$table_name) === "yes"){ $special[] = "unique"; } + $length = ''; + if (preg_match('/.+\(([0-9]+)\)/', $tableColumn['type'], $matches)) { + $length = $matches[1]; + } $columnInfo[] = [ - "name"=>$tableColumn->getName (), - "type"=>$tableColumn->getType ()->getName (), + "name"=>$tableColumn['name'], + "type"=>$tableColumn['type'], "special"=>$special, - "note"=>$tableColumn->getComment (), - "default_value"=>$tableColumn->getDefault (), - "is_nullable"=>($tableColumn->getNotnull () ? "no" : "yes"), - "length"=>$tableColumn->getLength () + "note"=>$tableColumn['comment'], + "default_value"=>$tableColumn['default'], + "is_nullable"=>($tableColumn['nullable'] ? "yes" : "no"), + "length"=>$length ]; } } @@ -68,25 +64,25 @@ private function getColumns($table_name, $type) } /** - * @throws Exception + * */ private function getForeignKey($table_name, $type) { $columnInfo = []; if (!empty($table_name)) { - $instance = $this->doctrine_instance->listTableForeignKeys ($table_name); + $instance = Schema::getForeignKeys ($table_name); foreach($instance as $tableFK){ - $fromColumns = implode(" | ",$tableFK->getColumns ()); - $toColumns = implode(" | ",$tableFK->getForeignColumns ()); + $fromColumns = implode(" | ",$tableFK['columns']); + $toColumns = implode(" | ",$tableFK['foreign_columns']); if($type === "artisan"){ - $columnInfo[] = "[{$table_name}][{$fromColumns}] -> "."[$toColumns] of [{$tableFK->getForeignTableName ()}]"; + $columnInfo[] = "[{$table_name}][{$fromColumns}] -> "."[$toColumns] of [{$tableFK['foreign_table']}]"; } if($type === "array"){ $columnInfo[] = [ "from"=>$table_name, "name"=>$fromColumns, "to"=>$toColumns, - "table"=>$tableFK->getForeignTableName () + "table"=>$tableFK['foreign_table'] ]; } } @@ -95,22 +91,22 @@ private function getForeignKey($table_name, $type) } /** - * @throws Exception + * */ private function getIndexes($table_name, $type) { $columnInfo = []; if (!empty($table_name)) { - $instance = $this->doctrine_instance->listTableIndexes ($table_name); + $instance = Schema::getIndexes ($table_name); foreach($instance as $tableIndex){ - $unique = $tableIndex->isUnique () ? "yes" : "no"; - $primary = $tableIndex->isPrimary () ? "yes" : "no"; + $unique = $tableIndex['unique'] ? "yes" : "no"; + $primary = $tableIndex['primary'] ? "yes" : "no"; if($type === "artisan"){ - $columns = implode(" | ",$tableIndex->getColumns ()); - $columnInfo[] = "name : {$tableIndex->getName ()}\n"."columns : {$columns}\n"."unique : {$unique}\n"."primary : {$primary}\n"; + $columns = implode(" | ",$tableIndex['columns']); + $columnInfo[] = "name : {$tableIndex['name']}\n"."columns : {$columns}\n"."unique : {$unique}\n"."primary : {$primary}\n"; } if($type === "array"){ - $columnInfo[] = ["name"=>$tableIndex->getName (),"columns"=>$tableIndex->getColumns (),"unique"=>$unique,"primary"=>$primary,"table"=>$table_name]; + $columnInfo[] = ["name"=>$tableIndex['name'],"columns"=>$tableIndex['columns'],"unique"=>$unique,"primary"=>$primary,"table"=>$table_name]; } } } @@ -118,45 +114,45 @@ private function getIndexes($table_name, $type) } /** - * @throws Exception + * */ private function isPrimaryKey($column, $table_name){ - $primaryKeyInstance = $this->doctrine_instance->listTableIndexes ($table_name); + $primaryKeyInstance = Schema::getIndexes ($table_name); foreach($primaryKeyInstance as $tableIndex){ - if($tableIndex->getColumns ()[0] === $column){ - return $tableIndex->isPrimary () ? "yes" : "no"; + if($tableIndex['name'] === $column){ + return $tableIndex['primary'] ? "yes" : "no"; } } return 0; } /** - * @throws Exception + * */ private function isUniqueKey($column, $table_name){ - $uniqueKeyInstance = $this->doctrine_instance->listTableIndexes ($table_name); + $uniqueKeyInstance = Schema::getIndexes ($table_name); foreach($uniqueKeyInstance as $tableIndex){ - if($tableIndex->getColumns ()[0] === $column){ - return $tableIndex->isUnique () ? "yes" : "no"; + if($tableIndex['name'] === $column){ + return $tableIndex['unique'] ? "yes" : "no"; } } return 0; } /** - * @throws Exception + * */ public function getDatabaseTable($type){ - $tableName = $this->doctrine_instance->listTableNames(); + $tableName = Schema::getTables(); $data = []; if($tableName){ if($type === "artisan"){ foreach($tableName as $tb){ $data[] = [ - "table_name" => $tb, - "columns" => implode("\n",$this->getColumns ($tb,$type)), - "foreign_key" => implode("\n",$this->getForeignKey ($tb,$type)), - "indexes"=> implode("\n",$this->getIndexes ($tb,$type)), - "comment"=> $this->doctrine_instance->listTableDetails($tb)->getComment() + "table_name" => $tb['name'], + "columns" => implode("\n",$this->getColumns ($tb['name'],$type)), + "foreign_key" => implode("\n",$this->getForeignKey ($tb['name'],$type)), + "indexes"=> implode("\n",$this->getIndexes ($tb['name'],$type)), + "comment"=> $tb['comment'] ]; } return $data; @@ -164,11 +160,11 @@ public function getDatabaseTable($type){ if($type === "array"){ foreach($tableName as $tb){ $data[] = [ - "table_name" => $tb, - "columns" => $this->getColumns ($tb,$type), - "foreign_key" => $this->getForeignKey ($tb,$type), - "indexes"=> $this->getIndexes ($tb,$type), - "comment"=> $this->doctrine_instance->listTableDetails($tb)->getComment() + "table_name" => $tb['name'], + "columns" => $this->getColumns ($tb['name'],$type), + "foreign_key" => $this->getForeignKey ($tb['name'],$type), + "indexes"=> $this->getIndexes ($tb['name'],$type), + "comment"=> $tb['comment'] ]; } return $data; @@ -189,14 +185,12 @@ public function parseToDBML() { try{ $table = $this->getDatabaseTable ("array"); - $syntax = ""; - $foreign = ""; - $syntax .= $this->getDatabasePlatform(); + $syntax = $this->getDatabasePlatform(); foreach($table as $info){ if($info['table_name']){ $syntax .= $this->table ($info['table_name']) . $this->start (); foreach($info['columns'] as $col){ - $syntax .= $this->column ($col['name'],$col['type'],$col['special'],$col['note'],$col['is_nullable'],$col['default_value'],$col['length']); + $syntax .= $this->column ($col['name'],$col['type'],$col['special'],$col['note'],$col['is_nullable'],$col['default_value'] ?? null,''); } if($info['comment']){ $syntax .= "\n\tNote: '".$info['comment']."'\n"; diff --git a/src/Traits/DBMLSyntaxTraits.php b/src/Traits/DBMLSyntaxTraits.php index 751d111..f130fd9 100644 --- a/src/Traits/DBMLSyntaxTraits.php +++ b/src/Traits/DBMLSyntaxTraits.php @@ -29,7 +29,7 @@ public function table($name) public function index() { - return "\n\tindexes"; + return "\n\tindexes "; } public function start() @@ -98,13 +98,17 @@ public function column($name,$type,$special,$note,$nullable,$default,$length) } if($default){ - $annotation[] = "default: '$default'"; + $annotation[] = "default: " . ((str_starts_with($default, "'") && str_ends_with($default, "'")) ? $default : "'" . $default . "'"); } if($length){ $len_annotate = "($length)"; } + if (str_contains($type, ' ')) { + $type = '"'.$type.'"'; + } + $results = implode(",",$annotation); return "\t{$name} {$type}{$len_annotate} [{$results}]\n"; }