将参数引用到Golang的sqlc中。

huangapple go评论84阅读模式
英文:

quoted parameters to golang sqlc

问题

使用sqlc生成数据库方法。以下是要翻译的查询:

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL '30' SECOND);

我想将30作为参数传递,但是我使用了以下查询,然后引号没有出现,数据库报错。

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL $1 SECOND);

尝试引用$1,但是sqlc认为它是字符串。

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL '$1' SECOND);
英文:

using sqlc to generate db methods.
Have below query

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated &lt; (CURRENT_TIMESTAMP - INTERVAL &#39;30&#39; SECOND);

I want to pass 30 as parameter but I used below query, then quotes are not coming and db giving error.

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated &lt; (CURRENT_TIMESTAMP - INTERVAL $1 SECOND);

Tried quoting $1 but sqlc thinking it is string

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated &lt; (CURRENT_TIMESTAMP - INTERVAL &#39;$1&#39; SECOND);

答案1

得分: 2

或者,而不是在尝试欺骗类型系统时纠缠不清,只需执行以下操作:

... - ($1 * interval '1 minute')
英文:

Or rather than mucking around trying to trick the type system just do

... - ($1 * interval &#39;1 minute&#39;)

huangapple
  • 本文由 发表于 2023年6月10日 17:39:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76445642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定