当前位置:网站首页>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).
边栏推荐
- Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
- 不要用jOOQ串联字符串
- Unknown CMake command “add_action_files“
- Statement执行update语句
- 【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
- JSP out.print()和out.write()方法的不同之处
- 460. LFU cache
- 控制电机的几种控制电路原理图
- bgp 聚合 反射器 联邦实验
- 磁盘与文件系统管理
猜你喜欢
随机推荐
Double queue implementation stack?Dual stack implementation queue?
Disk and file system management
【HCIP】BGP小型实验(联邦,优化)
【无标题】
JSP page指令errorPage属性起什么作用呢?
业务测试如何避免漏测 ?
els 方块边界变形处理
Redis的集群模式
07-SDRAM :FIFO控制模块
Simpson's paradox
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
双队列实现栈?双栈实现队列?
How to find new potential projects?Tools recommended
Redis - message publish and subscribe
一文概览最实用的 DeFi 工具
Redis-消息发布订阅
基于编码策略的电网假数据注入攻击检测
These 4 computer notepad software, you have to try
短视频seo搜索优化主要内容
How to use the go language standard library fmt package








