JBDI - 吃掉 SQL 语句的一部分

JBDI - Eating up part of the SQL statement

这是我的 JDBI 方法

@Override
@SqlQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CAMPAIGN_ID + " = :campaignId AND " + COLUMN_STATUS
                + " = 0 AND " + COLUMN_STAGE_ID + " IS NULL AND " + COLUMN_MODIFICATION_DATE
                + " < NOW() - INTERVAL '<timeInterval>'")
List<Idea> getDraftIdeasBeforeTime(@Bind("campaignId") long campaignId,
                @Define("timeInterval") String timeInterval);

执行时,它因以下错误而中断

ERROR [2021-10-22 10:06:00,041] com.addons.advanced.jobs.DraftIdeasJob: Exception received while trying to delete draft ideas : org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: org.postgresql.util.PSQLException: ERROR: argument of AND must be type boolean, not type timestamp without time zone
  Position: 127 [statement:"SELECT * FROM ideas WHERE campaign_id = :campaignId AND status = 0 AND stage_id IS NULL AND modification_date < NOW() - INTERVAL '<timeInterval>'", located:"SELECT * FROM ideas WHERE campaign_id = :campaignId AND status = 0 AND stage_id IS NULL AND modification_date ", rewritten:"/* IdeasDaoJdbi.getDraftIdeasBeforeTime */ SELECT * FROM ideas WHERE campaign_id = ? AND status = 0 AND stage_id IS NULL AND modification_date ", arguments:{ positional:{}, named:{campaignId:10}, finder:[]}]

如您所见,post 位置,它只考虑 ... AND modification_date 之前的内容,而忽略之后的内容。

为什么会这样?

您需要在查询中转义 <(更改为 \<)符号,因为它与模板引擎使用的相同。

编辑: 添加到更具体的答案,问题在

" < NOW() - INTERVAL '<timeInterval>'"

需要

" \< NOW() - INTERVAL '<timeInterval>'"