当前位置:网站首页>Don't concatenate strings with jOOQ
Don't concatenate strings with jOOQ
2022-08-02 00:32:00 【YYniannian】
jOOQ supports a large number of SQL syntaxes out of the box.Therefore, most users do not use string concatenation as before when writing dynamic SQL using JDBC.But every now and then, jOOQ doesn't support a certain vendor's specific feature (yes, it happens).In this case, jOOQ supports various "Plain SQL" API, which can be used to construct almost all types of jOOQ API elements, eg.
// Static import is implied, as alwaysimport static org.jooq.impl.DSL.*;// Column expressionsField f = field("cool_function(1, 2, 3)", String.class);// PredicatesCondition c = condition("col1 col2");// TablesTable> t = table("wicked_table_valued_function(x, y)");Copy code
However, sometimes, you need to dynamically pass an argument to such a function, such as an expression for another column.And you want to do it in a type-safe way, because the jOOQ code generator already produces type-safe column expressions.So you might be inclined towards concatenation, though.
field("cool_function(1, " + MY_TABLE.MY_COLUMN + ", 3)");Copy code
**Don't do this!**For these reasons.
- Although jOOQ is very safe for SQL injection in general, you can actually introduce a common SQL injection vulnerability here.Not in this case because the column is generated code, but maybe, you'll concatenate the user's input.Note that for added SQL injection protection, it can be done by adding our PlainSQL Checker,Use a checker framework or Google ErrorProne to prevent the use of plain SQL globally and only allow local use when needed.
- As usual with string concatenation, you are prone to SQL syntax errors.In this case, the generated SQL does not target any dialect, because
MY_TABLE.MY_COLUMN.toString()
, does not have any contextual information likeSQLDialect
and all other configuration flags.
Instead, use jOOQ's Plain SQL Template small language, which allows template placeholders like {0}, {1}, {2}
.
field("cool_function(1, {0}, 3)", MY_TABLE.MY_COLUMN);Copy code
If you do this often, you can consider this call in your own mini-DSL.
public static Field coolFunction(Field> field) {field("cool_function(1, {0}, 3)", field);}Copy code
And now, call it like this.
coolFunction(MY_TABLE.MY_COLUMN)Copy code
As a rule of thumb: With jOOQ, you should never need to resort to SQL string concatenation, you can always use the following two methods.
- Type-safe jOOQ DSL API
- The normal SQL template API (preferably hiding this usage behind your own type-safe DSL API).
边栏推荐
- Axure tutorial - the new base (small white strongly recommended!!!)
- 玩转NFT夏季:这份工具宝典值得收藏
- 【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
- 基于数据驱动的变电站巡检机器人自抗扰控制
- 鲲鹏编译调试插件实战
- Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
- Routing strategy
- 电机原理动图合集
- Async/await principle and execution sequence analysis
- 632. 最小区间
猜你喜欢
随机推荐
uni-app项目总结
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
How to design a circular queue?Come and learn~
GetHashCode方法与=
JSP page指令errorPage属性起什么作用呢?
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
在不完全恢复、控制文件被创建或还原后,必须使用 RESETLOGS 打开数据库,解释 RESETLOGS.
Pytorch seq2seq 模型架构实现英译法任务
els 方块变形判断。
Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
Stapler:1 靶机渗透测试-Vulnhub(STAPLER: 1)
[HCIP] BGP Small Experiment (Federation, Optimization)
NodeJs, all kinds of path
JSP out.write()方法具有什么功能呢?
Short video seo search optimization main content
字符串分割函数strtok练习
JSP how to obtain the path information in the request object?
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
鲲鹏编译调试插件实战
632. Minimum interval