diff --git a/build.gradle b/build.gradle index 9e85357..f8e40b5 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'io.github.dynomake' -version '1.0.5' +version '1.0.6' tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } @@ -21,7 +21,7 @@ dependencies { annotationProcessor 'org.projectlombok:lombok:1.18.24' testCompileOnly 'org.projectlombok:lombok:1.18.24' testAnnotationProcessor 'org.projectlombok:lombok:1.18.24' - testImplementation group: 'mysql', name: 'mysql-connector-java', version: '5.1.47' + testImplementation 'mysql:mysql-connector-java:8.0.31' } diff --git a/src/main/java/net/wonsi/api/Wonsi.java b/src/main/java/net/wonsi/api/Wonsi.java index 2b465ad..b5513ad 100644 --- a/src/main/java/net/wonsi/api/Wonsi.java +++ b/src/main/java/net/wonsi/api/Wonsi.java @@ -19,4 +19,15 @@ public interface Wonsi { * @return - wonsi table handle */ WonsiTable getTable(@NonNull Class tClass, @NonNull Function deserializer); + + + /** + * Get or create (if not exits) table by class + * if you are using this way with auto-serialisation & de, you can + * use only long number types. DO NOT USE Long, int, short, Integer. + * + * @param tClass - class + * @return - wonsi table handle + */ + WonsiTable getTable(@NonNull Class tClass); } diff --git a/src/main/java/net/wonsi/api/repository/BaseRepository.java b/src/main/java/net/wonsi/api/repository/BaseRepository.java index 6ab37cc..1ab4537 100644 --- a/src/main/java/net/wonsi/api/repository/BaseRepository.java +++ b/src/main/java/net/wonsi/api/repository/BaseRepository.java @@ -13,8 +13,8 @@ public interface BaseRepository { Collection findAll(); Optional findByIdentifier(@NonNull Identifier identifier); Collection findByCondition(@NonNull Condition condition); - CompletableFuture deleteByIdentifier(@NonNull Identifier identifier); - CompletableFuture save(@NonNull MappingType mappingType); + void deleteByIdentifier(@NonNull Identifier identifier); + void save(@NonNull MappingType mappingType); WonsiTable getWrappedTable(); } diff --git a/src/main/java/net/wonsi/column/ColumnUtil.java b/src/main/java/net/wonsi/column/ColumnUtil.java index 10037bf..0d0d84b 100644 --- a/src/main/java/net/wonsi/column/ColumnUtil.java +++ b/src/main/java/net/wonsi/column/ColumnUtil.java @@ -6,6 +6,7 @@ import java.math.BigDecimal; import java.sql.Date; +import java.sql.ResultSet; import java.sql.Timestamp; import java.util.Collection; import java.util.HashMap; diff --git a/src/main/java/net/wonsi/column/type/BooleanType.java b/src/main/java/net/wonsi/column/type/BooleanType.java index 41e9ae5..d5b8436 100644 --- a/src/main/java/net/wonsi/column/type/BooleanType.java +++ b/src/main/java/net/wonsi/column/type/BooleanType.java @@ -1,9 +1,20 @@ package net.wonsi.column.type; -public class BooleanType implements ColumnType { +import lombok.NonNull; +import lombok.SneakyThrows; + +import java.sql.ResultSet; + +public class BooleanType implements ColumnType { @Override public String convertToString(int length) { return "boolean"; } + + @Override + @SneakyThrows + public Boolean get(@NonNull String name, @NonNull ResultSet resultSet) { + return resultSet.getBoolean(name); + } } diff --git a/src/main/java/net/wonsi/column/type/ColumnType.java b/src/main/java/net/wonsi/column/type/ColumnType.java index 8c5fb0d..38e5fab 100644 --- a/src/main/java/net/wonsi/column/type/ColumnType.java +++ b/src/main/java/net/wonsi/column/type/ColumnType.java @@ -1,5 +1,10 @@ package net.wonsi.column.type; -public interface ColumnType { +import lombok.NonNull; + +import java.sql.ResultSet; + +public interface ColumnType { String convertToString(int length); + T get(@NonNull String name, @NonNull ResultSet resultSet); } diff --git a/src/main/java/net/wonsi/column/type/DateType.java b/src/main/java/net/wonsi/column/type/DateType.java index 436584d..0a463ee 100644 --- a/src/main/java/net/wonsi/column/type/DateType.java +++ b/src/main/java/net/wonsi/column/type/DateType.java @@ -1,9 +1,21 @@ package net.wonsi.column.type; -public class DateType implements ColumnType { +import lombok.NonNull; +import lombok.SneakyThrows; + +import java.sql.Date; +import java.sql.ResultSet; + +public class DateType implements ColumnType { @Override public String convertToString(int length) { return "date"; } + + @Override + @SneakyThrows + public Date get(@NonNull String name, @NonNull ResultSet resultSet) { + return resultSet.getDate(name); + } } diff --git a/src/main/java/net/wonsi/column/type/DatetimeType.java b/src/main/java/net/wonsi/column/type/DatetimeType.java index 78e3272..9975a75 100644 --- a/src/main/java/net/wonsi/column/type/DatetimeType.java +++ b/src/main/java/net/wonsi/column/type/DatetimeType.java @@ -1,9 +1,21 @@ package net.wonsi.column.type; -public class DatetimeType implements ColumnType { +import lombok.NonNull; +import lombok.SneakyThrows; + +import java.sql.ResultSet; +import java.sql.Timestamp; + +public class DatetimeType implements ColumnType { @Override public String convertToString(int length) { return "datetime"; } + + @Override + @SneakyThrows + public Timestamp get(@NonNull String name, @NonNull ResultSet resultSet) { + return resultSet.getTimestamp(name); + } } diff --git a/src/main/java/net/wonsi/column/type/FloatType.java b/src/main/java/net/wonsi/column/type/FloatType.java index 0113c0e..77c4684 100644 --- a/src/main/java/net/wonsi/column/type/FloatType.java +++ b/src/main/java/net/wonsi/column/type/FloatType.java @@ -1,9 +1,20 @@ package net.wonsi.column.type; -public class FloatType implements ColumnType { +import lombok.NonNull; +import lombok.SneakyThrows; + +import java.sql.ResultSet; + +public class FloatType implements ColumnType { @Override public String convertToString(int length) { return "float"; } + + @Override + @SneakyThrows + public Float get(@NonNull String name, @NonNull ResultSet resultSet) { + return resultSet.getFloat(name); + } } diff --git a/src/main/java/net/wonsi/column/type/IntType.java b/src/main/java/net/wonsi/column/type/IntType.java index 48acaa7..76af86e 100644 --- a/src/main/java/net/wonsi/column/type/IntType.java +++ b/src/main/java/net/wonsi/column/type/IntType.java @@ -1,6 +1,11 @@ package net.wonsi.column.type; -public class IntType implements ColumnType { +import lombok.NonNull; +import lombok.SneakyThrows; + +import java.sql.ResultSet; + +public class IntType implements ColumnType { @Override public String convertToString(int length) { @@ -9,4 +14,10 @@ public String convertToString(int length) { if (length < 10) return "int"; else return "bigint"; } + + @Override + @SneakyThrows + public Long get(@NonNull String name, @NonNull ResultSet resultSet) { + return resultSet.getLong(name); + } } diff --git a/src/main/java/net/wonsi/column/type/JsonType.java b/src/main/java/net/wonsi/column/type/JsonType.java index 73eab6d..832cec3 100644 --- a/src/main/java/net/wonsi/column/type/JsonType.java +++ b/src/main/java/net/wonsi/column/type/JsonType.java @@ -1,9 +1,20 @@ package net.wonsi.column.type; -public class JsonType implements ColumnType { +import lombok.NonNull; +import lombok.SneakyThrows; + +import java.sql.ResultSet; + +public class JsonType implements ColumnType { @Override public String convertToString(int length) { return "json"; } + + @Override + @SneakyThrows + public String get(@NonNull String name, @NonNull ResultSet resultSet) { + return resultSet.getString(name); + } } \ No newline at end of file diff --git a/src/main/java/net/wonsi/column/type/VarcharType.java b/src/main/java/net/wonsi/column/type/VarcharType.java index f80d61b..7c209bb 100644 --- a/src/main/java/net/wonsi/column/type/VarcharType.java +++ b/src/main/java/net/wonsi/column/type/VarcharType.java @@ -1,11 +1,21 @@ package net.wonsi.column.type; -import java.util.Map; -public class VarcharType implements ColumnType { +import lombok.NonNull; +import lombok.SneakyThrows; + +import java.sql.ResultSet; + +public class VarcharType implements ColumnType { @Override public String convertToString(int length) { return "varchar(" + length + ')'; } + + @Override + @SneakyThrows + public String get(@NonNull String name, @NonNull ResultSet resultSet) { + return resultSet.getString(name); + } } diff --git a/src/main/java/net/wonsi/proxy/RealWonsi.java b/src/main/java/net/wonsi/proxy/RealWonsi.java index dd92e6d..699c5e2 100644 --- a/src/main/java/net/wonsi/proxy/RealWonsi.java +++ b/src/main/java/net/wonsi/proxy/RealWonsi.java @@ -2,10 +2,15 @@ import lombok.AllArgsConstructor; import lombok.NonNull; +import lombok.val; import net.wonsi.api.Wonsi; import net.wonsi.api.mapping.Table; +import net.wonsi.api.mapping.WonsiColumn; import net.wonsi.api.table.WonsiTable; +import net.wonsi.column.ColumnUtil; import net.wonsi.proxy.table.RealWonsiTable; + +import java.lang.reflect.Field; import java.sql.Connection; import java.sql.ResultSet; import java.util.function.Function; @@ -28,4 +33,29 @@ public WonsiTable getTable(@NonNull Class tClass, @NonNull Function WonsiTable getTable(@NonNull Class tClass) { + return getTable(tClass, (resultSet) -> { + try { + T object = tClass.newInstance(); + + for (Field field : tClass.getDeclaredFields()) { + WonsiColumn column = field.getAnnotation(WonsiColumn.class); + val name = column.name().equals("null_wonsidv") ? field.getName() : column.name(); + field.trySetAccessible(); + val value = ColumnUtil.get(field.getType()).get(name, resultSet); +// field.getType().cast( + field.set(object, value.getClass().isAssignableFrom(field.getType()) ? value : + value.getClass().equals(Long.class) ? (((Long) value).longValue()) : + field.getType().cast(value)); + } + + return object; + } catch (Exception exception) { + exception.printStackTrace(); + return null; + } + }); + } + } diff --git a/src/main/java/net/wonsi/proxy/repository/SimpleRepository.java b/src/main/java/net/wonsi/proxy/repository/SimpleRepository.java index 5ee2de8..189a3da 100644 --- a/src/main/java/net/wonsi/proxy/repository/SimpleRepository.java +++ b/src/main/java/net/wonsi/proxy/repository/SimpleRepository.java @@ -21,6 +21,7 @@ public class SimpleRepository implements BaseRepository { + private final WonsiTable wrappedTable; private final String idColumnName; @@ -40,17 +41,17 @@ public Collection findByCondition(@NonNull Condition condition) { } @Override - public CompletableFuture deleteByIdentifier(@NonNull Identifier identifier) { - return wrappedTable.delete() + public void deleteByIdentifier(@NonNull Identifier identifier) { + wrappedTable.delete() .where(Condition.is(idColumnName, identifier)) .limit(1) - .async(); + .sync(); } @Override - public CompletableFuture save(@NonNull MappingType mappingType) { - return wrappedTable.insert() + public void save(@NonNull MappingType mappingType) { + wrappedTable.insert() .updateOnDuplicate() .data(map -> { for (Field field : mappingType.getClass().getDeclaredFields()) { @@ -64,6 +65,6 @@ public CompletableFuture save(@NonNull MappingType mappingType) { } } }) - .async(); + .sync(); } } diff --git a/src/main/java/net/wonsi/proxy/request/RealDeleteRequest.java b/src/main/java/net/wonsi/proxy/request/RealDeleteRequest.java index c78dbd5..428c60b 100644 --- a/src/main/java/net/wonsi/proxy/request/RealDeleteRequest.java +++ b/src/main/java/net/wonsi/proxy/request/RealDeleteRequest.java @@ -42,12 +42,7 @@ public CompletableFuture async() { @Override public void sync() { try { - PreparedStatement statement = connection - .prepareStatement("DELETE FROM " + tableName + " WHERE " + condition + " LIMIT ?"); - - statement.setInt(1, limit); - - ExecutorUtil.execute(statement, connection); + ExecutorUtil.execute("DELETE FROM " + tableName + " WHERE " + condition + " LIMIT " + limit, connection); } catch (Exception exception) { exception.printStackTrace(); } diff --git a/src/main/java/net/wonsi/proxy/request/RealSelectRequest.java b/src/main/java/net/wonsi/proxy/request/RealSelectRequest.java index 7513233..e81990c 100644 --- a/src/main/java/net/wonsi/proxy/request/RealSelectRequest.java +++ b/src/main/java/net/wonsi/proxy/request/RealSelectRequest.java @@ -37,9 +37,7 @@ public CompletableFuture> async() { public ExecutedReturningAction sync() { try { PreparedStatement statement = connection - .prepareStatement("SELECT * FROM " + tableName + " WHERE " + condition + (limit > 0 ? " LIMIT ?" : "")); - - statement.setInt(1, limit); + .prepareStatement("SELECT * FROM " + tableName + " WHERE " + condition + (limit > 0 ? " LIMIT " + limit : "")); return new RealExecutedReturningAction(ExecutorUtil.getResult(statement, connection), deserializer); } catch (Exception exception) { diff --git a/src/test/java/net/wonsi/test/TemplateRepoBootstrap.java b/src/test/java/net/wonsi/test/TemplateRepoBootstrap.java new file mode 100644 index 0000000..ac2b015 --- /dev/null +++ b/src/test/java/net/wonsi/test/TemplateRepoBootstrap.java @@ -0,0 +1,39 @@ +package net.wonsi.test; + +import lombok.SneakyThrows; +import lombok.val; +import net.wonsi.WonsiFactory; +import net.wonsi.api.Wonsi; +import net.wonsi.api.table.WonsiTable; +import net.wonsi.test.model.Car; +import net.wonsi.test.model.User; +import net.wonsi.test.mysql.MySqlUtil; +import net.wonsi.test.repo.UserRepo; + +public class TemplateRepoBootstrap { + + @SneakyThrows + public static void main(String[] args) { + Wonsi wonsi = WonsiFactory.createInstance(MySqlUtil.create()); + WonsiTable table = wonsi.getTable(Car.class); + + val repository = table.createRepository(long.class); + +// repository.save(new Car(1, "BMW", "x3")); +// repository.save(new Car(2, "BMW", "x5")); +// repository.save(new Car(3, "BMW", "x7")); +// repository.save(new Car(6, "BMW", "cla")); +// +// repository.save(new Car(9, "Porche", "911")); +// repository.save(new Car(11, "Mersedes-Benz", "GLA")); + + System.out.println("[Test-Select] Model from row where id = 2: " + repository.findByIdentifier(2L).get().getModel()); + + System.out.println("[Find-All-Test] Items:"); + for (val car: repository.findAll()) + System.out.println(String.format(" #%s %s %s", car.getId(), car.getConcern(), car.getModel())); + + System.out.println("[Delete-Test] Trying to delete item with id=6"); + repository.deleteByIdentifier(6L); + } +} diff --git a/src/test/java/net/wonsi/test/TestBootstrap.java b/src/test/java/net/wonsi/test/TestBootstrap.java index 0d15e55..924420b 100644 --- a/src/test/java/net/wonsi/test/TestBootstrap.java +++ b/src/test/java/net/wonsi/test/TestBootstrap.java @@ -1,5 +1,6 @@ package net.wonsi.test; +import lombok.SneakyThrows; import net.wonsi.WonsiFactory; import net.wonsi.api.Wonsi; import net.wonsi.api.table.WonsiTable; @@ -7,8 +8,11 @@ import net.wonsi.test.mysql.MySqlUtil; import net.wonsi.test.repo.UserRepo; +import java.sql.DriverManager; + public class TestBootstrap { + @SneakyThrows public static void main(String[] args) { Wonsi wonsi = WonsiFactory.createInstance(MySqlUtil.create()); WonsiTable table = wonsi.getTable(User.class, User::deserialize); diff --git a/src/test/java/net/wonsi/test/model/Car.java b/src/test/java/net/wonsi/test/model/Car.java new file mode 100644 index 0000000..f829b3f --- /dev/null +++ b/src/test/java/net/wonsi/test/model/Car.java @@ -0,0 +1,37 @@ +package net.wonsi.test.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import net.wonsi.api.mapping.Table; +import net.wonsi.api.mapping.WonsiColumn; +import net.wonsi.api.mapping.WonsiPrimary; + +import java.sql.ResultSet; + +@Setter +@Getter +@AllArgsConstructor +@NoArgsConstructor +@Table("cars") +public class Car { + + @WonsiPrimary + @WonsiColumn + private long id; + + @WonsiColumn + private String concern; + + @WonsiColumn + private String model; + + public static Car deserialize(ResultSet data) { + try { + return new Car(data.getInt("id"), data.getString("concern"), data.getString("model")); + } catch (Exception exception) { + throw new RuntimeException(exception); + } + } +} diff --git a/src/test/java/net/wonsi/test/mysql/MySqlUtil.java b/src/test/java/net/wonsi/test/mysql/MySqlUtil.java index ae36542..2506ba2 100644 --- a/src/test/java/net/wonsi/test/mysql/MySqlUtil.java +++ b/src/test/java/net/wonsi/test/mysql/MySqlUtil.java @@ -1,6 +1,5 @@ package net.wonsi.test.mysql; -import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import lombok.SneakyThrows; import lombok.experimental.UtilityClass; @@ -13,7 +12,9 @@ public class MySqlUtil { @SneakyThrows public Connection create() { - var url = "jdbc:h2:mem:"; - return DriverManager.getConnection(url); + String url = "jdbc:mysql://sql11.freemysqlhosting.net:3306/sql111?useSSL=true"; + Class.forName("com.mysql.cj.jdbc.Driver"); + + return DriverManager.getConnection(url, "sql111", "i_will_never_show_it"); } }