Trait sqlx::Type

source ยท
pub trait Type<DB>
where DB: Database,
{ // Required method fn type_info() -> <DB as Database>::TypeInfo; // Provided method fn compatible(ty: &<DB as Database>::TypeInfo) -> bool { ... } }
Expand description

Indicates that a SQL type is supported for a database.

ยงCompile-time verification

With compile-time verification, the use of type overrides is currently required to make use of any user-defined types.

โ“˜
struct MyUser { id: UserId, name: String }

// fetch all properties from user and override the type in Rust for `id`
let user = query_as!(MyUser, r#"SELECT users.*, id as "id: UserId" FROM users"#)
    .fetch_one(&pool).await?;

ยงDerivable

This trait can be derived by SQLx to support Rust-only wrapper types, enumerations, and (for postgres) structured records. Additionally, an implementation of Encode and Decode is generated.

ยงTransparent

Rust-only domain wrappers around SQL types. The generated implementations directly delegate to the implementation of the inner type.

โ“˜
#[derive(sqlx::Type)]
#[sqlx(transparent)]
struct UserId(i64);
ยงNote: PgHasArrayType

If you have the postgres feature enabled, this derive also generates a PgHasArrayType impl so that you may use it with Vec and other types that decode from an array in Postgres:

โ“˜
let user_ids: Vec<UserId> = sqlx::query_scalar("select '{ 123, 456 }'::int8[]")
   .fetch(&mut pg_connection)
   .await?;

However, if you are wrapping a type that does not implement PgHasArrayType (e.g. Vec itself, because we donโ€™t currently support multidimensional arrays), you may receive an error:

โ“˜
#[derive(sqlx::Type)] // ERROR: `Vec<i64>` does not implement `PgHasArrayType`
#[sqlx(transparent)]
struct UserIds(Vec<i64>);

To remedy this, add #[sqlx(no_pg_array)], which disables the generation of the PgHasArrayType impl:

โ“˜
#[derive(sqlx::Type)]
#[sqlx(transparent, no_pg_array)]
struct UserIds(Vec<i64>);
ยงAttributes
  • #[sqlx(type_name = "<SQL type name>")] on struct definition: instead of inferring the SQL type name from the inner field (in the above case, BIGINT), explicitly set it to <SQL type name> instead. May trigger errors or unexpected behavior if the encoding of the given type is different than that of the inferred type (e.g. if you rename the above to VARCHAR). Affects Postgres only.
  • #[sqlx(rename_all = "<strategy>")] on struct definition: See derive docs in FromRow
  • #[sqlx(no_pg_array)]: do not emit a PgHasArrayType impl (see above).

ยงEnumeration

Enumerations may be defined in Rust and can match SQL by integer discriminant or variant name.

With #[repr(_)] the integer representation is used when converting from/to SQL and expects that SQL type (e.g., INT). Without, the names of the variants are used instead and expects a textual SQL type (e.g., VARCHAR, TEXT).

โ“˜
#[derive(sqlx::Type)]
#[repr(i32)]
enum Color { Red = 1, Green = 2, Blue = 3 }
โ“˜
#[derive(sqlx::Type)]
#[sqlx(type_name = "color")] // only for PostgreSQL to match a type definition
#[sqlx(rename_all = "lowercase")]
enum Color { Red, Green, Blue }

ยงRecords

User-defined composite types are supported through deriving a struct.

This is only supported for PostgreSQL.

โ“˜
#[derive(sqlx::Type)]
#[sqlx(type_name = "interface_type")]
struct InterfaceType {
    name: String,
    supplier_id: i32,
    price: f64
}

Required Methodsยง

source

fn type_info() -> <DB as Database>::TypeInfo

Returns the canonical SQL type for this Rust type.

When binding arguments, this is used to tell the database what is about to be sent; which, the database then uses to guide query plans. This can be overridden by Encode::produces.

A map of SQL types to Rust types is populated with this and used to determine the type that is returned from the anonymous struct type from query!.

Provided Methodsยง

source

fn compatible(ty: &<DB as Database>::TypeInfo) -> bool

Determines if this Rust type is compatible with the given SQL type.

When decoding values from a row, this method is checked to determine if we should continue or raise a runtime type mismatch error.

When binding arguments with query! or query_as!, this method is consulted to determine if the Rust type is acceptable.

Defaults to checking TypeInfo::type_compatible().

Object Safetyยง

This trait is not object safe.

Implementations on Foreign Typesยง

sourceยง

impl Type<Any> for bool

sourceยง

impl Type<Any> for f32

sourceยง

impl Type<Any> for f64

sourceยง

impl Type<Any> for i16

sourceยง

impl Type<Any> for i32

sourceยง

impl Type<Any> for i64

sourceยง

impl Type<Any> for str

sourceยง

impl Type<Any> for String

sourceยง

impl Type<Any> for Vec<u8>

sourceยง

impl Type<Any> for [u8]

sourceยง

impl Type<MySql> for Cow<'_, str>

sourceยง

impl Type<MySql> for IpAddr

sourceยง

impl Type<MySql> for bool

sourceยง

impl Type<MySql> for f32

sourceยง

impl Type<MySql> for f64

sourceยง

impl Type<MySql> for i8

sourceยง

impl Type<MySql> for i16

sourceยง

impl Type<MySql> for i32

sourceยง

impl Type<MySql> for i64

sourceยง

impl Type<MySql> for str

sourceยง

impl Type<MySql> for u8

sourceยง

impl Type<MySql> for u16

sourceยง

impl Type<MySql> for u32

sourceยง

impl Type<MySql> for u64

sourceยง

impl Type<MySql> for Box<str>

sourceยง

impl Type<MySql> for Box<[u8]>

sourceยง

impl Type<MySql> for String

sourceยง

impl Type<MySql> for Vec<u8>

sourceยง

impl Type<MySql> for Ipv4Addr

sourceยง

impl Type<MySql> for Ipv6Addr

sourceยง

impl Type<MySql> for Duration

sourceยง

impl Type<MySql> for TimeDelta

sourceยง

impl Type<MySql> for Duration

sourceยง

impl Type<MySql> for [u8]

sourceยง

impl Type<Postgres> for Cow<'_, str>

sourceยง

impl Type<Postgres> for IpAddr

sourceยง

impl Type<Postgres> for bool

sourceยง

impl Type<Postgres> for f32

sourceยง

impl Type<Postgres> for f64

sourceยง

impl Type<Postgres> for i8

sourceยง

impl Type<Postgres> for i16

sourceยง

impl Type<Postgres> for i32

sourceยง

impl Type<Postgres> for i64

sourceยง

impl Type<Postgres> for str

sourceยง

impl Type<Postgres> for ()

sourceยง

impl Type<Postgres> for Box<str>

sourceยง

impl Type<Postgres> for String

sourceยง

impl Type<Postgres> for Duration

sourceยง

impl Type<Postgres> for TimeDelta

sourceยง

impl Type<Postgres> for Duration

sourceยง

impl Type<Sqlite> for Cow<'_, str>

sourceยง

impl Type<Sqlite> for bool

sourceยง

impl Type<Sqlite> for f32

sourceยง

impl Type<Sqlite> for f64

sourceยง

impl Type<Sqlite> for i8

sourceยง

impl Type<Sqlite> for i16

sourceยง

impl Type<Sqlite> for i32

sourceยง

impl Type<Sqlite> for i64

sourceยง

impl Type<Sqlite> for str

sourceยง

impl Type<Sqlite> for u8

sourceยง

impl Type<Sqlite> for u16

sourceยง

impl Type<Sqlite> for u32

sourceยง

impl Type<Sqlite> for u64

sourceยง

impl Type<Sqlite> for Box<str>

sourceยง

impl Type<Sqlite> for Box<[u8]>

sourceยง

impl Type<Sqlite> for String

sourceยง

impl Type<Sqlite> for Vec<u8>

sourceยง

impl Type<Sqlite> for [u8]

sourceยง

impl<DB> Type<DB> for NonZero<i8>
where DB: Database, i8: Type<DB>,

sourceยง

impl<DB> Type<DB> for NonZero<i16>
where DB: Database, i16: Type<DB>,

sourceยง

impl<DB> Type<DB> for NonZero<i32>
where DB: Database, i32: Type<DB>,

sourceยง

impl<DB> Type<DB> for NonZero<i64>
where DB: Database, i64: Type<DB>,

sourceยง

impl<DB> Type<DB> for NonZero<u8>
where DB: Database, u8: Type<DB>,

sourceยง

impl<DB> Type<DB> for NonZero<u16>
where DB: Database, u16: Type<DB>,

sourceยง

impl<DB> Type<DB> for NonZero<u32>
where DB: Database, u32: Type<DB>,

sourceยง

impl<DB> Type<DB> for NonZero<u64>
where DB: Database, u64: Type<DB>,

sourceยง

impl<T1> Type<Postgres> for (T1,)

sourceยง

impl<T1, T2> Type<Postgres> for (T1, T2)

sourceยง

impl<T1, T2, T3> Type<Postgres> for (T1, T2, T3)

sourceยง

impl<T1, T2, T3, T4> Type<Postgres> for (T1, T2, T3, T4)

sourceยง

impl<T1, T2, T3, T4, T5> Type<Postgres> for (T1, T2, T3, T4, T5)

sourceยง

impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for (T1, T2, T3, T4, T5, T6)

sourceยง

impl<T1, T2, T3, T4, T5, T6, T7> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7)

sourceยง

impl<T1, T2, T3, T4, T5, T6, T7, T8> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8)

sourceยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)

sourceยง

impl<T> Type<Postgres> for [T]
where T: PgHasArrayType,

sourceยง

impl<T> Type<Postgres> for Vec<T>
where T: PgHasArrayType,

sourceยง

impl<T, DB> Type<DB> for Option<T>
where T: Type<DB>, DB: Database,

sourceยง

impl<T, DB> Type<DB> for &T
where T: Type<DB> + ?Sized, DB: Database,

sourceยง

impl<T, const N: usize> Type<Postgres> for [T; N]
where T: PgHasArrayType,

Implementorsยง

sourceยง

impl Type<MySql> for MySqlTime

sourceยง

impl Type<MySql> for DateTime<Local>

sourceยง

impl Type<MySql> for DateTime<Utc>

sourceยง

impl Type<MySql> for NaiveDate

sourceยง

impl Type<MySql> for NaiveDateTime

sourceยง

impl Type<MySql> for NaiveTime

sourceยง

impl Type<MySql> for BigDecimal

sourceยง

impl Type<MySql> for Decimal

sourceยง

impl Type<MySql> for Uuid

sourceยง

impl Type<MySql> for Date

sourceยง

impl Type<MySql> for OffsetDateTime

sourceยง

impl Type<MySql> for PrimitiveDateTime

sourceยง

