简体   繁体   English

用户定义类型过滤时如何在jooq中形成where子句

[英]How to form where clause in jooq when filtering by user defined type

I'm using postgresql (10.2) with jooq (3.8.7), lets say I have a type and a table like: 我在jooq(3.8.7)中使用postgresql(10.2),可以说我有一个类型和一个表,例如:

CREATE TYPE my_type AS (
    id INTEGER,
    name TEXT
);

CREATE table my_table (
    id INTEGER,
    something my_type 
);

Then how do I fetch all records of my_table where my_table.something.name = 'test' by using jooq? 然后,如何使用jooq获取my_table的所有记录,其中my_table.something.name ='test'? I tried doing something like this: 我试图做这样的事情:

ctx.selectFrom(MY_TABLE)
   .where(MY_TABLE.SOMETHING.NAME.eq("test")) \\ SOMETHING is a TableField 
   ...                                        \\ and does not have NAME field

But that does not work (explained in code comments). 但这不起作用(在代码注释中说明)。 This is what I want to do in jooq: 这是我要在jooq中执行的操作:

SELECT * FROM my_table WHERE (something).name = 'test';

Accessing user defined type attributes through the DSL is currently not implemented in jOOQ 3.11. jOOQ 3.11当前未实现通过DSL访问用户定义的类型属性。 The relevant pending feature request is here: https://github.com/jOOQ/jOOQ/issues/228 相关的待处理功能请求位于此处: https : //github.com/jOOQ/jOOQ/issues/228

As always, when running in such a limitation, you can resort to using plain SQL templating : 与往常一样,在这种限制下运行时,您可以诉诸使用简单的SQL模板

ctx.selectFrom(MY_TABLE)
   .where("{0}.{1} = {2}", MY_TABLE.SOMETHING, MY_TYPE.NAME, DSL.val("test"))
   ...

Of course, if you're doing this more often, you might want to wrap this kind of logic into your own client side templating API to improve reusing similar patterns. 当然,如果您经常执行此操作,则可能需要将这种逻辑包装到自己的客户端模板API中,以提高重用相似模式的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM