Skip to content

Commit

Permalink
2.0.5
Browse files Browse the repository at this point in the history
various fixes, builder-like setter methods for fields, more restrictive checking if SQL definition is valid
  • Loading branch information
Osiris-Team committed Dec 11, 2024
1 parent 0b6d795 commit d22fd66
Show file tree
Hide file tree
Showing 14 changed files with 919 additions and 478 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.3'
version = '2.0.5'
ext.appName = 'Desku-App'
repositories {
mavenCentral()
Expand Down
7 changes: 7 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ dependencies {
implementation "mysql:mysql-connector-java:8.0.24"
implementation 'net.java.dev.jna:jna:5.14.0'
implementation 'net.java.dev.jna:jna-platform:5.14.0'
implementation "ru.lanwen.verbalregex:java-verbal-expressions:1.8"


testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.0"
testImplementation "org.junit.jupiter:junit-jupiter-engine:5.3.0"
testImplementation "com.vaadin:vaadin-bom:24.4.7"
testImplementation "com.vaadin:vaadin-core:24.4.7"


}
Expand All @@ -37,3 +40,7 @@ sourceSets {
}
// This must also be included if you want to generate the sources jar without issues
tasks.withType(Jar).configureEach { duplicatesStrategy = DuplicatesStrategy.EXCLUDE }

tasks.test {
useJUnitPlatform() // For JUnit 5
}
62 changes: 6 additions & 56 deletions core/src/main/java/com/osiris/jsqlgen/MainView.java
Original file line number Diff line number Diff line change
Expand Up @@ -930,46 +930,12 @@ private void updateColumn(Vertical listColumns, String dbName, String tableName,
Objects.requireNonNull(t);
Column col = Data.findColumn(t.columns, oldName);
Objects.requireNonNull(col);
col.updateName(newName);
String oldDefinition = col.definition;
col.definition = newDefinition;
col.comment = newComment;
Column colOld = col.duplicate();

// 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);
}
}
t.updateCol(col, oldName, newName, newDefinition, newComment);

if(!oldDefinition.equals(newDefinition))
AL.info("Updating column definition "+oldDefinition+ " -> " + newDefinition);
if(!colOld.definition.equals(newDefinition))
AL.info("Updating column definition "+colOld.definition+ " -> " + newDefinition);
Data.save();
AL.info("OK!");
}
Expand All @@ -979,14 +945,7 @@ private void addNewColumn(Vertical listColumns, String dbName, String tableName,
Table t = Data.findTable(db.tables, tableName);
Objects.requireNonNull(t);
col.id = Main.idCounter.getAndIncrement();
t.columns.add(col);

// 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);
}
t.addCol(col);

Data.save();
updateColumnsList(listColumns, dbName, tableName);
Expand All @@ -998,17 +957,8 @@ private void deleteColumn(Vertical listColumns, String dbName, String tableName,
Objects.requireNonNull(t);
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);
}
}
t.removeCol(col);

