【发布时间】:2022-01-17 20:36:40
【问题描述】:
我在 Grails 中有这样的查询:
def strQuery = """select date_trunc('${type}', range) as range, sum(total_count) as total_count from connector_message_statistic
where range >= '${startDate}' and range < '${endDate}'
group by date_trunc('${type}', range)
order by 1 asc;"""
我在 catalina 日志中有这个警告:
groovy.sql.Sql.asSql In Groovy SQL please do not use quotes around dynamic expressions (which start with $) as this means we cannot use a JDBC PreparedStatement and so is a security hole. Groovy has worked around your mistake but the security hole is still there. The expression so far is: select date_trunc('?', range) as range, is_internal,direction, sum(total_count) as total_count, sum(total_message_size) as total_message_size
我应该如何摆脱它?问题是动态 date_trunc 参数。 当我尝试这样的事情时:
select date_trunc(:type, range) ....... group by date_trunc(:type, range)
sql.eachRow(strQuery, type: type)
然后我得到这个异常:
ERROR: column "connector_message_statistic.range" must appear in the GROUP BY clause or be used in an aggregate function Position: 23
如何重写这样的查询以避免这些警告?
【问题讨论】: