-
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
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/io/github/eaxdev/jsonsql4j/model/Update.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 io.github.eaxdev.jsonsql4j.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.github.eaxdev.jsonsql4j.model.criteria.Criteria; | ||
import lombok.Value; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author eaxdev | ||
*/ | ||
@Value | ||
public class Update { | ||
|
||
@JsonProperty(value = "table", required = true) | ||
Table table; | ||
|
||
@JsonProperty(value = "modify", required = true) | ||
Map<String, Object> modify; | ||
|
||
@JsonProperty("where") | ||
Criteria criteria; | ||
|
||
public String getTableView() { | ||
return (Objects.isNull(table.getSchemaName()) || table.getSchemaName().isEmpty()) | ||
? table.getTableName() : table.getSchemaName() + "." + table.getTableName(); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/io/github/eaxdev/jsonsql4j/query/update/UpdateQuery.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,51 @@ | ||
package io.github.eaxdev.jsonsql4j.query.update; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.github.eaxdev.jsonsql4j.exception.JsonSQL4JParseException; | ||
import io.github.eaxdev.jsonsql4j.model.Update; | ||
import io.github.eaxdev.jsonsql4j.query.ClauseBuilder; | ||
import io.github.eaxdev.jsonsql4j.query.Query; | ||
import io.github.eaxdev.jsonsql4j.query.WhereClauseBuilder; | ||
|
||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author eaxdev | ||
*/ | ||
public class UpdateQuery implements Query { | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
private final Update update; | ||
|
||
private final ClauseBuilder whereBuilder; | ||
|
||
public UpdateQuery(String jsonQuery) { | ||
//validate by schema | ||
try { | ||
this.update = MAPPER.readValue(jsonQuery, Update.class); | ||
} catch (JsonProcessingException e) { | ||
throw new JsonSQL4JParseException("Can not parse json query: [" + jsonQuery + "]", e); | ||
} | ||
whereBuilder = new WhereClauseBuilder(update.getCriteria()); | ||
} | ||
|
||
@Override | ||
public String getQuery() { | ||
return "UPDATE " + update.getTableView() + " SET " + constructUpdate() + whereBuilder.build() + ";"; | ||
} | ||
|
||
private String constructUpdate() { | ||
return update.getModify().entrySet().stream() | ||
.map(entry -> { | ||
if (entry.getValue() instanceof String) { | ||
return entry.getKey() + " = '" + entry.getValue().toString() + "'"; | ||
} else { | ||
return Objects.isNull(entry.getValue()) ? "" : entry.getKey() + " = " + entry.getValue().toString(); | ||
} | ||
}) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/io/github/eaxdev/jsonsql4j/query/UpdateTest.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,28 @@ | ||
package io.github.eaxdev.jsonsql4j.query; | ||
|
||
import io.github.eaxdev.jsonsql4j.TestUtil; | ||
import io.github.eaxdev.jsonsql4j.exception.JsonSQL4JParseException; | ||
import io.github.eaxdev.jsonsql4j.query.delete.DeleteQuery; | ||
import io.github.eaxdev.jsonsql4j.query.update.UpdateQuery; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* @author eaxdev | ||
*/ | ||
class UpdateTest { | ||
|
||
@Test | ||
@DisplayName("Should get update") | ||
void shouldGetSelect() { | ||
String json = TestUtil.readFileByPath("update/Update.json"); | ||
Query updateQuery = new UpdateQuery(json); | ||
assertEquals("UPDATE security.audit " + | ||
"SET eventType = 'MODIFY', eventDate = '2020-01-01T23:28:56.782Z', userId = 100 " + | ||
"WHERE id = 5;", updateQuery.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,17 @@ | ||
{ | ||
"table": { | ||
"table": "audit", | ||
"schema": "security" | ||
}, | ||
"modify": { | ||
"eventType": "MODIFY", | ||
"eventDate": "2020-01-01T23:28:56.782Z", | ||
"userId": 100 | ||
}, | ||
"where": { | ||
"eq": { | ||
"fieldName": "id", | ||
"value": "5" | ||
} | ||
} | ||
} |