Skip to content

Commit

Permalink
1.57
Browse files Browse the repository at this point in the history
- fix logs text area collapsed
- add basic SQL syntax checking
- check projects for newer versions of the database and replace used one if so
- add "merge from projects" button, which allows to import one or multiple databases from existing database_structure.json files, via recursive search
- add more vaadin related features
  • Loading branch information
Osiris-Team committed Jun 1, 2024
1 parent d91e0f3 commit d51723a
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 58 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,16 @@ to the database directly could result in issues.
which also contains all the rows of that table and allows the user to change it.
- DB "Reflection", meaning you can loop through table names, their columns, definitions and
even execute get/add/update/delete. Provided in the generated Database class.
- If a project contains a table with more/newer changes, that database is replaced/updated in jSQL-Gen.
- UI: "Merge from projects" button, which allows to import one or multiple databases from existing database_structure.json files, via recursive file search.

### 🔴 Cons / Todo
PRs for these issues are greatly appreciated (sorted from most important, to least important).
- You need to know a bit of SQL, especially about definitions and defaults. This could be fixed by simplifying the GUI further.
- Internally a `idCounter` is used for each table, meaning if rows are added by another program the counter won't be accurate anymore and thus further insert operations will fail.
- Java Code Generator: No support for `FOREIGN KEY` / references between tables. However note that the idea of references is supported (columns named tableNameId are refs).
- Java Code Generator: No support for `VIEW, JOIN, UNION` / merged tables/results. This might never get fixed if its not possible to create a developer-friendly / simple API for this.
- Since there is a global id counter for all tables and columns, in each application, collaboration and syncing changes might be difficult.

## Tipps
- You can select a project directory to directly generate the code in there. The generated code/files can also be found in the `generated` folder (press `Show Data` on the first tab, to open the location).
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.osiris.jsqlgen</groupId>
<artifactId>jSQL-Gen</artifactId>
<version>1.56</version>
<version>1.57</version>
<name>jSQL-Gen</name>
<!-- TODO add id reference deletion UI for vaadin -->

Expand Down Expand Up @@ -72,6 +72,11 @@
<version>11.1.0</version>
</dependency>
<!-- OTHER -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/osiris/jsqlgen/Data.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package com.osiris.jsqlgen;

import com.google.gson.*;
import com.osiris.jsqlgen.generator.JavaCodeGenerator;
import com.osiris.jsqlgen.model.Column;
import com.osiris.jsqlgen.model.Database;
import com.osiris.jsqlgen.model.Table;
import com.osiris.jsqlgen.utils.FileTypeAdapter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

public class Data {
public static final File file = new File(Main.dir + "/data.yml");
public static final File backupDir = new File(Main.dir + "/backup");
public static final DataJson instance;
private static AtomicBoolean save = new AtomicBoolean(false);
public static Gson parser = new GsonBuilder().registerTypeAdapter(File.class, new FileTypeAdapter())
Expand All @@ -26,13 +35,37 @@ public static void save(){

static {
try{
backupDir.mkdirs();
synchronized (file) {
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
Files.writeString(file.toPath(), "{}");
}
instance = parser.fromJson(new BufferedReader(new FileReader(file)), DataJson.class);
// Check if there is a project that contains a newer version of the database (aka with more changes)
CopyOnWriteArrayList<Database> databases = instance.databases;
Map<Database, Database> oldAndNew = getOldAndNewDBsMap(databases, null);
// Backup before replacing, then replace
databases.replaceAll(dbOld -> {
Database dbNew = oldAndNew.get(dbOld);
if(dbNew != null) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = getFormatter();
File backup = new File(backupDir+"/backup-db-"+dbOld.name+"-pre-import-"+now.format(formatter)+".json");
try {
Files.writeString(backup.toPath(), parser.toJson(dbOld, Database.class));
System.out.println("LOADED NEWER DATABASE STRUCTURE FROM: "+dbNew.javaProjectDir);
return dbNew;
} catch (IOException e) {
e.printStackTrace();
System.err.println("FAILED TO LOAD NEWER DATABASE STRUCTURE, DUE TO FAILING TO BACKUP OLDER DATABASE STRUCTURE!");
return dbOld;
}
}
else return dbOld;
});
// Update names
for (Database db : instance.databases) {
for (Table t : db.tables) {
for (Column col : t.columns) {
Expand Down Expand Up @@ -83,6 +116,58 @@ public static void save(){
}
}

@NotNull
public static DateTimeFormatter getFormatter() {
return DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
}

@NotNull
public static Map<Database, Database> getOldAndNewDBsMap(@NotNull CopyOnWriteArrayList<Database> oldDBs,
@Nullable CopyOnWriteArrayList<Database> newDBs) {
Map<Database, Database> oldAndNew = new HashMap<>();
for (int k = 0; k < oldDBs.size(); k++) {
Database db = oldDBs.get(k);
JavaProjectGenDir javaProjectGenDir = getJavaProjectGenDir(db);
File databaseStructureFile = getDatabaseStructureFile(db, javaProjectGenDir);
Database dbNew = null;
try{
dbNew = parser.fromJson(new BufferedReader(new FileReader(databaseStructureFile)), Database.class);
} catch (Exception e) {}
if(dbNew == null && newDBs != null){
for (Database dbNew1 : newDBs) {
if(dbNew1.name.equals(db.name)) {
dbNew = dbNew1;
break;
}
}
}
if(dbNew == null) continue;
CopyOnWriteArrayList<Table> tablesNew = dbNew.tables;
CopyOnWriteArrayList<Table> tablesOld = db.tables;
boolean isNewer = false;
if((!tablesNew.isEmpty() && !tablesOld.isEmpty()) &&
(tablesNew.get(0).changes != null && tablesOld.get(0).changes == null)){
// Support older jsqlgen data json formats
isNewer = true;
}
if(!isNewer)
for (Table tNew : tablesNew) {
CopyOnWriteArrayList<Table> oldTables = db.tables;
for (Table tOld : oldTables) {
if (tOld.id == tNew.id) {
if (tOld.changes.size() > tNew.changes.size()) isNewer = true;
break;
}
}
if (isNewer) break;
}
if (isNewer) {
oldAndNew.put(db, dbNew);
}
}
return oldAndNew;
}

public Data() throws IOException {
super();
}
Expand Down Expand Up @@ -127,4 +212,25 @@ public static void saveNow() {
e.printStackTrace();
}
}

@NotNull
public static JavaProjectGenDir getJavaProjectGenDir(Database db) {
return new JavaProjectGenDir(db.javaProjectDir + "/src/main/java/com/osiris/jsqlgen/" + db.name);
}

public static class JavaProjectGenDir extends File{
public JavaProjectGenDir(@NotNull String pathname) {
super(pathname);
}
}

@NotNull
public static File getDatabaseStructureFile(Database db, JavaProjectGenDir javaProjectDir) {
return new File(javaProjectDir.getParentFile() + "/" + db.name + "_structure.json");
}

@NotNull
public static File getDatabaseFile(JavaProjectGenDir javaProjectDir) {
return new File(javaProjectDir + "/Database.java");
}
}
1 change: 1 addition & 0 deletions src/main/java/com/osiris/jsqlgen/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public class Main {
public static File dir = new File(System.getProperty("user.home") + "/jSQL-Gen");
public static File generatedDir = new File(Main.dir + "/generated");
public static AtomicInteger idCounter = new AtomicInteger(new Config().idCounter.asInt());

public static void main(String[] args) {
Expand Down
Loading

0 comments on commit d51723a

Please sign in to comment.