Jdbi: Échapper à l'opérateur de point d'interrogation jsonb dans postgres

Créé le 18 sept. 2015  ·  6Commentaires  ·  Source: jdbi/jdbi

J'essaie d'utiliser jdbi avec postgres et jsonb pour écrire une requête comme celle-ci :

select id, data from some_table where data ? :key

Malheureusement, jdbi interprète ce point d'interrogation comme un espace réservé pour une variable. Comment puis-je échapper au point d'interrogation pour pouvoir utiliser l'opérateur de point d'interrogation postgres ?

Commentaire le plus utile

Il s'avère que c'est un problème en amont dans jdbc. Ils ont mis en place un moyen d'y échapper. Vous devez utiliser ???. Ainsi:

select id, data from some_table where data ?? :key

Tous les 6 commentaires

Avez-vous essayé la barre oblique inverse \? ?

Oui, je suppose que j'aurais dû le mentionner. C'était mon premier réflexe. Mais j'obtiens toujours une erreur.

Exception in thread "main" org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: org.postgresql.util.PSQLException: No value specified for parameter 2. [statement:"select id, createdDate, data from something where data \? ?", located:"select id, createdDate, data from something where data \? ?", rewritten:"select id, createdDate, data from something where data ? ?", arguments:{ positional:{0:'pattern'}, named:{}, finder:[]}]
    at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1334)
    at org.skife.jdbi.v2.Query.fold(Query.java:173)
    at org.skife.jdbi.v2.Query.list(Query.java:82)
    at org.skife.jdbi.v2.Query.list(Query.java:75)
    at com.ngc.vault.eventer.Query.hasKey(Query.java:45)
    at com.ngc.vault.eventer.Main.main(Main.java:34)
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 2.
    at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:228)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:163)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:615)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:465)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:458)
    at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1328)
    ... 5 more

Il s'avère que c'est un problème en amont dans jdbc. Ils ont mis en place un moyen d'y échapper. Vous devez utiliser ???. Ainsi:

select id, data from some_table where data ?? :key

requête Postgres
SELECT json_data FROM employee where json_data -> 'employee' @> '{"name":"Aman"}'

La requête ci-dessus fonctionne correctement lorsqu'elle est exécutée dans postgres. Mais lorsqu'il est exécuté avec jdbctemplate, il renvoie une erreur.

Code Java

String sql="SELECT json_data FROM employee where json_data -> 'employee' @> '{\"name\":\"?\"}'";

List<Map<String, Object>> emp = jdbcTemplate.queryForList(sql,param);

En rencontrant la dernière ligne du code, il renvoie une erreur :- L'index de colonne est hors limites : 1, nombre de colonnes : 0. ; l'exception imbriquée est org.postgresql.util.PSQLException.

Il n'est pas capable de remplacer le '?' espace réservé.

C'est selon la spécification JDBC. ? marqueurs

Si vous devez interpoler un paramètre dans cette chaîne JSON, utilisez la concaténation. En utilisant la syntaxe de concaténation Postgres, la requête ressemblerait à :

SELECT json_data
FROM employee
WHERE json_data -> 'employee' @> ('{"name": "' || ? || '"}')

Veuillez également noter qu'il s'agit du projet Jdbi, pas de JdbcTemplate. :)

Cette page vous a été utile?
0 / 5 - 0 notes

Questions connexes

keith-miller picture keith-miller  ·  3Commentaires

buremba picture buremba  ·  5Commentaires

goxr3plus picture goxr3plus  ·  4Commentaires

dhardtke picture dhardtke  ·  3Commentaires

anjeyy picture anjeyy  ·  3Commentaires