99import com .osiris .desku .App ;
1010import com .osiris .desku .Icon ;
1111import com .osiris .desku .ui .DesktopUI ;
12- import com .osiris .desku .Route ;
1312import com .osiris .desku .ui .UI ;
1413import com .osiris .desku .ui .Component ;
1514import com .osiris .desku .ui .input .*;
1918import com .osiris .desku .ui .layout .Popup ;
2019import com .osiris .desku .ui .layout .TabLayout ;
2120import com .osiris .desku .ui .layout .Vertical ;
22- import com .osiris .dyml .utils .UtilsFile ;
2321import com .osiris .jlib .logger .AL ;
2422import com .osiris .jsqlgen .generator .GenDatabaseFile ;
2523import com .osiris .jsqlgen .generator .JavaCodeGenerator ;
@@ -594,18 +592,7 @@ public <T extends Node> List<File> generateCode(List<Database> databases, File o
594592 dir .mkdirs ();
595593 }
596594
597- if (!db .getJavaProjectDirs ().isEmpty ()) {
598- // Write json structure data
599- for (File jsonData : getDatabaseStructureFile (db , dirs )) {
600- AL .info (jsonData .getAbsolutePath ());
601- jsonData .createNewFile ();
602- StringWriter sw = new StringWriter (); // Passing the filewriter directly results in a blank file
603- Data .parser .toJson (db , sw );
604- String out = sw .toString ();
605- //AL.info(out);
606- Files .writeString (jsonData .toPath (), out );
607- }
608- }
595+
609596 // Write Database class files and Tables files
610597 for (JavaProjectGenDir javaProjectGenDir : dirs ) {
611598 File databaseFile = getDatabaseFile (javaProjectGenDir );
@@ -652,6 +639,20 @@ else if (Objects.equals(var.getName().asString(), "password"))
652639 JavaCodeGenerator .generateTableFile (javaFile , t , db ));
653640 }
654641 }
642+
643+ // After generation, since db object might still change
644+ if (!db .getJavaProjectDirs ().isEmpty ()) {
645+ // Write json structure data
646+ for (File jsonData : getDatabaseStructureFile (db , dirs )) {
647+ AL .info (jsonData .getAbsolutePath ());
648+ jsonData .createNewFile ();
649+ StringWriter sw = new StringWriter (); // Passing the filewriter directly results in a blank file
650+ Data .parser .toJson (db , sw );
651+ String out = sw .toString ();
652+ //AL.info(out);
653+ Files .writeString (jsonData .toPath (), out );
654+ }
655+ }
655656 }
656657 return files ;
657658 }
@@ -695,7 +696,7 @@ else if (command.equals("Duplicate")) {
695696 tableName .input .sty ("font-weight" , "bold" ).sty ("font-size" , "larger" );
696697 hl .add (tableName );
697698 tableName .setTooltip ("The table name. Changes are auto-saved." );
698- tableName .onValueChange (e -> { // enter pressed event
699+ tableName .onValueChange (e -> {
699700 try {
700701 renameTable (dbName , e .valueBefore , e .value );
701702 } catch (Exception ex ) {
@@ -770,6 +771,10 @@ private void renameTable(String dbName, String oldName, String newName) throws I
770771 Table t = Data .findTable (db .tables , oldName );
771772 Objects .requireNonNull (t );
772773 t .name = newName ;
774+
775+ // Update current change
776+ t .currentChange .newTableName = newName ;
777+
773778 Data .save ();
774779 AL .info ("OK!" );
775780 }
@@ -785,6 +790,11 @@ private void addNewTable(String dbName, String tableName) throws IOException {
785790 t .addIdColumn ();
786791 db .tables .add (t );
787792 t .name = tableName ;
793+
794+ // Update current change
795+ t .currentChange .newTableName = tableName ;
796+ t .currentChange .oldTableName = tableName ;
797+
788798 Data .save ();
789799 updateTablesList (dbName );
790800 }
@@ -905,7 +915,7 @@ private void updateColumnsList(Vertical listColumns, String dbName, String table
905915 Column col = new Column (colName .getValue ());
906916 col .definition = colDef .getValue ();
907917 col .comment = colComment .getValue ();
908- addNewColumn (listColumns , dbName , t .name , col , null );
918+ addNewColumn (listColumns , dbName , t .name , col );
909919 colName .setValue ("" );
910920 } catch (Exception e ) {
911921 e .printStackTrace ();
@@ -914,28 +924,70 @@ private void updateColumnsList(Vertical listColumns, String dbName, String table
914924 }
915925
916926 private void updateColumn (Vertical listColumns , String dbName , String tableName , String oldName , String newName , String newDefinition , String newComment ) throws IOException {
917- AL .info ("Updating column..." );
927+ AL .info ("Updating column " + oldName + " -> " + newName );
918928 Database db = Data .getDatabase (dbName );
919929 Table t = Data .findTable (db .tables , tableName );
920930 Objects .requireNonNull (t );
921931 Column col = Data .findColumn (t .columns , oldName );
922932 Objects .requireNonNull (col );
923- AL .info ("OLD: " + col .name + " " + col .definition + " " + col .comment );
924933 col .updateName (newName );
934+ String oldDefinition = col .definition ;
925935 col .definition = newDefinition ;
926936 col .comment = newComment ;
927- AL .info ("NEW: " + col .name + " " + col .definition + " " + col .comment );
937+
938+ // Update current change
939+ t .currentChange .deletedColumnNames .remove (oldName );
940+ if (t .currentChange .addedColumnNames .contains (oldName )){
941+ int i = t .currentChange .addedColumnNames .indexOf (oldName );
942+ t .currentChange .addedColumnNames .set (i , newName );
943+ t .currentChange .addedColumnDefinitions .set (i , newDefinition );
944+ } else {
945+ // Existing column, check for changes
946+ int i = t .currentChange .newColumnNames .indexOf (oldName );
947+ if (i >= 0 ){
948+ t .currentChange .newColumnNames .remove (i );
949+ t .currentChange .newColumnNames_Definitions .remove (i );
950+ t .currentChange .oldColumnNames .remove (i );
951+ }
952+ if (!newName .equals (oldName )) {
953+ t .currentChange .newColumnNames .add (newName );
954+ t .currentChange .newColumnNames_Definitions .add (newDefinition );
955+ t .currentChange .oldColumnNames .add (oldName );
956+ }
957+
958+ i = t .currentChange .newColumnDefinitions .indexOf (oldDefinition );
959+ if (i >= 0 ){
960+ t .currentChange .newColumnDefinitions .remove (i );
961+ t .currentChange .oldColumnDefinitions .remove (i );
962+ t .currentChange .newColumnDefinitions_Names .remove (i );
963+ }
964+ if (!newDefinition .equals (oldDefinition )) {
965+ t .currentChange .newColumnDefinitions .add (newDefinition );
966+ t .currentChange .oldColumnDefinitions .add (oldDefinition );
967+ t .currentChange .newColumnDefinitions_Names .add (newName );
968+ }
969+ }
970+
971+ if (!oldDefinition .equals (newDefinition ))
972+ AL .info ("Updating column definition " +oldDefinition + " -> " + newDefinition );
928973 Data .save ();
929974 AL .info ("OK!" );
930975 }
931976
932- private void addNewColumn (Vertical listColumns , String dbName , String tableName , Column col , String columnDefinition ) throws IOException {
977+ private void addNewColumn (Vertical listColumns , String dbName , String tableName , Column col ) throws IOException {
933978 Database db = Data .getDatabase (dbName );
934979 Table t = Data .findTable (db .tables , tableName );
935980 Objects .requireNonNull (t );
936981 col .id = Main .idCounter .getAndIncrement ();
937982 t .columns .add (col );
938- col .definition = columnDefinition ;
983+
984+ // Update current change
985+ if (!t .currentChange .addedColumnNames .contains (col .name )){
986+ t .currentChange .addedColumnNames .add (col .name );
987+ t .currentChange .addedColumnDefinitions .add (col .definition );
988+ t .currentChange .deletedColumnNames .remove (col .name );
989+ }
990+
939991 Data .save ();
940992 updateColumnsList (listColumns , dbName , tableName );
941993 }
@@ -947,6 +999,17 @@ private void deleteColumn(Vertical listColumns, String dbName, String tableName,
947999 Column col = Data .findColumn (t .columns , columnName );
9481000 Objects .requireNonNull (col );
9491001 t .columns .remove (col );
1002+
1003+ // Update current change
1004+ if (!t .currentChange .deletedColumnNames .contains (col .name )){
1005+ t .currentChange .deletedColumnNames .add (col .name );
1006+ int i = t .currentChange .addedColumnNames .indexOf (col .name );
1007+ if (i >= 0 ) {
1008+ t .currentChange .addedColumnNames .remove (i );
1009+ t .currentChange .addedColumnDefinitions .remove (i );
1010+ }
1011+ }
1012+
9501013 Data .save ();
9511014 updateColumnsList (listColumns , dbName , tableName );
9521015 }
0 commit comments