Skip to content

Commit 358a853

Browse files
committed
implement update
1 parent dca099d commit 358a853

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.eaxdev.jsonsql4j.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.github.eaxdev.jsonsql4j.model.criteria.Criteria;
5+
import lombok.Value;
6+
7+
import java.util.Map;
8+
import java.util.Objects;
9+
10+
/**
11+
* @author eaxdev
12+
*/
13+
@Value
14+
public class Update {
15+
16+
@JsonProperty(value = "table", required = true)
17+
Table table;
18+
19+
@JsonProperty(value = "modify", required = true)
20+
Map<String, Object> modify;
21+
22+
@JsonProperty("where")
23+
Criteria criteria;
24+
25+
public String getTableView() {
26+
return (Objects.isNull(table.getSchemaName()) || table.getSchemaName().isEmpty())
27+
? table.getTableName() : table.getSchemaName() + "." + table.getTableName();
28+
}
29+
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.github.eaxdev.jsonsql4j.query.update;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import io.github.eaxdev.jsonsql4j.exception.JsonSQL4JParseException;
6+
import io.github.eaxdev.jsonsql4j.model.Update;
7+
import io.github.eaxdev.jsonsql4j.query.ClauseBuilder;
8+
import io.github.eaxdev.jsonsql4j.query.Query;
9+
import io.github.eaxdev.jsonsql4j.query.WhereClauseBuilder;
10+
11+
import java.util.Objects;
12+
import java.util.stream.Collectors;
13+
14+
/**
15+
* @author eaxdev
16+
*/
17+
public class UpdateQuery implements Query {
18+
19+
private static final ObjectMapper MAPPER = new ObjectMapper();
20+
21+
private final Update update;
22+
23+
private final ClauseBuilder whereBuilder;
24+
25+
public UpdateQuery(String jsonQuery) {
26+
//validate by schema
27+
try {
28+
this.update = MAPPER.readValue(jsonQuery, Update.class);
29+
} catch (JsonProcessingException e) {
30+
throw new JsonSQL4JParseException("Can not parse json query: [" + jsonQuery + "]", e);
31+
}
32+
whereBuilder = new WhereClauseBuilder(update.getCriteria());
33+
}
34+
35+
@Override
36+
public String getQuery() {
37+
return "UPDATE " + update.getTableView() + " SET " + constructUpdate() + whereBuilder.build() + ";";
38+
}
39+
40+
private String constructUpdate() {
41+
return update.getModify().entrySet().stream()
42+
.map(entry -> {
43+
if (entry.getValue() instanceof String) {
44+
return entry.getKey() + " = '" + entry.getValue().toString() + "'";
45+
} else {
46+
return Objects.isNull(entry.getValue()) ? "" : entry.getKey() + " = " + entry.getValue().toString();
47+
}
48+
})
49+
.collect(Collectors.joining(", "));
50+
}
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.eaxdev.jsonsql4j.query;
2+
3+
import io.github.eaxdev.jsonsql4j.TestUtil;
4+
import io.github.eaxdev.jsonsql4j.exception.JsonSQL4JParseException;
5+
import io.github.eaxdev.jsonsql4j.query.delete.DeleteQuery;
6+
import io.github.eaxdev.jsonsql4j.query.update.UpdateQuery;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
/**
14+
* @author eaxdev
15+
*/
16+
class UpdateTest {
17+
18+
@Test
19+
@DisplayName("Should get update")
20+
void shouldGetSelect() {
21+
String json = TestUtil.readFileByPath("update/Update.json");
22+
Query updateQuery = new UpdateQuery(json);
23+
assertEquals("UPDATE security.audit " +
24+
"SET eventType = 'MODIFY', eventDate = '2020-01-01T23:28:56.782Z', userId = 100 " +
25+
"WHERE id = 5;", updateQuery.getQuery());
26+
}
27+
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"table": {
3+
"table": "audit",
4+
"schema": "security"
5+
},
6+
"modify": {
7+
"eventType": "MODIFY",
8+
"eventDate": "2020-01-01T23:28:56.782Z",
9+
"userId": 100
10+
},
11+
"where": {
12+
"eq": {
13+
"fieldName": "id",
14+
"value": "5"
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)