postgis_diesel/
operators.rs

1use diesel::expression::AsExpression;
2use diesel::sql_types::SqlType;
3use diesel::Expression;
4
5diesel::infix_operator!(BBIntersects2D, " && ");
6diesel::infix_operator!(BBOverlapsOrLeft, " &< ");
7diesel::infix_operator!(BBOverlapsOrBelow, " &<| ");
8diesel::infix_operator!(BBOverlapsOrRight, " &> ");
9diesel::infix_operator!(BBStrictlyLeft, " << ");
10diesel::infix_operator!(BBStrictlyBelow, " <<| ");
11diesel::infix_operator!(GSame, " = ");
12diesel::infix_operator!(BBStrictlyRight, " >> ");
13diesel::infix_operator!(BBContainedBy, " @ ");
14diesel::infix_operator!(BBOverlapsOrAbove, " |&> ");
15diesel::infix_operator!(BBStrictlyAbove, " |>> ");
16diesel::infix_operator!(BBContains, " ~ ");
17diesel::infix_operator!(BBSame, " ~= ");
18
19diesel::infix_operator!(Distance2d, " <-> ", diesel::sql_types::Double);
20diesel::infix_operator!(Distance3dTrajectories, " <-> ", diesel::sql_types::Double);
21diesel::infix_operator!(Distance2BBs, " <#> ", diesel::sql_types::Double);
22diesel::infix_operator!(DistanceNdCentroidsBBs, " <<->> ", diesel::sql_types::Double);
23diesel::infix_operator!(DistanceNdBBs, " <<#>> ", diesel::sql_types::Double);
24
25/// The @ operator returns TRUE if the bounding box of geometry A is completely contained by the bounding box of geometry B.
26pub fn contained_by<T, U>(left: T, right: U) -> BBContainedBy<T, U::Expression>
27where
28    T: Expression,
29    <T as diesel::Expression>::SqlType: SqlType,
30    U: AsExpression<T::SqlType>,
31{
32    BBContainedBy::new(left, right.as_expression())
33}
34
35/// The ~ operator returns TRUE if the bounding box of geometry A completely contains the bounding box of geometry B.
36pub fn contains<T, U>(left: T, right: U) -> BBContains<T, U::Expression>
37where
38    T: Expression,
39    <T as diesel::Expression>::SqlType: SqlType,
40    U: AsExpression<T::SqlType>,
41{
42    BBContains::new(left, right.as_expression())
43}
44
45/// The && operator returns TRUE if the 2D bounding box of geometry A intersects the 2D bounding box of geometry B.
46pub fn intersects_2d<T, U>(left: T, right: U) -> BBIntersects2D<T, U::Expression>
47where
48    T: Expression,
49    <T as diesel::Expression>::SqlType: SqlType,
50    U: AsExpression<T::SqlType>,
51{
52    BBIntersects2D::new(left, right.as_expression())
53}
54
55/// The &< operator returns TRUE if the bounding box of geometry A overlaps or is to the left of the bounding box of geometry B, or more accurately, overlaps or is NOT to the right of the bounding box of geometry B.
56pub fn overlaps_or_left<T, U>(left: T, right: U) -> BBOverlapsOrLeft<T, U::Expression>
57where
58    T: Expression,
59    <T as diesel::Expression>::SqlType: SqlType,
60    U: AsExpression<T::SqlType>,
61{
62    BBOverlapsOrLeft::new(left, right.as_expression())
63}
64
65/// The &<| operator returns TRUE if the bounding box of geometry A overlaps or is below of the bounding box of geometry B, or more accurately, overlaps or is NOT above the bounding box of geometry B.
66pub fn overlaps_or_below<T, U>(left: T, right: U) -> BBOverlapsOrBelow<T, U::Expression>
67where
68    T: Expression,
69    <T as diesel::Expression>::SqlType: SqlType,
70    U: AsExpression<T::SqlType>,
71{
72    BBOverlapsOrBelow::new(left, right.as_expression())
73}
74
75/// The &> operator returns TRUE if the bounding box of geometry A overlaps or is to the right of the bounding box of geometry B, or more accurately, overlaps or is NOT to the left of the bounding box of geometry B.
76pub fn overlaps_or_right<T, U>(left: T, right: U) -> BBOverlapsOrRight<T, U::Expression>
77where
78    T: Expression,
79    <T as diesel::Expression>::SqlType: SqlType,
80    U: AsExpression<T::SqlType>,
81{
82    BBOverlapsOrRight::new(left, right.as_expression())
83}
84
85/// The |&> operator returns TRUE if the bounding box of geometry A overlaps or is above the bounding box of geometry B, or more accurately, overlaps or is NOT below the bounding box of geometry B.
86pub fn overlaps_or_above<T, U>(left: T, right: U) -> BBOverlapsOrAbove<T, U::Expression>
87where
88    T: Expression,
89    <T as diesel::Expression>::SqlType: SqlType,
90    U: AsExpression<T::SqlType>,
91{
92    BBOverlapsOrAbove::new(left, right.as_expression())
93}
94
95/// The << operator returns TRUE if the bounding box of geometry A is strictly to the left of the bounding box of geometry B.
96pub fn strictly_left<T, U>(left: T, right: U) -> BBStrictlyLeft<T, U::Expression>
97where
98    T: Expression,
99    <T as diesel::Expression>::SqlType: SqlType,
100    U: AsExpression<T::SqlType>,
101{
102    BBStrictlyLeft::new(left, right.as_expression())
103}
104
105/// The <<| operator returns TRUE if the bounding box of geometry A is strictly below the bounding box of geometry B.
106pub fn strictly_below<T, U>(left: T, right: U) -> BBStrictlyBelow<T, U::Expression>
107where
108    T: Expression,
109    <T as diesel::Expression>::SqlType: SqlType,
110    U: AsExpression<T::SqlType>,
111{
112    BBStrictlyBelow::new(left, right.as_expression())
113}
114
115/// The >> operator returns TRUE if the bounding box of geometry A is strictly to the right of the bounding box of geometry B.
116pub fn strictly_right<T, U>(left: T, right: U) -> BBStrictlyRight<T, U::Expression>
117where
118    T: Expression,
119    <T as diesel::Expression>::SqlType: SqlType,
120    U: AsExpression<T::SqlType>,
121{
122    BBStrictlyRight::new(left, right.as_expression())
123}
124
125/// The |>> operator returns TRUE if the bounding box of geometry A is strictly above the bounding box of geometry B.
126pub fn strictly_above<T, U>(left: T, right: U) -> BBStrictlyAbove<T, U::Expression>
127where
128    T: Expression,
129    <T as diesel::Expression>::SqlType: SqlType,
130    U: AsExpression<T::SqlType>,
131{
132    BBStrictlyAbove::new(left, right.as_expression())
133}
134
135/// The = operator returns TRUE if the coordinates and coordinate order geometry/geography A are the same as the coordinates and coordinate order of geometry/geography B.
136pub fn g_same<T, U>(left: T, right: U) -> GSame<T, U::Expression>
137where
138    T: Expression,
139    <T as diesel::Expression>::SqlType: SqlType,
140    U: AsExpression<T::SqlType>,
141{
142    GSame::new(left, right.as_expression())
143}
144
145/// The ~= operator returns TRUE if the bounding box of geometry/geography A is the same as the bounding box of geometry/geography B.
146pub fn bb_same<T, U>(left: T, right: U) -> BBSame<T, U::Expression>
147where
148    T: Expression,
149    <T as diesel::Expression>::SqlType: SqlType,
150    U: AsExpression<T::SqlType>,
151{
152    BBSame::new(left, right.as_expression())
153}
154
155/// The <-> operator returns the 2D distance between A and B.
156pub fn distance_2d<T, U>(left: T, right: U) -> Distance2d<T, U::Expression>
157where
158    T: Expression,
159    <T as diesel::Expression>::SqlType: SqlType,
160    U: AsExpression<T::SqlType>,
161{
162    Distance2d::new(left, right.as_expression())
163}
164
165/// The |=| operator returns the 3D distance between two trajectories.
166pub fn distance_3d_trajectories<T, U>(left: T, right: U) -> Distance3dTrajectories<T, U::Expression>
167where
168    T: Expression,
169    <T as diesel::Expression>::SqlType: SqlType,
170    U: AsExpression<T::SqlType>,
171{
172    Distance3dTrajectories::new(left, right.as_expression())
173}
174
175/// The <#> operator returns the 2D distance between A and B bounding boxes.
176pub fn distance_2d_bbs<T, U>(left: T, right: U) -> Distance2BBs<T, U::Expression>
177where
178    T: Expression,
179    <T as diesel::Expression>::SqlType: SqlType,
180    U: AsExpression<T::SqlType>,
181{
182    Distance2BBs::new(left, right.as_expression())
183}
184
185/// The <<->> operator returns the n-D distance between the centroids of A and B bounding boxes.
186pub fn distance_nd_centroids_bbs<T, U>(
187    left: T,
188    right: U,
189) -> DistanceNdCentroidsBBs<T, U::Expression>
190where
191    T: Expression,
192    <T as diesel::Expression>::SqlType: SqlType,
193    U: AsExpression<T::SqlType>,
194{
195    DistanceNdCentroidsBBs::new(left, right.as_expression())
196}
197
198/// The <<#>> operator returns the n-D distance between A and B bounding boxes.
199pub fn distance_nd_bbs<T, U>(left: T, right: U) -> DistanceNdBBs<T, U::Expression>
200where
201    T: Expression,
202    <T as diesel::Expression>::SqlType: SqlType,
203    U: AsExpression<T::SqlType>,
204{
205    DistanceNdBBs::new(left, right.as_expression())
206}