Skip to content

Commit

Permalink
Merge pull request #6 from pranavrd/postgres-example
Browse files Browse the repository at this point in the history
Postgres example
  • Loading branch information
kailash authored Mar 14, 2022
2 parents 6849b31 + 9283295 commit a19583d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ public class Constants {

// json
public static final String ID = "id";
public static final String SEARCH_TYPE = "searchType";
public static final String TIME_REL = "timerel";
public static final String TIME = "time";
public static final String END_TIME = "endTime";
public static final String ATTRS = "attrs";
public static final String BEFORE = "before";
public static final String AFTER = "after";
public static final String ATTR_QUERY = "attr_query";
public static final String ATTRIBUTE = "attribute";
public static final String OPERATOR = "operator";
public static final String VALUE = "value";

//regex
public static final String GEOSEARCH_REGEX = "(.*)geoSearch(.*)";
public static final String ATTRIBUTE_SEARCH_REGEX = "(.*)attributeSearch(.*)";
public static final String TEMPORAL_SEARCH_REGEX = "(.*)temporalSearch(.*)";

// SQL
public static String PSQL_TABLE_EXISTS_QUERY =
"SELECT EXISTS ( SELECT FROM pg_tables WHERE schemaname='public' AND tablename='$1');";
public static String PSQL_SELECT_QUERY = "SELECT $1 FROM $$ WHERE time BETWEEN '$2' and '$3'";
public static String PSQL_SELECT_QUERY = "SELECT $1 FROM $$";
public static String PSQL_TEMPORAL_CONDITION = " WHERE time BETWEEN '$2' and '$3'";
public static String PSQL_ATTR_CONDITION = "$4 $op $5";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@
import static iudx.rs.proxy.database.example.postgres.Constants.POOL_SIZE;
import static iudx.rs.proxy.database.example.postgres.Constants.PSQL_SELECT_QUERY;
import static iudx.rs.proxy.database.example.postgres.Constants.PSQL_TABLE_EXISTS_QUERY;
import static iudx.rs.proxy.database.example.postgres.Constants.PSQL_TEMPORAL_CONDITION;
import static iudx.rs.proxy.database.example.postgres.Constants.PSQL_ATTR_CONDITION;
import static iudx.rs.proxy.database.example.postgres.Constants.TIME;
import static iudx.rs.proxy.database.example.postgres.Constants.TIME_REL;
import static iudx.rs.proxy.database.example.postgres.Constants.SEARCH_TYPE;
import static iudx.rs.proxy.database.example.postgres.Constants.TEMPORAL_SEARCH_REGEX;
import static iudx.rs.proxy.database.example.postgres.Constants.GEOSEARCH_REGEX;
import static iudx.rs.proxy.database.example.postgres.Constants.ATTRIBUTE_SEARCH_REGEX;
import static iudx.rs.proxy.database.example.postgres.Constants.ATTR_QUERY;
import static iudx.rs.proxy.database.example.postgres.Constants.ATTRIBUTE;
import static iudx.rs.proxy.database.example.postgres.Constants.OPERATOR;
import static iudx.rs.proxy.database.example.postgres.Constants.VALUE;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collector;
Expand Down Expand Up @@ -74,7 +84,14 @@ public PostgresServiceImpl(Vertx vertx, JsonObject config) {
@Override
public DatabaseService searchQuery(JsonObject request, Handler<AsyncResult<JsonObject>> handler)
throws ServiceException {
String tableID = request.getString(ID);

if (!request.containsKey(ID)
|| request.getJsonArray(ID).isEmpty()
|| !request.containsKey(SEARCH_TYPE)) {
throw new ServiceException(HttpStatus.SC_BAD_REQUEST, "message for failure");
}

String tableID = request.getJsonArray(ID).getString(0);

if (!tableExists(tableID)) {
ServiceExceptionMessage detailedMsg =
Expand Down Expand Up @@ -117,7 +134,14 @@ public DatabaseService searchQuery(JsonObject request, Handler<AsyncResult<JsonO
@Override
public DatabaseService countQuery(JsonObject request, Handler<AsyncResult<JsonObject>> handler)
throws ServiceException {
String tableID = request.getString(ID);

if (!request.containsKey(ID)
|| request.getJsonArray(ID).isEmpty()
|| !request.containsKey(SEARCH_TYPE)) {
throw new ServiceException(HttpStatus.SC_BAD_REQUEST, "message for failure");
}

String tableID = request.getJsonArray(ID).getString(0);

if (!tableExists(tableID)) {
ServiceExceptionMessage detailedMsg =
Expand Down Expand Up @@ -149,10 +173,37 @@ public DatabaseService countQuery(JsonObject request, Handler<AsyncResult<JsonOb

private String queryBuilder(JsonObject request, boolean isCount) {
StringBuilder query;
String selection;
String tableID = request.getString(ID);
String timerel = request.getString(TIME_REL);
String searchType = request.getString(SEARCH_TYPE);
String tableID = request.getJsonArray(ID).getString(0);
String[] attrs = (String[]) request.getValue(ATTRS);

String selection = PSQL_SELECT_QUERY.replace("$$", tableID);

if (attrs == null || attrs.length == 0) {
if (isCount) {
query = new StringBuilder(selection.replace("$1", "count(*)"));
} else {
query = new StringBuilder(selection.replace("$1", "*"));
}
} else {
query = new StringBuilder(selection.replace("$1", String.join(",", attrs)));
}

if (searchType.matches(TEMPORAL_SEARCH_REGEX)) {
query = temporalQueryBuilder(request, query);
}
if (searchType.matches(GEOSEARCH_REGEX)) {
query = new StringBuilder(geoQueryBuilder(request, query));
}
if (searchType.matches(ATTRIBUTE_SEARCH_REGEX)) {
query = attrsQueryBuilder(request, query);
}

return query.toString();
}

private StringBuilder temporalQueryBuilder(JsonObject request, StringBuilder query) {
String timerel = request.getString(TIME_REL);
LocalDateTime time, endTime;
time = LocalDateTime.parse(request.getString(TIME));
endTime = LocalDateTime.parse(request.getString(END_TIME));
Expand All @@ -164,24 +215,36 @@ private String queryBuilder(JsonObject request, boolean isCount) {
endTime = time.plusDays(10);
}

if (attrs == null || attrs.length == 0) {
if (isCount) {
selection = PSQL_SELECT_QUERY.replace("$1", "count(*)");
} else {
selection = PSQL_SELECT_QUERY.replace("$1", "*");
}
} else {
selection = PSQL_SELECT_QUERY.replace("$1", String.join(",", attrs));
}
query.append(
PSQL_TEMPORAL_CONDITION
.replace("$2", time.toString())
.replace("$3", endTime.toString()));

query =
new StringBuilder(
selection
.replace("$$", tableID)
.replace("$2", time.toString())
.replace("$3", endTime.toString()));
return query;
}

return query.toString();
private StringBuilder geoQueryBuilder(JsonObject request, StringBuilder query) {
// TODO: implementation
return null;
}

private StringBuilder attrsQueryBuilder(JsonObject request, StringBuilder query) {
JsonObject attr_query = request.getJsonArray(ATTR_QUERY).getJsonObject(0);
String attribute = attr_query.getString(ATTRIBUTE);
String operator = attr_query.getString(OPERATOR);
double value = attr_query.getDouble(VALUE);

if (query.toString().contains("WHERE")) {
query.append(" AND ");
} else {
query.append(" WHERE ");
}
query.append(
PSQL_ATTR_CONDITION
.replace("$4", attribute)
.replace("$op", operator)
.replace("$5", String.valueOf(value)));
return query;
}

private boolean tableExists(String tableID) {
Expand Down

0 comments on commit a19583d

Please sign in to comment.