Debian Codegraph @

src/types/mod.rs

raw symbols SCIP
1 //! Conversions between Rust and **Postgres** types.
2 //!
3 //! # Types
4 //!
5 //! | Rust type | Postgres type(s) |
6 //! |---------------------------------------|------------------------------------------------------|
7 //! | `bool` | BOOL |
8 //! | `i8` | "CHAR" |
9 //! | `i16` | SMALLINT, SMALLSERIAL, INT2 |
10 //! | `i32` | INT, SERIAL, INT4 |
11 //! | `i64` | BIGINT, BIGSERIAL, INT8 |
12 //! | `f32` | REAL, FLOAT4 |
13 //! | `f64` | DOUBLE PRECISION, FLOAT8 |
14 //! | `&str`, [`String`] | VARCHAR, CHAR(N), TEXT, NAME, CITEXT |
15 //! | `&[u8]`, `Vec<u8>` | BYTEA |
16 //! | `()` | VOID |
17 //! | [`PgInterval`] | INTERVAL |
18 //! | [`PgRange<T>`](PgRange) | INT8RANGE, INT4RANGE, TSRANGE, TSTZRANGE, DATERANGE, NUMRANGE |
19 //! | [`PgMoney`] | MONEY |
20 //! | [`PgLTree`] | LTREE |
21 //! | [`PgLQuery`] | LQUERY |
22 //! | [`PgCiText`] | CITEXT<sup>1</sup> |
23 //! | [`PgCube`] | CUBE |
24 //! | [`PgPoint] | POINT |
25 //! | [`PgLine] | LINE |
26 //! | [`PgHstore`] | HSTORE |
27 //!
28 //! <sup>1</sup> SQLx generally considers `CITEXT` to be compatible with `String`, `&str`, etc.,
29 //! but this wrapper type is available for edge cases, such as `CITEXT[]` which Postgres
30 //! does not consider to be compatible with `TEXT[]`.
31 //!
32 //! ### [`bigdecimal`](https://crates.io/crates/bigdecimal)
33 //! Requires the `bigdecimal` Cargo feature flag.
34 //!
35 //! | Rust type | Postgres type(s) |
36 //! |---------------------------------------|------------------------------------------------------|
37 //! | `bigdecimal::BigDecimal` | NUMERIC |
38 //!
39 #![doc=include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("bigdecimal-range.md")]
40 //!
41 //! ### [`rust_decimal`](https://crates.io/crates/rust_decimal)
42 //! Requires the `rust_decimal` Cargo feature flag.
43 //!
44 //! | Rust type | Postgres type(s) |
45 //! |---------------------------------------|------------------------------------------------------|
46 //! | `rust_decimal::Decimal` | NUMERIC |
47 //!
48 #![doc=include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("rust_decimal-range.md")]
49 //!
50 //! ### [`chrono`](https://crates.io/crates/chrono)
51 //!
52 //! Requires the `chrono` Cargo feature flag.
53 //!
54 //! | Rust type | Postgres type(s) |
55 //! |---------------------------------------|------------------------------------------------------|
56 //! | `chrono::DateTime<Utc>` | TIMESTAMPTZ |
57 //! | `chrono::DateTime<Local>` | TIMESTAMPTZ |
58 //! | `chrono::NaiveDateTime` | TIMESTAMP |
59 //! | `chrono::NaiveDate` | DATE |
60 //! | `chrono::NaiveTime` | TIME |
61 //! | [`PgTimeTz`] | TIMETZ |
62 //!
63 //! ### [`time`](https://crates.io/crates/time)
64 //!
65 //! Requires the `time` Cargo feature flag.
66 //!
67 //! | Rust type | Postgres type(s) |
68 //! |---------------------------------------|------------------------------------------------------|
69 //! | `time::PrimitiveDateTime` | TIMESTAMP |
70 //! | `time::OffsetDateTime` | TIMESTAMPTZ |
71 //! | `time::Date` | DATE |
72 //! | `time::Time` | TIME |
73 //! | [`PgTimeTz`] | TIMETZ |
74 //!
75 //! ### [`uuid`](https://crates.io/crates/uuid)
76 //!
77 //! Requires the `uuid` Cargo feature flag.
78 //!
79 //! | Rust type | Postgres type(s) |
80 //! |---------------------------------------|------------------------------------------------------|
81 //! | `uuid::Uuid` | UUID |
82 //!
83 //! ### [`ipnetwork`](https://crates.io/crates/ipnetwork)
84 //!
85 //! Requires the `ipnetwork` Cargo feature flag.
86 //!
87 //! | Rust type | Postgres type(s) |
88 //! |---------------------------------------|------------------------------------------------------|
89 //! | `ipnetwork::IpNetwork` | INET, CIDR |
90 //! | `std::net::IpAddr` | INET, CIDR |
91 //!
92 //! Note that because `IpAddr` does not support network prefixes, it is an error to attempt to decode
93 //! an `IpAddr` from a `INET` or `CIDR` value with a network prefix smaller than the address' full width:
94 //! `/32` for IPv4 addresses and `/128` for IPv6 addresses.
95 //!
96 //! `IpNetwork` does not have this limitation.
97 //!
98 //! ### [`mac_address`](https://crates.io/crates/mac_address)
99 //!
100 //! Requires the `mac_address` Cargo feature flag.
101 //!
102 //! | Rust type | Postgres type(s) |
103 //! |---------------------------------------|------------------------------------------------------|
104 //! | `mac_address::MacAddress` | MACADDR |
105 //!
106 //! ### [`bit-vec`](https://crates.io/crates/bit-vec)
107 //!
108 //! Requires the `bit-vec` Cargo feature flag.
109 //!
110 //! | Rust type | Postgres type(s) |
111 //! |---------------------------------------|------------------------------------------------------|
112 //! | `bit_vec::BitVec` | BIT, VARBIT |
113 //!
114 //! ### [`json`](https://crates.io/crates/serde_json)
115 //!
116 //! Requires the `json` Cargo feature flag.
117 //!
118 //! | Rust type | Postgres type(s) |
119 //! |---------------------------------------|------------------------------------------------------|
120 //! | [`Json<T>`] | JSON, JSONB |
121 //! | `serde_json::Value` | JSON, JSONB |
122 //! | `&serde_json::value::RawValue` | JSON, JSONB |
123 //!
124 //! `Value` and `RawValue` from `serde_json` can be used for unstructured JSON data with
125 //! Postgres.
126 //!
127 //! [`Json<T>`](crate::types::Json) can be used for structured JSON data with Postgres.
128 //!
129 //! # [Composite types](https://www.postgresql.org/docs/current/rowtypes.html)
130 //!
131 //! User-defined composite types are supported through a derive for `Type`.
132 //!
133 //! ```text
134 //! CREATE TYPE inventory_item AS (
135 //! name text,
136 //! supplier_id integer,
137 //! price numeric
138 //! );
139 //! ```
140 //!
141 //! ```rust,ignore
142 //! #[derive(sqlx::Type)]
143 //! #[sqlx(type_name = "inventory_item")]
144 //! struct InventoryItem {
145 //! name: String,
146 //! supplier_id: i32,
147 //! price: BigDecimal,
148 //! }
149 //! ```
150 //!
151 //! Anonymous composite types are represented as tuples. Note that anonymous composites may only
152 //! be returned and not sent to Postgres (this is a limitation of postgres).
153 //!
154 //! # Arrays
155 //!
156 //! One-dimensional arrays are supported as `Vec<T>` or `&[T]` where `T` implements `Type`.
157 //!
158 //! # [Enumerations](https://www.postgresql.org/docs/current/datatype-enum.html)
159 //!
160 //! User-defined enumerations are supported through a derive for `Type`.
161 //!
162 //! ```text
163 //! CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
164 //! ```
165 //!
166 //! ```rust,ignore
167 //! #[derive(sqlx::Type)]
168 //! #[sqlx(type_name = "mood", rename_all = "lowercase")]
169 //! enum Mood { Sad, Ok, Happy }
170 //! ```
171 //!
172 //! Rust enumerations may also be defined to be represented as an integer using `repr`.
173 //! The following type expects a SQL type of `INTEGER` or `INT4` and will convert to/from the
174 //! Rust enumeration.
175 //!
176 //! ```rust,ignore
177 //! #[derive(sqlx::Type)]
178 //! #[repr(i32)]
179 //! enum Mood { Sad = 0, Ok = 1, Happy = 2 }
180 //! ```
181 //!
182
183 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::type_inforust-analyzer cargo sqlx-postgres 0.8.3 type_info/::PgTypeKindrust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeKind#;
184 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::{PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#};
185
186 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::typesrust-analyzer cargo sqlx-core 0.8.3 types/::{Jsonrust-analyzer cargo sqlx-core 0.8.3 types/json/Json#, Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#};
187
188 mod arrayrust-analyzer cargo sqlx-postgres 0.8.3 types/array/;
189 mod boolrust-analyzer cargo sqlx-postgres 0.8.3 types/bool/;
190 mod bytesrust-analyzer cargo sqlx-postgres 0.8.3 types/bytes/;
191 mod citextrust-analyzer cargo sqlx-postgres 0.8.3 types/citext/;
192 mod floatrust-analyzer cargo sqlx-postgres 0.8.3 types/float/;
193 mod hstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/;
194 mod intrust-analyzer cargo sqlx-postgres 0.8.3 types/int/;
195 mod intervalrust-analyzer cargo sqlx-postgres 0.8.3 types/interval/;
196 mod lqueryrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/;
197 mod ltreerust-analyzer cargo sqlx-postgres 0.8.3 types/ltree/;
198 // Not behind a Cargo feature because we require JSON in the driver implementation.
199 mod jsonrust-analyzer cargo sqlx-postgres 0.8.3 types/json/;
200 mod moneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/;
201 mod oidrust-analyzer cargo sqlx-postgres 0.8.3 types/oid/;
202 mod rangerust-analyzer cargo sqlx-postgres 0.8.3 types/range/;
203 mod recordrust-analyzer cargo sqlx-postgres 0.8.3 types/record/;
204 mod strrust-analyzer cargo sqlx-postgres 0.8.3 types/str/;
205 mod textrust-analyzer cargo sqlx-postgres 0.8.3 types/text/;
206 mod tuplerust-analyzer cargo sqlx-postgres 0.8.3 types/tuple/;
207 mod voidrust-analyzer cargo sqlx-postgres 0.8.3 types/void/;
208
209 #[cfg(any(feature = "chrono", feature = "time"))]
210 mod time_tzrust-analyzer cargo sqlx-postgres 0.8.3 types/time_tz/;
211
212 #[cfg(feature = "bigdecimal")]
213 mod bigdecimalrust-analyzer cargo sqlx-postgres 0.8.3 types/bigdecimal/;
214
215 mod cuberust-analyzer cargo sqlx-postgres 0.8.3 types/cube/;
216
217 mod geometryrust-analyzer cargo sqlx-postgres 0.8.3 types/geometry/;
218
219 #[cfg(any(feature = "bigdecimal", feature = "rust_decimal"))]
220 mod numericrust-analyzer cargo sqlx-postgres 0.8.3 types/numeric/;
221
222 #[cfg(feature = "rust_decimal")]
223 mod rust_decimalrust-analyzer cargo sqlx-postgres 0.8.3 types/rust_decimal/;
224
225 #[cfg(feature = "chrono")]
226 mod chronorust-analyzer cargo sqlx-postgres 0.8.3 types/chrono/;
227
228 #[cfg(feature = "time")]
229 mod timerust-analyzer cargo sqlx-postgres 0.8.3 types/time/;
230
231 #[cfg(feature = "uuid")]
232 mod uuidrust-analyzer cargo sqlx-postgres 0.8.3 types/uuid/;
233
234 #[cfg(feature = "ipnetwork")]
235 mod ipnetworkrust-analyzer cargo sqlx-postgres 0.8.3 types/ipnetwork/;
236
237 #[cfg(feature = "ipnetwork")]
238 mod ipaddrrust-analyzer cargo sqlx-postgres 0.8.3 types/ipaddr/;
239
240 #[cfg(feature = "mac_address")]
241 mod mac_addressrust-analyzer cargo sqlx-postgres 0.8.3 types/mac_address/;
242
243 #[cfg(feature = "bit-vec")]
244 mod bit_vecrust-analyzer cargo sqlx-postgres 0.8.3 types/bit_vec/;
245
246 pub use arrayrust-analyzer cargo sqlx-postgres 0.8.3 types/array/::PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#;
247 pub use citextrust-analyzer cargo sqlx-postgres 0.8.3 types/citext/::PgCiTextrust-analyzer cargo sqlx-postgres 0.8.3 types/citext/PgCiText#;
248 pub use cuberust-analyzer cargo sqlx-postgres 0.8.3 types/cube/::PgCuberust-analyzer cargo sqlx-postgres 0.8.3 types/cube/PgCube#;
249 pub use geometryrust-analyzer cargo sqlx-postgres 0.8.3 types/geometry/::linerust-analyzer cargo sqlx-postgres 0.8.3 types/geometry/line/::PgLinerust-analyzer cargo sqlx-postgres 0.8.3 types/geometry/line/PgLine#;
250 pub use geometryrust-analyzer cargo sqlx-postgres 0.8.3 types/geometry/::pointrust-analyzer cargo sqlx-postgres 0.8.3 types/geometry/point/::PgPointrust-analyzer cargo sqlx-postgres 0.8.3 types/geometry/point/PgPoint#;
251 pub use hstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/::PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#;
252 pub use intervalrust-analyzer cargo sqlx-postgres 0.8.3 types/interval/::PgIntervalrust-analyzer cargo sqlx-postgres 0.8.3 types/interval/PgInterval#;
253 pub use lqueryrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/::PgLQueryrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/PgLQuery#;
254 pub use lqueryrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/::PgLQueryLevelrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/PgLQueryLevel#;
255 pub use lqueryrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/::PgLQueryVariantrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/PgLQueryVariant#;
256 pub use lqueryrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/::PgLQueryVariantFlagrust-analyzer cargo sqlx-postgres 0.8.3 types/lquery/PgLQueryVariantFlag#;
257 pub use ltreerust-analyzer cargo sqlx-postgres 0.8.3 types/ltree/::PgLTreerust-analyzer cargo sqlx-postgres 0.8.3 types/ltree/PgLTree#;
258 pub use ltreerust-analyzer cargo sqlx-postgres 0.8.3 types/ltree/::PgLTreeLabelrust-analyzer cargo sqlx-postgres 0.8.3 types/ltree/PgLTreeLabel#;
259 pub use ltreerust-analyzer cargo sqlx-postgres 0.8.3 types/ltree/::PgLTreeParseErrorrust-analyzer cargo sqlx-postgres 0.8.3 types/ltree/PgLTreeParseError#;
260 pub use moneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/::PgMoneyrust-analyzer cargo sqlx-postgres 0.8.3 types/money/PgMoney#;
261 pub use oidrust-analyzer cargo sqlx-postgres 0.8.3 types/oid/::Oidrust-analyzer cargo sqlx-postgres 0.8.3 types/oid/Oid#;
262 pub use rangerust-analyzer cargo sqlx-postgres 0.8.3 types/range/::PgRangerust-analyzer cargo sqlx-postgres 0.8.3 types/range/PgRange#;
263
264 #[cfg(any(feature = "chrono", feature = "time"))]
265 pub use time_tzrust-analyzer cargo sqlx-postgres 0.8.3 types/time_tz/::PgTimeTzrust-analyzer cargo sqlx-postgres 0.8.3 types/time_tz/PgTimeTz#;
266
267 // used in derive(Type) for `struct`
268 // but the interface is not considered part of the public API
269 #[doc(hidden)]
270 pub use recordrust-analyzer cargo sqlx-postgres 0.8.3 types/record/::{PgRecordDecoderrust-analyzer cargo sqlx-postgres 0.8.3 types/record/PgRecordDecoder#, PgRecordEncoderrust-analyzer cargo sqlx-postgres 0.8.3 types/record/PgRecordEncoder#};
271
272 // Type::compatible impl appropriate for arrays
273 fn array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array_compatible().<E: Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(ty: &PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#) -> bool {
274 // we require the declared type to be an _array_ with an
275 // element type that is acceptable
276 if let PgTypeKindrust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeKind#::Arrayrust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeKind#Array#(element) = &ty.kindrust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]kind().() {
277 return E::compatiblerust-analyzer cargo sqlx-core 0.8.3 types/Type#compatible().(element);
278 }
279
280 false
281 }