[英]Is there a way to dynamically generate JOOQ sql statement without knowing column names in advance?
I'm trying to put together a SQL generator, so I could dynamically build SQL statements, and pass them onto Spark Job Server. 我试图将SQL生成器放在一起,以便可以动态构建SQL语句,并将它们传递到Spark Job Server。 When the table columns are known in advance, it seems easy (using JOOQ):
当预先知道表列时,似乎很容易(使用JOOQ):
String sql = DSL.select(field("field-1"), field("field-2"), field("field-3"))
.from(table("myTable"))
However, the goal here is to be able to parse an incoming HTTP POST, get the JSON schema out of it, and generate a SQL select statement based on the table structure described in the JSON. 但是,这里的目标是能够解析传入的HTTP POST,从中获取JSON模式,并根据JSON中描述的表结构生成SQL select语句。
... ...
List<String> fieldNames = new ArrayList<>();
fieldNames.add("field-1");
fieldNames.add("field-2");
fieldNames.add("field-3");
... ...
JOOQ "field" appears to be a strongly typed object. JOOQ“字段”似乎是强类型对象。
Is there a way to build such JOOQ select statement, using such dynamically constructed list of columns? 有没有办法使用这种动态构造的列列表来构造这样的JOOQ select语句?
Every query that you build with jOOQ is a dynamic SQL query . 使用jOOQ构建的每个查询都是动态SQL查询 。 For instance, in your first example:
例如,在您的第一个示例中:
DSL.select(field("field-1"), field("field-2"), field("field-3"))
.from(table("myTable"))
What did you really do? 你到底是做什么的 You called
DSL.select(SelectField, SelectField, SelectField)
, which is a convenience overload of DSL.select(SelectField...)
, which you could have called like this just the same: 您调用了
DSL.select(SelectField, SelectField, SelectField)
,这是DSL.select(SelectField...)
一个便利重载,您可以像这样调用它:
SelectField[] fields = {
field("field-1"),
field("field-2"),
field("field-3")
};
DSL.select(fields)
.from(table("myTable"))
Or like this, calling DSL.select(Collection<? extends SelectField<?>>)
: 或者像这样,调用
DSL.select(Collection<? extends SelectField<?>>)
:
DSL.select(Arrays.asList(fields))
.from(table("myTable"));
Always remember, even if ordinary jOOQ queries read , write , and feel like static SQL, they're really dynamic SQL queries, and you can always assign your different jOOQ queryparts to local variables, or retrieve them from methods. 永远记住,即使普通的jOOQ查询读 , 写和感觉像是静态SQL,它们实际上也是动态SQL查询,您始终可以将不同的jOOQ查询部分分配给局部变量,或者从方法中检索它们。
This is particularly useful when used in a more functional programming paradigm: 在更实用的编程范例中使用时,这特别有用:
https://blog.jooq.org/2017/01/16/a-functional-programming-approach-to-dynamic-sql-with-jooq/ https://blog.jooq.org/2017/01/16/a-functional-programming-approach-to-dynamic-sql-with-jooq/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.