【问题标题】:How do I create a custom Diesel query using SQL functions with user-provided inputs?如何使用 SQL 函数和用户提供的输入创建自定义 Diesel 查询?
【发布时间】:2019-05-04 22:27:18
【问题描述】:

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

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

ll_to_earthearth_box 是 PostGIS 函数。如何使用 Diesel 以 latlng 的值作为输入进行此查询?

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

【问题讨论】:

    标签: sql rust rust-diesel


    【解决方案1】:

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

    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")
    }
    

    【讨论】:

      【解决方案2】:

      Searching the Diesel documentation for function 直接导致sql_function 宏:

      Diesel 仅提供对极少数 SQL 函数的支持。此宏使您能够添加来自 SQL 标准的其他函数,以及您的应用程序可能具有的任何自定义函数。

      【讨论】:

        猜你喜欢
        • 2011-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-11
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多