【问题标题】:JDBI/Postgress Select query failed with datetime comparisonJDBI/Postgress Select 查询因日期时间比较而失败
【发布时间】:2021-07-10 05:04:01
【问题描述】:

Postgress 版本:10.4

表列名/类型:updated_at timestamp(6) with time zone

SQL 查询:

final String SELECT_PAYMENT_INFO_QUERY =
    "SELECT i.code, p.* 
    FROM information i 
    INNER JOIN product p ON i.ref = p.information_ref 
    WHERE p.reference = :reference AND p.type = :paymentMethodType AND p.error_state = :errorState 
    AND p.updated_at IS NOT NULL AND p.updated_at >= :queryFromTime 
    ORDER BY p.updated_at ASC 
    FETCH FIRST :numberOfRows ROWS ONLY"

Java 方法:

    public List<ProductInformationDTO> retrieveProductInformations(
      ZonedDateTime fetchFromTime, int noOfRecords)
  {
    try
    {
      return dbi.inTransaction((HandleCallback<List<ProductInformationDTO>, Exception>) handle ->
      {
        final List<ProductInformationDTO> productInfos = handle.createQuery(SELECT_PAYMENT_INFO_QUERY)
            .bind("reference", "ABCD")
            .bind("paymentMethodType", "CREDIT_CARD")
            .bind("errorState", "COMPLETED_WITH_ERRORS")
            .bind("queryFromTime", fetchFromTime.toOffsetDateTime())
            .bind("numberOfRows", noOfRecords)
            .map(productInfoMapper)
            .list();

        return productInfos;
      });
    }
    catch (Exception e)
    {
      LOG.error("Failed to complete the retrieve operation.", e);
      throw new TransactionException(e);
    }
  }

此查询和方法适用于 H2 数据库。但是,当使用 Postgress 数据库对其进行测试时,出现以下异常

org.jdbi.v3.core.statement.UnableToExecuteStatementException: org.postgresql.util.PSQLException: 错误: "$5" 或附近的语法错误 Position: 234 [statement:"SELECT i.code, p.* FROM information i INNER JOIN product p ON i.ref = p.information_ref WHERE p.reference = :reference AND p.type = :paymentMethodType AND p.error_state = : errorState AND p.updated_at 不为 NULL AND p.updated_at >= :queryFromTime ORDER BY p.updated_at ASC FETCH FIRST :numberOfRows ROWS ONLY",参数:{positional:{},命名:{queryFromTime:2021-04-15T16:28 :20.365795+12:00,numberOfRows:10,paymentMethodType:CREDIT_CARD,reference:ABCD,errorState:COMPLETED_WITH_ERRORS},finder:[]}] 在 org.jdbi.v3.core.statement.SqlStatement.internalExecute(SqlStatement.java:1794) 在 org.jdbi.v3.core.result.ResultProducers.lambda$getResultSet$2(ResultProducers.java:64) 在 org.jdbi.v3.core.result.ResultIterable.lambda$of$0(ResultIterable.java:54) 在 org.jdbi.v3.core.result.ResultIterable.stream(ResultIterable.java:228) 在 org.jdbi.v3.core.result.ResultIterable.collect(ResultIterable.java:284) 在 org.jdbi.v3.core.result.ResultIterable.list(ResultIterable.java:273) 在 au.com.abcd.products.v2.database.store.dao.ProductsDaoImpl.lambda$retrieveProductInformations$6(ProductsDaoImpl.java:260) 在 org.jdbi.v3.core.Handle.inTransaction(Handle.java:424) 在 org.jdbi.v3.core.Jdbi.lambda$inTransaction$4(Jdbi.java:375) 在 org.jdbi.v3.core.Jdbi.withHandle(Jdbi.java:341) 在 org.jdbi.v3.core.Jdbi.inTransaction(Jdbi.java:375) 在 au.com.abcd.products.v2.database.store.dao.ProductsDaoImpl.retrievePaymentIntents(IntentDaoImpl.java:251) 在 au.com.abcd.products.v2.business.services.complete.ProductServiceImpl.reProcessProducts(ProductServiceImpl.java:114) 在 au.com.abcd.products.v2.business.background.TransactionReProcessManager.lambda$reProcessProducts$1(TransactionReProcessManager.java:81) 在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 在 java.base/java.lang.Thread.run(Thread.java:829) 原因:org.postgresql.util.PSQLException: ERROR: syntax error at or near "$5" 职位:234 在 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532) 在 org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267) 在 org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312) 在 org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) 在 org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) 在 org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:153) 在 org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:142) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:566) 在 org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114) 在 com.sun.proxy.$Proxy120.execute(未知来源) 在 org.jdbi.v3.core.statement.SqlLoggerUtil.wrap(SqlLoggerUtil.java:31) 在 org.jdbi.v3.core.statement.SqlStatement.internalExecute(SqlStatement.java:1786) ...省略了16个常用框架

我也尝试使用 fetchFromTime.toLocalDateTime(),但得到相同的异常。

【问题讨论】:

    标签: postgresql jdbi3


    【解决方案1】:

    您不能在FETCH FIRST(或LIMIT)子句中使用位置参数。一种解决方法(假设您可以容忍运行本机查询)是使用 ROW_NUMBER 代替:

    SELECT *
    FROM
    (
        SELECT i.code, p.*, ROW_NUMBER() OVER (ORDER BY p.updated_at) rn
        FROM information i 
        INNER JOIN product p ON i.ref = p.information_ref 
        WHERE p.reference = :reference AND p.type = :paymentMethodType AND
              p.error_state = :errorState AND p.updated_at IS NOT NULL AND
              p.updated_at >= :queryFromTime 
    ) t
    WHERE rn <= :numberOfRows
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多