在CRUD vertx之后将数据返回到处理程序中以再次使用它

时间:2016-11-03 18:06:46

标签: java handler vert.x

public static void addMessage(RoutingContext routingContext) {
    System.out.println("Add  Message");
    String message = routingContext.getBodyAsJson().getString("message");
    JsonObject params = new JsonObject();
    params.put("QUERY", Queries.DB_INSERT_MESSAGE);
    params.put("PARAMS", new JsonArray().add(message));
    DB.insert(params, res -> {
        if (res.succeeded()) {
            ClientCallBack.sendJsonResponce(routingContext, res.result().getJsonArray("rows"));
        } else {
            ClientCallBack.sendErrorResponce(routingContext, res.cause().getMessage());
        }
    });

她的我的DB.Insert方法

public static void insert (JsonObject reqParams, Handler<AsyncResult<JsonObject>> handler){
    insert(config.getJsonObject(CONS_CONFIG.DATABASE).getString(CONS_CONFIG.DEFAULT_DATABASE), reqParams, handler);
}

public static void insert (String DB_PREFIX, JsonObject reqParams, Handler<AsyncResult<JsonObject>> handler){
    if(CONS_CONFIG.ORACLE.equalsIgnoreCase(DB_PREFIX)){
        oracle.updateWithParams(reqParams, handler);
    }else{
        mysql.updateWithParams(reqParams, handler);
    }
}

和updateWithParams

 public void updateWithParams(JsonObject paramsQuery, Handler<AsyncResult<JsonObject>> resultHandler) {
    logger.debug("START WITH PARAMS : "+paramsQuery.encode());
    String sql = paramsQuery.getString("QUERY");
    JsonArray params = paramsQuery.getJsonArray("PARAMS");
    getConnection(connection->{
        if (connection.succeeded()) {
            connection.result().updateWithParams(sql, params, query -> {
                if (query.succeeded()) {
                    resultHandler.handle(Future.succeededFuture(query.result().toJson()));
                } else {
                    logger.debug(query.cause().getMessage());
                    resultHandler.handle(Future.failedFuture(Msg.UPDATE_WITH_PARAMS_FAIL));
                }
            });
        }else{
            logger.debug(connection.cause().getMessage());
            resultHandler.handle(Future.failedFuture(Msg.CONNECT_TO_DATA_BASE_FAIL));
        }
            connection.result().close();

    });
}

我的问题是即使数据在数据库中注册,但处理程序返回null值。有没有办法在每次CRUD操作后返回结果? 我的方法addMessage中的res.result()。getJsonArray(&#34; rows&#34;)返回null。 谢谢

1 个答案:

答案 0 :(得分:0)

正如@Paulo Lopes建议的那样,你应该使用.getKeys()方法:

mySQLClient.getConnection((res)->{
        if (res.succeeded()) {
            SQLConnection conn = res.result();

        JsonArray params  = new JsonArray().add(UUID.randomUUID().toString()).add("A").add("abcd");
        conn.updateWithParams("insert into user (email, name, password) values (?, ?, ?)", params,
                (r) -> {
                    if (r.succeeded()) {

                        // Your IDs
                        JsonArray keys = r.result().getKeys();

                        // This should work for MySQL
                        //String q = "select * from user where id in (?)";

                        // This should work with Oracle, since Oracle returns ROWID (string), not ID (integer)
                        String q = "select id from user where rowid in (?)";
                        conn.queryWithParams(q, keys, (result) -> {
                            // Return it instead of printing
                            if (result.succeeded()) {
                                System.out.println(result.result().toJson());
                            }
                        });
                    }
                    else {
                        // Handle error
                    }
        });
        }
        else {
            res.cause().printStackTrace();
        }
        // Deal with errors
    });