Skip to main content

sqlx_postgres/
type_checking.rs

1use crate::Postgres;
2
3// The paths used below will also be emitted by the macros so they have to match the final facade.
4#[allow(unused_imports, dead_code)]
5mod sqlx {
6    pub use crate as postgres;
7    pub use sqlx_core::*;
8}
9
10impl_type_checking!(
11        Postgres {
12        (),
13        bool,
14        String | &str,
15        i8,
16        i16,
17        i32,
18        i64,
19        f32,
20        f64,
21        Vec<u8> | &[u8],
22
23        sqlx::postgres::types::Oid,
24
25        sqlx::postgres::types::PgInterval,
26
27        sqlx::postgres::types::PgMoney,
28
29        sqlx::postgres::types::PgLTree,
30
31        sqlx::postgres::types::PgLQuery,
32
33        sqlx::postgres::types::PgCube,
34
35        sqlx::postgres::types::PgPoint,
36
37        sqlx::postgres::types::PgLine,
38
39        sqlx::postgres::types::PgLSeg,
40
41        sqlx::postgres::types::PgBox,
42
43        sqlx::postgres::types::PgPath,
44
45        sqlx::postgres::types::PgPolygon,
46
47        sqlx::postgres::types::PgCircle,
48
49        #[cfg(feature = "uuid")]
50        sqlx::types::Uuid,
51
52        #[cfg(feature = "ipnetwork")]
53        sqlx::types::ipnetwork::IpNetwork,
54
55        #[cfg(feature = "ipnet")]
56        sqlx::types::ipnet::IpNet,
57
58        #[cfg(feature = "mac_address")]
59        sqlx::types::mac_address::MacAddress,
60
61        #[cfg(feature = "json")]
62        sqlx::types::JsonValue,
63
64        #[cfg(feature = "bit-vec")]
65        sqlx::types::BitVec,
66
67        sqlx::postgres::types::PgHstore,
68        // Arrays
69
70        Vec<bool> | &[bool],
71        Vec<String> | &[String],
72        Vec<Vec<u8>> | &[Vec<u8>],
73        Vec<i8> | &[i8],
74        Vec<i16> | &[i16],
75        Vec<i32> | &[i32],
76        Vec<i64> | &[i64],
77        Vec<f32> | &[f32],
78        Vec<f64> | &[f64],
79        Vec<sqlx::postgres::types::Oid> | &[sqlx::postgres::types::Oid],
80        Vec<sqlx::postgres::types::PgMoney> | &[sqlx::postgres::types::PgMoney],
81        Vec<sqlx::postgres::types::PgInterval> | &[sqlx::postgres::types::PgInterval],
82
83        #[cfg(feature = "uuid")]
84        Vec<sqlx::types::Uuid> | &[sqlx::types::Uuid],
85
86        #[cfg(feature = "ipnetwork")]
87        Vec<sqlx::types::ipnetwork::IpNetwork> | &[sqlx::types::ipnetwork::IpNetwork],
88
89        #[cfg(feature = "ipnet")]
90        Vec<sqlx::types::ipnet::IpNet> | &[sqlx::types::ipnet::IpNet],
91
92        #[cfg(feature = "mac_address")]
93        Vec<sqlx::types::mac_address::MacAddress> | &[sqlx::types::mac_address::MacAddress],
94
95        #[cfg(feature = "json")]
96        Vec<sqlx::types::JsonValue> | &[sqlx::types::JsonValue],
97
98        Vec<sqlx::postgres::types::PgHstore> | &[sqlx::postgres::types::PgHstore],
99
100        // Ranges
101
102        sqlx::postgres::types::PgRange<i32>,
103        sqlx::postgres::types::PgRange<i64>,
104
105        // Range arrays
106
107        Vec<sqlx::postgres::types::PgRange<i32>> | &[sqlx::postgres::types::PgRange<i32>],
108        Vec<sqlx::postgres::types::PgRange<i64>> | &[sqlx::postgres::types::PgRange<i64>],
109    },
110    ParamChecking::Strong,
111    feature-types: info => info.__type_feature_gate(),
112    // The expansion of the macro automatically applies the correct feature name
113    // and checks `[macros.preferred-crates]`
114    datetime-types: {
115        chrono: {
116            // Scalar types
117            sqlx::types::chrono::NaiveTime,
118
119            sqlx::types::chrono::NaiveDate,
120
121            sqlx::types::chrono::NaiveDateTime,
122
123            sqlx::types::chrono::DateTime<sqlx::types::chrono::Utc> | sqlx::types::chrono::DateTime<_>,
124
125            sqlx::postgres::types::PgTimeTz<sqlx::types::chrono::NaiveTime, sqlx::types::chrono::FixedOffset>,
126
127            // Array types
128            Vec<sqlx::types::chrono::NaiveTime> | &[sqlx::types::chrono::NaiveTime],
129
130            Vec<sqlx::types::chrono::NaiveDate> | &[sqlx::types::chrono::NaiveDate],
131
132            Vec<sqlx::types::chrono::NaiveDateTime> | &[sqlx::types::chrono::NaiveDateTime],
133
134            Vec<sqlx::types::chrono::DateTime<sqlx::types::chrono::Utc>> | &[sqlx::types::chrono::DateTime<_>],
135
136            // Range types
137            sqlx::postgres::types::PgRange<sqlx::types::chrono::NaiveDate>,
138
139            sqlx::postgres::types::PgRange<sqlx::types::chrono::NaiveDateTime>,
140
141            sqlx::postgres::types::PgRange<sqlx::types::chrono::DateTime<sqlx::types::chrono::Utc>> |
142                sqlx::postgres::types::PgRange<sqlx::types::chrono::DateTime<_>>,
143
144            // Arrays of ranges
145            Vec<sqlx::postgres::types::PgRange<sqlx::types::chrono::NaiveDate>> |
146                &[sqlx::postgres::types::PgRange<sqlx::types::chrono::NaiveDate>],
147
148            Vec<sqlx::postgres::types::PgRange<sqlx::types::chrono::NaiveDateTime>> |
149                &[sqlx::postgres::types::PgRange<sqlx::types::chrono::NaiveDateTime>],
150
151            Vec<sqlx::postgres::types::PgRange<sqlx::types::chrono::DateTime<sqlx::types::chrono::Utc>>> |
152                &[sqlx::postgres::types::PgRange<sqlx::types::chrono::DateTime<_>>],
153        },
154        time: {
155            // Scalar types
156            sqlx::types::time::Time,
157
158            sqlx::types::time::Date,
159
160            sqlx::types::time::PrimitiveDateTime,
161
162            sqlx::types::time::OffsetDateTime,
163
164            sqlx::postgres::types::PgTimeTz<sqlx::types::time::Time, sqlx::types::time::UtcOffset>,
165
166            // Array types
167            Vec<sqlx::types::time::Time> | &[sqlx::types::time::Time],
168
169            Vec<sqlx::types::time::Date> | &[sqlx::types::time::Date],
170
171            Vec<sqlx::types::time::PrimitiveDateTime> | &[sqlx::types::time::PrimitiveDateTime],
172
173            Vec<sqlx::types::time::OffsetDateTime> | &[sqlx::types::time::OffsetDateTime],
174
175            // Range types
176            sqlx::postgres::types::PgRange<sqlx::types::time::Date>,
177
178            sqlx::postgres::types::PgRange<sqlx::types::time::PrimitiveDateTime>,
179
180            sqlx::postgres::types::PgRange<sqlx::types::time::OffsetDateTime>,
181
182            // Arrays of ranges
183            Vec<sqlx::postgres::types::PgRange<sqlx::types::time::Date>> |
184                &[sqlx::postgres::types::PgRange<sqlx::types::time::Date>],
185
186            Vec<sqlx::postgres::types::PgRange<sqlx::types::time::PrimitiveDateTime>> |
187                &[sqlx::postgres::types::PgRange<sqlx::types::time::PrimitiveDateTime>],
188
189            Vec<sqlx::postgres::types::PgRange<sqlx::types::time::OffsetDateTime>> |
190                &[sqlx::postgres::types::PgRange<sqlx::types::time::OffsetDateTime>],
191        },
192    },
193    numeric-types: {
194        bigdecimal: {
195            sqlx::types::BigDecimal,
196
197            Vec<sqlx::types::BigDecimal> | &[sqlx::types::BigDecimal],
198
199            sqlx::postgres::types::PgRange<sqlx::types::BigDecimal>,
200
201            Vec<sqlx::postgres::types::PgRange<sqlx::types::BigDecimal>> |
202                &[sqlx::postgres::types::PgRange<sqlx::types::BigDecimal>],
203        },
204        rust_decimal: {
205            sqlx::types::Decimal,
206
207            Vec<sqlx::types::Decimal> | &[sqlx::types::Decimal],
208
209            sqlx::postgres::types::PgRange<sqlx::types::Decimal>,
210
211            Vec<sqlx::postgres::types::PgRange<sqlx::types::Decimal>> |
212                &[sqlx::postgres::types::PgRange<sqlx::types::Decimal>],
213        },
214    },
215);
216
217#[cfg(all(
218    test,
219    feature = "time",
220    feature = "chrono",
221    feature = "bigdecimal",
222    feature = "rust_decimal"
223))]
224mod tests {
225    use super::*;
226    use crate::PgTypeInfo;
227    use sqlx_core::config::macros::{DateTimeCrate, NumericCrate, PreferredCrates};
228    use sqlx_core::type_checking::{Error, TypeChecking};
229
230    #[test]
231    fn test_preferred_crates_date_time() {
232        let timestamptz = PgTypeInfo::TIMESTAMPTZ;
233
234        // Inferred with both features enabled should return AmbiguousDateTimeType
235        let mut prefs = PreferredCrates::default();
236        assert!(matches!(
237            Postgres::param_type_for_id(&timestamptz, &prefs),
238            Err(Error::AmbiguousDateTimeType { .. })
239        ));
240        assert!(matches!(
241            Postgres::return_type_for_id(&timestamptz, &prefs),
242            Err(Error::AmbiguousDateTimeType { .. })
243        ));
244
245        // Explicit Time should succeed even with chrono enabled
246        prefs.date_time = DateTimeCrate::Time;
247        assert!(Postgres::param_type_for_id(&timestamptz, &prefs).is_ok());
248        assert!(Postgres::return_type_for_id(&timestamptz, &prefs).is_ok());
249
250        // Explicit Chrono should succeed even with time enabled
251        prefs.date_time = DateTimeCrate::Chrono;
252        assert!(Postgres::param_type_for_id(&timestamptz, &prefs).is_ok());
253        assert!(Postgres::return_type_for_id(&timestamptz, &prefs).is_ok());
254    }
255
256    #[test]
257    fn test_preferred_crates_numeric() {
258        let numeric = PgTypeInfo::NUMERIC;
259
260        // Inferred with both features enabled should return AmbiguousNumericType
261        let mut prefs = PreferredCrates::default();
262        assert!(matches!(
263            Postgres::param_type_for_id(&numeric, &prefs),
264            Err(Error::AmbiguousNumericType { .. })
265        ));
266        assert!(matches!(
267            Postgres::return_type_for_id(&numeric, &prefs),
268            Err(Error::AmbiguousNumericType { .. })
269        ));
270
271        // Explicit BigDecimal should succeed even with rust_decimal enabled
272        prefs.numeric = NumericCrate::BigDecimal;
273        assert!(Postgres::param_type_for_id(&numeric, &prefs).is_ok());
274        assert!(Postgres::return_type_for_id(&numeric, &prefs).is_ok());
275
276        // Explicit RustDecimal should succeed even with bigdecimal enabled
277        prefs.numeric = NumericCrate::RustDecimal;
278        assert!(Postgres::param_type_for_id(&numeric, &prefs).is_ok());
279        assert!(Postgres::return_type_for_id(&numeric, &prefs).is_ok());
280    }
281}