Postgres bytea error when binding null to prepared statements

前端 未结 2 814
慢半拍i
慢半拍i 2021-01-23 07:50

I am working with a Java application which uses JPA and a Postgres database, and I am trying to create a flexible prepared statement which can handle a variable number of input

2条回答
  •  再見小時候
    2021-01-23 08:09

    If you want to keep using plain queries with automatic parameter binding, you could try the following.

    WHERE (? IS NULL OR (CAST(CAST(? AS TEXT) AS DOUBLE PRECISION) = double_col
    

    This seems to satisfy the PostgreSQL driver's type checks as well as yielding the correct results. I haven't done much testing, but the performance hit seems minimal because the CASTs happen on a constant value rather than rows from the database.

提交回复
热议问题