Skip to content

Commit

Permalink
Refactor geometry handling in quaint visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreilles committed Oct 6, 2024
1 parent 424671a commit 7b24702
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 372 deletions.
8 changes: 8 additions & 0 deletions quaint/src/ast/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ impl<'a> Column<'a> {
}
}

pub(crate) fn into_bare_with_table(self) -> Self {
Self {
name: self.name,
table: self.table,
..Default::default()
}
}

/// Sets the default value for the column.
pub fn default<V>(mut self, value: V) -> Self
where
Expand Down
1 change: 0 additions & 1 deletion quaint/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ impl<'a> ExpressionKind<'a> {
..
})) => true,
Self::Column(c) if c.is_geometry => true,
Self::Function(f) => f.returns_geometry(),
Self::Value(expr) => expr.is_geometry_expr(),
_ => false,
}
Expand Down
23 changes: 0 additions & 23 deletions quaint/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ mod search;
mod sum;
mod upper;

mod geom_as_geojson;
mod geom_as_text;
mod geom_from_geojson;
mod geom_from_text;

mod uuid;

pub use aggregate_to_string::*;
Expand All @@ -43,11 +38,6 @@ pub use search::*;
pub use sum::*;
pub use upper::*;

pub use geom_as_geojson::*;
pub use geom_as_text::*;
pub use geom_from_geojson::*;
pub use geom_from_text::*;

pub use self::uuid::*;

use super::{Aliasable, Expression};
Expand All @@ -70,12 +60,6 @@ impl<'a> Function<'a> {
| FunctionType::JsonExtractFirstArrayElem(_)
)
}
pub fn returns_geometry(&self) -> bool {
matches!(
self.typ_,
FunctionType::GeomFromText(_) | FunctionType::GeomFromGeoJson(_)
)
}
}

