Skip to content

Commit

Permalink
feat: auto deserialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
dynomake committed Feb 21, 2024
1 parent e4e21af commit e2f0e03
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 31 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'io.github.dynomake'
version '1.0.5'
version '1.0.6'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Expand All @@ -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'
}


Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/wonsi/api/Wonsi.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ public interface Wonsi {
* @return - wonsi table handle
*/
<T> WonsiTable<T> getTable(@NonNull Class<T> tClass, @NonNull Function<ResultSet, T> 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
*/
<T> WonsiTable<T> getTable(@NonNull Class<T> tClass);
}
4 changes: 2 additions & 2 deletions src/main/java/net/wonsi/api/repository/BaseRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public interface BaseRepository<MappingType, Identifier> {
Collection<MappingType> findAll();
Optional<MappingType> findByIdentifier(@NonNull Identifier identifier);
Collection<MappingType> findByCondition(@NonNull Condition condition);
CompletableFuture<Void> deleteByIdentifier(@NonNull Identifier identifier);
CompletableFuture<Void> save(@NonNull MappingType mappingType);
void deleteByIdentifier(@NonNull Identifier identifier);
void save(@NonNull MappingType mappingType);
WonsiTable<MappingType> getWrappedTable();

}
1 change: 1 addition & 0 deletions src/main/java/net/wonsi/column/ColumnUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/net/wonsi/column/type/BooleanType.java
Original file line number Diff line number Diff line change
@@ -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<Boolean> {

@Override
public String convertToString(int length) {
return "boolean";
}

@Override
@SneakyThrows
public Boolean get(@NonNull String name, @NonNull ResultSet resultSet) {
return resultSet.getBoolean(name);
}
}
7 changes: 6 additions & 1 deletion src/main/java/net/wonsi/column/type/ColumnType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package net.wonsi.column.type;

public interface ColumnType {
import lombok.NonNull;

import java.sql.ResultSet;

public interface ColumnType<T> {
String convertToString(int length);
T get(@NonNull String name, @NonNull ResultSet resultSet);
}
14 changes: 13 additions & 1 deletion src/main/java/net/wonsi/column/type/DateType.java
Original file line number Diff line number Diff line change
@@ -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<Date> {

@Override
public String convertToString(int length) {
return "date";
}

@Override
@SneakyThrows
public Date get(@NonNull String name, @NonNull ResultSet resultSet) {
return resultSet.getDate(name);
}
}
14 changes: 13 additions & 1 deletion src/main/java/net/wonsi/column/type/DatetimeType.java
Original file line number Diff line number Diff line change
@@ -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<Timestamp> {

@Override
public String convertToString(int length) {
return "datetime";
}

@Override
@SneakyThrows
public Timestamp get(@NonNull String name, @NonNull ResultSet resultSet) {
return resultSet.getTimestamp(name);
}
}
13 changes: 12 additions & 1 deletion src/main/java/net/wonsi/column/type/FloatType.java
Original file line number Diff line number Diff line change
@@ -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<Float> {

@Override
public String convertToString(int length) {
return "float";
}

@Override
@SneakyThrows
public Float get(@NonNull String name, @NonNull ResultSet resultSet) {
return resultSet.getFloat(name);
}
}
13 changes: 12 additions & 1 deletion src/main/java/net/wonsi/column/type/IntType.java
Original file line number Diff line number Diff line change
@@ -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<Long> {

@Override
public String convertToString(int length) {
Expand All @@ -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);
}
}
13 changes: 12 additions & 1 deletion src/main/java/net/wonsi/column/type/JsonType.java
Original file line number Diff line number Diff line change
@@ -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<String> {

@Override
public String convertToString(int length) {
return "json";
}

@Override
@SneakyThrows
public String get(@NonNull String name, @NonNull ResultSet resultSet) {
return resultSet.getString(name);
}
}
14 changes: 12 additions & 2 deletions src/main/java/net/wonsi/column/type/VarcharType.java
Original file line number Diff line number Diff line change
@@ -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<String> {

@Override
public String convertToString(int length) {
return "varchar(" + length + ')';
}

@Override
@SneakyThrows
public String get(@NonNull String name, @NonNull ResultSet resultSet) {
return resultSet.getString(name);
}
}
30 changes: 30 additions & 0 deletions src/main/java/net/wonsi/proxy/RealWonsi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,4 +33,29 @@ public <T> WonsiTable<T> getTable(@NonNull Class<T> tClass, @NonNull Function<Re
return table;
}

@Override
public <T> WonsiTable<T> getTable(@NonNull Class<T> 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;
}
});
}

}
13 changes: 7 additions & 6 deletions src/main/java/net/wonsi/proxy/repository/SimpleRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class SimpleRepository<MappingType, Identifier>
implements BaseRepository<MappingType, Identifier> {


private final WonsiTable<MappingType> wrappedTable;
private final String idColumnName;

Expand All @@ -40,17 +41,17 @@ public Collection<MappingType> findByCondition(@NonNull Condition condition) {
}

@Override
public CompletableFuture<Void> 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<Void> save(@NonNull MappingType mappingType) {
return wrappedTable.insert()
public void save(@NonNull MappingType mappingType) {
wrappedTable.insert()
.updateOnDuplicate()
.data(map -> {
for (Field field : mappingType.getClass().getDeclaredFields()) {
Expand All @@ -64,6 +65,6 @@ public CompletableFuture<Void> save(@NonNull MappingType mappingType) {
}
}
})
.async();
.sync();
}
}
7 changes: 1 addition & 6 deletions src/main/java/net/wonsi/proxy/request/RealDeleteRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public CompletableFuture<Void> 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();
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/wonsi/proxy/request/RealSelectRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public CompletableFuture<ExecutedReturningAction<T>> async() {
public ExecutedReturningAction<T> 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<T>(ExecutorUtil.getResult(statement, connection), deserializer);
} catch (Exception exception) {
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/net/wonsi/test/TemplateRepoBootstrap.java
Original file line number Diff line number Diff line change
@@ -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<Car> 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);
}
}
4 changes: 4 additions & 0 deletions src/test/java/net/wonsi/test/TestBootstrap.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package net.wonsi.test;

import lombok.SneakyThrows;
import net.wonsi.WonsiFactory;
import net.wonsi.api.Wonsi;
import net.wonsi.api.table.WonsiTable;
import net.wonsi.test.model.User;
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<User> table = wonsi.getTable(User.class, User::deserialize);
Expand Down
Loading

0 comments on commit e2f0e03

Please sign in to comment.