如何清理传递到st_形心的sql参数

mcdcgff0  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(378)

我试图清理传递给st_centroid的参数,但我得到了一个语法错误。
st_形心

"SELECT ST_AsText(ST_Centroid(MULTIPOINT ( 0 0, 0 0, 0 0, 0 0 ))) as centroid"

sql = ActiveRecord::Base::sanitize_sql_array(['SELECT ST_AsText(ST_Centroid(MULTIPOINT ( ? ?, ? ?, ? ?, ? ? ))) as centroid', min_longitude, min_latitude, min_longitude, max_latitude, max_longitude, min_latitude, max_longitude, max_latitude])

ActiveRecord::Base.connection.execute(sql)&.to_a&.first["centroid"] || ""

语法错误

from /home/aniket/.rvm/gems/ruby-2.6.5@project/gems/activerecord-6.0.3.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `exec'
Caused by PG::SyntaxError: ERROR:  syntax error at or near "0"
LINE 1: SELECT ST_AsText(ST_Centroid(MULTIPOINT ( 0 0, 0 0, 0 0, 0 0..

如果我不消毒的话,效果很好

sql = "SELECT ST_AsText(ST_Centroid('MULTIPOINT ( #{min_longitude} #{min_latitude}, #{min_longitude} #{max_latitude}, #{max_longitude} #{min_latitude}, #{max_longitude} #{max_latitude} )')) as centroid";

ActiveRecord::Base.connection.execute(sql)&.to_a&.first["centroid"]
   (0.7ms)  SELECT ST_AsText(ST_Centroid('MULTIPOINT ( 0 0, 0 0, 0 0, 0 0 )')) as centroid
=> "POINT(0 0)"
oogrdqng

oogrdqng1#

根据文件 MULTIPOINT 报价人 ' ,我看到你在 sanitize_sql_array ,那么试试这个

sql = ActiveRecord::Base::sanitize_sql_array([
 "SELECT ST_AsText(ST_Centroid('MULTIPOINT ( ? ?, ? ?, ? ?, ? ? )')) as centroid", 
 min_longitude, min_latitude, min_longitude, 
 max_latitude, max_longitude, min_latitude, 
 max_longitude, max_latitude
])

相关问题