Skip to content

Commit

Permalink
- added toMinimalPrintString()
Browse files Browse the repository at this point in the history
- added db reflection
  • Loading branch information
Osiris-Team committed Apr 4, 2024
1 parent 5e35c9e commit db112fb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ Person.whereName().is("John").remove(); // Removes all rows where the name equal

## Overview

### ⚡️ 0% boilerplate, minimal code, fast development and prototyping via GUI
### ⚡️ Compile-/Typesafe SQL queries via WHERE class
### ⚡️ Various utility methods, like fetching results lazily
### ⚡️ Low code
#### Fast development and prototyping due to 0% boilerplate and developer friendly API

### ⚡️ Bug and SQL safety
#### Compile-/Typesafe SQL queries via WHERE class

### ⚡️ Powerful API
#### Extensive utility methods, like fetching results lazily and DB reflection

#### How does it work?
Generates one class for each table.
Expand Down Expand Up @@ -115,6 +120,8 @@ to the database directly could result in issues.
- (Optional) Supports generating Vaadin Flow Form to create/update/delete each object/row.
- The Vaadin Flow form also supports references to other tables (your field must be named `<table>Id` for example `personId`), meaning it will create a ComboBox that displays the object/row as string instead of only the id
which also contains all the rows of that table and allows the user to change it.
- DB "Reflection", meaning you can access table names, their columns, definitions and
even execute get/add/update/delete in the generated database class.

