Skip to content

Commit 6412853

Browse files
committed
Improved response handling of simple SQL query execution
1 parent e75434c commit 6412853

File tree

6 files changed

+64
-39
lines changed

6 files changed

+64
-39
lines changed

pom.xml

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.blobcity</groupId>
66
<artifactId>db-java-adapter</artifactId>
7-
<version>1.2.9</version>
7+
<version>1.2.10</version>
88
<packaging>jar</packaging>
99

1010
<name>BlobCity DB Java Adapter</name>
@@ -172,39 +172,39 @@
172172
</execution>
173173
</executions>
174174
</plugin>
175-
<plugin>
176-
<groupId>org.apache.maven.plugins</groupId>
177-
<artifactId>maven-gpg-plugin</artifactId>
178-
<version>1.5</version>
179-
<!-- <executions>
180-
<execution>
181-
<id>sign-artifacts</id>
182-
<phase>verify</phase>
183-
<goals>
184-
<goal>sign</goal>
185-
</goals>
186-
</execution>
187-
</executions> -->
188-
</plugin>
189-
<plugin>
190-
<groupId>org.apache.maven.plugins</groupId>
191-
<artifactId>maven-failsafe-plugin</artifactId>
192-
<version>2.13</version>
193-
<executions>
194-
<execution>
195-
<id>integration-test</id>
196-
<goals>
197-
<goal>integration-test</goal>
198-
</goals>
199-
</execution>
200-
<execution>
201-
<id>verify</id>
202-
<goals>
203-
<goal>verify</goal>
204-
</goals>
205-
</execution>
206-
</executions>
207-
</plugin>
175+
<!--<plugin>-->
176+
<!--<groupId>org.apache.maven.plugins</groupId>-->
177+
<!--<artifactId>maven-gpg-plugin</artifactId>-->
178+
<!--<version>1.5</version>-->
179+
<!--&lt;!&ndash; <executions>-->
180+
<!--<execution>-->
181+
<!--<id>sign-artifacts</id>-->
182+
<!--<phase>verify</phase>-->
183+
<!--<goals>-->
184+
<!--<goal>sign</goal>-->
185+
<!--</goals>-->
186+
<!--</execution>-->
187+
<!--</executions> &ndash;&gt;-->
188+
<!--</plugin>-->
189+
<!--<plugin>-->
190+
<!--<groupId>org.apache.maven.plugins</groupId>-->
191+
<!--<artifactId>maven-failsafe-plugin</artifactId>-->
192+
<!--<version>2.13</version>-->
193+
<!--<executions>-->
194+
<!--<execution>-->
195+
<!--<id>integration-test</id>-->
196+
<!--<goals>-->
197+
<!--<goal>integration-test</goal>-->
198+
<!--</goals>-->
199+
<!--</execution>-->
200+
<!--<execution>-->
201+
<!--<id>verify</id>-->
202+
<!--<goals>-->
203+
<!--<goal>verify</goal>-->
204+
<!--</goals>-->
205+
<!--</execution>-->
206+
<!--</executions>-->
207+
<!--</plugin>-->
208208
</plugins>
209209
</build>
210210

src/main/java/com/blobcity/db/Db.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,11 @@ public static <T extends Db> Object execute(final Credentials credentials, final
379379
}
380380
return responseList;
381381
} else {
382-
JsonObject jsonObject = response.getPayload().getAsJsonObject();
383-
if (jsonObject.has("count")) {
384-
return jsonObject.get("count").getAsLong();
385-
}
382+
return response.getPayload();
383+
// JsonObject jsonObject = response.getPayload().getAsJsonObject();
384+
// if (jsonObject.has("count")) {
385+
// return jsonObject.get("count").getAsLong();
386+
// }
386387
}
387388

388389
}

src/main/java/com/blobcity/db/DbQueryResponse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class DbQueryResponse {
2929
private final String errorCause;
3030
// Response data
3131
private final JsonElement payload;
32+
private final int rows;
33+
private final long time;
3234

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

50+
rows = jsonObj.get(QueryConstants.ROWS) != null ? jsonObj.get(QueryConstants.ROWS).getAsInt() : -1;
51+
time = jsonObj.get(QueryConstants.TIME) != null ? jsonObj.get(QueryConstants.TIME).getAsLong() : -1;
52+
4853
payload = jsonObj.get(QueryConstants.PAYLOAD);
4954
}
5055

@@ -72,6 +77,14 @@ public String getErrorCause() {
7277
return errorCause;
7378
}
7479

80+
public int getRows() {
81+
return rows;
82+
}
83+
84+
public long getTime() {
85+
return time;
86+
}
87+
7588
public DbOperationException createException() {
7689
return new DbOperationException(errorCode, errorCause);
7790
}

src/main/java/com/blobcity/db/QueryConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ class QueryConstants {
2828
public static final String CAUSE = "cause";
2929
public static final String KEYS = "keys";
3030
public static final String CONTAINS = "contains";
31+
public static final String ROWS = "rows";
32+
public static final String TIME = "time";
3133
}

src/main/java/com/blobcity/db/QueryExecuter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import com.blobcity.db.config.Credentials;
77
import com.blobcity.db.exceptions.InternalAdapterException;
8+
import com.google.gson.JsonElement;
9+
import com.google.gson.JsonObject;
10+
811
import java.io.BufferedReader;
912
import java.io.DataOutputStream;
1013
import java.io.IOException;
@@ -107,7 +110,7 @@ private static DbQueryResponse executeQuery(final String serviceUrl, final Strin
107110
while ((inputLine = in.readLine()) != null) {
108111
response.append(inputLine);
109112
}
110-
113+
111114
return new DbQueryResponse(response.toString());
112115
} catch (MalformedURLException ex) {
113116
throw new InternalAdapterException("Invalid database endpoint address format", ex);

src/test/java/demo/DemoMain.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package demo;
22

33
import com.blobcity.db.Db;
4+
import com.blobcity.db.DbQueryResponse;
45
import com.blobcity.db.config.Credentials;
56
import com.blobcity.db.enums.CollectionType;
67
import com.blobcity.db.search.Query;
78
import com.blobcity.db.search.SearchParam;
9+
import com.google.gson.JsonElement;
810
import com.google.gson.JsonObject;
911

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

48+
System.out.println("count *: " + Db.execute("select count(*) from `test`.`Person`").getPayload().getAsJsonArray().get(0).getAsJsonObject().get("COUNT(*)").getAsInt());
49+
50+
DbQueryResponse dbQueryResponse = Db.execute("delete from `test`.`Person`");
51+
System.out.println("Deleted rows: " + dbQueryResponse.getRows());
4652

4753
Db.dropDs("test");
4854
}

0 commit comments

Comments
 (0)