regexp_count在redshiftpostgresql中不支持动态参数来设置模式值。

问题描述 投票:0回答:2

以下是准备好的声明

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美元作为占位符,但没有成功。我不想改变查询或构造没有动态参数的查询,因为它也用于其他数据库驱动程序。

jdbc pattern-matching amazon-redshift regexp-like
2个回答
0
投票

试着将regex字词绑定为一个字符串。

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);

0
投票

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

我建议向亚马逊提交一个功能请求,以使这个问题得到改变。作为一个变通方法,你可以在查询文本中使用文字。

© www.soinside.com 2019 - 2024. All rights reserved.