### 🔴 Cons / Todo
PRs for these issues are greatly appreciated (sorted from most important, to least important).
Expand Down
56 changes: 53 additions & 3 deletions src/main/java/com/osiris/jsqlgen/generator/GenDatabaseFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,28 @@ public static void s(Database db, File databaseFile, String rawUrl, String url,
int latestTableVersion = t.changes.size(); // TODO t.changes has not latest change included at this point yet
// TODO since this gets generated before the tables get generated.
// id, tableVersion, steps
s.append("new TableMetaData("+t.id+", "+latestTableVersion+", 0)");
s.append("new TableMetaData("+t.id+", "+latestTableVersion+", 0, \""+t.name+"\", ");
s.append("new String[]{");
for (int j = 0; j < t.columns.size(); j++) {
s.append("\""+t.columns.get(j).name+"\"");
if(j != t.columns.size() - 1) s.append(", ");
}
s.append("}, new String[]{");
for (int j = 0; j < t.columns.size(); j++) {
s.append("\""+t.columns.get(j).definition+"\"");
if(j != t.columns.size() - 1) s.append(", ");
}
s.append("})");

// Create overriding methods
s.append("{");
s.append("public Class<?> getTableClass(){return "+t.name+".class;}");
s.append("public List<Database.Row> get(){List<Database.Row> l = new ArrayList<>(); for("+t.name+" obj : "+t.name+".get()) l.add(obj); return l;}");
s.append("public Database.Row get(int i){return "+t.name+".get(i);}");
s.append("public void update(Database.Row obj){"+t.name+".update(("+ t.name +")obj);}");
s.append("public void add(Database.Row obj){"+t.name+".add(("+ t.name +")obj);}");
s.append("public void remove(Database.Row obj){"+t.name+".remove(("+ t.name +")obj);}");
s.append("}");

if(i != tables.size() - 1) s.append(", ");
}
Expand Down Expand Up @@ -148,7 +169,14 @@ public static void s(Database db, File databaseFile, String rawUrl, String url,
" }\n" +
"" +
" public static TableMetaData getTableMetaData(int tableId) {\n" +
" TableMetaData t = new TableMetaData(tableId, 0, 0);\n" + // tableId, tableVersion, steps
" TableMetaData t = null;\n" +
" for (TableMetaData t_ : tables) {\n" +
" if(t_.id == tableId){\n" +
" t = t_;\n" +
" break;\n" +
" }\n" +
" }\n" +
" Objects.requireNonNull(t);\n" +
" try (Connection c = DriverManager.getConnection(Database.url, Database.username, Database.password);\n" +
" Statement s = c.createStatement()) {\n" +
" try (ResultSet rs = s.executeQuery(\"SELECT `tableId`,`tableVersion`,`steps`\" +\n" +
Expand Down Expand Up @@ -204,17 +232,39 @@ public static void s(Database db, File databaseFile, String rawUrl, String url,
" System.err.println(\"Something went really wrong during database initialisation, program will exit.\");\n" +
" System.exit(1);\n" +
" }\n" +
"\n" +
" // Implementations for the following methods are provided in the array initialisation of 'tables'\n" +
"\n" +
" public Class<?> getTableClass(){throw new RuntimeException(\"Not implemented!\");}\n" + // Class is not provided as field to prevent static constructor execution
" public List<Database.Row> get(){throw new RuntimeException(\"Not implemented!\");}\n" +
" public Database.Row get(int i){throw new RuntimeException(\"Not implemented!\");}\n" +
" public void update(Database.Row obj){throw new RuntimeException(\"Not implemented!\");}\n" +
" public void add(Database.Row obj){throw new RuntimeException(\"Not implemented!\");}\n" +
" public void remove(Database.Row obj){throw new RuntimeException(\"Not implemented!\");}\n" +
" }\n" +
" public interface Row<T extends Row>{\n" +
" T update();\n" +
" T add();\n" +
" T remove();\n" +
" String toPrintString();\n" +
" String toMinimalPrintString();\n" +
" }\n" +
"\n" +
" public static class TableMetaData {\n" +
" public int id;\n" +
" public int version;\n" +
" public int steps;\n" +
" public String name;\n" +
" public String[] columns;\n" +
" public String[] definitions;\n" +
"\n" +
" public TableMetaData(int id, int version, int steps) {\n" +
" public TableMetaData(int id, int version, int steps, String name, String[] columns, String[] definitions) {\n" +
" this.id = id;\n" +
" this.version = version;\n" +
" this.steps = steps;\n" +
" this.name = name;\n" +
" this.columns = columns;\n" +
" this.definitions = definitions;\n" +
" }\n" +
" }\n");
for (Table t : tables) {
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/com/osiris/jsqlgen/generator/GenVaadinFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,10 @@ public static String s(Database db, Table t, LinkedHashSet<String> importsList)
s.append(" {"+fieldName+".setItems("+refTable.name+".get());\n" +
" "+fieldName+".setRenderer(new ComponentRenderer<>(obj -> {\n" +
" Div div = new Div();\n"+
" div.setText(");
String valAsString = "\"\"+";
for (Column refCol : refTable.columns) {
if (refCol.type.isBlob() || refCol.type.isDateOrTime()) continue;
valAsString += "obj." + refCol.name;
valAsString += "+\"; \"+";
}
valAsString += "\"\"";
s.append(valAsString + ");\n" +
" div.setText(obj.toMinimalPrintString());\n" +
" return div;}));\n" +
" "+fieldName+".setItemLabelGenerator(obj -> {\n" +
" return "+valAsString+";\n" +
" return obj.toMinimalPrintString();\n" +
" });\n" +
" }\n");
} else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static String generateTableFile(File oldGeneratedClass, Table t, Database
}
classContentBuilder.append(
"*/\n" +
"public class " + t.name + "{\n"); // Open class
"public class " + t.name + " implements Database.Row<"+t.name+">{\n"); // Open class

classContentBuilder.append("// The code below will not be removed when re-generating this class.\n");

Expand Down Expand Up @@ -407,6 +407,16 @@ else if (line.contains("// Additional code start ->")) {
classContentBuilder.substring(0, classContentBuilder.length() - 1);
classContentBuilder.append(";\n}\n");

classContentBuilder.append("public String toMinimalPrintString(){\n");
String valAsString = "\"\"+";
for (Column refCol : t.columns) {
if (refCol.type.isBlob() || refCol.type.isDateOrTime()) continue;
valAsString += "this." + refCol.name;
valAsString += "+\"; \"+";
}
valAsString += "\"\"";
classContentBuilder.append("return "+valAsString+";\n}\n");

// CREATE OBJ TOVAADINCOMPONENT METHOD
if (t.isVaadinFlowUI)
classContentBuilder.append(GenVaadinFlow.s(db, t, importsList));
Expand Down

0 comments on commit db112fb

Please sign in to comment.