复杂的 JPQL 字符串查询

发布于 2024-09-18 10:59:06 字数 571 浏览 3 评论 0原文

我正在尝试对数据库中的字符串字段执行非常复杂的查询。我在 JPQL 方面经验不是很丰富,所以我想我会尝试获得一些帮助。

我的数据库中有一个名为 FILE_PATH 的字段。在 FILE_PATH 字段中,将包含以下值:

  • 'C:\temp\files\filename.txt'
  • 'file:\\\C:\testing\testfolder\innerfolder\filename2.txt'

我需要能够仅根据用户给定的查询对文件名进行搜索。因此,不是简单地执行 SELECT Table FROM Table AS t WHERE t.filePath LIKE '%:query%' ,事情必须变得更复杂一点才能仅容纳文件名部分小路。文件路径和文件名是动态数据,因此我不能只在其中硬编码前缀字符串。这让我很困惑,但我知道 JPQL 中有一些字符串表达式可能能够处理这个要求。

基本上,我只需要返回与 FILE_PATH 字段中最后一个“\”之后的任何内容与给定查询匹配的所有行。这可能吗?

感谢您的帮助。

编辑:正在使用的数据库是 SQL Server。

I am trying to execute a pretty-sophisticated query on a string field in the database. I am not very experienced at JPQL, so I thought I would try to get some help.

I have a field in the database called FILE_PATH. Within the FILE_PATH field, there will be values such as:

  • 'C:\temp\files\filename.txt'
  • 'file:\\\C:\testing\testfolder\innerfolder\filename2.txt'

I need to be able to do a search from a user-given query on the file name only. So, instead of simply doing a SELECT Table FROM Table AS t WHERE t.filePath LIKE '%:query%', things will have to get a bit more complicated to accomodate for just the filename portion of the path. The file path and file name are dynamic data, so I can't just hard-code a prefix string in there. This has me pretty confused, but I know there are some string expressions in JPQL that might be able to handle this requirement.

Basically, I just need to return all rows that match the given query on whatever comes after the last '\' in the FILE_PATH field. Is this possible?

Thanks for the help.

EDIT: Database that is being used is SQL Server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2024-09-25 10:59:06

最好的解决方案可能是添加一个仅包含文件名的单独列。如果你不能,那么这可能会起作用(取决于你使用的数据库):

drop table test;
create table test(name varchar(255));
insert into test values('C:\temp\name2\filename.txt');
insert into test values('file:\\\C:\\innerfolder\filename2.txt');
select * from test 
where substring(name, locate('\', name, -1)) like '%name2%'

这是纯 SQL,但据我了解 JPQL 中支持所有函数: http://www.datanucleus.org/products/accessplatform/jpa/jpql_functions.html

一个问题是locate(, ,-1)。它的意思是“从字符串的末尾开始”。它适用于 H2 数据库,但不适用于 MySQL 和 Apache Derby。它可能适用于 Oracle、SQL Server(我没有测试过)。对于某些数据库,可能需要将 '\' 替换为 '\\'(MySQL、PostgreSQL;不确定 Hibernate 是否为您执行此操作)。

Probably the best solution is to add a separate column that contains just the file name. If you can't, then this might work (depending on the database you use):

drop table test;
create table test(name varchar(255));
insert into test values('C:\temp\name2\filename.txt');
insert into test values('file:\\\C:\\innerfolder\filename2.txt');
select * from test 
where substring(name, locate('\', name, -1)) like '%name2%'

This is pure SQL, but as far as I understand all the functions are supported within JPQL: http://www.datanucleus.org/products/accessplatform/jpa/jpql_functions.html

One problem is the locate(,,-1). It means 'start from the end of the string'. It works for the H2 database, but not MySQL and Apache Derby. It might work for Oracle, SQL Server (I didn't test it). For some databases may need to replace '\' with '\\' (MySQL, PostgreSQL; not sure if Hibernate does that for you).

Hello爱情风 2024-09-25 10:59:06

最后的 WHERE 子句:

LOWER(SUBSTRING(fs.filePath, LENGTH(fs.filePath) - (LOCATE('\\', REVERSE(fs.filePath)) - 2), (LOCATE('\\', REVERSE(fs.filePath)) - 1))) LIKE '%:query%'

注意:为了提高性能,您可能需要保存斜杠的位置。

感谢托马斯·穆勒的帮助。

Final WHERE Clause:

LOWER(SUBSTRING(fs.filePath, LENGTH(fs.filePath) - (LOCATE('\\', REVERSE(fs.filePath)) - 2), (LOCATE('\\', REVERSE(fs.filePath)) - 1))) LIKE '%:query%'

NOTE: For performance, you might want to save the location of the slash.

Thanks to Thomas Mueller for the assistance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文