-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99a9bce
commit 85ec7f5
Showing
41 changed files
with
206 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
src/main/java/es/jaimetruman/DefaultDatabaseTypeMapper.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/es/jaimetruman/_shared/DatabaseTypeSerializerMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/es/jaimetruman/_shared/serializers/BooleanSerialzier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/es/jaimetruman/_shared/serializers/CollectionSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/es/jaimetruman/_shared/serializers/EnumSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/es/jaimetruman/_shared/serializers/LocalDateTimeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/es/jaimetruman/_shared/serializers/StringSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/es/jaimetruman/_shared/serializers/UUIDSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/test/java/es/jaimetruman/_shared/serializers/CollectionSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/es/jaimetruman/_shared/serializers/EnumSerialzierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1.79 KB
target/classes/es/jaimetruman/_shared/DatabaseTypeSerializerMapper.class
Binary file not shown.
Binary file added
BIN
+913 Bytes
target/classes/es/jaimetruman/_shared/serializers/BooleanSerialzier.class
Binary file not shown.
Binary file added
BIN
+1.33 KB
target/classes/es/jaimetruman/_shared/serializers/CollectionSerializer.class
Binary file not shown.
Binary file added
BIN
+969 Bytes
target/classes/es/jaimetruman/_shared/serializers/EnumSerializer.class
Binary file not shown.
Binary file added
BIN
+1.15 KB
target/classes/es/jaimetruman/_shared/serializers/LocalDateTimeSerializer.class
Binary file not shown.
Binary file added
BIN
+909 Bytes
target/classes/es/jaimetruman/_shared/serializers/StringSerializer.class
Binary file not shown.
Binary file added
BIN
+967 Bytes
target/classes/es/jaimetruman/_shared/serializers/UUIDSerializer.class
Binary file not shown.
Binary file modified
BIN
+6 Bytes
(100%)
target/classes/es/jaimetruman/delete/DeleteOptionsCompare.class
Binary file not shown.
Binary file modified
BIN
+20 Bytes
(100%)
target/classes/es/jaimetruman/insert/InsertOptionFinal.class
Binary file not shown.
Binary file modified
BIN
+6 Bytes
(100%)
target/classes/es/jaimetruman/select/SelectOptionCompare.class
Binary file not shown.
Binary file modified
BIN
+6 Bytes
(100%)
target/classes/es/jaimetruman/update/UpdateOptionCompare.class
Binary file not shown.
Binary file modified
BIN
+14 Bytes
(100%)
target/classes/es/jaimetruman/update/UpdateOptionFull1.class
Binary file not shown.
Binary file modified
BIN
+14 Bytes
(100%)
target/classes/es/jaimetruman/update/UpdateOptionInitial.class
Binary file not shown.
Binary file added
BIN
+852 Bytes
.../jaimetruman/_shared/serializers/CollectionSerializerTest$ColletionSerializerObject.class
Binary file not shown.
Binary file added
BIN
+1.26 KB
target/test-classes/es/jaimetruman/_shared/serializers/CollectionSerializerTest.class
Binary file not shown.
Binary file added
BIN
+1.34 KB
target/test-classes/es/jaimetruman/_shared/serializers/EnumSerialzierTest$EjemploEnum.class
Binary file not shown.
Binary file added
BIN
+1003 Bytes
target/test-classes/es/jaimetruman/_shared/serializers/EnumSerialzierTest.class
Binary file not shown.
Binary file not shown.