由于响应国家自主可控号召,我们项目被迫使用了opengaussdb,以前是spring-boot2.x,使用的是hibernate5.x, 只要把设置数据库类型为postgres就可以了(即:spring.jpa.database=postgresql)。但现在发现它生成的分页sql语句在opengauss中执行出错。如:

select * from table1 where ... fetch first 100 rows only

异常信息如下:

Caused by: org.opengauss.util.PSQLException: [127.0.0.1:56751/ocalhost/127.0.0.1:35432] ERROR: syntax error at or near "$1"
  位置:172
	at org.opengauss.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2901) ~[opengauss-jdbc-3.0.0.jar:na]
	at org.opengauss.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2630) ~[opengauss-jdbc-3.0.0.jar:na]
	at org.opengauss.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:362) ~[opengauss-jdbc-3.0.0.jar:na]
	at org.opengauss.jdbc.PgStatement.runQueryExecutor(PgStatement.java:562) ~[opengauss-jdbc-3.0.0.jar:na]
	at org.opengauss.jdbc.PgStatement.executeInternal(PgStatement.java:539) ~[opengauss-jdbc-3.0.0.jar:na]
	at org.opengauss.jdbc.PgStatement.execute(PgStatement.java:397) ~[opengauss-jdbc-3.0.0.jar:na]
	at org.opengauss.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:156) ~[opengauss-jdbc-3.0.0.jar:na]
	at org.opengauss.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:112) ~[opengauss-jdbc-3.0.0.jar:na]
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-3.1.0.jar:na]

ps: 有哪位大侠能解决这个问题就好了,这很可能是opengaussa驱动的问题,语句本身是支持的。
我也向opengauss报告了这个问题:
opengauss问题链接

后记:opengauss回复是:应该是数据库的内核不支持 fetch first $1 rows only绑定变量。

既然有问题,我们就回避吧,由于我们的数据库是A Format, 即Oracle兼容,那么我们设置数据库类型为oracle试试(即:spring.jpa.database=oracle),结果又报all_sequences表找不到,好吧,我先做一个假表欺骗一下它:


CREATE TABLE all_sequences (
	column1 varchar NULL
)

果然,这一关又过去了,接下来报daul找不到,同样建一个dual表,并且插入一条记录(只能一条记录哦,多了不行)

CREATE TABLE dual (
	id int4 NULL
)

果然这一关又过去了。
接下来,发现它不支持group_concat, 报错说是:
SQL 错误 [42601]: [127.0.0.1:54623/ocalhost/127.0.0.1:35432] ERROR: group_concat is supported only in B-format database 位置:393

原本想删除它自己的group_concat, 重建自定义的group_concat, 结果好像行不通,那我们就建一个group_concat2自己用吧,代码如下:

CREATE OR REPLACE FUNCTION _array_append(anyarray, anyelement)
RETURNS anyarray AS
$BODY$
select array_append($1, cast($2 as text));
$BODY$
LANGUAGE 'sql' IMMUTABLE;

create AGGREGATE group_concat2(anyelement)
(
sfunc = _array_append, -- 每行的操作函数,将本行append到数组里
stype = anyarray, -- 聚集后返回数组类型
initcond = '{}' -- 初始化空数组
);

再把我们代码里的group_concat替换成group_concat2, 一切完美了。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