如何在 groovy 的 gstring 数据库查询中执行函数
我希望更多地使用 Groovy 作为一种函数式语言,而不是使用 Java,但似乎有一个问题是当我调用存储过程时,因为我在一次调用中可能传递了 40 个参数,但是,我在我打电话之前,还需要做一些准备工作。
因此,例如,我需要一个时间戳,所以我会有类似的东西(所以可能会有错误,但这是我想要的概念)
def timestamp = (int) (Calendar.instance.timeInMillis/1000)
def ismanager = input.isManager ? 1 : 0
sql.call("{call myfunction($timestamp, ..., $ismanager, ..."})
如果我可以在查询中执行这些类型的调用,那将会很有帮助,因为这会让发生的事情更有意义,因为存储过程中有很多参数,所以必须四处寻找初始化 ise manager
的内容可能会出现问题。
有没有办法让这些函数在调用 gstring 中执行?
I am hoping to use Groovy more as a functional language than I can with Java, but one area that seems to be a problem is when I call to a stored procedure, as I am passing perhaps 40 parameters in a single call, but, I also need to do some prep work, at the moment before I even call.
So, for example, I need a time stamp, so I will have something similar to (so there may be errors, but it is the concept I am going for)
def timestamp = (int) (Calendar.instance.timeInMillis/1000)
def ismanager = input.isManager ? 1 : 0
sql.call("{call myfunction($timestamp, ..., $ismanager, ..."})
It would be helpful if I could do these type of calls inside the query, as it would make more sense what is going on, since there are so many parameters in the stored procedure, so having to look around for what went into initializing ise manager
can be problematic.
Is there a way to have these functions executed within the call gstring?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过以下方式将它们嵌入到字符串中来实现:
然后,由于 SQL 应该忽略不在单词中间的换行符(我绝对不太记得这一点),因此您可以使用三双转义字符串(多么强大的名字):
不会因为长字符串而烦扰自己。
You can do it by embedding them inside the string in this way:
then since SQL should ignore line breaks if not in the middle of a word (I absolutely don't remember this well) you can use triple-double-escaped-strings (what a powerful name):
without annoying yourself with a long string.