Groovy GString 问题

发布于 2024-08-08 16:39:21 字数 363 浏览 7 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

束缚m 2024-08-15 16:39:22

如果引用不是来自您的 IDE,或者您正在评估代码的任何内容,您可以执行以下操作:

['cdata','tdata'].each { def sql = "select * from ${it.replaceAll("'","")}_1" } 

If the quotes are not from your IDE, or whatever you're evaluating your code in, you can do this:

['cdata','tdata'].each { def sql = "select * from ${it.replaceAll("'","")}_1" } 
风蛊 2024-08-15 16:39:22
groovy:000> ['cdata','tdata'].each { def sql = "select * from ${it}_1"; println sql }
select * from cdata_1
select * from tdata_1
===> [cdata, tdata]

我没有看到任何引用...这就是为什么我要求澄清

groovy:000> ['cdata','tdata'].each { def sql = "select * from ${it}_1"; println sql }
select * from cdata_1
select * from tdata_1
===> [cdata, tdata]

I dont see any quotes... that is why I was asking for clarification

冬天旳寂寞 2024-08-15 16:39:22

真正的答案在问题后面,所以我很抱歉。
Groovy SQL 从 GString 进行参数化查询,因此在定义 GString 后,

def sql = "select * from ${it}_1";

我将其传递给 Groovy SQL,当我尝试执行查询时,实际查询是

"select * from :?_1";

当然,这让 MSSQL 发疯。
再次感谢大家,也许有人会发现这很有用。

Real answer was behind the question, so I'm sorry.
Groovy SQL makes parameterized query from GString, so after I had defined GString

def sql = "select * from ${it}_1";

I passed it to Groovy SQL and when I tried to execute the query, the actual query was

"select * from :?_1";

Of cource this drives MSSQL crazy.
Thanks to you all again, maybe someone will find this useful.

橘和柠 2024-08-15 16:39:21

您可以使用 Groovy 的 Sql 扩展功能来提供帮助。下面的代码可以解决这个问题:

['cdata','tdata'].each {table -> def sql = "select * from ${Sql.expand table}_1" }

如果 GString 中有其他参数,则使用此方法尤其重要:

def name = 'Charlie Sheen'
def tables = ['normalPeople','crazyPeople']
tables.each { table -> 
    def sqlString = "select * from ${Sql.expand table} where name = ${name}"
    /* Execute SQL here */
}

在上面的示例中,仍将使用准备好的语句,并且“name”变量的内容仍将被处理为参数(从而帮助保护您免受 SQL 注入攻击),但表变量参数将正确扩展。

You can use Groovy's Sql expand feature to help here. The following code will do the trick:

['cdata','tdata'].each {table -> def sql = "select * from ${Sql.expand table}_1" }

Using this method is particularly important if you have other parameters in your GString:

def name = 'Charlie Sheen'
def tables = ['normalPeople','crazyPeople']
tables.each { table -> 
    def sqlString = "select * from ${Sql.expand table} where name = ${name}"
    /* Execute SQL here */
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文