Data.save();
updateColumnsList(listColumns, dbName, tableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.osiris.jsqlgen.model.Column;
import com.osiris.jsqlgen.model.Table;

import static com.osiris.jsqlgen.utils.UString.containsIgnoreCase;

public class GenCreateMethods {
public static String s(Table t, String tNameQuoted, JavaCodeGenerator.Constructor constructor, JavaCodeGenerator.Constructor minimalConstructor, boolean hasMoreFields) {
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.concurrent.CopyOnWriteArrayList;

public class GenDatabaseFile {
public static void s(Database db, File databaseFile, String rawUrl, String url, String name, String username, String password) throws IOException {
databaseFile.getParentFile().mkdirs();
databaseFile.createNewFile();

StringBuilder s = new StringBuilder((!db.getJavaProjectDirs().isEmpty() ? "package com.osiris.jsqlgen." + db.name + ";\n" : "") +
"import java.sql.*;\n" +
"import java.util.*;\n" +
"import java.io.File;\n" +
(db.isWithMariadb4j ? "" +
"import ch.vorburger.exec.ManagedProcessException;\n" +
"import ch.vorburger.mariadb4j.DB;\n" +
"import ch.vorburger.mariadb4j.DBConfigurationBuilder;\n" : "")+
"\n"+
LinkedHashSet<String> imports = new LinkedHashSet<>();
imports.add("import java.sql.*;");
imports.add("import java.util.*;");
imports.add("import java.io.File;");

StringBuilder s = new StringBuilder(
"/*\n" +
"Auto-generated class that is used by all table classes to create connections. <br>\n" +
"It holds the database credentials (set by you at first run of jSQL-Gen).<br>\n" +
Expand Down Expand Up @@ -330,9 +329,27 @@ public static void initIntegratedMariaDB() {
""");
}

// Add other dependencies
s.append(GenDefBlobClass.s(imports));

s.append("}\n");


Files.writeString(databaseFile.toPath(), s.toString());

String sNoImports = s.toString();

String finalS = (!db.getJavaProjectDirs().isEmpty() ? "package com.osiris.jsqlgen." + db.name + ";\n" : "") +
(db.isWithMariadb4j ? "" +
"import ch.vorburger.exec.ManagedProcessException;\n" +
"import ch.vorburger.mariadb4j.DB;\n" +
"import ch.vorburger.mariadb4j.DBConfigurationBuilder;\n" : "")+
"\n";
for (String anImport : imports) {
finalS += anImport+"\n";
}
finalS += sNoImports;


Files.writeString(databaseFile.toPath(), finalS.toString());
}
}
126 changes: 64 additions & 62 deletions core/src/main/java/com/osiris/jsqlgen/generator/GenDefBlobClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,69 @@ public static String s(LinkedHashSet<String> imports) {
imports.add("import java.sql.Blob;");
imports.add("import java.sql.SQLException;");

return "public static class DefaultBlob implements Blob{\n" +
" private byte[] data;\n" +
"\n" +
" // Constructor that accepts a byte array\n" +
" public DefaultBlob(byte[] data) {\n" +
" this.data = data;\n" +
" }\n" +
" @Override\n" +
" public long length() throws SQLException {\n" +
" return data.length;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public byte[] getBytes(long pos, int length) throws SQLException {\n" +
" return data;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public InputStream getBinaryStream() throws SQLException {\n" +
" return new ByteArrayInputStream(data);\n" +
" }\n" +
"\n" +
" @Override\n" +
" public long position(byte[] pattern, long start) throws SQLException {\n" +
" return 0;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public long position(Blob pattern, long start) throws SQLException {\n" +
" return 0;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public int setBytes(long pos, byte[] bytes) throws SQLException {\n" +
" return 0;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {\n" +
" return 0;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public OutputStream setBinaryStream(long pos) throws SQLException {\n" +
" return null;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public void truncate(long len) throws SQLException {\n" +
"\n" +
" }\n" +
"\n" +
" @Override\n" +
" public void free() throws SQLException {\n" +
"\n" +
" }\n" +
"\n" +
" @Override\n" +
" public InputStream getBinaryStream(long pos, long length) throws SQLException {\n" +
" return new ByteArrayInputStream(data);\n" +
" }\n" +
"}\n";
return """
public static class DefaultBlob implements Blob{
private byte[] data;
// Constructor that accepts a byte array
public DefaultBlob(byte[] data) {
this.data = data;
}
@Override
public long length() throws SQLException {
return data.length;
}
@Override
public byte[] getBytes(long pos, int length) throws SQLException {
return data;
}
@Override
public InputStream getBinaryStream() throws SQLException {
return new ByteArrayInputStream(data);
}
@Override
public long position(byte[] pattern, long start) throws SQLException {
return 0;
}
@Override
public long position(Blob pattern, long start) throws SQLException {
return 0;
}
@Override
public int setBytes(long pos, byte[] bytes) throws SQLException {
return 0;
}
@Override
public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
return 0;
}
@Override
public OutputStream setBinaryStream(long pos) throws SQLException {
return null;
}
@Override
public void truncate(long len) throws SQLException {
}
@Override
public void free() throws SQLException {
}
@Override
public InputStream getBinaryStream(long pos, long length) throws SQLException {
return new ByteArrayInputStream(data);
}
}
""";
}
}
Loading

0 comments on commit d22fd66

Please sign in to comment.