Skip to content

Commit

Permalink
implement insert clause
Browse files Browse the repository at this point in the history
  • Loading branch information
eaxdev committed Jul 6, 2020
1 parent a26c4f2 commit dca099d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/main/java/io/github/eaxdev/jsonsql4j/model/Insert.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> values;

public String getIntoView() {
return (Objects.isNull(table.getSchemaName()) || table.getSchemaName().isEmpty())
? table.getTableName() : table.getSchemaName() + "." + table.getTableName();
}
}

Original file line number Diff line number Diff line change
@@ -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(", "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(", "));
}
}
22 changes: 22 additions & 0 deletions src/test/java/io/github/eaxdev/jsonsql4j/query/InsertTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
11 changes: 11 additions & 0 deletions src/test/resources/insert/Insert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"table": {
"table": "audit",
"schema": "security"
},
"values": {
"eventType": "MODIFY",
"eventDate": "2020-01-01T23:28:56.782Z",
"userId": 100
}
}

0 comments on commit dca099d

Please sign in to comment.