-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/io/github/eaxdev/jsonsql4j/model/Insert.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,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(); | ||
} | ||
} | ||
|
57 changes: 57 additions & 0 deletions
57
src/main/java/io/github/eaxdev/jsonsql4j/query/insert/InsertQuery.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,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(", ")); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
src/test/java/io/github/eaxdev/jsonsql4j/query/InsertTest.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,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()); | ||
} | ||
|
||
} |
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,11 @@ | ||
{ | ||
"table": { | ||
"table": "audit", | ||
"schema": "security" | ||
}, | ||
"values": { | ||
"eventType": "MODIFY", | ||
"eventDate": "2020-01-01T23:28:56.782Z", | ||
"userId": 100 | ||
} | ||
} |