我在 Groovy 中遇到一个有趣的问题。希望可以有人帮帮我。
基本上我使用的是 Groovy Sql。我需要选择不同数据库中的多个表。我的第二个查询取决于其他查询,例如:"select * from table1-in-db1 where key = ${result-of-other-query1}
。如果我在函数。但是,我的问题是 sql 是在 xml 文件中定义的,并且在我检索作为字符串传入函数之后。它不再解释内联变量,即使我确实有一个名为 result 的变量-of-other-query1
在范围内。
这是一段 sudo 代码:
doQuery(String squery, String tquery) {
//query db1.
//squery = "select key1 from table1"
db1.rows(squery).each {row->
//query db2.
//tquery ="select * where myKey ='${row.key1}'"
println tquery
tdb.rows(tquery).each { row1 ->
.... // get error here, coz groovy did not replace ${row.key1}
}
}
}
有什么方法可以告诉 Groovy 替换内联变量,即使它是作为字符串传入的?
非常感谢您提前提供的帮助
最佳答案
尝试
tquery = 'select * where myKey =:foo'
tdb.rows(tquery,[foo:"$row.key1"]).each
您可能还想考虑使用eachRow而不是rows.(query).each
关于Groovy,如何传入内联变量,即文件中的 ${variable} 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17131347/