如何在 groovy 的 gstring 数据库查询中执行函数

发布于 2024-08-25 06:09:55 字数 498 浏览 12 评论 0原文

我希望更多地使用 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 技术交流群。

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

发布评论

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

评论(1

地狱即天堂 2024-09-01 06:09:55

您可以通过以下方式将它们嵌入到字符串中来实现:

sql.call("{call myfunction(${(int) (Calendar.instance.timeInMillis/1000)}, 
                          ..., ${input.isManager ? 1 : 0}, ...}")

然后,由于 SQL 应该忽略不在单词中间的换行符(我绝对不太记得这一点),因此您可以使用三双转义字符串(多么强大的名字):

"""call myfunction${(int) (Calendar.instance.timeInMillis/1000)}
..., ${input.isManager ? 1 : 0}, ...}"""

不会因为长字符串而烦扰自己。

You can do it by embedding them inside the string in this way:

sql.call("{call myfunction(${(int) (Calendar.instance.timeInMillis/1000)}, 
                          ..., ${input.isManager ? 1 : 0}, ...}")

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):

"""call myfunction${(int) (Calendar.instance.timeInMillis/1000)}
..., ${input.isManager ? 1 : 0}, ...}"""

without annoying yourself with a long string.

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