Modification to use the HandlerCollection + slight cleanup
This commit is contained in:
Binary file not shown.
BIN
build/classes/java/main/cavecomm/HandlerCollection.class
Normal file
BIN
build/classes/java/main/cavecomm/HandlerCollection.class
Normal file
Binary file not shown.
Binary file not shown.
@ -1,10 +1,6 @@
|
||||
package cavecomm;
|
||||
|
||||
import io.vertx.core.AsyncResult;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.pgclient.PgConnectOptions;
|
||||
import io.vertx.pgclient.PgPool;
|
||||
@ -14,10 +10,15 @@ import io.vertx.sqlclient.RowSet;
|
||||
import io.vertx.sqlclient.SqlClient;
|
||||
|
||||
public class Database {
|
||||
PgConnectOptions connectOptions;
|
||||
SqlClient client;
|
||||
PoolOptions poolOptions;
|
||||
Vertx vertx;
|
||||
public static final int OUTPUT_DATATYPE_JSON = 0;
|
||||
public static final int OUTPUT_DATATYPE_HTML = 1;
|
||||
public static final int OUTPUT_DATATYPE_NONE = 2;
|
||||
|
||||
private final PgConnectOptions connectOptions;
|
||||
private final PoolOptions poolOptions;
|
||||
private final Vertx vertx;
|
||||
|
||||
private SqlClient client;
|
||||
|
||||
public Database(Vertx vertx, int port, String host, String database, String user, String password) {
|
||||
this.vertx = vertx;
|
||||
@ -55,29 +56,17 @@ public class Database {
|
||||
|
||||
//Executes an SQL query while delivering a context to handle the data
|
||||
//context output is a JSON
|
||||
public void executeQuery(String query, RoutingContext rc)
|
||||
public void executeQuery(String query, RoutingContext rc, int outputDataType)
|
||||
{
|
||||
createClient();
|
||||
client
|
||||
.query(query)
|
||||
.execute(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
RowSet<Row> result = ar.result();
|
||||
//test output
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (Row r : result) {
|
||||
JsonObject jsonRow = new JsonObject();
|
||||
jsonRow.put("testcolumn1", r.getJson("testcolumn1"));
|
||||
jsonRow.put("column2", r.getJson("column2"));
|
||||
jsonArray.add(jsonRow);
|
||||
switch (outputDataType) {
|
||||
case OUTPUT_DATATYPE_JSON -> HandlerCollection.outputJSON(ar, rc);
|
||||
case OUTPUT_DATATYPE_HTML -> HandlerCollection.outputHTML(ar, rc);
|
||||
}
|
||||
//sets the header to json
|
||||
rc.response().putHeader("Content-Type", "application/json; charset=UTF8")
|
||||
.end(jsonArray.encode());
|
||||
|
||||
} else {
|
||||
System.out.println("Failure: " + ar.cause().getMessage());
|
||||
}
|
||||
closeClient();
|
||||
});
|
||||
}
|
||||
|
@ -1,18 +1,9 @@
|
||||
package cavecomm;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.AsyncResult;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.MultiMap;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.pgclient.PgConnectOptions;
|
||||
import io.vertx.pgclient.PgPool;
|
||||
import io.vertx.sqlclient.PoolOptions;
|
||||
import io.vertx.sqlclient.Row;
|
||||
import io.vertx.sqlclient.RowSet;
|
||||
import io.vertx.sqlclient.SqlClient;
|
||||
|
||||
public class MainVerticle extends AbstractVerticle {
|
||||
@Override
|
||||
@ -29,10 +20,13 @@ public class MainVerticle extends AbstractVerticle {
|
||||
"testuser",
|
||||
"123");
|
||||
|
||||
//db.executeQuery("SELECT * FROM testtable");
|
||||
|
||||
router.get("/jsonTest").handler(rc -> {
|
||||
db.executeQuery("SELECT * FROM testtable", rc);
|
||||
db.executeQuery("SELECT * FROM testtable", rc, Database.OUTPUT_DATATYPE_JSON);
|
||||
}
|
||||
);
|
||||
router.get("/htmlTest").handler(rc -> {
|
||||
db.executeQuery("SELECT * FROM testtable", rc, Database.OUTPUT_DATATYPE_HTML);
|
||||
}
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user