当前位置:网站首页>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 likeSQLDialectand 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 codeIf 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 codeAs 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).
边栏推荐
- 不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
- TCL:在Quartus中使用tcl脚本语言进行管脚约束
- 基于编码策略的电网假数据注入攻击检测
- SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
- 中缀转后缀、前缀表达式快速解决办法
- 扑克牌问题
- 【21天学习挑战赛】顺序查找和二分查找的小总结
- Transient Stability Distributed Control of Power System with External Energy Storage
- An interesting project--Folder comparison tool (1)
- JSP out.write()方法具有什么功能呢?
猜你喜欢
随机推荐
JSP如何使用request获取当前访问者的真实IP呢?
CRS management and maintenance
Redis - message publish and subscribe
【HCIP】BGP小型实验(联邦,优化)
bgp aggregation reflector federation experiment
具有通信时延的多自主体系统时变参考输入的平均一致性跟踪
Transient Stability Distributed Control of Power System with External Energy Storage
CRS 管理与维护
632. 最小区间
Simpson's paradox
Redis的集群模式
中缀转后缀、前缀表达式快速解决办法
The Statement update Statement execution
460. LFU cache
Axure tutorial - the new base (small white strongly recommended!!!)
Grid false data injection attacks detection based on coding strategy
07-SDRAM: FIFO control module
2022/08/01 学习笔记 (day21) 泛型和枚举
When Netflix's NFTs Forget Web2 Business Security
实现删除-一个字符串中的指定字母,如:字符串“abcd”,删除其中的”a”字母,剩余”bcd”,也可以传递多个需要删除的字符,传递”ab”也可以做到删除”ab”,剩余”cd”。






![[21-Day Learning Challenge] A small summary of sequential search and binary search](/img/81/7339a33de3b9e3aec0474a15825a53.png)

