Skip to content

Commit

Permalink
implement update
Browse files Browse the repository at this point in the history
  • Loading branch information
eaxdev committed Jul 7, 2020
1 parent dca099d commit 358a853
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/io/github/eaxdev/jsonsql4j/model/Update.java
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();
}

}
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 src/test/java/io/github/eaxdev/jsonsql4j/query/UpdateTest.java
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());
}

}
17 changes: 17 additions & 0 deletions src/test/resources/update/Update.json
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"
}
}
}

0 comments on commit 358a853

Please sign in to comment.