当前位置:网站首页>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).
边栏推荐
- 07-SDRAM: FIFO control module
- CRS 管理与维护
- 短视频seo搜索优化主要内容
- IO stream basics
- Difference between JSP out.print() and out.write() methods
- 链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
- bgp aggregation reflector federation experiment
- Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
- 业务测试如何避免漏测 ?
- 含外部储能的电力系统暂态稳定分布式控制
猜你喜欢
随机推荐
How to find new potential projects?Tools recommended
JSP如何使用request获取当前访问者的真实IP呢?
IO stream basics
链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
Quick solution for infix to suffix and prefix expressions
Redis的集群模式
nodeJs--各种路径
Automatic conversion of Oracle-style implicit joins to ANSI JOINs using jOOQ
Disk and file system management
22. The support vector machine (SVM), gaussian kernel function
Day11 shell脚本基础知识
When Netflix's NFTs Forget Web2 Business Security
How to use the go language standard library fmt package
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
短视频SEO搜索运营获客系统功能介绍
Ansible中的任务执行控制
JSP built-in object out object function introduction
JSP out. The write () method has what function?
els 长条变形
短视频seo搜索优化主要内容









