从 GString 中转义点
我想学习如何转义 GString 中的点,以便 groovy (1.8) 不会将其视为 sql.execute 内变量的一部分。我有以下代码:
Map<String, String> dbSettings = [schemaName:"testSchema"];
String myDbPrefix = dbSetting.schemaName + ".";
sql.execute "DELETE FROM ${myDbPrefix}myTable"
并且我收到此错误:
Ignoring groovy.lang.MissingPropertyException: No such property: myTable for class: java.lang.String
清楚地表明 .被解释为变量 ${myDbPrefix} 的一部分。
I would like to learn how to escape dot in GString so groovy (1.8) does not treat it as a part of an variable inside sql.execute. I have the following code:
Map<String, String> dbSettings = [schemaName:"testSchema"];
String myDbPrefix = dbSetting.schemaName + ".";
sql.execute "DELETE FROM ${myDbPrefix}myTable"
And I got this error:
Ignoring groovy.lang.MissingPropertyException: No such property: myTable for class: java.lang.String
Clearly indicating that . was interpreted as part of variable ${myDbPrefix}.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
转义嵌入变量有帮助吗?
Does escaping the embedded variable help?
今天我就被这个问题困扰了。 GString 在 GroovySQL 中通过特殊方式处理。 javadoc里有提到。它进行自动参数绑定。
GString 中的每个值都将成为一个参数 (?),该参数被设置为 JDBC 准备好的语句参数。
真是一个惊喜!
我将通过子类化 Sql 类并使用普通的 ".toString()" 覆盖 GString 处理来解决应用程序中的问题。
在 Groovy wiki 中记录:
I got hit by this problem today. GStrings get handled by a special way in GroovySQL. It's mentioned in the javadoc. It does automatic parameter binding.
Each value in the GString will become a parameter (?) which gets set as a JDBC prepared statement parameter.
What a surprise!
I'm going to fix the problem in my application by subclassing the Sql class and overriding the GString handling with plain ".toString()" .
Documented in Groovy wiki: