Skip to content

Commit 0b6d795

Browse files
committed
2.0.3
various fixes
1 parent e9494bd commit 0b6d795

File tree

8 files changed

+113
-30
lines changed

8 files changed

+113
-30
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ configure(subprojects - project(':android')) {
2929
}
3030

3131
subprojects {
32-
version = '2.0.2'
32+
version = '2.0.3'
3333
ext.appName = 'Desku-App'
3434
repositories {
3535
mavenCentral()

core/src/main/java/com/osiris/jsqlgen/Data.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static Map<Database, DBWrapper> getOldAndNewDBsMap(@NotNull CopyOnWriteAr
175175
if (isNewer) {
176176

177177
// If there are missing changes add them (which might happen when importing databases generated by older jSQL-Gen versions).
178-
// For example if the table contains a column, but there is no change referencing that column, then it will be added to the first
178+
// 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.
179179
// 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
180180
// which then saves the table with the change object missing.
181181
for (Table t : dbNew.tables) {

core/src/main/java/com/osiris/jsqlgen/Main.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public static void main(String[] _args) {
3232
App.init(null, loggerParams);
3333
AL.info("DB initialized at: "+com.osiris.jsqlgen.jsqlgen.Database.url); // Init DB by static constructor
3434

35+
// Update id counter if there is an imported table with larger ids
36+
for (Database db : Data.instance.databases) {
37+
for (Table t : db.tables) {
38+
for (Column col : t.columns) {
39+
if(col.id > idCounter.get())
40+
idCounter.set((int) (col.id + 1));
41+
}
42+
}
43+
}
44+
3545
for (Database db : Data.instance.databases) {
3646
// If there are missing ids set them
3747
for (Table t : db.tables) {

core/src/main/java/com/osiris/jsqlgen/MainView.java

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.osiris.desku.App;
1010
import com.osiris.desku.Icon;
1111
import com.osiris.desku.ui.DesktopUI;
12-
import com.osiris.desku.Route;
1312
import com.osiris.desku.ui.UI;
1413
import com.osiris.desku.ui.Component;
1514
import com.osiris.desku.ui.input.*;
@@ -19,7 +18,6 @@
1918
import com.osiris.desku.ui.layout.Popup;
2019
import com.osiris.desku.ui.layout.TabLayout;
2120
import com.osiris.desku.ui.layout.Vertical;
22-
import com.osiris.dyml.utils.UtilsFile;
2321
import com.osiris.jlib.logger.AL;
2422
import com.osiris.jsqlgen.generator.GenDatabaseFile;
2523
import 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
}

core/src/main/java/com/osiris/jsqlgen/generator/GetTableChange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static TableChange get(Table t, List<Database> oldDatabases) {
2424
TableChange newChange = new TableChange();
2525

2626
if(oldT == null || t.changes.isEmpty()){
27-
// Means this is a brand new table, or a table without the initial change to create it
27+
// Means this is a brand new table ( == a table without the initial change to create it)
2828
newChange.oldTableName = t.name;
2929
newChange.newTableName = t.name;
3030
for (Column c : t.columns) {

core/src/main/java/com/osiris/jsqlgen/generator/JavaCodeGenerator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ public static void prepareTables(Database db) throws Exception {
9696
public static List<Database> oldDatabases = new ArrayList<>();
9797

9898
/**
99-
* Generates Java source code, for the provided table.
99+
* Generates Java source code, for the provided table. <br>
100+
* Modifies the provided db object, for example to reset the {@link Table#currentChange}.
100101
*/
101102
public static String generateTableFile(File oldGeneratedClass, Table t, Database db) throws Exception {
102103

@@ -206,9 +207,13 @@ public static String generateTableFile(File oldGeneratedClass, Table t, Database
206207
"public void setId(int id){this.id = id;}\n");
207208

208209
// STATIC TABLE INIT METHOD
209-
TableChange currentTableChange = GetTableChange.get(t, oldDatabases);
210-
if(t.changes.isEmpty() || currentTableChange.hasChanges()) {
211-
t.changes.add(currentTableChange);
210+
// TODO make sure SQL is valid before saving JSON, make this check directly inse the update/delete/add methods for columns.
211+
// TODO also check if a type conversion was made and check if that conversion is valid.
212+
if(t.changes.isEmpty() || t.currentChange.hasChanges()) {
213+
if(t.currentChange.oldTableName.isEmpty()) t.currentChange.oldTableName = t.name;
214+
if(t.currentChange.newTableName.isEmpty()) t.currentChange.newTableName = t.name;
215+
t.changes.add(t.currentChange);
216+
t.currentChange = new TableChange();
212217
AL.info("Detected change in table '"+t.name+"' and added it.");
213218
}
214219
classContentBuilder.append(GenStaticTableConstructor.s(t, tNameQuoted));

core/src/main/java/com/osiris/jsqlgen/model/Table.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public class Table {
1313
public boolean isCache = false;
1414
public boolean isVaadinFlowUI = false;
1515
public ArrayList<TableChange> changes = new ArrayList<>();
16-
16+
/**
17+
* Should be added to {@link #changes} before generating the code. <br>
18+
* Should be always up-to-date with the latest user changes (like adding/removing/changing columns). <br>
19+
*/
20+
public TableChange currentChange = new TableChange();
1721

1822
public Table addIdColumn(){
1923
Column idColumn = new Column("id");
@@ -36,6 +40,7 @@ public Table duplicate() {
3640
t.isVaadinFlowUI = isVaadinFlowUI;
3741
t.changes.clear();
3842
t.changes.addAll(changes); // TODO is proper duplicate function required for this?
43+
t.currentChange = currentChange;
3944
return t;
4045
}
4146
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ org.gradle.jvmargs=-Xms512M -Xmx4096m -XX:MaxMetaspaceSize=1G
33
org.gradle.configureondemand=false
44
robovmVersion=2.3.16
55
androidPluginVersion=7.3.0
6-
deskuVersion=b3346e2b3c
6+
deskuVersion=1.2.1

0 commit comments

Comments
 (0)