JPA中的Oracle文本标准查询
-
26-10-2019 - |
题
是否可以使用Oracle Text的包含语句来执行JPA标准查询,如果是的话?
解决方案
怀疑。 API在所有RDBMS上都存在,并提供了某些诸如“喜欢”/“ substring”之类的构造 标准 SQL。没有符合标准的方式坚持这一点
其他提示
标准支持函数()API,该函数允许通过名称调用数据库函数。
qb.gt(qb.function("CONTAINS", root.get("name"), qb.parameter("name"), qb.literal(1)), 1)
Eclipselink还使用Func关键字在JPQL中支持这一点。
我刚刚为OpenJPA编写了一个OracleTextDictionary,当该参数带有“神奇”标记的前缀时,将普通的“类似“运算符”转换为“包含”运算符。
通过这种方式,可以将QueryDSL或标准语言(或JPQL)与Oracle Text一起使用。
该字典在参数中以魔法标记为类似于语句,并重写SQL以使用CTX包含呼叫。
一个缺点是,得分无法以简单的方式访问,但是可以增强驱动程序以按分数订购。随意编辑代码:-)
我想可以假设有类似的机制将数据库查询调查到特定的DB。
package se.grynna.dict;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
import org.apache.openjpa.jdbc.sql.OracleDictionary;
import org.apache.openjpa.jdbc.sql.SQLBuffer;
import org.apache.openjpa.jdbc.sql.Select;
public class OracleTextDictionary extends OracleDictionary {
public static final String CTX_MAGIC_MARKER = "@CTX@";
final static Pattern likePattern = Pattern
.compile("t(\\d+)\\.(\\S+) LIKE (\\?)");
@Override
protected SQLBuffer toSelect(SQLBuffer select,
JDBCFetchConfiguration fetch, SQLBuffer tables, SQLBuffer where,
SQLBuffer group, SQLBuffer having, SQLBuffer order,
boolean distinct, boolean forUpdate, long start, long end,Select sel) {
SQLBuffer sqlBuffer = super.toSelect(select, fetch, tables, where,
group, having, order, distinct, forUpdate, start, end, sel);
SQLBuffer tmpBuf = sqlBuffer;
String sql = tmpBuf.getSQL();
int label = 1;
for (Matcher m = likePattern.matcher(sql); m.find(); sql = tmpBuf.getSQL()) {
int argPos = m.start(3);
int argIdx = findArgIdx(sql, argPos);
Object o = tmpBuf.getParameters().get(argIdx);
if( o == null) break;
String arg = o.toString();
if (arg.startsWith(CTX_MAGIC_MARKER)) {
if (tmpBuf == sqlBuffer) {
tmpBuf = new SQLBuffer(sqlBuffer);
}
arg = arg.substring(CTX_MAGIC_MARKER.length());
setParameter(tmpBuf, argIdx, arg);
String aliasNo = m.group(1);
String colName = m.group(2);
}
String replace = String.format("(CONTAINS(t%s.%s,?,%d)>0)",
aliasNo, colName, label++);
tmpBuf.replaceSqlString(m.start(), m.end(), replace);
m.reset(tmpBuf.getSQL());
}
}
return tmpBuf;
}
@SuppressWarnings("unchecked")
private void setParameter(SQLBuffer tmpBuf, int argIdx, String arg) {
tmpBuf.getParameters().set(argIdx, arg);
}
private int findArgIdx(String sql, int argPos) {
int count = -1;
for (int i = 0; i <= argPos; i++) {
char c = sql.charAt(i);
if (c == '?') {
count++;
}
}
return count;
}
}
示例:以下(显然是人为的)输入产生的参数调用:
:1 "@CTX@omg near ponies"
:2 "@CTX@rainbow"
:3 "@CTX@rain%"
:4 "abc1%" <-- an ordinary like :-)
:5 "@CTX@mushroom%"
JPQL
select distinct customer
from Customer customer
where customer.custName like :a1 and customer.custName like :a2 and customer.custName like :a1 and customer.custId in (select d.custId
from Customer d
where d.custName like :a3 or d.custName like :a1)
SQL
SELECT t0.custId,
t0.custName
FROM Customer t0
WHERE ((CONTAINS(t0.custName,?,1)>1)
AND (CONTAINS(t0.custName,?,2) >1)
AND (CONTAINS(t0.custName,?,3) >1)
AND t0.custId IN
(SELECT t1.custId
FROM Customer t1
WHERE (t1.custName LIKE ? <---- the like survives....
OR (CONTAINS(t1.custName,?,1)>1))
))
AND ROWNUM <= ?
附带说明:QueryDSL实际上确实有一个``contains''运算符,据说是为Lucene后端,JPA和SQL后端为此生成了“类似”语句。
我还没有想出一种将包含操作员超载的方法,以便可以使用它。 (除了重写代码之外,我无法做到这一点,因为我使用了与WebSphere捆绑的版本。)
因此,我求助于一种小型静态方法,以使其在使用QuertyDSL时看起来不错。
// x.where(c.custName.like(CTX.contains("omg near ponies"))));
如果JPQL可以为全文搜索引擎提供一些抽象(或插件),那就更好了...
不隶属于 StackOverflow