Skip to content

Commit

Permalink
Merge pull request #2 from brokenhandsio/distanceFromModel
Browse files Browse the repository at this point in the history
Add ability to provide distance from another DB field
  • Loading branch information
0xTim authored Sep 19, 2019
2 parents 93f82df + 55cfad5 commit c13f032
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Sources/FluentPostGIS/Queries/QueryBuilder+DistanceWithin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,23 @@ extension QueryBuilder where
return filterGeographyDistanceWithin(Database.queryField(.keyPath(key)), Database.queryFilterValueGeographic(filter), Database.queryFilterValue([value]))
}

@discardableResult
public func filterGeographyDistanceWithin<T,V, OtherResult>(_ key: KeyPath<Result, T>, _ filter: V, _ valueKey: KeyPath<OtherResult, Double>) -> Self
where T: GeometryConvertible, V: GeometryConvertible, OtherResult: Model, OtherResult.Database == Database
{
return filterGeographyDistanceWithin(Database.queryField(.keyPath(key)), Database.queryFilterValueGeographic(filter), Database.queryField(.keyPath(valueKey)))
}

@discardableResult
private func filterGeographyDistanceWithin(_ field: Database.QueryField, _ filter: Database.QueryFilterValue, _ value: Database.QueryFilterValue) -> Self {
return self.filter(custom: Database.filterGeographyDistanceWithin(field, filter, value))
}

@discardableResult
private func filterGeographyDistanceWithin(_ field: Database.QueryField, _ filter: Database.QueryFilterValue, _ value: Database.QueryField) -> Self {
return self.filter(custom: Database.filterGeographyDistanceWithin(field, filter, value))
}

}

extension QuerySupporting where
Expand Down Expand Up @@ -91,4 +103,13 @@ extension QuerySupporting where
] as! [QueryFilter.Function.Argument]
return .function("ST_DWithin", args)
}

public static func filterGeographyDistanceWithin(_ field: QueryField, _ filter: QueryFilterValue, _ valueField: QueryField) -> QueryFilter {
let args: [QueryFilter.Function.Argument] = [
GenericSQLFunctionArgument<PostgreSQLExpression>.expression(PostgreSQLExpression.column(field as! PostgreSQLColumnIdentifier)),
GenericSQLFunctionArgument<PostgreSQLExpression>.expression(filter as! PostgreSQLExpression),
GenericSQLFunctionArgument<PostgreSQLExpression>.expression(PostgreSQLExpression.column(valueField as! PostgreSQLColumnIdentifier)),
] as! [QueryFilter.Function.Argument]
return .function("ST_DWithin", args)
}
}

0 comments on commit c13f032

Please sign in to comment.