From dca099d24ca8e835c39869dde632f164ad3b3ed7 Mon Sep 17 00:00:00 2001 From: eaxdev Date: Mon, 6 Jul 2020 22:01:26 +0300 Subject: [PATCH] implement insert clause --- .../github/eaxdev/jsonsql4j/model/Insert.java | 26 +++++++++ .../jsonsql4j/query/insert/InsertQuery.java | 57 +++++++++++++++++++ .../query/select/TargetClauseBuilder.java | 2 +- .../eaxdev/jsonsql4j/query/InsertTest.java | 22 +++++++ src/test/resources/insert/Insert.json | 11 ++++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/github/eaxdev/jsonsql4j/model/Insert.java create mode 100644 src/main/java/io/github/eaxdev/jsonsql4j/query/insert/InsertQuery.java create mode 100644 src/test/java/io/github/eaxdev/jsonsql4j/query/InsertTest.java create mode 100644 src/test/resources/insert/Insert.json diff --git a/src/main/java/io/github/eaxdev/jsonsql4j/model/Insert.java b/src/main/java/io/github/eaxdev/jsonsql4j/model/Insert.java new file mode 100644 index 0000000..b30a70d --- /dev/null +++ b/src/main/java/io/github/eaxdev/jsonsql4j/model/Insert.java @@ -0,0 +1,26 @@ +package io.github.eaxdev.jsonsql4j.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Value; + +import java.util.Map; +import java.util.Objects; + +/** + * @author eaxdev + */ +@Value +public class Insert { + + @JsonProperty(value = "table", required = true) + Table table; + + @JsonProperty(value = "values", required = true) + Map values; + + public String getIntoView() { + return (Objects.isNull(table.getSchemaName()) || table.getSchemaName().isEmpty()) + ? table.getTableName() : table.getSchemaName() + "." + table.getTableName(); + } +} + diff --git a/src/main/java/io/github/eaxdev/jsonsql4j/query/insert/InsertQuery.java b/src/main/java/io/github/eaxdev/jsonsql4j/query/insert/InsertQuery.java new file mode 100644 index 0000000..18a0fa1 --- /dev/null +++ b/src/main/java/io/github/eaxdev/jsonsql4j/query/insert/InsertQuery.java @@ -0,0 +1,57 @@ +package io.github.eaxdev.jsonsql4j.query.insert; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import io.github.eaxdev.jsonsql4j.exception.JsonSQL4JParseException; +import io.github.eaxdev.jsonsql4j.model.Insert; +import io.github.eaxdev.jsonsql4j.model.target.TargetClause; +import io.github.eaxdev.jsonsql4j.model.target.TargetClauseDeserializer; +import io.github.eaxdev.jsonsql4j.query.Query; + +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * @author eaxdev + */ +public class InsertQuery implements Query { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + static { + SimpleModule module = new SimpleModule(); + module.addDeserializer(TargetClause.class, new TargetClauseDeserializer(TargetClause.class)); + MAPPER.registerModule(module); + } + + private final Insert insert; + + public InsertQuery(String jsonQuery) { + //validate by schema + try { + this.insert = MAPPER.readValue(jsonQuery, Insert.class); + } catch (JsonProcessingException e) { + throw new JsonSQL4JParseException("Can not parse json query: [" + jsonQuery + "]", e); + } + } + + @Override + public String getQuery() { + return "INSERT INTO " + insert.getIntoView() + + " (" + String.join(", ", insert.getValues().keySet()) + ") " + + "VALUES (" + constructValues() + ");"; + } + + private String constructValues() { + return insert.getValues().values().stream() + .map(o -> { + if (o instanceof String) { + return "'" + o.toString() + "'"; + } else { + return Objects.isNull(o) ? "" : o.toString(); + } + }) + .collect(Collectors.joining(", ")); + } +} diff --git a/src/main/java/io/github/eaxdev/jsonsql4j/query/select/TargetClauseBuilder.java b/src/main/java/io/github/eaxdev/jsonsql4j/query/select/TargetClauseBuilder.java index 656e237..cedf67d 100644 --- a/src/main/java/io/github/eaxdev/jsonsql4j/query/select/TargetClauseBuilder.java +++ b/src/main/java/io/github/eaxdev/jsonsql4j/query/select/TargetClauseBuilder.java @@ -18,7 +18,7 @@ public class TargetClauseBuilder implements ClauseBuilder { @Override public String build() { return targetClauses.stream() - .map(t -> t.getQueryView()) + .map(TargetClause::getQueryView) .collect(Collectors.joining(", ")); } } diff --git a/src/test/java/io/github/eaxdev/jsonsql4j/query/InsertTest.java b/src/test/java/io/github/eaxdev/jsonsql4j/query/InsertTest.java new file mode 100644 index 0000000..b8a66f1 --- /dev/null +++ b/src/test/java/io/github/eaxdev/jsonsql4j/query/InsertTest.java @@ -0,0 +1,22 @@ +package io.github.eaxdev.jsonsql4j.query; + +import io.github.eaxdev.jsonsql4j.TestUtil; +import io.github.eaxdev.jsonsql4j.query.insert.InsertQuery; +import io.github.eaxdev.jsonsql4j.query.select.SelectQuery; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class InsertTest { + + @Test + @DisplayName("Should get insert") + void shouldGetSelect() { + String json = TestUtil.readFileByPath("insert/Insert.json"); + Query insertQuery = new InsertQuery(json); + assertEquals("INSERT INTO security.audit (eventType, eventDate, userId) " + + "VALUES ('MODIFY', '2020-01-01T23:28:56.782Z', 100);", insertQuery.getQuery()); + } + +} diff --git a/src/test/resources/insert/Insert.json b/src/test/resources/insert/Insert.json new file mode 100644 index 0000000..9be7e84 --- /dev/null +++ b/src/test/resources/insert/Insert.json @@ -0,0 +1,11 @@ +{ + "table": { + "table": "audit", + "schema": "security" + }, + "values": { + "eventType": "MODIFY", + "eventDate": "2020-01-01T23:28:56.782Z", + "userId": 100 + } +} \ No newline at end of file