| 1 |
//! PostgreSQL specific types |
| 2 |
|
| 3 |
mod array; |
| 4 |
#[doc(hidden)] |
| 5 |
pub(in crate::pg) mod date_and_time; |
| 6 |
#[doc(hidden)] |
| 7 |
pub(in crate::pg) mod floats; |
| 8 |
mod integers; |
| 9 |
#[cfg(feature = "ipnet-address")] |
| 10 |
mod ipnet_address; |
| 11 |
#[cfg(feature = "serde_json")] |
| 12 |
mod json; |
| 13 |
mod json_function_enum; |
| 14 |
mod mac_addr; |
| 15 |
mod mac_addr_8; |
| 16 |
#[doc(hidden)] |
| 17 |
pub(in crate::pg) mod money; |
| 18 |
mod multirange; |
| 19 |
#[cfg(feature = "network-address")] |
| 20 |
mod network_address; |
| 21 |
mod numeric; |
| 22 |
pub(in crate::pg) mod pg_lsn; |
| 23 |
mod primitives; |
| 24 |
mod ranges; |
| 25 |
mod record; |
| 26 |
#[cfg(feature = "uuid")] |
| 27 |
mod uuid; |
| 28 |
|
| 29 |
/// PostgreSQL specific SQL types |
| 30 |
/// |
| 31 |
/// Note: All types in this module can be accessed through `diesel::sql_types` |
| 32 |
pub mod sql_types { |
| 33 |
use crate::query_builder::QueryId; |
| 34 |
use crate::sql_types::SqlType; |
| 35 |
|
| 36 |
/// The [`OID`] SQL type. This is a PostgreSQL specific type. |
| 37 |
/// |
| 38 |
/// ### [`ToSql`] impls |
| 39 |
/// |
| 40 |
/// - [`u32`] |
| 41 |
/// |
| 42 |
/// ### [`FromSql`] impls |
| 43 |
/// |
| 44 |
/// - [`u32`] |
| 45 |
/// |
| 46 |
/// [`ToSql`]: crate::serialize::ToSql |
| 47 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 48 |
/// [`u32`]: https://doc.rust-lang.org/nightly/std/primitive.u32.html |
| 49 |
/// [`OID`]: https://www.postgresql.org/docs/current/datatype-oid.html |
| 50 |
#[cfg(feature = "postgres_backend")] |
| 51 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 52 |
#[diesel(postgres_type(oid = 26, array_oid = 1028))] |
| 53 |
pub struct Oid; |
| 54 |
|
| 55 |
/// The ["timestamp with time zone" SQL type][tz], which PostgreSQL abbreviates |
| 56 |
/// to `timestamptz`. |
| 57 |
/// |
| 58 |
/// ### [`ToSql`] impls |
| 59 |
/// |
| 60 |
/// - [`PgTimestamp`] |
| 61 |
/// - [`chrono::NaiveDateTime`] with `feature = "chrono"` |
| 62 |
/// - [`chrono::DateTime`] with `feature = "chrono"` |
| 63 |
/// - [`time::PrimitiveDateTime`] with `feature = "time"` |
| 64 |
/// - [`time::OffsetDateTime`] with `feature = "time"` |
| 65 |
/// |
| 66 |
/// ### [`FromSql`] impls |
| 67 |
/// |
| 68 |
/// - [`PgTimestamp`] |
| 69 |
/// - [`chrono::NaiveDateTime`] with `feature = "chrono"` |
| 70 |
/// - [`chrono::DateTime`] with `feature = "chrono"` |
| 71 |
/// - [`time::PrimitiveDateTime`] with `feature = "time"` |
| 72 |
/// - [`time::OffsetDateTime`] with `feature = "time"` |
| 73 |
/// |
| 74 |
/// [`ToSql`]: crate::serialize::ToSql |
| 75 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 76 |
/// [`PgTimestamp`]: super::super::data_types::PgTimestamp |
| 77 |
/// [tz]: https://www.postgresql.org/docs/current/datatype-datetime.html |
| 78 |
#[cfg_attr( |
| 79 |
feature = "chrono", |
| 80 |
doc = " [`chrono::NaiveDateTime`]: chrono::naive::NaiveDateTime" |
| 81 |
)] |
| 82 |
#[cfg_attr( |
| 83 |
not(feature = "chrono"), |
| 84 |
doc = " [`chrono::NaiveDateTime`]: https://docs.rs/chrono/0.4.19/chrono/naive/struct.NaiveDateTime.html" |
| 85 |
)] |
| 86 |
#[cfg_attr(feature = "chrono", doc = " [`chrono::DateTime`]: chrono::DateTime")] |
| 87 |
#[cfg_attr( |
| 88 |
not(feature = "chrono"), |
| 89 |
doc = " [`chrono::DateTime`]: https://docs.rs/chrono/0.4.19/chrono/struct.DateTime.html" |
| 90 |
)] |
| 91 |
#[cfg_attr( |
| 92 |
feature = "time", |
| 93 |
doc = " [`time::PrimitiveDateTime`]: time::PrimitiveDateTime" |
| 94 |
)] |
| 95 |
#[cfg_attr( |
| 96 |
not(feature = "time"), |
| 97 |
doc = " [`time::PrimitiveDateTime`]: https://docs.rs/time/0.3.9/time/struct.PrimitiveDateTime.html" |
| 98 |
)] |
| 99 |
#[cfg_attr( |
| 100 |
feature = "time", |
| 101 |
doc = " [`time::OffsetDateTime`]: time::OffsetDateTime" |
| 102 |
)] |
| 103 |
#[cfg_attr( |
| 104 |
not(feature = "time"), |
| 105 |
doc = " [`time::OffsetDateTime`]: https://docs.rs/time/0.3.9/time/struct.OffsetDateTime.html" |
| 106 |
)] |
| 107 |
#[cfg(feature = "postgres_backend")] |
| 108 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 109 |
#[diesel(postgres_type(oid = 1184, array_oid = 1185))] |
| 110 |
pub struct Timestamptz; |
| 111 |
|
| 112 |
/// The [`Array`] SQL type. |
| 113 |
/// |
| 114 |
/// This wraps another type to represent a SQL array of that type. |
| 115 |
/// Multidimensional arrays are not supported. |
| 116 |
/// |
| 117 |
/// ### [`ToSql`] impls |
| 118 |
/// |
| 119 |
/// - [`Vec<T>`][Vec] for any `T` which implements `ToSql<ST>` |
| 120 |
/// - [`&[T]`][slice] for any `T` which implements `ToSql<ST>` |
| 121 |
/// |
| 122 |
/// ### [`FromSql`] impls |
| 123 |
/// |
| 124 |
/// - [`Vec<T>`][Vec] for any `T` which implements `ToSql<ST>` |
| 125 |
/// |
| 126 |
/// [`ToSql`]: crate::serialize::ToSql |
| 127 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 128 |
/// [Vec]: std::vec::Vec |
| 129 |
/// [slice]: https://doc.rust-lang.org/nightly/std/primitive.slice.html |
| 130 |
/// [`Array`]: https://www.postgresql.org/docs/current/arrays.html |
| 131 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 132 |
#[cfg(feature = "postgres_backend")] |
| 133 |
pub struct Array<ST: 'static>(ST); |
| 134 |
|
| 135 |
/// The [`Range`] SQL type. |
| 136 |
/// |
| 137 |
/// This wraps another type to represent a SQL range of that type. |
| 138 |
/// |
| 139 |
/// ### [`ToSql`] impls |
| 140 |
/// |
| 141 |
/// - [`(Bound<T>, Bound<T>)`][bound] for any `T` which implements `ToSql<ST>`. |
| 142 |
/// - [`Range<T>`][std::range] (aka `start..end`) for any `T` which implements `ToSql<ST>`. |
| 143 |
/// - [`RangeInclusive<T>`] (aka `start..=end`) for any `T` which implements `ToSql<ST>`. |
| 144 |
/// - [`RangeFrom<T>`] (aka `start..`) for any `T` which implements `ToSql<ST>`. |
| 145 |
/// - [`RangeTo<T>`] (aka `..end`) for any `T` which implements `ToSql<ST>`. |
| 146 |
/// - [`RangeToInclusive<T>`] (aka `..=end`) for any `T` which implements `ToSql<ST>`. |
| 147 |
/// |
| 148 |
/// ### [`FromSql`] impls |
| 149 |
/// |
| 150 |
/// - [`(Bound<T>, Bound<T>)`][bound] for any `T` which implements `FromSql<ST>`. |
| 151 |
/// |
| 152 |
/// [`ToSql`]: crate::serialize::ToSql |
| 153 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 154 |
/// [bound]: std::collections::Bound |
| 155 |
/// [std::range]: std::ops::Range |
| 156 |
/// [`RangeInclusive<T>`]: std::ops::RangeInclusive |
| 157 |
/// [`RangeFrom<T>`]: std::ops::RangeFrom |
| 158 |
/// [`RangeTo<T>`]: std::ops::RangeTo |
| 159 |
/// [`RangeToInclusive<T>`]: std::ops::RangeToInclusive |
| 160 |
/// [`Range`]: https://www.postgresql.org/docs/current/rangetypes.html |
| 161 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 162 |
#[cfg(feature = "postgres_backend")] |
| 163 |
pub struct Range<ST: 'static>(ST); |
| 164 |
|
| 165 |
#[doc(hidden)] |
| 166 |
pub type Int4range = Range<crate::sql_types::Int4>; |
| 167 |
#[doc(hidden)] |
| 168 |
pub type Int8range = Range<crate::sql_types::Int8>; |
| 169 |
#[doc(hidden)] |
| 170 |
pub type Daterange = Range<crate::sql_types::Date>; |
| 171 |
#[doc(hidden)] |
| 172 |
pub type Numrange = Range<crate::sql_types::Numeric>; |
| 173 |
#[doc(hidden)] |
| 174 |
pub type Tsrange = Range<crate::sql_types::Timestamp>; |
| 175 |
#[doc(hidden)] |
| 176 |
pub type Tstzrange = Range<crate::sql_types::Timestamptz>; |
| 177 |
|
| 178 |
/// The [`Multirange`] SQL type. |
| 179 |
/// |
| 180 |
/// This wraps another type to represent a SQL range of that type. |
| 181 |
/// |
| 182 |
/// ### [`ToSql`] impls |
| 183 |
/// |
| 184 |
/// - [`Vec<T>`][Vec] for any `T` which `Range<T>` implements `ToSql<ST>` |
| 185 |
/// - [`&[T]`][slice] for any `T` which `Range<T>` implements `ToSql<ST>` |
| 186 |
/// |
| 187 |
/// ### [`FromSql`] impls |
| 188 |
/// |
| 189 |
/// - [`Vec<T>`][Vec] for any `T` which implements `ToSql<Range<ST>>` |
| 190 |
/// |
| 191 |
/// [`ToSql`]: crate::serialize::ToSql |
| 192 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 193 |
/// [Vec]: std::vec::Vec |
| 194 |
/// [slice]: https://doc.rust-lang.org/nightly/std/primitive.slice.html |
| 195 |
/// [`Multirange`]: https://www.postgresql.org/docs/current/rangetypes.html |
| 196 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 197 |
#[cfg(feature = "postgres_backend")] |
| 198 |
pub struct Multirange<ST: 'static>(ST); |
| 199 |
|
| 200 |
#[doc(hidden)] |
| 201 |
pub type Int4multirange = Multirange<crate::sql_types::Int4>; |
| 202 |
#[doc(hidden)] |
| 203 |
pub type Int8multirange = Multirange<crate::sql_types::Int8>; |
| 204 |
#[doc(hidden)] |
| 205 |
pub type Datemultirange = Multirange<crate::sql_types::Date>; |
| 206 |
#[doc(hidden)] |
| 207 |
pub type Nummultirange = Multirange<crate::sql_types::Numeric>; |
| 208 |
#[doc(hidden)] |
| 209 |
pub type Tsmultirange = Multirange<crate::sql_types::Timestamp>; |
| 210 |
#[doc(hidden)] |
| 211 |
pub type Tstzmultirange = Multirange<crate::sql_types::Timestamptz>; |
| 212 |
|
| 213 |
/// This is a wrapper for [`NullValueTreatment`] to represent null_value_treatment for jsonb_seet_lax: |
| 214 |
/// 'raise_exception' 'use_json_null' 'delete_key' 'return_target' |
| 215 |
/// used in functions jsonb_set_lax |
| 216 |
#[derive(Debug, Clone, Copy, QueryId, SqlType)] |
| 217 |
#[cfg(feature = "postgres_backend")] |
| 218 |
#[diesel(postgres_type(name = "text"))] |
| 219 |
pub struct NullValueTreatmentEnum; |
| 220 |
|
| 221 |
/// Represent null_value_treatment for jsonb_seet_lax: |
| 222 |
/// 'raise_exception' 'use_json_null' 'delete_key' 'return_target' |
| 223 |
/// used in functions jsonb_seet_lax. |
| 224 |
#[derive(Debug, Clone, Copy, diesel_derives::AsExpression)] |
| 225 |
#[diesel(sql_type = NullValueTreatmentEnum)] |
| 226 |
#[allow(clippy::enum_variant_names)] |
| 227 |
pub enum NullValueTreatment { |
| 228 |
/// postgres 'raise_exception' |
| 229 |
RaiseException, |
| 230 |
/// postgres 'use_json_null' |
| 231 |
UseJsonNull, |
| 232 |
/// postgres 'delete_key' |
| 233 |
DeleteKey, |
| 234 |
/// postgres 'return_target' |
| 235 |
ReturnTarget, |
| 236 |
} |
| 237 |
|
| 238 |
/// This is a wrapper for [`RangeBound`] to represent range bounds: '[]', '(]', '[)', '()', |
| 239 |
/// used in functions int4range, int8range, numrange, tsrange, tstzrange, daterange. |
| 240 |
#[derive(Debug, Clone, Copy, QueryId, SqlType)] |
| 241 |
#[cfg(feature = "postgres_backend")] |
| 242 |
#[diesel(postgres_type(name = "text"))] |
| 243 |
pub struct RangeBoundEnum; |
| 244 |
|
| 245 |
/// Represent postgres range bounds: '[]', '(]', '[)', '()', |
| 246 |
/// used in functions int4range, int8range, numrange, tsrange, tstzrange, daterange. |
| 247 |
#[derive(Debug, Clone, Copy, diesel_derives::AsExpression)] |
| 248 |
#[diesel(sql_type = RangeBoundEnum)] |
| 249 |
#[allow(clippy::enum_variant_names)] |
| 250 |
pub enum RangeBound { |
| 251 |
/// postgres '[]' |
| 252 |
LowerBoundInclusiveUpperBoundInclusive, |
| 253 |
/// postgres '[)' |
| 254 |
LowerBoundInclusiveUpperBoundExclusive, |
| 255 |
/// postgres '(]' |
| 256 |
LowerBoundExclusiveUpperBoundInclusive, |
| 257 |
/// postgres '()' |
| 258 |
LowerBoundExclusiveUpperBoundExclusive, |
| 259 |
} |
| 260 |
|
| 261 |
/// The [`Record`] (a.k.a. tuple) SQL type. |
| 262 |
/// |
| 263 |
/// ### [`ToSql`] impls |
| 264 |
/// |
| 265 |
/// - Any tuple which can be serialized to each of the elements |
| 266 |
/// (note: There are major caveats, see the section below) |
| 267 |
/// |
| 268 |
/// ### [`FromSql`] impls |
| 269 |
/// |
| 270 |
/// - Any tuple which can be deserialized from each of the elements. |
| 271 |
/// |
| 272 |
/// [`ToSql`]: crate::serialize::ToSql |
| 273 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 274 |
/// |
| 275 |
/// ### Caveats about serialization |
| 276 |
/// |
| 277 |
/// Typically in the documentation for SQL types, we use "`FromSql` impls" |
| 278 |
/// as a shorthand for "Rust types that you can use to represent this type". |
| 279 |
/// For every other type, that means there is specifically an implementation |
| 280 |
/// of the `FromSql` trait. |
| 281 |
/// |
| 282 |
/// However, PostgreSQL does not support transmission of anonymous record |
| 283 |
/// types as bind parameters. It only supports transmission for named |
| 284 |
/// composite types. For this reason, if you tried to do |
| 285 |
/// `int_tuple_col.eq((1, 2))`, we will generate the SQL `int_tuple_col = |
| 286 |
/// ($1, $2)` rather than `int_tuple_col = $1` as we would for anything |
| 287 |
/// else. |
| 288 |
/// |
| 289 |
/// This should not be visible during normal usage. The only time this would |
| 290 |
/// affect you is if you were attempting to use `sql_query` with tuples. |
| 291 |
/// Your code would not compile in that case, as the `ToSql` trait itself is |
| 292 |
/// not implemented. |
| 293 |
/// |
| 294 |
/// You can implement `ToSql` for named composite types. See [`WriteTuple`] |
| 295 |
/// for details. |
| 296 |
/// |
| 297 |
/// [`WriteTuple`]: super::super::super::serialize::WriteTuple |
| 298 |
/// [`Record`]: https://www.postgresql.org/docs/current/rowtypes.html |
| 299 |
#[cfg(feature = "postgres_backend")] |
| 300 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 301 |
#[diesel(postgres_type(oid = 2249, array_oid = 2287))] |
| 302 |
pub struct Record<ST: 'static>(ST); |
| 303 |
|
| 304 |
/// Alias for [`SmallInt`](crate::sql_types::SmallInt) |
| 305 |
#[cfg(feature = "postgres_backend")] |
| 306 |
pub type SmallSerial = crate::sql_types::SmallInt; |
| 307 |
|
| 308 |
/// Alias for [`Integer`](crate::sql_types::Integer) |
| 309 |
#[cfg(feature = "postgres_backend")] |
| 310 |
pub type Serial = crate::sql_types::Integer; |
| 311 |
|
| 312 |
/// Alias for [`BigInt`](crate::sql_types::BigInt) |
| 313 |
#[cfg(feature = "postgres_backend")] |
| 314 |
pub type BigSerial = crate::sql_types::BigInt; |
| 315 |
|
| 316 |
/// The [`UUID`] SQL type. This type can only be used with `feature = "uuid"` |
| 317 |
/// |
| 318 |
/// ### [`ToSql`] impls |
| 319 |
/// |
| 320 |
/// - [`uuid::Uuid`][Uuid] |
| 321 |
/// |
| 322 |
/// ### [`FromSql`] impls |
| 323 |
/// |
| 324 |
/// - [`uuid::Uuid`][Uuid] |
| 325 |
/// |
| 326 |
/// [`ToSql`]: crate::serialize::ToSql |
| 327 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 328 |
/// [Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html |
| 329 |
/// [`UUID`]: https://www.postgresql.org/docs/current/datatype-uuid.html |
| 330 |
#[cfg(feature = "postgres_backend")] |
| 331 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 332 |
#[diesel(postgres_type(oid = 2950, array_oid = 2951))] |
| 333 |
pub struct Uuid; |
| 334 |
|
| 335 |
/// Alias for `Binary`, to ensure `diesel print-schema` works |
| 336 |
pub type Bytea = crate::sql_types::Binary; |
| 337 |
|
| 338 |
#[doc(hidden)] |
| 339 |
pub type Bpchar = crate::sql_types::VarChar; |
| 340 |
|
| 341 |
/// The PostgreSQL [Money](https://www.postgresql.org/docs/current/static/datatype-money.html) type. |
| 342 |
/// |
| 343 |
/// ### [`ToSql`] impls |
| 344 |
/// |
| 345 |
/// - [`Cents` (also aliased as `PgMoney`)][PgMoney] |
| 346 |
/// |
| 347 |
/// ### [`FromSql`] impls |
| 348 |
/// |
| 349 |
/// - [`Cents` (also aliased as `PgMoney`)][PgMoney] |
| 350 |
/// |
| 351 |
/// [`ToSql`]: crate::serialize::ToSql |
| 352 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 353 |
/// [PgMoney]: crate::data_types::PgMoney |
| 354 |
/// |
| 355 |
/// # Examples |
| 356 |
/// |
| 357 |
/// ```rust |
| 358 |
/// # include!("../../doctest_setup.rs"); |
| 359 |
/// use diesel::data_types::Cents; |
| 360 |
/// |
| 361 |
/// table! { |
| 362 |
/// items { |
| 363 |
/// id -> Integer, |
| 364 |
/// name -> VarChar, |
| 365 |
/// price -> Money, |
| 366 |
/// } |
| 367 |
/// } |
| 368 |
/// |
| 369 |
/// # fn main() { |
| 370 |
/// # use diesel::insert_into; |
| 371 |
/// # use self::items::dsl::*; |
| 372 |
/// # let connection = &mut connection_no_data(); |
| 373 |
/// # diesel::sql_query("CREATE TABLE items ( |
| 374 |
/// # id SERIAL PRIMARY KEY, |
| 375 |
/// # name VARCHAR NOT NULL, |
| 376 |
/// # price MONEY NOT NULL |
| 377 |
/// # )").execute(connection).unwrap(); |
| 378 |
/// let inserted_price = insert_into(items) |
| 379 |
/// .values((name.eq("Shiny Thing"), price.eq(Cents(123_456)))) |
| 380 |
/// .returning(price) |
| 381 |
/// .get_result(connection); |
| 382 |
/// assert_eq!(Ok(Cents(123_456)), inserted_price); |
| 383 |
/// # } |
| 384 |
/// ``` |
| 385 |
#[cfg(feature = "postgres_backend")] |
| 386 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 387 |
#[diesel(postgres_type(oid = 790, array_oid = 791))] |
| 388 |
pub struct Money; |
| 389 |
|
| 390 |
/// The [`MACADDR`](https://www.postgresql.org/docs/current/static/datatype-net-types.html) SQL type. |
| 391 |
/// |
| 392 |
/// ### [`ToSql`] impls |
| 393 |
/// |
| 394 |
/// - `[u8; 6]` |
| 395 |
/// |
| 396 |
/// ### [`FromSql`] impls |
| 397 |
/// |
| 398 |
/// - `[u8; 6]` |
| 399 |
/// |
| 400 |
/// [`ToSql`]: crate::serialize::ToSql |
| 401 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 402 |
/// |
| 403 |
/// # Examples |
| 404 |
/// |
| 405 |
/// ```rust |
| 406 |
/// # include!("../../doctest_setup.rs"); |
| 407 |
/// table! { |
| 408 |
/// devices { |
| 409 |
/// id -> Integer, |
| 410 |
/// macaddr -> MacAddr, |
| 411 |
/// } |
| 412 |
/// } |
| 413 |
/// |
| 414 |
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 415 |
/// # use diesel::insert_into; |
| 416 |
/// # use self::devices::dsl::*; |
| 417 |
/// # let connection = &mut connection_no_data(); |
| 418 |
/// # diesel::sql_query("CREATE TABLE devices ( |
| 419 |
/// # id SERIAL PRIMARY KEY, |
| 420 |
/// # macaddr MACADDR NOT NULL |
| 421 |
/// # )").execute(connection)?; |
| 422 |
/// let inserted_macaddr = insert_into(devices) |
| 423 |
/// .values(macaddr.eq([0x08, 0x00, 0x2b, 0x01, 0x02, 0x03])) |
| 424 |
/// .returning(macaddr) |
| 425 |
/// .get_result::<[u8; 6]>(connection)?; |
| 426 |
/// assert_eq!([0x08, 0x00, 0x2b, 0x01, 0x02, 0x03], inserted_macaddr); |
| 427 |
/// # Ok(()) |
| 428 |
/// # } |
| 429 |
/// ``` |
| 430 |
#[cfg(feature = "postgres_backend")] |
| 431 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 432 |
#[diesel(postgres_type(oid = 829, array_oid = 1040))] |
| 433 |
pub struct MacAddr; |
| 434 |
|
| 435 |
/// Alias for `MacAddr` to be able to use it with `diesel print-schema`. |
| 436 |
pub type Macaddr = MacAddr; |
| 437 |
|
| 438 |
/// The [`MACADDR8`](https://www.postgresql.org/docs/current/static/datatype-net-types.html) SQL type. |
| 439 |
/// |
| 440 |
/// ### [`ToSql`] impls |
| 441 |
/// |
| 442 |
/// - `[u8; 8]` |
| 443 |
/// |
| 444 |
/// ### [`FromSql`] impls |
| 445 |
/// |
| 446 |
/// - `[u8; 8]` |
| 447 |
/// |
| 448 |
/// [`ToSql`]: crate::serialize::ToSql |
| 449 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 450 |
/// |
| 451 |
/// # Examples |
| 452 |
/// |
| 453 |
/// ```rust |
| 454 |
/// # include!("../../doctest_setup.rs"); |
| 455 |
/// table! { |
| 456 |
/// devices { |
| 457 |
/// id -> Integer, |
| 458 |
/// macaddr -> MacAddr8, |
| 459 |
/// } |
| 460 |
/// } |
| 461 |
/// |
| 462 |
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 463 |
/// # use diesel::insert_into; |
| 464 |
/// # use self::devices::dsl::*; |
| 465 |
/// # let connection = &mut connection_no_data(); |
| 466 |
/// # diesel::sql_query("CREATE TABLE devices ( |
| 467 |
/// # id SERIAL PRIMARY KEY, |
| 468 |
/// # macaddr MACADDR8 NOT NULL |
| 469 |
/// # )").execute(connection)?; |
| 470 |
/// let inserted_macaddr = insert_into(devices) |
| 471 |
/// .values(macaddr.eq([0x08, 0x00, 0x2b, 0x01, 0x02, 0x03, 0x04, 0x05])) |
| 472 |
/// .returning(macaddr) |
| 473 |
/// .get_result::<[u8; 8]>(connection)?; |
| 474 |
/// assert_eq!( |
| 475 |
/// [0x08, 0x00, 0x2b, 0x01, 0x02, 0x03, 0x04, 0x05], |
| 476 |
/// inserted_macaddr |
| 477 |
/// ); |
| 478 |
/// # Ok(()) |
| 479 |
/// # } |
| 480 |
/// ``` |
| 481 |
#[cfg(feature = "postgres_backend")] |
| 482 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 483 |
#[diesel(postgres_type(oid = 774, array_oid = 775))] |
| 484 |
pub struct MacAddr8; |
| 485 |
|
| 486 |
/// Alias for `MacAddr` to be able to use it with `diesel print-schema`. |
| 487 |
pub type Macaddr8 = MacAddr8; |
| 488 |
|
| 489 |
/// The [`INET`](https://www.postgresql.org/docs/current/static/datatype-net-types.html) SQL type. This type can only be used with `feature = "network-address"` or `feature = "ipnet-address"`. |
| 490 |
/// |
| 491 |
/// ### [`ToSql`] impls |
| 492 |
#[cfg_attr( |
| 493 |
feature = "network-address", |
| 494 |
doc = " - [`ipnetwork::IpNetwork`][IpNetwork]" |
| 495 |
)] |
| 496 |
#[cfg_attr(feature = "ipnet-address", doc = " - [`ipnet::IpNet`][IpNet]")] |
| 497 |
#[cfg_attr( |
| 498 |
not(any(feature = "network-address", feature = "ipnet-address")), |
| 499 |
doc = "N/A" |
| 500 |
)] |
| 501 |
/// |
| 502 |
/// ### [`FromSql`] impls |
| 503 |
#[cfg_attr( |
| 504 |
feature = "network-address", |
| 505 |
doc = " - [`ipnetwork::IpNetwork`][IpNetwork]" |
| 506 |
)] |
| 507 |
#[cfg_attr(feature = "ipnet-address", doc = " - [`ipnet::IpNet`][IpNet]")] |
| 508 |
#[cfg_attr( |
| 509 |
not(any(feature = "network-address", feature = "ipnet-address")), |
| 510 |
doc = "N/A" |
| 511 |
)] |
| 512 |
/// |
| 513 |
/// [`ToSql`]: crate::serialize::ToSql |
| 514 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 515 |
#[cfg_attr( |
| 516 |
feature = "network-address", |
| 517 |
doc = " [IpNetwork]: ipnetwork::IpNetwork" |
| 518 |
)] |
| 519 |
#[cfg_attr(feature = "ipnet-address", doc = " [IpNet]: ipnet::IpNet")] |
| 520 |
/// |
| 521 |
/// # Examples |
| 522 |
/// |
| 523 |
/// ```rust |
| 524 |
/// # include!("../../doctest_setup.rs"); |
| 525 |
/// # |
| 526 |
/// table! { |
| 527 |
/// clients { |
| 528 |
/// id -> Integer, |
| 529 |
/// ip_address -> Inet, |
| 530 |
/// } |
| 531 |
/// } |
| 532 |
/// |
| 533 |
/// # #[cfg(any(feature = "network-address", feature = "ipnet-address"))] |
| 534 |
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 535 |
/// |
| 536 |
/// # use diesel::insert_into; |
| 537 |
/// # use self::clients::dsl::*; |
| 538 |
/// # let connection = &mut connection_no_data(); |
| 539 |
/// # diesel::sql_query("CREATE TABLE clients ( |
| 540 |
/// # id SERIAL PRIMARY KEY, |
| 541 |
/// # ip_address INET NOT NULL |
| 542 |
/// # )").execute(connection)?; |
| 543 |
/// // Parsing "ipnet::IpNet" would also work. |
| 544 |
/// let addr = "10.1.9.32/32".parse::<ipnetwork::IpNetwork>()?; |
| 545 |
/// let inserted_address = insert_into(clients) |
| 546 |
/// .values(ip_address.eq(&addr)) |
| 547 |
/// .returning(ip_address) |
| 548 |
/// .get_result(connection)?; |
| 549 |
/// assert_eq!(addr, inserted_address); |
| 550 |
/// # Ok(()) |
| 551 |
/// # } |
| 552 |
/// # |
| 553 |
/// # #[cfg(not(any(feature = "network-address", feature = "ipnet-address")))] |
| 554 |
/// # fn main() {} |
| 555 |
/// ``` |
| 556 |
#[cfg(feature = "postgres_backend")] |
| 557 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 558 |
#[diesel(postgres_type(oid = 869, array_oid = 1041))] |
| 559 |
pub struct Inet; |
| 560 |
|
| 561 |
/// The [`CIDR`](https://www.postgresql.org/docs/postgresql/static/datatype-net-types.html) SQL type. This type can only be used with `feature = "network-address"` or `feature = "ipnet-address"`. |
| 562 |
/// |
| 563 |
/// ### [`ToSql`] impls |
| 564 |
#[cfg_attr( |
| 565 |
feature = "network-address", |
| 566 |
doc = " - [`ipnetwork::IpNetwork`][IpNetwork]" |
| 567 |
)] |
| 568 |
#[cfg_attr(feature = "ipnet-address", doc = " - [`ipnet::IpNet`][IpNet]")] |
| 569 |
#[cfg_attr( |
| 570 |
not(any(feature = "network-address", feature = "ipnet-address")), |
| 571 |
doc = "N/A" |
| 572 |
)] |
| 573 |
/// |
| 574 |
/// ### [`FromSql`] impls |
| 575 |
#[cfg_attr( |
| 576 |
feature = "network-address", |
| 577 |
doc = " - [`ipnetwork::IpNetwork`][IpNetwork]" |
| 578 |
)] |
| 579 |
#[cfg_attr(feature = "ipnet-address", doc = " - [`ipnet::IpNet`][IpNet]")] |
| 580 |
#[cfg_attr( |
| 581 |
not(any(feature = "network-address", feature = "ipnet-address")), |
| 582 |
doc = "N/A" |
| 583 |
)] |
| 584 |
/// |
| 585 |
/// [`ToSql`]: crate::serialize::ToSql |
| 586 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 587 |
#[cfg_attr( |
| 588 |
feature = "network-address", |
| 589 |
doc = " [IpNetwork]: ipnetwork::IpNetwork" |
| 590 |
)] |
| 591 |
#[cfg_attr(feature = "ipnet-address", doc = " [IpNet]: ipnet::IpNet")] |
| 592 |
/// |
| 593 |
/// # Examples |
| 594 |
/// |
| 595 |
/// ```rust |
| 596 |
/// # #![allow(dead_code)] |
| 597 |
/// # include!("../../doctest_setup.rs"); |
| 598 |
/// table! { |
| 599 |
/// clients { |
| 600 |
/// id -> Integer, |
| 601 |
/// ip_address -> Cidr, |
| 602 |
/// } |
| 603 |
/// } |
| 604 |
/// |
| 605 |
/// # #[cfg(any(feature = "network-address", feature = "ipnet-address"))] |
| 606 |
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 607 |
/// |
| 608 |
/// # use diesel::insert_into; |
| 609 |
/// # use self::clients::dsl::*; |
| 610 |
/// # let connection = &mut connection_no_data(); |
| 611 |
/// # diesel::sql_query("CREATE TABLE clients ( |
| 612 |
/// # id SERIAL PRIMARY KEY, |
| 613 |
/// # ip_address CIDR NOT NULL |
| 614 |
/// # )").execute(connection)?; |
| 615 |
/// // Parsing "ipnet::IpNet" would also work. |
| 616 |
/// let addr = "10.1.9.32/32".parse::<ipnetwork::IpNetwork>()?; |
| 617 |
/// let inserted_addr = insert_into(clients) |
| 618 |
/// .values(ip_address.eq(&addr)) |
| 619 |
/// .returning(ip_address) |
| 620 |
/// .get_result(connection)?; |
| 621 |
/// assert_eq!(addr, inserted_addr); |
| 622 |
/// # Ok(()) |
| 623 |
/// # } |
| 624 |
/// # #[cfg(not(any(feature = "network-address", feature = "ipnet-address")))] |
| 625 |
/// # fn main() {} |
| 626 |
/// ``` |
| 627 |
#[cfg(feature = "postgres_backend")] |
| 628 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 629 |
#[diesel(postgres_type(oid = 650, array_oid = 651))] |
| 630 |
pub struct Cidr; |
| 631 |
|
| 632 |
/// The [`"char"`] SQL type. This is a PostgreSQL specific type. Used for e.g. [setweight]. [Do not use in user tables]. |
| 633 |
/// |
| 634 |
/// ### [`ToSql`] impls |
| 635 |
/// |
| 636 |
/// - [`u8`] |
| 637 |
/// |
| 638 |
/// ### [`FromSql`] impls |
| 639 |
/// |
| 640 |
/// - [`u8`] |
| 641 |
/// |
| 642 |
/// [`ToSql`]: crate::serialize::ToSql |
| 643 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 644 |
/// [`u8`]: https://doc.rust-lang.org/nightly/std/primitive.u8.html |
| 645 |
/// [`"char"`]: https://www.postgresql.org/docs/current/datatype-character.html#DATATYPE-CHARACTER-SPECIAL-TABLE |
| 646 |
/// [setweight]: https://www.postgresql.org/docs/current/functions-textsearch.html |
| 647 |
/// [Do not use in user tables]: https://www.postgresql.org/docs/current/datatype-character.html#DATATYPE-CHARACTER-SPECIAL-TABLE |
| 648 |
#[cfg(feature = "postgres_backend")] |
| 649 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 650 |
#[diesel(postgres_type(oid = 18, array_oid = 1002))] |
| 651 |
pub struct CChar; |
| 652 |
|
| 653 |
/// The [`Citext`] SQL type. This is a PostgreSQL specific type. |
| 654 |
/// |
| 655 |
/// Strings must be valid UTF-8. |
| 656 |
/// |
| 657 |
/// ### [`ToSql`] impls |
| 658 |
/// |
| 659 |
/// - [`String`] |
| 660 |
/// - [`&str`][str] |
| 661 |
/// |
| 662 |
/// ### [`FromSql`] impls |
| 663 |
/// |
| 664 |
/// - [`String`] |
| 665 |
/// |
| 666 |
/// [`ToSql`]: crate::serialize::ToSql |
| 667 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 668 |
/// [`Citext`]: https://www.postgresql.org/docs/current/citext.html |
| 669 |
#[cfg(feature = "postgres_backend")] |
| 670 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 671 |
#[diesel(postgres_type(name = "citext"))] |
| 672 |
pub struct Citext; |
| 673 |
|
| 674 |
/// The [`pg_lsn`] SQL type. This is a PostgreSQL specific type. Encodes a position in the PostgreSQL *Write Ahead Log* (WAL). |
| 675 |
/// |
| 676 |
/// ### [`ToSql`] impls |
| 677 |
/// |
| 678 |
/// - [`u64`] |
| 679 |
/// |
| 680 |
/// ### [`FromSql`] impls |
| 681 |
/// |
| 682 |
/// - [`u64`] |
| 683 |
/// |
| 684 |
/// [`ToSql`]: crate::serialize::ToSql |
| 685 |
/// [`FromSql`]: crate::deserialize::FromSql |
| 686 |
/// [`u64`]: https://doc.rust-lang.org/nightly/std/primitive.u64.html |
| 687 |
/// [`pg_lsn`]: https://www.postgresql.org/docs/current/datatype-pg-lsn.html |
| 688 |
#[cfg(feature = "postgres_backend")] |
| 689 |
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] |
| 690 |
#[diesel(postgres_type(oid = 3220, array_oid = 3221))] |
| 691 |
pub struct PgLsn; |
| 692 |
|
| 693 |
#[doc(inline)] |
| 694 |
pub use crate::sql_types::Jsonb; |
| 695 |
} |
| 696 |
|
| 697 |
mod ops { |
| 698 |
use super::sql_types::*; |
| 699 |
use crate::sql_types::ops::*; |
| 700 |
use crate::sql_types::{Bigint, Interval}; |
| 701 |
|
| 702 |
impl Add for Timestamptz { |
| 703 |
type Rhs = Interval; |
| 704 |
type Output = Timestamptz; |
| 705 |
} |
| 706 |
|
| 707 |
impl Sub for Timestamptz { |
| 708 |
type Rhs = Interval; |
| 709 |
type Output = Timestamptz; |
| 710 |
} |
| 711 |
|
| 712 |
impl Add for Cidr { |
| 713 |
type Rhs = Bigint; |
| 714 |
type Output = Inet; |
| 715 |
} |
| 716 |
|
| 717 |
impl Add for Inet { |
| 718 |
type Rhs = Bigint; |
| 719 |
type Output = Inet; |
| 720 |
} |
| 721 |
|
| 722 |
impl Sub for Cidr { |
| 723 |
type Rhs = Bigint; |
| 724 |
type Output = Inet; |
| 725 |
} |
| 726 |
|
| 727 |
impl Sub for Inet { |
| 728 |
type Rhs = Bigint; |
| 729 |
type Output = Inet; |
| 730 |
} |
| 731 |
} |