impl Type<MySql> for Time

sourceยง

impl Type<MySql> for Hyphenated

sourceยง

impl Type<MySql> for Simple

sourceยง

impl Type<Postgres> for PgCube

sourceยง

impl Type<Postgres> for IpNetwork

sourceยง

impl Type<Postgres> for Oid

sourceยง

impl Type<Postgres> for PgCiText

sourceยง

impl Type<Postgres> for PgHstore

sourceยง

impl Type<Postgres> for PgInterval

sourceยง

impl Type<Postgres> for PgLQuery

sourceยง

impl Type<Postgres> for PgLTree

sourceยง

impl Type<Postgres> for PgMoney

sourceยง

impl Type<Postgres> for PgRange<i32>

sourceยง

impl Type<Postgres> for PgRange<i64>

sourceยง

impl Type<Postgres> for PgRange<NaiveDate>

sourceยง

impl Type<Postgres> for PgRange<NaiveDateTime>

sourceยง

impl Type<Postgres> for PgRange<BigDecimal>

sourceยง

impl Type<Postgres> for PgRange<Decimal>

sourceยง

impl Type<Postgres> for PgRange<Date>

sourceยง

impl Type<Postgres> for PgRange<OffsetDateTime>

sourceยง

impl Type<Postgres> for PgRange<PrimitiveDateTime>

sourceยง

impl Type<Postgres> for PgTimeTz

sourceยง

impl Type<Postgres> for PgTimeTz<NaiveTime, FixedOffset>

sourceยง

impl Type<Postgres> for NaiveDate

sourceยง

impl Type<Postgres> for NaiveDateTime

sourceยง

impl Type<Postgres> for NaiveTime

sourceยง

impl Type<Postgres> for MacAddress

sourceยง

impl Type<Postgres> for BigDecimal

sourceยง

impl Type<Postgres> for BitVec

sourceยง

impl Type<Postgres> for Decimal

sourceยง

impl Type<Postgres> for Uuid

sourceยง

impl Type<Postgres> for Date

sourceยง

impl Type<Postgres> for OffsetDateTime

sourceยง

impl Type<Postgres> for PrimitiveDateTime

sourceยง

impl Type<Postgres> for Time

sourceยง

impl Type<Sqlite> for NaiveDate

sourceยง

impl Type<Sqlite> for NaiveDateTime

sourceยง

impl Type<Sqlite> for NaiveTime

sourceยง

impl Type<Sqlite> for Uuid

sourceยง

impl Type<Sqlite> for Date

sourceยง

impl Type<Sqlite> for OffsetDateTime

sourceยง

impl Type<Sqlite> for PrimitiveDateTime

sourceยง

impl Type<Sqlite> for Time

sourceยง

impl Type<Sqlite> for Hyphenated

sourceยง

impl Type<Sqlite> for Simple

sourceยง

impl<DB> Type<DB> for Value
where Json<Value>: Type<DB>, DB: Database,

sourceยง

impl<DB> Type<DB> for RawValue
where Json<&'a RawValue>: for<'a> Type<DB>, DB: Database,

sourceยง

impl<T> Type<MySql> for Json<T>

sourceยง

impl<T> Type<MySql> for Text<T>

sourceยง

impl<T> Type<Postgres> for Json<T>

sourceยง

impl<T> Type<Postgres> for Text<T>

sourceยง

impl<T> Type<Sqlite> for Json<T>

sourceยง

impl<T> Type<Sqlite> for Text<T>

sourceยง

impl<Tz> Type<Postgres> for PgRange<DateTime<Tz>>
where Tz: TimeZone,

sourceยง

impl<Tz> Type<Postgres> for DateTime<Tz>
where Tz: TimeZone,

sourceยง

impl<Tz> Type<Sqlite> for DateTime<Tz>
where Tz: TimeZone,