redshift / postgresql中的regexp_count不支持用于设置模式值的动态参数

以下准备好的语句

String pattern = "[\\w\\-\\.]+@([\\w\\-]+\\.)+[\\w\\-]{2,4}";
PreparedStatement statement = conn.prepareStatement("SELECT sum(case when regexp_count(email,?) > 0 then 1 else 0 end) AS email_1 FROM \"testschema\".\"test_table\"")
statement.setObject(1,pattern);

引发以下异常,

java.sql.SQLException: [Amazon](500310) Invalid operation: The pattern must be a valid UTF-8 literal character expression
Details: 
 -----------------------------------------------
  error:  The pattern must be a valid UTF-8 literal character expression
  code:      8001
  context:   
  query:     1976234
  location:  cg_expr_fn_builder.cpp:3542
  process:   padbmaster [pid=5571]
  -----------------------------------------------;
    at com.amazon.redshift.client.messages.inbound.ErrorResponse.toErrorException(Unknown Source)
    at com.amazon.redshift.client.PGMessagingContext.handleErrorResponse(Unknown Source)
    at com.amazon.redshift.client.PGMessagingContext.handleMessage(Unknown Source)
    at com.amazon.jdbc.communications.InboundMessagesPipeline.getNextMessageOfClass(Unknown Source)
    at com.amazon.redshift.client.PGMessagingContext.doMoveToNextClass(Unknown Source)
    at com.amazon.redshift.client.PGMessagingContext.getErrorResponse(Unknown Source)
    at com.amazon.redshift.client.PGClient.handleErrorsScenario2ForPrepareExecution(Unknown Source)
    at com.amazon.redshift.client.PGClient.handleErrorsPrepareExecute(Unknown Source)
    at com.amazon.redshift.client.PGClient.executePreparedStatement(Unknown Source)
    at com.amazon.redshift.dataengine.PGQueryExecutor.executePreparedStatement(Unknown Source)
    at com.amazon.redshift.dataengine.PGQueryExecutor.execute(Unknown Source)
    at com.amazon.jdbc.common.SPreparedStatement.executeWithParams(Unknown Source)
    at com.amazon.jdbc.common.SPreparedStatement.executeQuery(Unknown Source)
Caused by: com.amazon.support.exceptions.ErrorException: [Amazon](500310) Invalid operation: The pattern must be a valid UTF-8 literal character expression
Details: 
 -----------------------------------------------
  error:  The pattern must be a valid UTF-8 literal character expression
  code:      8001
  context:   
  query:     1976234
  location:  cg_expr_fn_builder.cpp:3542
  process:   padbmaster [pid=5571]
  -----------------------------------------------;
    ... 13 more

我相信它不会用动态参数代替。如何克服这个问题。我已经尝试过(?)和$ 1作为占位符,但是没有运气。我不想更改查询或在没有动态参数的情况下构造查询,因为它也用于其他数据库驱动程序。

iCMS 回答:redshift / postgresql中的regexp_count不支持用于设置模式值的动态参数

尝试将正则表达式文字绑定为字符串:

String pattern = "[\\w\\-\\.]+@([\\w\\-]+\\.)+[\\w\\-]{2,4}";
PreparedStatement statement = conn.prepareStatement("SELECT SUM(CASE WHEN REGEXP_COUNT(email,?) > 0 THEN 1 ELSE 0 END) AS email_1 FROM \"testschema\".\"test_table\"");
statement.setString(1,pattern);
,

错误表明它必须是文字字符表达式。换句话说,它不能是参数,因为参数不是文字字符表达式。

我建议向Amazon提出功能请求以进行更改。解决方法是,可以在查询文本中使用文字。

本文链接:https://www.f2er.com/2265113.html

大家都在问