REST API: pass parameters to preparedQuery in Vertx pgclient

918 views Asked by At

This is the response from my API:

"The number of parameters to execute should be consistent with the expected number of parameters = [2] but the actual number is [0]."

I use pgClient, Vert.x 3.9.3 and consume various REST API's without problems, but....in this query (probably it's wrong)

private static final String SELECT_CBA = "select art.leyenda, $1::numeric(10,3) as cantidad, uni.abreviatura, \r"
+ "round(((art.precio_costo * (art.utilidad_fraccionado/100)) + art.precio_costo) * $2::numeric(10,3),2) as totpagar \r"
+ "FROM public.articulos art join public.unidades uni on uni.idunidad = art.idunidad \r"
+ "WHERE (substring(art.codigobarra,1,2) = \'$3\' and substring(art.codigobarra,3,6) = \'$4\')";

Some explanation
$1 and $2 are the same parameters; $2 and $3 must be quoted parameters. This is my rest verticle:

poolClient = PgPool.pool(vertx, options, poolOptions);      
bizArticulo = new BizArticulo(poolClient);
Router router = Router.router(vertx);
router.route("/api/articulos*").handler(BodyHandler.create());
router.get("/api/articulos").handler(bizArticulo::getAll);
router.get("/api/articulos/:cantcomp1/:cantcomp2/:tipoprod/:prodpadre").handler(bizArticulo::getOneReadingBarcode);

Where :cantcomp1 -> $1, :cantcomp2 -> $2, :tipoprod -> $3 and $4 -> :prodpadre

and finally, this my "business"

public void getOneReadingBarcode(RoutingContext routingContext) {       
        HttpServerResponse response = routingContext.response();
        pgClient
        .preparedQuery(SELECT_CBA)      
        .execute(ar -> {
            if (ar.succeeded()) {
                RowSet<Row> rows = ar.result();
                List<Articulo> articulos = new ArrayList<>();
                rows.forEach(row -> {
                    articulos.add(fromBarCode(row));
                });
                response.putHeader("content-type", "application/json; charset=utf-8")
                .setStatusCode(200)
                .end(Json.encodePrettily(articulos));
            } else {
                System.out.println("Failure: " + ar.cause().getMessage());
                response.putHeader("content-type", "application/json; charset=utf-8")
                .end(Json.encodePrettily(ar.cause().getMessage()));
            }
        });
    }

In Postman, I wrote:

192.168.0.15:8092/api/articulos/0.750/0.750/20/021162; where I assume the parameters match, but it returns the error mentioned above. ¿What's wrong? Any help would be appreciated Ernesto

1

There are 1 answers

0
Ernesto Andres Zapata Icart On

Ok, I answer to myself... First: little fix to my query (SELECT_CB), I put the parameters $1, $2... in parentheses..

private static final String SELECT_CBA = "select art.leyenda, ($1::numeric(10,3)) as cantidad, uni.abreviatura, \r"
+ "round(((art.precio_costo * (art.utilidad_fraccionado/100)) + art.precio_costo) * ($2::numeric(10,3)),2) as totpagar \r"
+ "FROM public.articulos art join public.unidades uni on uni.idunidad = art.idunidad \r"
+ "WHERE (substring(art.codigobarra,1,2) = ($3) and substring(art.codigobarra,3,6) = ($4))";

Second: got the params from context (see router.context in previous question)

Double cantComprada1 = Double.parseDouble(routingContext.request().getParam("cantcomp1"));
Double cantComprada2 = Double.parseDouble(routingContext.request().getParam("cantcomp2"));
String tipoProducto = routingContext.request().getParam("tipoprod");
String productoPadre = routingContext.request().getParam("prodpadre");

and Third: put the params as arguments

pgClient
.preparedQuery(SELECT_CBA)
.execute(Tuple.of(cantComprada1, cantComprada2, tipoProducto, productoPadre), ar -> {
......
......
}

All work's fine