Skip to main content

vantage_sql/primitives/
point.rs

1use vantage_expressions::{Expression, Expressive};
2
3/// Vendor-aware spatial point literal.
4///
5/// Renders as:
6/// - **MySQL:**      `ST_GeomFromText('POINT(x y)', srid)`
7/// - **PostgreSQL:** `ST_SetSRID(ST_MakePoint(x, y), srid)`
8/// - **SQLite:**     `MakePoint(x, y, srid)` (SpatiaLite)
9///
10/// Default SRID is 0.
11///
12/// # Examples
13///
14/// ```ignore
15/// Point::new(-0.12, 51.5)        // lon, lat for London
16/// Point::new(-0.12, 51.5).srid(4326)
17/// ```
18#[derive(Debug, Clone)]
19pub struct Point {
20    x: f64,
21    y: f64,
22    srid: i32,
23}
24
25impl Point {
26    /// Create a point with (x, y) coordinates. For geographic data, use (longitude, latitude).
27    pub fn new(x: f64, y: f64) -> Self {
28        Self { x, y, srid: 0 }
29    }
30
31    pub fn srid(mut self, srid: i32) -> Self {
32        self.srid = srid;
33        self
34    }
35}
36
37// -- MySQL: ST_GeomFromText('POINT(x y)', srid) -------------------------------
38
39#[cfg(feature = "mysql")]
40impl Expressive<crate::mysql::types::AnyMysqlType> for Point {
41    fn expr(&self) -> Expression<crate::mysql::types::AnyMysqlType> {
42        Expression::new(
43            format!(
44                "ST_GeomFromText('POINT({} {})', {})",
45                self.x, self.y, self.srid
46            ),
47            vec![],
48        )
49    }
50}
51
52// -- PostgreSQL: ST_SetSRID(ST_MakePoint(x, y), srid) -------------------------
53
54#[cfg(feature = "postgres")]
55impl Expressive<crate::postgres::types::AnyPostgresType> for Point {
56    fn expr(&self) -> Expression<crate::postgres::types::AnyPostgresType> {
57        Expression::new(
58            format!(
59                "ST_SetSRID(ST_MakePoint({}, {}), {})",
60                self.x, self.y, self.srid
61            ),
62            vec![],
63        )
64    }
65}
66
67// -- SQLite: MakePoint(x, y, srid) (SpatiaLite) ------------------------------
68
69#[cfg(feature = "sqlite")]
70impl Expressive<crate::sqlite::types::AnySqliteType> for Point {
71    fn expr(&self) -> Expression<crate::sqlite::types::AnySqliteType> {
72        Expression::new(
73            format!("MakePoint({}, {}, {})", self.x, self.y, self.srid),
74            vec![],
75        )
76    }
77}