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#[cfg(feature = "mysql")]
38impl Expressive<crate::mysql::types::AnyMysqlType> for Point {
39    fn expr(&self) -> Expression<crate::mysql::types::AnyMysqlType> {
40        Expression::new(
41            format!(
42                "ST_GeomFromText('POINT({} {})', {})",
43                self.x, self.y, self.srid
44            ),
45            vec![],
46        )
47    }
48}
49
50#[cfg(feature = "postgres")]
51impl Expressive<crate::postgres::types::AnyPostgresType> for Point {
52    fn expr(&self) -> Expression<crate::postgres::types::AnyPostgresType> {
53        Expression::new(
54            format!(
55                "ST_SetSRID(ST_MakePoint({}, {}), {})",
56                self.x, self.y, self.srid
57            ),
58            vec![],
59        )
60    }
61}
62
63#[cfg(feature = "sqlite")]
64impl Expressive<crate::sqlite::types::AnySqliteType> for Point {
65    fn expr(&self) -> Expression<crate::sqlite::types::AnySqliteType> {
66        Expression::new(
67            format!("MakePoint({}, {}, {})", self.x, self.y, self.srid),
68            vec![],
69        )
70    }
71}