Skip to content

Commit

Permalink
2.0.3
Browse files Browse the repository at this point in the history
various fixes
  • Loading branch information
Osiris-Team committed Sep 15, 2024
1 parent e9494bd commit 0b6d795
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ configure(subprojects - project(':android')) {
}

subprojects {
version = '2.0.2'
version = '2.0.3'
ext.appName = 'Desku-App'
repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/osiris/jsqlgen/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static Map<Database, DBWrapper> 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) {
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/com/osiris/jsqlgen/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
105 changes: 84 additions & 21 deletions core/src/main/java/com/osiris/jsqlgen/MainView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -594,18 +592,7 @@ public <T extends Node> List<File> generateCode(List<Database> 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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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!");
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static TableChange get(Table t, List<Database> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public static void prepareTables(Database db) throws Exception {
public static List<Database> oldDatabases = new ArrayList<>();

/**
* Generates Java source code, for the provided table.
* Generates Java source code, for the provided table. <br>
* 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 {

Expand Down Expand Up @@ -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));
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/com/osiris/jsqlgen/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public class Table {
public boolean isCache = false;
public boolean isVaadinFlowUI = false;
public ArrayList<TableChange> changes = new ArrayList<>();

/**
* Should be added to {@link #changes} before generating the code. <br>
* Should be always up-to-date with the latest user changes (like adding/removing/changing columns). <br>
*/
public TableChange currentChange = new TableChange();

public Table addIdColumn(){
Column idColumn = new Column("id");
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0b6d795

Please sign in to comment.