Skip to content

Commit

Permalink
Improved response handling of simple SQL query execution
Browse files Browse the repository at this point in the history
  • Loading branch information
sanketsarang committed Jan 16, 2019
1 parent e75434c commit 6412853
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 39 deletions.
68 changes: 34 additions & 34 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.blobcity</groupId>
<artifactId>db-java-adapter</artifactId>
<version>1.2.9</version>
<version>1.2.10</version>
<packaging>jar</packaging>

<name>BlobCity DB Java Adapter</name>
Expand Down Expand Up @@ -172,39 +172,39 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<!-- <executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions> -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.13</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-gpg-plugin</artifactId>-->
<!--<version>1.5</version>-->
<!--&lt;!&ndash; <executions>-->
<!--<execution>-->
<!--<id>sign-artifacts</id>-->
<!--<phase>verify</phase>-->
<!--<goals>-->
<!--<goal>sign</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions> &ndash;&gt;-->
<!--</plugin>-->
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-failsafe-plugin</artifactId>-->
<!--<version>2.13</version>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>integration-test</id>-->
<!--<goals>-->
<!--<goal>integration-test</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--<execution>-->
<!--<id>verify</id>-->
<!--<goals>-->
<!--<goal>verify</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
</plugins>
</build>

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/blobcity/db/Db.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,11 @@ public static <T extends Db> Object execute(final Credentials credentials, final
}
return responseList;
} else {
JsonObject jsonObject = response.getPayload().getAsJsonObject();
if (jsonObject.has("count")) {
return jsonObject.get("count").getAsLong();
}
return response.getPayload();
// JsonObject jsonObject = response.getPayload().getAsJsonObject();
// if (jsonObject.has("count")) {
// return jsonObject.get("count").getAsLong();
// }
}

}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/blobcity/db/DbQueryResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class DbQueryResponse {
private final String errorCause;
// Response data
private final JsonElement payload;
private final int rows;
private final long time;

public DbQueryResponse(final String response) {
final JsonObject jsonObj = new JsonParser().parse(response).getAsJsonObject();
Expand All @@ -45,6 +47,9 @@ public DbQueryResponse(final String response) {
final JsonElement causeElement = jsonObj.get(QueryConstants.CAUSE);
errorCause = causeElement != null ? jsonObj.get(QueryConstants.CAUSE).getAsString() : null;

rows = jsonObj.get(QueryConstants.ROWS) != null ? jsonObj.get(QueryConstants.ROWS).getAsInt() : -1;
time = jsonObj.get(QueryConstants.TIME) != null ? jsonObj.get(QueryConstants.TIME).getAsLong() : -1;

payload = jsonObj.get(QueryConstants.PAYLOAD);
}

Expand Down Expand Up @@ -72,6 +77,14 @@ public String getErrorCause() {
return errorCause;
}

public int getRows() {
return rows;
}

public long getTime() {
return time;
}

public DbOperationException createException() {
return new DbOperationException(errorCode, errorCause);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/blobcity/db/QueryConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ class QueryConstants {
public static final String CAUSE = "cause";
public static final String KEYS = "keys";
public static final String CONTAINS = "contains";
public static final String ROWS = "rows";
public static final String TIME = "time";
}
5 changes: 4 additions & 1 deletion src/main/java/com/blobcity/db/QueryExecuter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import com.blobcity.db.config.Credentials;
import com.blobcity.db.exceptions.InternalAdapterException;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -107,7 +110,7 @@ private static DbQueryResponse executeQuery(final String serviceUrl, final Strin
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}

return new DbQueryResponse(response.toString());
} catch (MalformedURLException ex) {
throw new InternalAdapterException("Invalid database endpoint address format", ex);
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/demo/DemoMain.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package demo;

import com.blobcity.db.Db;
import com.blobcity.db.DbQueryResponse;
import com.blobcity.db.config.Credentials;
import com.blobcity.db.enums.CollectionType;
import com.blobcity.db.search.Query;
import com.blobcity.db.search.SearchParam;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.List;
Expand Down Expand Up @@ -43,6 +45,10 @@ public static void main(String[] args) {
System.out.println(person.getName() + " " + person.getAge());
}

System.out.println("count *: " + Db.execute("select count(*) from `test`.`Person`").getPayload().getAsJsonArray().get(0).getAsJsonObject().get("COUNT(*)").getAsInt());

DbQueryResponse dbQueryResponse = Db.execute("delete from `test`.`Person`");
System.out.println("Deleted rows: " + dbQueryResponse.getRows());

Db.dropDs("test");
}
Expand Down

0 comments on commit 6412853

Please sign in to comment.