diff --git a/build.gradle b/build.gradle index 90e577d..247911b 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ configure(subprojects - project(':android')) { } subprojects { - version = '2.0.2' + version = '2.0.3' ext.appName = 'Desku-App' repositories { mavenCentral() diff --git a/core/src/main/java/com/osiris/jsqlgen/Data.java b/core/src/main/java/com/osiris/jsqlgen/Data.java index f367d97..89c8790 100644 --- a/core/src/main/java/com/osiris/jsqlgen/Data.java +++ b/core/src/main/java/com/osiris/jsqlgen/Data.java @@ -175,7 +175,7 @@ public static Map getOldAndNewDBsMap(@NotNull CopyOnWriteAr if (isNewer) { // If there are missing changes add them (which might happen when importing databases generated by older jSQL-Gen versions). - // For example if the table contains a column, but there is no change referencing that column, then it will be added to the first + // For example if the table contains a column, but there is no change referencing that column, then it will be added to the first change. // Note that this should only be execute here and not for all tables, since there are issues if the user closes the app without generating // which then saves the table with the change object missing. for (Table t : dbNew.tables) { diff --git a/core/src/main/java/com/osiris/jsqlgen/Main.java b/core/src/main/java/com/osiris/jsqlgen/Main.java index 6cd7fc6..3a36f76 100644 --- a/core/src/main/java/com/osiris/jsqlgen/Main.java +++ b/core/src/main/java/com/osiris/jsqlgen/Main.java @@ -32,6 +32,16 @@ public static void main(String[] _args) { App.init(null, loggerParams); AL.info("DB initialized at: "+com.osiris.jsqlgen.jsqlgen.Database.url); // Init DB by static constructor + // Update id counter if there is an imported table with larger ids + for (Database db : Data.instance.databases) { + for (Table t : db.tables) { + for (Column col : t.columns) { + if(col.id > idCounter.get()) + idCounter.set((int) (col.id + 1)); + } + } + } + for (Database db : Data.instance.databases) { // If there are missing ids set them for (Table t : db.tables) { diff --git a/core/src/main/java/com/osiris/jsqlgen/MainView.java b/core/src/main/java/com/osiris/jsqlgen/MainView.java index acd509f..1463723 100644 --- a/core/src/main/java/com/osiris/jsqlgen/MainView.java +++ b/core/src/main/java/com/osiris/jsqlgen/MainView.java @@ -9,7 +9,6 @@ import com.osiris.desku.App; import com.osiris.desku.Icon; import com.osiris.desku.ui.DesktopUI; -import com.osiris.desku.Route; import com.osiris.desku.ui.UI; import com.osiris.desku.ui.Component; import com.osiris.desku.ui.input.*; @@ -19,7 +18,6 @@ import com.osiris.desku.ui.layout.Popup; import com.osiris.desku.ui.layout.TabLayout; import com.osiris.desku.ui.layout.Vertical; -import com.osiris.dyml.utils.UtilsFile; import com.osiris.jlib.logger.AL; import com.osiris.jsqlgen.generator.GenDatabaseFile; import com.osiris.jsqlgen.generator.JavaCodeGenerator; @@ -594,18 +592,7 @@ public List generateCode(List databases, File o dir.mkdirs(); } - if (!db.getJavaProjectDirs().isEmpty()) { - // Write json structure data - for (File jsonData : getDatabaseStructureFile(db, dirs)) { - AL.info(jsonData.getAbsolutePath()); - jsonData.createNewFile(); - StringWriter sw = new StringWriter(); // Passing the filewriter directly results in a blank file - Data.parser.toJson(db, sw); - String out = sw.toString(); - //AL.info(out); - Files.writeString(jsonData.toPath(), out); - } - } + // Write Database class files and Tables files for (JavaProjectGenDir javaProjectGenDir : dirs) { File databaseFile = getDatabaseFile(javaProjectGenDir); @@ -652,6 +639,20 @@ else if (Objects.equals(var.getName().asString(), "password")) JavaCodeGenerator.generateTableFile(javaFile, t, db)); } } + + // After generation, since db object might still change + if (!db.getJavaProjectDirs().isEmpty()) { + // Write json structure data + for (File jsonData : getDatabaseStructureFile(db, dirs)) { + AL.info(jsonData.getAbsolutePath()); + jsonData.createNewFile(); + StringWriter sw = new StringWriter(); // Passing the filewriter directly results in a blank file + Data.parser.toJson(db, sw); + String out = sw.toString(); + //AL.info(out); + Files.writeString(jsonData.toPath(), out); + } + } } return files; } @@ -695,7 +696,7 @@ else if (command.equals("Duplicate")) { tableName.input.sty("font-weight", "bold").sty("font-size", "larger"); hl.add(tableName); tableName.setTooltip("The table name. Changes are auto-saved."); - tableName.onValueChange(e -> { // enter pressed event + tableName.onValueChange(e -> { try { renameTable(dbName, e.valueBefore, e.value); } catch (Exception ex) { @@ -770,6 +771,10 @@ private void renameTable(String dbName, String oldName, String newName) throws I Table t = Data.findTable(db.tables, oldName); Objects.requireNonNull(t); t.name = newName; + + // Update current change + t.currentChange.newTableName = newName; + Data.save(); AL.info("OK!"); } @@ -785,6 +790,11 @@ private void addNewTable(String dbName, String tableName) throws IOException { t.addIdColumn(); db.tables.add(t); t.name = tableName; + + // Update current change + t.currentChange.newTableName = tableName; + t.currentChange.oldTableName = tableName; + Data.save(); updateTablesList(dbName); } @@ -905,7 +915,7 @@ private void updateColumnsList(Vertical listColumns, String dbName, String table Column col = new Column(colName.getValue()); col.definition = colDef.getValue(); col.comment = colComment.getValue(); - addNewColumn(listColumns, dbName, t.name, col, null); + addNewColumn(listColumns, dbName, t.name, col); colName.setValue(""); } catch (Exception e) { e.printStackTrace(); @@ -914,28 +924,70 @@ private void updateColumnsList(Vertical listColumns, String dbName, String table } private void updateColumn(Vertical listColumns, String dbName, String tableName, String oldName, String newName, String newDefinition, String newComment) throws IOException { - AL.info("Updating column..."); + AL.info("Updating column "+oldName+" -> "+newName); Database db = Data.getDatabase(dbName); Table t = Data.findTable(db.tables, tableName); Objects.requireNonNull(t); Column col = Data.findColumn(t.columns, oldName); Objects.requireNonNull(col); - AL.info("OLD: " + col.name + " " + col.definition + " " + col.comment); col.updateName(newName); + String oldDefinition = col.definition; col.definition = newDefinition; col.comment = newComment; - AL.info("NEW: " + col.name + " " + col.definition + " " + col.comment); + + // Update current change + t.currentChange.deletedColumnNames.remove(oldName); + if(t.currentChange.addedColumnNames.contains(oldName)){ + int i = t.currentChange.addedColumnNames.indexOf(oldName); + t.currentChange.addedColumnNames.set(i, newName); + t.currentChange.addedColumnDefinitions.set(i, newDefinition); + } else{ + // Existing column, check for changes + int i = t.currentChange.newColumnNames.indexOf(oldName); + if(i >= 0){ + t.currentChange.newColumnNames.remove(i); + t.currentChange.newColumnNames_Definitions.remove(i); + t.currentChange.oldColumnNames.remove(i); + } + if(!newName.equals(oldName)) { + t.currentChange.newColumnNames.add(newName); + t.currentChange.newColumnNames_Definitions.add(newDefinition); + t.currentChange.oldColumnNames.add(oldName); + } + + i = t.currentChange.newColumnDefinitions.indexOf(oldDefinition); + if(i >= 0){ + t.currentChange.newColumnDefinitions.remove(i); + t.currentChange.oldColumnDefinitions.remove(i); + t.currentChange.newColumnDefinitions_Names.remove(i); + } + if(!newDefinition.equals(oldDefinition)) { + t.currentChange.newColumnDefinitions.add(newDefinition); + t.currentChange.oldColumnDefinitions.add(oldDefinition); + t.currentChange.newColumnDefinitions_Names.add(newName); + } + } + + if(!oldDefinition.equals(newDefinition)) + AL.info("Updating column definition "+oldDefinition+ " -> " + newDefinition); Data.save(); AL.info("OK!"); } - private void addNewColumn(Vertical listColumns, String dbName, String tableName, Column col, String columnDefinition) throws IOException { + private void addNewColumn(Vertical listColumns, String dbName, String tableName, Column col) throws IOException { Database db = Data.getDatabase(dbName); Table t = Data.findTable(db.tables, tableName); Objects.requireNonNull(t); col.id = Main.idCounter.getAndIncrement(); t.columns.add(col); - col.definition = columnDefinition; + + // Update current change + if(!t.currentChange.addedColumnNames.contains(col.name)){ + t.currentChange.addedColumnNames.add(col.name); + t.currentChange.addedColumnDefinitions.add(col.definition); + t.currentChange.deletedColumnNames.remove(col.name); + } + Data.save(); updateColumnsList(listColumns, dbName, tableName); } @@ -947,6 +999,17 @@ private void deleteColumn(Vertical listColumns, String dbName, String tableName, Column col = Data.findColumn(t.columns, columnName); Objects.requireNonNull(col); t.columns.remove(col); + + // Update current change + if(!t.currentChange.deletedColumnNames.contains(col.name)){ + t.currentChange.deletedColumnNames.add(col.name); + int i = t.currentChange.addedColumnNames.indexOf(col.name); + if(i >= 0) { + t.currentChange.addedColumnNames.remove(i); + t.currentChange.addedColumnDefinitions.remove(i); + } + } + Data.save(); updateColumnsList(listColumns, dbName, tableName); } diff --git a/core/src/main/java/com/osiris/jsqlgen/generator/GetTableChange.java b/core/src/main/java/com/osiris/jsqlgen/generator/GetTableChange.java index 261c86e..9e751eb 100644 --- a/core/src/main/java/com/osiris/jsqlgen/generator/GetTableChange.java +++ b/core/src/main/java/com/osiris/jsqlgen/generator/GetTableChange.java @@ -24,7 +24,7 @@ public static TableChange get(Table t, List oldDatabases) { TableChange newChange = new TableChange(); if(oldT == null || t.changes.isEmpty()){ - // Means this is a brand new table, or a table without the initial change to create it + // Means this is a brand new table ( == a table without the initial change to create it) newChange.oldTableName = t.name; newChange.newTableName = t.name; for (Column c : t.columns) { diff --git a/core/src/main/java/com/osiris/jsqlgen/generator/JavaCodeGenerator.java b/core/src/main/java/com/osiris/jsqlgen/generator/JavaCodeGenerator.java index 904dd9a..24cb0c8 100644 --- a/core/src/main/java/com/osiris/jsqlgen/generator/JavaCodeGenerator.java +++ b/core/src/main/java/com/osiris/jsqlgen/generator/JavaCodeGenerator.java @@ -96,7 +96,8 @@ public static void prepareTables(Database db) throws Exception { public static List oldDatabases = new ArrayList<>(); /** - * Generates Java source code, for the provided table. + * Generates Java source code, for the provided table.
+ * Modifies the provided db object, for example to reset the {@link Table#currentChange}. */ public static String generateTableFile(File oldGeneratedClass, Table t, Database db) throws Exception { @@ -206,9 +207,13 @@ public static String generateTableFile(File oldGeneratedClass, Table t, Database "public void setId(int id){this.id = id;}\n"); // STATIC TABLE INIT METHOD - TableChange currentTableChange = GetTableChange.get(t, oldDatabases); - if(t.changes.isEmpty() || currentTableChange.hasChanges()) { - t.changes.add(currentTableChange); + // TODO make sure SQL is valid before saving JSON, make this check directly inse the update/delete/add methods for columns. + // TODO also check if a type conversion was made and check if that conversion is valid. + if(t.changes.isEmpty() || t.currentChange.hasChanges()) { + if(t.currentChange.oldTableName.isEmpty()) t.currentChange.oldTableName = t.name; + if(t.currentChange.newTableName.isEmpty()) t.currentChange.newTableName = t.name; + t.changes.add(t.currentChange); + t.currentChange = new TableChange(); AL.info("Detected change in table '"+t.name+"' and added it."); } classContentBuilder.append(GenStaticTableConstructor.s(t, tNameQuoted)); diff --git a/core/src/main/java/com/osiris/jsqlgen/model/Table.java b/core/src/main/java/com/osiris/jsqlgen/model/Table.java index 1f58edc..b208540 100644 --- a/core/src/main/java/com/osiris/jsqlgen/model/Table.java +++ b/core/src/main/java/com/osiris/jsqlgen/model/Table.java @@ -13,7 +13,11 @@ public class Table { public boolean isCache = false; public boolean isVaadinFlowUI = false; public ArrayList changes = new ArrayList<>(); - + /** + * Should be added to {@link #changes} before generating the code.
+ * Should be always up-to-date with the latest user changes (like adding/removing/changing columns).
+ */ + public TableChange currentChange = new TableChange(); public Table addIdColumn(){ Column idColumn = new Column("id"); @@ -36,6 +40,7 @@ public Table duplicate() { t.isVaadinFlowUI = isVaadinFlowUI; t.changes.clear(); t.changes.addAll(changes); // TODO is proper duplicate function required for this? + t.currentChange = currentChange; return t; } } diff --git a/gradle.properties b/gradle.properties index 743eb82..c932e40 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,4 +3,4 @@ org.gradle.jvmargs=-Xms512M -Xmx4096m -XX:MaxMetaspaceSize=1G org.gradle.configureondemand=false robovmVersion=2.3.16 androidPluginVersion=7.3.0 -deskuVersion=b3346e2b3c +deskuVersion=1.2.1