Groovy GString 问题
我想在 groovy GString 中使用 $ 宏。当我写下这段代码时
['cdata','tdata'].each { def sql =“从$it_1中选择*” }
我收到错误未知属性 $it_
好的,我重写它
['cdata','tdata'].each { def sql = "从${it}_1中选择*" }
然后我在结果字符串中得到不需要的引号 - "select * from 'cdata'_1"
问题是我如何在 GString 中使用 $-macro 来获得 "select * from cdata_1" 结果字符串?
I'm want use $ macro in groovy GString. When i'm wrote this code
['cdata','tdata'].each {
def sql = "select * from $it_1"
}
i'm get error unknown property $it_
ok, i'm rewrite it
['cdata','tdata'].each {
def sql = "select * from ${it}_1"
}
then i'm get unwanted quotes in result string - "select * from 'cdata'_1"
Question is how i'm can use $-macro in GString to achive "select * from cdata_1" result string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果引用不是来自您的 IDE,或者您正在评估代码的任何内容,您可以执行以下操作:
If the quotes are not from your IDE, or whatever you're evaluating your code in, you can do this:
我没有看到任何引用...这就是为什么我要求澄清
I dont see any quotes... that is why I was asking for clarification
真正的答案在问题后面,所以我很抱歉。
Groovy SQL 从 GString 进行参数化查询,因此在定义 GString 后,
我将其传递给 Groovy SQL,当我尝试执行查询时,实际查询是
当然,这让 MSSQL 发疯。
再次感谢大家,也许有人会发现这很有用。
Real answer was behind the question, so I'm sorry.
Groovy SQL makes parameterized query from GString, so after I had defined GString
I passed it to Groovy SQL and when I tried to execute the query, the actual query was
Of cource this drives MSSQL crazy.
Thanks to you all again, maybe someone will find this useful.
您可以使用 Groovy 的 Sql 扩展功能来提供帮助。下面的代码可以解决这个问题:
如果 GString 中有其他参数,则使用此方法尤其重要:
在上面的示例中,仍将使用准备好的语句,并且“name”变量的内容仍将被处理为参数(从而帮助保护您免受 SQL 注入攻击),但表变量参数将正确扩展。
You can use Groovy's Sql expand feature to help here. The following code will do the trick:
Using this method is particularly important if you have other parameters in your GString:
In the example above a prepared statement will still be used, and the contents of the 'name' variable will still be handled as a parameter (thus helping to protect you against SQL injection attacks) but the table variable parameter will be expanded correctly.