实例化jooq字段<>按名字 [英] Instantiating a jooq Field<> by name

查看:685
本文介绍了实例化jooq字段<>按名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jOOQ在一些通用代码中构建SQL查询。我对使用jOOQ执行这些查询或检查结果不感兴趣。此外,这段代码是通用的,所以我不能使用jOOQ的代码生成。

I'm trying to use jOOQ to build SQL queries in some generic code. I'm not interested in using jOOQ to execute those queries or to examine the results. Also, this code is generic so I cannot use jOOQ's code generation.

我已经设法弄清楚了这一点:

I have managed to figure out this much:

List<org.jooq.Field<?>> fields = new ArrayList<org.jooq.Field<?>>();
Field<?> field = Factory.field("somefield");
fields.add(field);
field = Factory.field("someotherfield");
fields.add(field);

Field<Object> fieldPK = Factory.field("somePKField"); 
Condition condition = fieldPK.equal(123);

Factory factory = new Factory(connection, SQLDialect.POSTGRES);
SelectFinalStep step = factory.select(fields).from("sometable").where(condition);
String query = step.getQuery().getSQL(true);

但是Factory.field()和from()采用通用SQL而不是实际的表或字段名,所以没有引用(即使使用RenderNameStyle.QUOTED)并且没有针对SQL注入的保护。

But Factory.field() and from() take generic SQL rather than actual table or field names, so there's no quoting (even when using RenderNameStyle.QUOTED) and no protection against SQL injection.

有没有办法创建一个字段或表,知道它们的名字是什么?理想情况下,我可以通过其名称和父表指定一个字段,jOOQ为我构建sometable。somefield字符串。

Is there any way to create a Field or Table that know what their names are? Ideally, I could specify a Field by both its name and its parent table, with jOOQ building the "sometable"."somefield" string for me.

推荐答案

jOOQ知道 org。 jooq.Name 键入哪些模型标识符。它可以用 DSL.name(String ...) 来自字符串形式的完全限定名称,例如:

jOOQ knows the org.jooq.Name type which models identifiers. It can be constructed with DSL.name(String...) from fully qualified names in String form, e.g.:

Name name1 = name("column");
Name name2 = name("table", "column");
Name name3 = name("schema", "table", "column");
Name name4 = name("catalog", "schema", "table", "column");

然后您可以将此名称传递给 DSL.field(名称) 构造函数,例如:

You can then pass such a name to the DSL.field(Name) constructor, e.g.:

Field<Object> field1 = field(name("table", "column"));
Field<String> field2 = field(name("table", "column"), String.class);



在jOOQ版本



旁注:问题是在jOOQ 2.x的背景下提出的,但很少有人仍在使用这个旧版本,这就是为什么这个答案假定使用jOOQ 3.x

On jOOQ versions

Side-note: The question was asked in the context of jOOQ 2.x, but few people are still using this old version, which is why this answer assumes using jOOQ 3.x

这篇关于实例化jooq字段&lt;&gt;按名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