Postgres JDBC 驱动程序:PSQLException:在 RETURN 或附近出现语法错误

2022-09-04 08:01:08

由于某种原因,JDBC PostgreSQL 驱动程序正在添加:RETURN * 到 select 语句的末尾。为什么?

法典:

protected static final String AUTH_QUERY = "SELECT \"SECRET\" FROM \"user\" WHERE \"NAME\" = :name";

String password = sql2o.open().createQuery(AUTH_QUERY).addParameter("name", username).executeScalar(String.class);

例外:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "RETURNING"
  Position: 47
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:559)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
    at org.sql2o.Query.executeScalar(Query.java:533)
    at org.sql2o.Query.executeScalar(Query.java:577)
    at org.sql2o.Query.executeScalar(Query.java:568)

数据源 (JNDI):

<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">

    <New id="mydb" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>jdbc/mydb</Arg>
        <Arg>
            <New class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <Set name="driverClass">org.postgresql.Driver</Set>
                <Set name="jdbcUrl">jdbc:postgresql://localhost:5432/mydb</Set>
                <Set name="user">user</Set>
                <Set name="password">pass</Set>
            </New>
        </Arg>
    </New>
</Configure>

PostgreSQL JDBC 驱动程序版本

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.3-1101-jdbc41</version>
</dependency>

数据包捕获

https://postimg.cc/image/gbl2dq4zx/

No.     Time           Source                Destination           Protocol Length Info
     12 0.175636000    127.0.0.1             127.0.0.1             PGSQL    182    >P/B/D/E/S

Frame 12: 182 bytes on wire (1456 bits), 182 bytes captured (1456 bits) on interface 0
PostgreSQL
    Type: Parse
    Length: 69
    Statement: 
    Query: SELECT "SECRET" FROM "user" WHERE "NAME" = $1 RETURNING *
    Parameters: 1
        Type OID: 1043

答案 1

我这样做的最简单方法是在sql代码的末尾添加“;--”:

String sql = "INSERT INTO testTable(var1, var2) values ("1","2"), ("1","2") RETURNING id;--";

PreparedStatement ps = getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();

答案 2

这看起来像是 sql2o 的问题。对错误报告的评论说:

使用PostgreSQL,所有SELECT语句都将失败,并显示消息:org.postgresql.util.PSQLException:错误:语法错误在“RETURNING”处或附近

似乎与此问题有关

此问题已在版本 1.1.2 中修复。

此修复程序要求在创建新的 sql2o 实例时将 QuirkMode 枚举标志设置为 PostgreSQL。它将查询的默认行为更改为默认情况下不获取生成的密钥。当需要获取生成的密钥时,应设置 generateQuery 方法中的 returnGenerated Keys 参数。

从 Sql2o 1.6.0 开始,请包含 sql2o-postgres 依赖项并使用而不是 .new PostgresQuirks()QuirksMode


推荐