/// A database function type
Expand Down Expand Up @@ -104,10 +88,6 @@ pub(crate) enum FunctionType<'a> {
UuidToBin,
UuidToBinSwapped,
Uuid,
GeomAsText(GeomAsText<'a>),
GeomFromText(GeomFromText<'a>),
GeomAsGeoJson(GeomAsGeoJson<'a>),
GeomFromGeoJson(GeomFromGeoJson<'a>),
}

impl<'a> Aliasable<'a> for Function<'a> {
Expand Down Expand Up @@ -153,6 +133,3 @@ function!(
Coalesce,
Concat
);

function!(GeomAsText, GeomFromText);
function!(GeomAsGeoJson, GeomFromGeoJson);
31 changes: 0 additions & 31 deletions quaint/src/ast/function/geom_as_geojson.rs

This file was deleted.

31 changes: 0 additions & 31 deletions quaint/src/ast/function/geom_as_text.rs

This file was deleted.

35 changes: 0 additions & 35 deletions quaint/src/ast/function/geom_from_geojson.rs

This file was deleted.

41 changes: 0 additions & 41 deletions quaint/src/ast/function/geom_from_text.rs

This file was deleted.

14 changes: 14 additions & 0 deletions quaint/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn get_geometry_srid(geom: &geojson::Geometry) -> Option<i32> {

Check failure on line 1 in quaint/src/geometry.rs

View workflow job for this annotation

GitHub Actions / React Native / Android build for commit 2902e7fe71632d28e4e7bcff5068cba7e9c978c8

function `get_geometry_srid` is never used
geom.foreign_members
.as_ref()?
.get("crs")?
.as_object()?
.get("properties")?
.as_object()?
.get("name")?
.as_str()?
.rsplit_once(":")?
.1
.parse()
.ok()
}
2 changes: 2 additions & 0 deletions quaint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ pub mod visitor;
pub use ast::{Value, ValueType};

pub type Result<T> = std::result::Result<T, error::Error>;

mod geometry;
78 changes: 20 additions & 58 deletions quaint/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,6 @@ pub trait Visitor<'a> {

fn visit_json_build_object(&mut self, build_obj: JsonBuildObject<'a>) -> Result;

fn visit_geom_as_text(&mut self, geom: GeomAsText<'a>) -> Result;

fn visit_geom_from_text(&mut self, geom: GeomFromText<'a>) -> Result;

fn visit_geom_as_geojson(&mut self, _geom: GeomAsGeoJson<'a>) -> Result {
unimplemented!("GeomAsGeoJson not supported for the underlying database.")
}

fn visit_geom_from_geojson(&mut self, _geom: GeomFromGeoJson<'a>) -> Result {
unimplemented!("GeomFromGeoJson not supported for the underlying database.")
}

fn visit_geometry_type_equals(&mut self, left: Expression<'a>, right: GeometryType<'a>, not: bool) -> Result;

fn visit_geometry_empty(&mut self, left: Expression<'a>, not: bool) -> Result {
Expand Down Expand Up @@ -225,13 +213,9 @@ pub trait Visitor<'a> {
Ok(())
}

fn visit_parameterized_geometry(&mut self, geometry: geojson::Geometry) -> Result {
self.visit_function(geom_from_geojson(geometry.to_string()))
}
fn visit_parameterized_geometry(&mut self, geometry: geojson::Geometry) -> Result;

fn visit_parameterized_geography(&mut self, geometry: geojson::Geometry) -> Result {
self.visit_function(geom_from_geojson(geometry.to_string()))
}
fn visit_parameterized_geography(&mut self, geometry: geojson::Geometry) -> Result;

/// A visit to a value we parameterize
fn visit_parameterized(&mut self, value: Value<'a>) -> Result {
Expand Down Expand Up @@ -680,12 +664,14 @@ pub trait Visitor<'a> {
ExpressionKind::Default => self.write("DEFAULT")?,
}

if let Some(alias) = value.alias {
self.write(" AS ")?;
self.visit_alias(value.alias)
}

fn visit_alias(&mut self, alias: Option<Cow<'a, str>>) -> Result {
if let Some(alias) = alias {
self.write(" AS ")?;
self.delimited_identifiers(&[&*alias])?;
};

Ok(())
}

Expand Down Expand Up @@ -728,42 +714,30 @@ pub trait Visitor<'a> {
};

if include_alias {
if let Some(alias) = table.alias {
self.write(" AS ")?;

self.delimited_identifiers(&[&*alias])?;
};
self.visit_alias(table.alias)?;
}

Ok(())
}

fn visit_geometry_column(&mut self, mut column: Column<'a>) -> Result {
column.alias = None;
column.is_selected = false;
self.visit_function(geom_as_geojson(column))
}
fn visit_geometry_column(&mut self, column: Column<'a>) -> Result;

/// A database column identifier
fn visit_column(&mut self, column: Column<'a>) -> Result {
let alias = column.alias.clone();
if column.is_geometry && column.is_selected {
self.visit_geometry_column(column)?;
} else {
match column.table {
Some(table) => {
self.visit_table(table, false)?;
self.write(".")?;
self.delimited_identifiers(&[&*column.name])?;
}
_ => self.delimited_identifiers(&[&*column.name])?,
};
return self.visit_geometry_column(column);
}

if let Some(alias) = alias {
self.write(" AS ")?;
self.delimited_identifiers(&[&*alias])?;
}
match column.table {
Some(table) => {
self.visit_table(table, false)?;
self.write(".")?;
self.delimited_identifiers(&[&*column.name])?;
}
_ => self.delimited_identifiers(&[&*column.name])?,
};

self.visit_alias(column.alias)?;

Ok(())
}
Expand Down Expand Up @@ -1217,18 +1191,6 @@ pub trait Visitor<'a> {
FunctionType::JsonBuildObject(build_obj) => {
self.visit_json_build_object(build_obj)?;
}
FunctionType::GeomAsText(geom) => {
self.visit_geom_as_text(geom)?;
}
FunctionType::GeomFromText(geom) => {
self.visit_geom_from_text(geom)?;
}
FunctionType::GeomAsGeoJson(geom) => {
self.visit_geom_as_geojson(geom)?;
}
FunctionType::GeomFromGeoJson(geom) => {
self.visit_geom_from_geojson(geom)?;
}
};

if let Some(alias) = fun.alias {
Expand Down
Loading

0 comments on commit 7b24702

Please sign in to comment.