Skip to content

Commit

Permalink
Added custom serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
JaimePolidura committed Jun 14, 2023
1 parent 99a9bce commit 85ec7f5
Show file tree
Hide file tree
Showing 41 changed files with 206 additions and 56 deletions.
14 changes: 14 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.github.JaimeTruman</groupId>
<artifactId>Java-DDD</artifactId>
<version>1.1.12</version>
<version>1.1.15</version>
</dependency>

<dependency>
Expand Down
29 changes: 0 additions & 29 deletions src/main/java/es/jaimetruman/DefaultDatabaseTypeMapper.java

This file was deleted.

28 changes: 22 additions & 6 deletions src/main/java/es/jaimetruman/MySQLQueryBuilder.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package es.jaimetruman;

import es.jaime.javaddd.domain.database.DatabaseTypeMapper;
import es.jaime.javaddd.domain.database.DatabaseTypeSerializer;
import es.jaimetruman._shared.DatabaseTypeSerializerMapper;
import es.jaimetruman._shared.serializers.*;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.UUID;

public final class MySQLQueryBuilder {
private static DatabaseTypeMapper databaseTypeMapper = new DefaultDatabaseTypeMapper();
private final static DatabaseTypeSerializerMapper DATABASE_TYPE_SERIALIZER_MAPPER;

static {
DATABASE_TYPE_SERIALIZER_MAPPER = new DatabaseTypeSerializerMapper();
DATABASE_TYPE_SERIALIZER_MAPPER.addSerializer(Boolean.class, new BooleanSerialzier());
DATABASE_TYPE_SERIALIZER_MAPPER.addSerializer(Collection.class, new CollectionSerializer());
DATABASE_TYPE_SERIALIZER_MAPPER.addSerializer(Enum.class, new EnumSerializer());
DATABASE_TYPE_SERIALIZER_MAPPER.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
DATABASE_TYPE_SERIALIZER_MAPPER.addSerializer(String.class, new StringSerializer());
DATABASE_TYPE_SERIALIZER_MAPPER.addSerializer(UUID.class, new UUIDSerializer());
}

public static void setDatabaseTypeMapper(final DatabaseTypeMapper newDatabaseTypeMapper) {
databaseTypeMapper = newDatabaseTypeMapper;
public static <T> void addCustomSerializer(Class<? extends T> type, DatabaseTypeSerializer<T> serializer) {
DATABASE_TYPE_SERIALIZER_MAPPER.addSerializer(type, serializer);
}

public static DatabaseTypeMapper getDatabaseTypeMapper() {
return databaseTypeMapper;
public static DatabaseTypeSerializerMapper getDatabaseTypeSerializerMapper() {
return DATABASE_TYPE_SERIALIZER_MAPPER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package es.jaimetruman._shared;

import es.jaime.javaddd.domain.database.DatabaseTypeSerializer;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class DatabaseTypeSerializerMapper {
private final Map<Class<?>, DatabaseTypeSerializer> mappings;

public DatabaseTypeSerializerMapper() {
this.mappings = new ConcurrentHashMap<>();
}

public <T> void addSerializer(Class<? extends T> type, DatabaseTypeSerializer<T> serializer) {
this.mappings.put(type, serializer);
}

public <T> String serialize(T t) {
if(t == null){
return "";
}
if(!mappings.containsKey(t.getClass())){
return t.toString();
}

return this.mappings.get(t.getClass())
.serialize(t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package es.jaimetruman._shared.serializers;

import es.jaime.javaddd.domain.database.DatabaseTypeSerializer;

public final class BooleanSerialzier implements DatabaseTypeSerializer<Boolean> {
@Override
public String serialize(Boolean bool) {
return bool == Boolean.TRUE ? "1" : "0";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package es.jaimetruman._shared.serializers;

import com.fasterxml.jackson.databind.ObjectMapper;
import es.jaime.javaddd.domain.database.DatabaseTypeSerializer;
import lombok.SneakyThrows;

import java.util.Collection;

public final class CollectionSerializer implements DatabaseTypeSerializer<Collection> {
private final ObjectMapper objectMapper = new ObjectMapper();

@Override
@SneakyThrows
public String serialize(Collection objects) {
return String.format("'%s'", objectMapper.writeValueAsString(objects));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package es.jaimetruman._shared.serializers;

import es.jaime.javaddd.domain.database.DatabaseTypeSerializer;

public final class EnumSerializer implements DatabaseTypeSerializer<Enum> {
@Override
public String serialize(Enum anEnum) {
return String.format("'%s'", anEnum.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package es.jaimetruman._shared.serializers;

import es.jaime.javaddd.domain.database.DatabaseTypeSerializer;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public final class LocalDateTimeSerializer implements DatabaseTypeSerializer<LocalDateTime> {
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

@Override
public String serialize(LocalDateTime localDateTime) {
return localDateTime.format(formatter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package es.jaimetruman._shared.serializers;

import es.jaime.javaddd.domain.database.DatabaseTypeSerializer;

public final class StringSerializer implements DatabaseTypeSerializer<String> {
@Override
public String serialize(String s) {
return String.format("'%s'", s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package es.jaimetruman._shared.serializers;

import es.jaime.javaddd.domain.database.DatabaseTypeSerializer;

import java.util.UUID;

public final class UUIDSerializer implements DatabaseTypeSerializer<UUID> {
@Override
public String serialize(UUID uuid) {
return String.format("'%s'", uuid.toString());
}
}
10 changes: 5 additions & 5 deletions src/main/java/es/jaimetruman/delete/DeleteOptionsCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ public DeleteOptionsCompare(String query, String... toAppend) {
}

public DeleteOptionsFull equal (Object value) {
return new DeleteOptionsFull(builder.toString(), "= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new DeleteOptionsFull(builder.toString(), "= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public DeleteOptionsFull bigger(Object value) {
return new DeleteOptionsFull(builder.toString(), "> ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new DeleteOptionsFull(builder.toString(), "> ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public DeleteOptionsFull smaller(Object value) {
return new DeleteOptionsFull(builder.toString(), "< ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new DeleteOptionsFull(builder.toString(), "< ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public DeleteOptionsFull smallerOrEqual (Object value) {
return new DeleteOptionsFull(builder.toString(), "<= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new DeleteOptionsFull(builder.toString(), "<= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public DeleteOptionsFull biggerOrEqual(Object value) {
return new DeleteOptionsFull(builder.toString(), ">= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new DeleteOptionsFull(builder.toString(), ">= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}
}
2 changes: 1 addition & 1 deletion src/main/java/es/jaimetruman/insert/InsertOptionFinal.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String values (Object... values) {
StringBuilder newStringBuilder = new StringBuilder(this.builder);

newStringBuilder.append(Stream.of(values)
.map(value -> MySQLQueryBuilder.getDatabaseTypeMapper().map(value))
.map(value -> MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value))
.collect(Collectors.joining(", ")))
.append(")");

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/es/jaimetruman/select/SelectOptionCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ public SelectOptionCompare(String query, String... toAppend) {
}

public SelectOptionFull equal (Object value) {
return new SelectOptionFull(builder.toString(), "= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new SelectOptionFull(builder.toString(), "= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public SelectOptionFull bigger(Object value) {
return new SelectOptionFull(builder.toString(), "> ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new SelectOptionFull(builder.toString(), "> ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public SelectOptionFull smaller(Object value) {
return new SelectOptionFull(builder.toString(), "< ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new SelectOptionFull(builder.toString(), "< ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public SelectOptionFull smallerOrEqual (Object value) {
return new SelectOptionFull(builder.toString(), "<= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new SelectOptionFull(builder.toString(), "<= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public SelectOptionFull biggerOrEqual(Object value) {
return new SelectOptionFull(builder.toString(), ">= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new SelectOptionFull(builder.toString(), ">= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}
}
10 changes: 5 additions & 5 deletions src/main/java/es/jaimetruman/update/UpdateOptionCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ public UpdateOptionCompare(String string, String... toAppend) {
}

public UpdateOptionFull2 equal (Object value) {
return new UpdateOptionFull2(builder.toString(), "= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value), " ");
return new UpdateOptionFull2(builder.toString(), "= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value), " ");
}

public UpdateOptionFull2 bigger(Object value) {
return new UpdateOptionFull2(builder.toString(), "> ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value));
return new UpdateOptionFull2(builder.toString(), "> ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value));
}

public UpdateOptionFull2 smaller(Object value) {
return new UpdateOptionFull2(builder.toString(), "< ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value));
return new UpdateOptionFull2(builder.toString(), "< ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value));
}

public UpdateOptionFull2 smallerOrEqual (Object value) {
return new UpdateOptionFull2(builder.toString(), "<= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value));
return new UpdateOptionFull2(builder.toString(), "<= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value));
}

public UpdateOptionFull2 biggerOrEqual(Object value) {
return new UpdateOptionFull2(builder.toString(), ">= ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value));
return new UpdateOptionFull2(builder.toString(), ">= ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value));
}

}
2 changes: 1 addition & 1 deletion src/main/java/es/jaimetruman/update/UpdateOptionFull1.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public UpdateOptionFull1(String string, String... toAppend) {
}

public UpdateOptionFull1 andSet (String field, Object value) {
return new UpdateOptionFull1(builder.toString(), ", ", field, " = ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value));
return new UpdateOptionFull1(builder.toString(), ", ", field, " = ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value));
}

public UpdateOptionCompare where (String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public UpdateOptionInitial (String table) {
}

public UpdateOptionFull1 set (String field, Object value) {
return new UpdateOptionFull1(builder.toString(), "SET ", field, " = ", MySQLQueryBuilder.getDatabaseTypeMapper().map(value));
return new UpdateOptionFull1(builder.toString(), "SET ", field, " = ", MySQLQueryBuilder.getDatabaseTypeSerializerMapper().serialize(value));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package es.jaimetruman._shared.serializers;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public final class CollectionSerializerTest {
@Test
public void collectionSerializer() {
CollectionSerializer collectionSerializer = new CollectionSerializer();

String serialized = collectionSerializer.serialize(Arrays.asList(
new ColletionSerializerObject("jaime", 3),
new ColletionSerializerObject("pedro", 12)
));

Assert.assertEquals("'[{\"nombre\":\"jaime\",\"puntos\":3.0},{\"nombre\":\"pedro\",\"puntos\":12.0}]'", serialized);
}

@AllArgsConstructor
public static class ColletionSerializerObject {
@Getter private String nombre;
@Getter private double puntos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package es.jaimetruman._shared.serializers;

import org.junit.Assert;
import org.junit.Test;

public final class EnumSerialzierTest {
@Test
public void enumSerializer() {
EnumSerializer enumSerializer = new EnumSerializer();

Assert.assertEquals("'VALOR1'", enumSerializer.serialize(EjemploEnum.VALOR1));;
}

public enum EjemploEnum {
VALOR1, VALOR2
}
}
2 changes: 1 addition & 1 deletion src/test/java/es/jaimetruman/delete/DeleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public void deleteTest() {
.where("nombre").equal("jaimetruman")
.build();

Assert.assertEquals(query, "DELETE FROM jugadores WHERE nombre = 'jaimetruman' ");
Assert.assertEquals("DELETE FROM jugadores WHERE nombre = 'jaimetruman' ", query);
}
}
1 change: 0 additions & 1 deletion src/test/java/es/jaimetruman/insert/InsertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ public void testInsert () {

Assert.assertEquals(query, "INSERT INTO jugadores (nombre, dinero) VALUES ('JaimeTruman', 10)");
}

}
Binary file not shown.
Binary file modified target/classes/es/jaimetruman/MySQLQueryBuilder.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified target/classes/es/jaimetruman/delete/DeleteOptionsCompare.class
Binary file not shown.
Binary file modified target/classes/es/jaimetruman/insert/InsertOptionFinal.class
Binary file not shown.
Binary file modified target/classes/es/jaimetruman/select/SelectOptionCompare.class
Binary file not shown.
Binary file modified target/classes/es/jaimetruman/update/UpdateOptionCompare.class
Binary file not shown.
Binary file modified target/classes/es/jaimetruman/update/UpdateOptionFull1.class
Binary file not shown.
Binary file modified target/classes/es/jaimetruman/update/UpdateOptionInitial.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified target/test-classes/es/jaimetruman/delete/DeleteTest.class
Binary file not shown.

0 comments on commit 85ec7f5

Please sign in to comment.