Does Java JDBI `SqlQuery` have idiomatic support for SQL `=` AND SQL `IS NULL` with one expression?

Viewed 112

Let's take a simple SELECT query:

@SqlQuery("SELECT * FROM mytable WHERE some_column = :myVar")
List<MyObjects> getBySomeColumn(String myVar);

I'd like to know if there is something like the above, but handles both IS NULL and = similarly, automatically:

@SqlQuery("SELECT * FROM mytable WHERE some_column <MAGIC_HERE> :myVar")
List<MyObjects> getBySomeColumn(String myVar);

... which would run either:

SELECT * FROM mytable WHERE some_column IS NULL

or

SELECT * FROM mytable WHERE some_column = :myVar

... depending on whether myVar is null or not. What is the most idiomatic way to handle this kind of query?

1 Answers

There are two options:

  1. Rewrite SQL query to something like this:
SELECT * FROM mytable
WHERE (:myVar is null and some_column is null) or some_column = :myVar
  1. Use JDBI templating to dynamically rewrite queries
Related