【问题标题】:Quotes around dynamic expression Groovy SQL动态表达式 Groovy SQL 周围的引号
【发布时间】:2017-01-18 12:50:57
【问题描述】:

我正在使用 Groovy 对我的数据库进行查询,查询运行良好并返回正确的数据,但是我在终端中收到此错误。

在 Groovy SQL 中,请不要在动态表达式周围使用引号 (以 $ 开头),因为这意味着我们不能使用 JDBC PreparedStatement 等是一个安全漏洞。 Groovy 已经解决了 你的错误,但安全漏洞仍然存在。

这是我的查询

sql.firstRow("""select elem
                        from site_content,
                        lateral jsonb_array_elements(content->'playersContainer'->'series') elem
                        where elem @> '{"id": "${id}"}'
                     """)

如果我将其更改为 $id 或

sql.firstRow("""select elem
                        from site_content,
                        lateral jsonb_array_elements(content->'playersContainer'->'series') elem
                        where elem @> '{"id": ?}'
                     """, id)

我收到以下错误

org.postgresql.util.PSQLException:列索引超出范围: 1、列数:0。

【问题讨论】:

  • 你能不能试试:sql.firstRow("""select elem from site_content, lateral jsonb_array_elements(content->'playersContainer'->'series') elem where elem @> '{"id": ?}' """, [id]), [id] 而不是id

标签: mysql postgresql groovy


【解决方案1】:

Positionalnamed 参数由 groovy sql 正确处理,应使用而不是 "'$id'"

正如@Opal 提到的和here 所述,您应该将参数作为listmap 传递:

sql.execute "select * from tbl where a=? and b=?", [ 'aa', 'bb' ]
sql.execute "select * from tbl where a=:first and b=:last", first: 'aa', last: 'bb'

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多