-
Notifications
You must be signed in to change notification settings - Fork 1
/
DatabaseClient.java
111 lines (100 loc) · 4.92 KB
/
DatabaseClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.vnr;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Future;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonBuilderFactory;
import javax.json.JsonObject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class DatabaseClient {
private Client client;
private String baseURL;
private String tokenID;
public DatabaseClient(String url, String username, String password) {
this.client = ClientBuilder.newClient();
this.baseURL = url;
Form loginForm = new Form();
loginForm.param("user_name", username);
loginForm.param("password", password);
String response = this.client.target(this.baseURL + "/login/")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(loginForm, MediaType.APPLICATION_FORM_URLENCODED_TYPE), String.class);
JsonObject jsonResponse = Json.createReader(new StringReader(response)).readObject();
this.tokenID = jsonResponse.getString("token");
}
public Future<Response> insert(String tableName, List<Map<String, Object>> rows) {
JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(null);
JsonArrayBuilder jsonArrayBuilder = jsonBuilderFactory.createArrayBuilder();
for(Map<String, Object> row: rows) {
jsonArrayBuilder.add(jsonBuilderFactory.createObjectBuilder(row));
}
Form insertForm = new Form();
insertForm.param("token", this.tokenID);
insertForm.param("data", jsonArrayBuilder.build().toString());
return this.client.target(this.baseURL + "/table/insert/" + tableName)
.request(MediaType.APPLICATION_JSON)
.async().post(Entity.entity(insertForm, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
}
public Future<Response> select(List<String> tables, List<String> columns, Optional<String> whereClause, Optional<Integer> limit) {
WebTarget target = client.target(this.baseURL + "/select?")
.queryParam("token", this.tokenID)
.queryParam("table_name", String.join(",", tables))
.queryParam("columns", String.join(",", columns));
if(whereClause.isPresent()) {
target.queryParam("where", whereClause.get());
}
if(limit.isPresent())
target.queryParam("limit",limit.get());
return target.request(MediaType.APPLICATION_JSON)
.async().get();
}
public Future<Response> update(String tableName, Map<String, Object> setClause, Optional<Map<String, Object>> whereClause) {
String setClauseString = setClause.entrySet().stream()
.map(keyValuePair -> keyValuePair.getKey() + "=" + keyValuePair.getValue().toString())
.reduce((first, second) -> first + " AND " + second)
.orElse("");
Form updateForm = new Form();
updateForm.param("token", this.tokenID);
updateForm.param("set", setClauseString);
if(whereClause.isPresent()) {
String whereClauseString = whereClause.get().entrySet().stream()
.map(keyValuePair -> keyValuePair.getKey() + "=" + keyValuePair.getValue().toString())
.reduce((first, second) -> first + " AND " + second)
.orElse("");
updateForm.param("where", whereClauseString);
}
return this.client.target(this.baseURL + "/delete/" + tableName)
.request(MediaType.APPLICATION_JSON)
.async().put(Entity.entity(updateForm, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
}
public Future<Response> delete(String tableName, Optional<Map<String, Object>> whereClause) {
Form deleteForm = new Form();
deleteForm.param("token", this.tokenID);
if(whereClause.isPresent()) {
String whereClauseString = whereClause.get().entrySet().stream()
.map(keyValuePair -> keyValuePair.getKey() + "=" + keyValuePair.getValue().toString())
.reduce((first, second) -> first + " AND " + second)
.orElse("");
deleteForm.param("where", whereClauseString);
}
return this.client.target(this.baseURL + "/delete/" + tableName)
.request(MediaType.APPLICATION_JSON)
.async().method("DELETE", Entity.entity(deleteForm, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
}
public Future<Response> dropTable(String tableName) {
Form dropTableForm = new Form();
dropTableForm.param("token", this.tokenID);
return this.client.target(this.baseURL + "/drop/" + tableName)
.request(MediaType.APPLICATION_JSON)
.async().method("DELETE", Entity.entity(dropTableForm, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
}
}