如何使用 SQL 函数和用户提供的输入创建自定义 Diesel 查询?

lud*_*eed 3 sql rust rust-diesel

我想执行一个使用 PostGIS 包的自定义 SQL 函数的查询。例如,我可以使用 psql 运行后续查询:

SELECT * FROM places 
WHERE earth_box(ll_to_earth(40.6333125,-8.659492), 20)
@> ll_to_earth(places.lat, places.lng);
Run Code Online (Sandbox Code Playgroud)

ll_to_earthearth_box是 PostGIS 函数。我怎样才能让这个查询与柴油与这些值latlng作为输入?

我浏览了文档,但我无法理解它。

lud*_*eed 7

这是我最终得到的解决方案:

pub fn get_near(lat: f32, lng: f32, conn: &PgConnection) -> Vec<Places> {
    diesel::sql_query("SELECT * FROM places WHERE earth_box(ll_to_earth($1,$2), 20) @> ll_to_earth(places.lat, places.lng)")
        .bind::<diesel::sql_types::Float, _>(lat)
        .bind::<diesel::sql_types::Float, _>(lng)
        .load(conn)
        .expect("An error has occured")
}
Run Code Online (Sandbox Code Playgroud)