From 16294db3230056f33c88f3e17e6b35d4c27816ce Mon Sep 17 00:00:00 2001 From: MineKing Date: Tue, 6 Feb 2024 14:25:38 +0100 Subject: [PATCH 1/3] Fix and improve Table#insert Table#update and Table#upsert --- .../javautils/database/DataClass.java | 5 +- .../de/mineking/javautils/database/Table.java | 16 +- .../javautils/database/TableImpl.java | 187 +++++++----------- .../de/mineking/javautils/database/Where.java | 21 +- src/test/java/database/InsertTest.java | 23 +-- src/test/java/database/UpdateTest.java | 116 +++++++++++ 6 files changed, 230 insertions(+), 138 deletions(-) create mode 100644 src/test/java/database/UpdateTest.java diff --git a/src/main/java/de/mineking/javautils/database/DataClass.java b/src/main/java/de/mineking/javautils/database/DataClass.java index 4604250..86ea6d6 100644 --- a/src/main/java/de/mineking/javautils/database/DataClass.java +++ b/src/main/java/de/mineking/javautils/database/DataClass.java @@ -13,14 +13,15 @@ default T insert() throws ConflictException { return getTable().insert((T) this); } + @NotNull @SuppressWarnings("unchecked") - default boolean update() { + default T update() throws ConflictException { return getTable().update((T) this); } @NotNull @SuppressWarnings("unchecked") - default T upsert() { + default T upsert() throws ConflictException { return getTable().upsert((T) this); } diff --git a/src/main/java/de/mineking/javautils/database/Table.java b/src/main/java/de/mineking/javautils/database/Table.java index 0f764ec..aca1c05 100644 --- a/src/main/java/de/mineking/javautils/database/Table.java +++ b/src/main/java/de/mineking/javautils/database/Table.java @@ -22,6 +22,9 @@ public interface Table { @NotNull Map getColumns(); + @NotNull + Map getUnique(); + @NotNull Map getKeys(); @@ -49,15 +52,22 @@ default List selectAll() { @NotNull T insert(@NotNull T object) throws ConflictException; - boolean update(@NotNull T object); + @NotNull + T update(@NotNull T object) throws ConflictException; @NotNull - T upsert(@NotNull T object); + default T upsert(@NotNull T object) throws ConflictException { + try { + return insert(object); + } catch(ConflictException e) { + return update(object); + } + } int delete(@NotNull Where where); default int delete(@NotNull T object) { - return delete(Where.of(this, object)); + return delete(Where.identify(this, object)); } default int deleteAll() { diff --git a/src/main/java/de/mineking/javautils/database/TableImpl.java b/src/main/java/de/mineking/javautils/database/TableImpl.java index e7b0d48..25a4aed 100644 --- a/src/main/java/de/mineking/javautils/database/TableImpl.java +++ b/src/main/java/de/mineking/javautils/database/TableImpl.java @@ -1,7 +1,6 @@ package de.mineking.javautils.database; import de.mineking.javautils.database.exception.ConflictException; -import org.jdbi.v3.core.result.ResultProducers; import org.jdbi.v3.core.statement.StatementContext; import org.jdbi.v3.core.statement.Update; import org.jetbrains.annotations.NotNull; @@ -85,6 +84,12 @@ public Map getColumns() { return columns; } + @NotNull + @Override + public Map getUnique() { + return unique; + } + @NotNull @Override public Map getKeys() { @@ -145,7 +150,7 @@ public int delete(@NotNull Where where) { ); } - private boolean execute(@NotNull T object, Update query) { + private boolean execute(@NotNull T object, @NotNull Update query) { columns.forEach((name, field) -> { try { query.bind(name, manager.getArgument(field.getGenericType(), field, field.get(object))); @@ -154,134 +159,86 @@ private boolean execute(@NotNull T object, Update query) { } }); - return query.execute(ResultProducers.returningResults()) - .map((rs, ctx) -> { - columns.forEach((name, field) -> { - try { - field.set(object, manager.parse(field.getGenericType(), field, manager.extract(field.getGenericType(), field, name, rs))); - } catch(IllegalAccessException | SQLException e) { - throw new RuntimeException(e); - } - }); - return true; - }).findFirst().orElse(false); + return query.execute((statementSupplier, ctx) -> { + var stmt = statementSupplier.get(); + var rs = stmt.getResultSet(); + + if(rs.next()) { + columns.forEach((name, field) -> { + try { + field.set(object, manager.parse(field.getGenericType(), field, manager.extract(field.getGenericType(), field, name, rs))); + } catch(IllegalAccessException | SQLException e) { + throw new RuntimeException(e); + } + }); + return true; + } else return false; + }); } @NotNull @Override public T insert(@NotNull T object) throws ConflictException { - var sql = "insert into () values() on conflict do nothing returning *"; - - var updated = manager.db.withHandle(handle -> { - var query = handle.createUpdate(sql) - .define("name", name) - .define("columns", columns.entrySet().stream() - .filter(e -> { - try { - return !(e.getValue().getAnnotation(Column.class).autoincrement() && ((Number) e.getValue().get(object)).longValue() <= 0); - } catch(IllegalAccessException ex) { - throw new RuntimeException(ex); - } - }) - .map(e -> '"' + e.getKey() + '"') - .collect(Collectors.joining(", ")) - ) - .define("values", columns.entrySet().stream() - .filter(e -> { - try { - return !(e.getValue().getAnnotation(Column.class).autoincrement() && ((Number) e.getValue().get(object)).longValue() <= 0); - } catch(IllegalAccessException ex) { - throw new RuntimeException(ex); - } - }) - .map(e -> ":" + e.getKey()) - .collect(Collectors.joining(", ")) - ); - - return execute(object, query); - }); + var check = Where.detectConflict(this, object, true); + var sql = "insert into () (select where not exists (select from )) returning *"; + + var updated = manager.db.withHandle(handle -> execute(object, handle.createUpdate(sql) + .define("name", name) + .define("columns", columns.entrySet().stream() + .filter(e -> { + try { + return !(e.getValue().getAnnotation(Column.class).autoincrement() && ((Number) e.getValue().get(object)).longValue() <= 0); + } catch(IllegalAccessException ex) { + throw new RuntimeException(ex); + } + }) + .map(e -> '"' + e.getKey() + '"') + .collect(Collectors.joining(", ")) + ) + .define("values", columns.entrySet().stream() + .filter(e -> { + try { + return !(e.getValue().getAnnotation(Column.class).autoincrement() && ((Number) e.getValue().get(object)).longValue() <= 0); + } catch(IllegalAccessException ex) { + throw new RuntimeException(ex); + } + }) + .map(e -> ":" + e.getKey()) + .collect(Collectors.joining(", ")) + ) + .define("where", check.format()) + .bindMap(check.formatValues(this)) + )); if(updated) return object; else throw new ConflictException(); } + @NotNull @Override - public boolean update(@NotNull T object) { - var sql = "update set "; - var identifier = Where.of(this, object); - - if(unique.size() > keys.size()) sql += " and 0 not in (select 0 from and not ())"; + public T update(@NotNull T object) throws ConflictException { + var identifier = Where.identify(this, object); + var unique = Where.detectConflict(this, object, false); + var sql = "update set "; + if(this.unique.size() > keys.size()) sql += " and not exists (select from )"; final var fSql = sql + " returning *"; - return manager.db.withHandle(handle -> { - var query = handle.createUpdate(fSql) - .define("name", name) - .define("update", columns.keySet().stream() - .filter(k -> !this.keys.containsKey(k)) - .map(k -> '"' + k + "\" = :" + k) - .collect(Collectors.joining(", ")) - ) - .define("where", identifier.format()) - .define("unique", unique.keySet().stream() - .filter(k -> !this.keys.containsKey(k)) - .map(k -> '"' + k + "\" = :" + k) - .collect(Collectors.joining(", ")) - ) - .bindMap(identifier.formatValues(this)); - - return execute(object, query); - }); - } - - @NotNull - @Override - public T upsert(@NotNull T object) { - var sql = "insert into () values() "; - - if(!unique.isEmpty()) sql += " on conflict() do update set "; - - var fSql = sql; //Because java - - manager.db.useHandle(handle -> { - var query = handle.createUpdate(fSql) - .define("name", name) - .define("columns", columns.entrySet().stream() - .filter(e -> { - try { - return !(e.getValue().getAnnotation(Column.class).autoincrement() && ((Number) e.getValue().get(object)).longValue() <= 0); - } catch(IllegalAccessException ex) { - throw new RuntimeException(ex); - } - }) - .map(e -> '"' + e.getKey() + '"') - .collect(Collectors.joining(", ")) - ) - .define("values", columns.entrySet().stream() - .filter(e -> { - try { - return !(e.getValue().getAnnotation(Column.class).autoincrement() && ((Number) e.getValue().get(object)).longValue() <= 0); - } catch(IllegalAccessException ex) { - throw new RuntimeException(ex); - } - }) - .map(e -> ":" + e.getKey()) - .collect(Collectors.joining(", ")) - ) - .define("unique", this.unique.keySet().stream() - .map(k -> '"' + k + '"') - .collect(Collectors.joining(", ")) - ) - .define("update", columns.keySet().stream() - .filter(k -> !this.keys.containsKey(k)) - .map(k -> '"' + k + "\" = :" + k) - .collect(Collectors.joining(", ")) - ); - - execute(object, query); - }); + var updated = manager.db.withHandle(handle -> execute(object, handle.createUpdate(fSql) + .define("name", name) + .define("update", columns.keySet().stream() + .filter(k -> !this.keys.containsKey(k)) + .map(k -> '"' + k + "\" = :" + k) + .collect(Collectors.joining(", ")) + ) + .define("where", identifier.format()) + .define("unique", unique.format()) + .bindMap(identifier.formatValues(this)) + .bindMap(unique.formatValues(this)) + )); - return object; + if(updated) return object; + else throw new ConflictException(); } @Override diff --git a/src/main/java/de/mineking/javautils/database/Where.java b/src/main/java/de/mineking/javautils/database/Where.java index 8388684..2a63a5a 100644 --- a/src/main/java/de/mineking/javautils/database/Where.java +++ b/src/main/java/de/mineking/javautils/database/Where.java @@ -2,6 +2,7 @@ import de.mineking.javautils.ID; import de.mineking.javautils.Pair; +import org.jdbi.v3.core.argument.Argument; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -14,7 +15,7 @@ public interface Where { @NotNull - static Where of(@NotNull Table table, @NotNull T object) { + static Where identify(@NotNull Table table, @NotNull T object) { if(table.getKeys().isEmpty()) throw new IllegalArgumentException("Cannot identify object without keys"); return allOf(table.getKeys().entrySet().stream() .map(e -> { @@ -28,6 +29,22 @@ static Where of(@NotNull Table table, @NotNull T object) { ); } + @NotNull + static Where detectConflict(@NotNull Table table, @NotNull T object, boolean includeKeys) { + if(table.getUnique().isEmpty()) return empty(); + return Where.anyOf(table.getUnique().entrySet().stream() + .filter(e -> !e.getValue().getAnnotation(Column.class).key() || includeKeys) + .map(e -> { + try { + return equals(e.getKey(), e.getValue().get(object)); + } catch(IllegalAccessException ex) { + throw new RuntimeException(ex); + } + }) + .toList() + ).and(includeKeys ? empty() : not(identify(table, object))); + } + @NotNull static Where empty() { return new Where() { @@ -165,7 +182,7 @@ default Where not() { Map> values(); @SuppressWarnings({"rawtypes", "unchecked"}) - default Map formatValues(@NotNull Table table) { + default Map formatValues(@NotNull Table table) { return values().entrySet().stream() .map(e -> { Field f = table.getColumns().get(e.getValue().key()); diff --git a/src/test/java/database/InsertTest.java b/src/test/java/database/InsertTest.java index 02a4e23..b442ceb 100644 --- a/src/test/java/database/InsertTest.java +++ b/src/test/java/database/InsertTest.java @@ -5,15 +5,18 @@ import de.mineking.javautils.database.exception.ConflictException; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; +import lombok.ToString; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class InsertTest { private final DatabaseManager manager; private final Table table; + @ToString @NoArgsConstructor @AllArgsConstructor private class TestClass implements DataClass { @@ -37,6 +40,9 @@ public InsertTest() { @Test public void insert() { + //Reset table for clean test + table.deleteAll(); + var test = new TestClass(); assertTrue(table.selectOne(Where.equals("id", test.id)).isEmpty()); @@ -50,19 +56,4 @@ public void insert() { test.insert(); assertThrows(ConflictException.class, test::insert); } - - @Test - public void update() { - var test = new TestClass(); - table.insert(test); - - assertNull(table.selectOne(Where.equals("id", test.id)).get().test); - - test.test = "abc"; - assertTrue(test.update()); - assertEquals(table.selectOne(Where.equals("id", test.id)).get().test, "abc"); - - test.id = ID.generate(); - assertFalse(test.update()); - } } diff --git a/src/test/java/database/UpdateTest.java b/src/test/java/database/UpdateTest.java new file mode 100644 index 0000000..d986f71 --- /dev/null +++ b/src/test/java/database/UpdateTest.java @@ -0,0 +1,116 @@ +package database; + +import de.mineking.javautils.ID; +import de.mineking.javautils.database.*; +import de.mineking.javautils.database.exception.ConflictException; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.jdbi.v3.core.statement.SqlLogger; +import org.jdbi.v3.core.statement.StatementContext; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class UpdateTest { + private final DatabaseManager manager; + private final Table table; + + @ToString + @NoArgsConstructor + @AllArgsConstructor + private class TestClass implements DataClass { + @Column(key = true) + public ID id; + + @Column(unique = true) + public String test; + + @Column(unique = true) + public int x; + + @NotNull + @Override + public Table getTable() { + return table; + } + } + + public UpdateTest() { + manager = new DatabaseManager("jdbc:postgresql://localhost:5433/postgres", "postgres", "postgres"); + table = manager.getTable(TestClass.class, TestClass::new, "update").createTable(); + + manager.getDriver().setSqlLogger(new SqlLogger() { + @Override + public void logBeforeExecution(StatementContext context) { + System.out.println(context.getParsedSql().getSql()); + System.out.println(context.getBinding()); + } + }); + } + + @Test + public void update() { + //Reset table for clean test + table.deleteAll(); + + var test = new TestClass(); + test.insert(); + + assertNull(table.selectOne(Where.equals("id", test.id)).get().test); + + test.test = "abc"; + test.update(); + assertEquals(table.selectOne(Where.equals("id", test.id)).get().test, "abc"); + } + + @Test + public void updateConflict() { + //Reset table for clean test + table.deleteAll(); + + var test1 = new TestClass(); + test1.insert(); + + var test2 = new TestClass(); + assertThrows(ConflictException.class, test2::insert); + + test2.test = "abc"; + test2.x = 5; + test2.insert(); + + test1.update(); + + test1.test = "def"; + test1.x = 5; + assertThrows(ConflictException.class, test1::update); + + test1.x = 10; + test1.update(); + test1.update(); + + test2.x = 10; + assertThrows(ConflictException.class, test2::update); + + test2.x = 15; + test2.update(); + } + + @Test + public void upsert() { + //Reset table for clean test + table.deleteAll(); + + var test = new TestClass(); + test.upsert(); + + test.test = "abc"; + test.upsert(); + assertEquals(table.selectOne(Where.equals("id", test.id)).get().test, "abc"); + + var test2 = new TestClass(); + test2.test = "abc"; + assertThrows(ConflictException.class, test2::upsert); + } +} From 99a27722ec6e87a2d79addbb8f828e6f5c950e6f Mon Sep 17 00:00:00 2001 From: MineKing Date: Tue, 6 Feb 2024 14:25:45 +0100 Subject: [PATCH 2/3] Reformat code --- .../de/mineking/javautils/database/Column.java | 1 + .../javautils/database/DatabaseManager.java | 2 +- .../javautils/function/ThrowingConsumer.java | 5 ++++- .../javautils/function/ThrowingFunction.java | 10 ++++------ .../javautils/function/ThrowingRunnable.java | 2 +- .../de/mineking/javautils/function/TryCatch.java | 16 ++++++++-------- .../de/mineking/javautils/io/BufferUtils.java | 2 +- .../java/de/mineking/javautils/io/FileUtils.java | 2 +- 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/mineking/javautils/database/Column.java b/src/main/java/de/mineking/javautils/database/Column.java index e133711..9304642 100644 --- a/src/main/java/de/mineking/javautils/database/Column.java +++ b/src/main/java/de/mineking/javautils/database/Column.java @@ -11,6 +11,7 @@ String name() default ""; boolean key() default false; + boolean unique() default false; String modifier() default ""; diff --git a/src/main/java/de/mineking/javautils/database/DatabaseManager.java b/src/main/java/de/mineking/javautils/database/DatabaseManager.java index 615902b..5bfc6ff 100644 --- a/src/main/java/de/mineking/javautils/database/DatabaseManager.java +++ b/src/main/java/de/mineking/javautils/database/DatabaseManager.java @@ -99,7 +99,7 @@ private class TableBuilder> { public TableBuilder(Class table, Class type, Supplier instance, String name) { this.table = (T) Proxy.newProxyInstance( getClass().getClassLoader(), - new Class[]{table}, + new Class[] {table}, new TableImpl<>(DatabaseManager.this, this::getTable, type, instance, name) ); } diff --git a/src/main/java/de/mineking/javautils/function/ThrowingConsumer.java b/src/main/java/de/mineking/javautils/function/ThrowingConsumer.java index a612e3a..4a085d7 100644 --- a/src/main/java/de/mineking/javautils/function/ThrowingConsumer.java +++ b/src/main/java/de/mineking/javautils/function/ThrowingConsumer.java @@ -26,7 +26,10 @@ public interface ThrowingConsumer { */ default ThrowingConsumer andThen(ThrowingConsumer after) { Objects.requireNonNull(after); - return (T t) -> { accept(t); after.accept(t); }; + return (T t) -> { + accept(t); + after.accept(t); + }; } } diff --git a/src/main/java/de/mineking/javautils/function/ThrowingFunction.java b/src/main/java/de/mineking/javautils/function/ThrowingFunction.java index 313acd9..c8c59d0 100644 --- a/src/main/java/de/mineking/javautils/function/ThrowingFunction.java +++ b/src/main/java/de/mineking/javautils/function/ThrowingFunction.java @@ -19,13 +19,12 @@ public interface ThrowingFunction { * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * - * @param the type of input to the {@code before} function, and to the - * composed function + * @param the type of input to the {@code before} function, and to the + * composed function * @param before the function to apply before this function is applied * @return a composed function that first applies the {@code before} * function and then applies this function * @throws NullPointerException if before is null - * * @see #andThen(ThrowingFunction) */ default ThrowingFunction compose(ThrowingFunction before) { @@ -39,13 +38,12 @@ default ThrowingFunction compose(ThrowingFunction the type of output of the {@code after} function, and of the - * composed function + * @param the type of output of the {@code after} function, and of the + * composed function * @param after the function to apply after this function is applied * @return a composed function that first applies this function and then * applies the {@code after} function * @throws NullPointerException if after is null - * * @see #compose(ThrowingFunction) */ default ThrowingFunction andThen(ThrowingFunction after) { diff --git a/src/main/java/de/mineking/javautils/function/ThrowingRunnable.java b/src/main/java/de/mineking/javautils/function/ThrowingRunnable.java index 19a9048..9f04c04 100644 --- a/src/main/java/de/mineking/javautils/function/ThrowingRunnable.java +++ b/src/main/java/de/mineking/javautils/function/ThrowingRunnable.java @@ -12,7 +12,7 @@ public interface ThrowingRunnable { * The general contract of the method {@code run} is that it may * take any action whatsoever. * - * @see java.lang.Thread#run() + * @see java.lang.Thread#run() */ void run() throws E; diff --git a/src/main/java/de/mineking/javautils/function/TryCatch.java b/src/main/java/de/mineking/javautils/function/TryCatch.java index 5f6a133..fc86149 100644 --- a/src/main/java/de/mineking/javautils/function/TryCatch.java +++ b/src/main/java/de/mineking/javautils/function/TryCatch.java @@ -17,13 +17,13 @@ private TryCatch() { static void tryAndThrow(@NotNull ThrowingRunnable tryMethod) { tryAndHandle(tryMethod, e -> { - if (e instanceof RuntimeException re) throw re; + if(e instanceof RuntimeException re) throw re; throw new RuntimeException(e); }); } static void tryAndIgnore(@NotNull ThrowingRunnable tryMethod) { - tryAndHandle(tryMethod, e -> {}); + tryAndHandle(tryMethod, e -> { }); } static void tryAndPrint(@NotNull ThrowingRunnable tryMethod) { @@ -49,13 +49,13 @@ static void tryAndHandle(@NotNull ThrowingRunnable tryM static void tryAndThrow(@NotNull ThrowingConsumer tryMethod, T arg) { tryAndHandle(tryMethod, arg, e -> { - if (e instanceof RuntimeException re) throw re; + if(e instanceof RuntimeException re) throw re; throw new RuntimeException(e); }); } static void tryAndIgnore(@NotNull ThrowingConsumer tryMethod, T arg) { - tryAndHandle(tryMethod, arg, e -> {}); + tryAndHandle(tryMethod, arg, e -> { }); } static void tryAndPrint(@NotNull ThrowingConsumer tryMethod, T arg) { @@ -82,13 +82,13 @@ static void tryAndHandle(@NotNull ThrowingConsumer Optional tryAndThrow(@NotNull ThrowingFunction tryMethod, T arg) { return tryAndHandle(tryMethod, arg, e -> { - if (e instanceof RuntimeException re) throw re; + if(e instanceof RuntimeException re) throw re; throw new RuntimeException(e); }); } static Optional tryAndIgnore(@NotNull ThrowingFunction tryMethod, T arg) { - return tryAndHandle(tryMethod, arg, e -> {}); + return tryAndHandle(tryMethod, arg, e -> { }); } static Optional tryAndPrint(@NotNull ThrowingFunction tryMethod, T arg) { @@ -116,13 +116,13 @@ static Optional tryAndHandle(@NotNull ThrowingFun static Optional tryAndThrow(@NotNull ThrowingSupplier tryMethod) { return tryAndHandle(tryMethod, e -> { - if (e instanceof RuntimeException re) throw re; + if(e instanceof RuntimeException re) throw re; throw new RuntimeException(e); }); } static Optional tryAndIgnore(@NotNull ThrowingSupplier tryMethod) { - return tryAndHandle(tryMethod, e -> {}); + return tryAndHandle(tryMethod, e -> { }); } static Optional tryAndPrint(@NotNull ThrowingSupplier tryMethod) { diff --git a/src/main/java/de/mineking/javautils/io/BufferUtils.java b/src/main/java/de/mineking/javautils/io/BufferUtils.java index 9fa510c..aee231f 100644 --- a/src/main/java/de/mineking/javautils/io/BufferUtils.java +++ b/src/main/java/de/mineking/javautils/io/BufferUtils.java @@ -8,7 +8,7 @@ public final class BufferUtils { - private BufferUtils() {} + private BufferUtils() { } @NotNull public static ByteBuffer wrapFile(@NotNull String name) throws IOException { diff --git a/src/main/java/de/mineking/javautils/io/FileUtils.java b/src/main/java/de/mineking/javautils/io/FileUtils.java index def1348..e86f968 100644 --- a/src/main/java/de/mineking/javautils/io/FileUtils.java +++ b/src/main/java/de/mineking/javautils/io/FileUtils.java @@ -9,7 +9,7 @@ public final class FileUtils { - private FileUtils() {} + private FileUtils() { } public static byte[] readBytes(@NotNull File file) throws IOException { try(FileInputStream fis = new FileInputStream(file)) { From 64c77e1ff8b007bd50006b242af282ddad0b4c0b Mon Sep 17 00:00:00 2001 From: MineKing Date: Tue, 6 Feb 2024 14:43:21 +0100 Subject: [PATCH 3/3] Add sql logger to InsertTest --- src/test/java/database/InsertTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/database/InsertTest.java b/src/test/java/database/InsertTest.java index b442ceb..c7b6443 100644 --- a/src/test/java/database/InsertTest.java +++ b/src/test/java/database/InsertTest.java @@ -6,6 +6,8 @@ import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import lombok.ToString; +import org.jdbi.v3.core.statement.SqlLogger; +import org.jdbi.v3.core.statement.StatementContext; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; @@ -36,6 +38,14 @@ public Table getTable() { public InsertTest() { manager = new DatabaseManager("jdbc:postgresql://localhost:5433/postgres", "postgres", "postgres"); table = manager.getTable(TestClass.class, TestClass::new, "insert").createTable(); + + manager.getDriver().setSqlLogger(new SqlLogger() { + @Override + public void logBeforeExecution(StatementContext context) { + System.out.println(context.getParsedSql().getSql()); + System.out.println(context.getBinding()); + } + }); } @Test