生成带有Flyway占位符的SQL语句

huangapple go评论81阅读模式
英文:

Generate sql statements with flyway placeholders

问题

我试图将一些 SQL 插入语句保存到文件中,以便在测试时使用。我想使用 flyway 占位符 来实现,但是我找不到任何相关信息。

以下是 Java 的一些示例代码:

var sqlTXT = sql.insertInto(table("TBLNAME"))
     .set(field("strCol"), field("strVal").toString())
     .set(field("placeHolderCol"), field(inline("${flyway:user}")))
     .getSQL(ParamType.INLINED);

这将生成类似于以下 SQL 字符串:

insert into TBLNAME (strCol, placeHolderCol) values ('strVal', '${flyway:user}')

我希望得到类似这样的结果:

insert into TBLNAME (strCol, placeHolderCol) values ('strVal', ${flyway:user})

这样 Flyway 就可以替换 ${flyway:user} 并插入用户名。是否有办法实现这样的 SQL 渲染,还是我必须要进行"手动"处理?

英文:

I trying to save some sql insert statements to files to use them for testing.
I would like to use flyway placeholders for that but I'm not able to find any.

Some example in Java:

var sqlTXT = sql.insertInto(table("TBLNAME"))
     .set(field("strCol"), field("strVal").toString())
     .set(field("placeHolderCol"), field(inline("${flyway:user}")))
     .getSQL(ParamType.INLINED);

This will produce SQL string like this one:

insert into TBLNAME (strCol, placeHolderCol) values ('strVal', '${flyway:user}')

and I'm looking for something like this

insert into TBLNAME (strCol, placeHolderCol) values ('strVal', ${flyway:user})

So flyway can substitute ${flyway:user} and insert user name.

Is there any way to render sql like this or will I have to do it "manually"?

答案1

得分: 2

Flyway的占位符与任何其他“供应商特定”SQL语法没有区别,这种语法不能被jOOQ直接支持,所以纯SQL模板化是答案。

只需使用

field(" ${flyway:user}")

不要使用DSL.inline(),该方法用于创建“内联值”(例如字符串文字)。

英文:

Flyway's placeholders are no different from any other "vendor specific" SQL syntax, which isn't supported out of the box by jOOQ, so plain SQL templating has the answer.

Just use

field("${flyway:user}")

Don't use DSL.inline(), which is used for creating "inline values" (e.g. a string literal).

huangapple
  • 本文由 发表于 2020年8月24日 22:14:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63562814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定