[][src]Trait sqlx::Type

pub trait Type<DB> where
    DB: Database
{ pub fn type_info() -> <DB as Database>::TypeInfo; pub fn compatible(ty: &<DB as Database>::TypeInfo) -> bool { ... } }

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.

β“˜This example is not tested
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 or wrappers around SQL types. The generated implementations directly delegate to the implementation of the inner type.

β“˜This example is not tested
#[derive(sqlx::Type)]
#[sqlx(transparent)]
struct UserId(i64);
Attributes
  • #[sqlx(rename = "<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.

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).

β“˜This example is not tested
#[derive(sqlx::Type)]
#[repr(i32)]
enum Color { Red = 1, Green = 2, Blue = 3 }
β“˜This example is not tested
#[derive(sqlx::Type)]
#[sqlx(rename = "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.

β“˜This example is not tested
#[derive(sqlx::Type)]
#[sqlx(rename = "interface_type")]
struct InterfaceType {
    name: String,
    supplier_id: i32,
    price: f64
}

Required methods

pub fn type_info() -> <DB as Database>::TypeInfo[src]

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!.

Loading content...

Provided methods

pub fn compatible(ty: &<DB as Database>::TypeInfo) -> bool[src]

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.

Loading content...

Implementations on Foreign Types

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

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

impl Type<Postgres> for [u8][src]

impl Type<Mssql> for i16[src]

impl Type<Postgres> for Duration[src]

impl Type<Postgres> for i64[src]

impl Type<Postgres> for [String][src]

impl Type<Postgres> for [Vec<u8, Global>][src]

impl<T> Type<Postgres> for [Json<T>][src]

impl<'_> Type<Postgres> for [&'_ [u8]][src]

impl Type<Postgres> for Vec<NaiveTime, Global>[src]

impl Type<Mssql> for i64[src]

impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6), Global>[src]

impl Type<Postgres> for [Duration][src]

impl Type<Postgres> for [Duration][src]

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

impl Type<Postgres> for i32[src]

impl Type<Postgres> for Vec<BitVec<u32>, Global>[src]

impl Type<Postgres> for [BitVec<u32>][src]

impl Type<Postgres> for Vec<PgMoney, Global>[src]

impl Type<Postgres> for i8[src]

impl Type<MySql> for str[src]

impl Type<MySql> for i32[src]

impl Type<Postgres> for Vec<PgRange<NaiveDateTime>, Global>[src]

impl Type<Postgres> for Vec<PgRange<Date>, Global>[src]

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

impl Type<Mssql> for str[src]

impl<'_> Type<Postgres> for [&'_ str][src]

impl Type<MySql> for u16[src]

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

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

impl Type<Postgres> for Vec<PgRange<OffsetDateTime>, Global>[src]

impl<T1, T2, T3, T4, T5> Type<Postgres> for Vec<(T1, T2, T3, T4, T5), Global>[src]

impl Type<MySql> for u64[src]

impl Type<Sqlite> for i64[src]

impl Type<Sqlite> for i16[src]

impl<Tz> Type<Postgres> for [PgRange<DateTime<Tz>>] where
    Tz: TimeZone
[src]

impl Type<Any> for f32 where
    f32: AnyType
[src]

impl Type<MySql> for bool[src]

impl Type<Sqlite> for f32[src]

impl Type<Postgres> for Vec<PgRange<BigDecimal>, Global>[src]

impl Type<Any> for i64 where
    i64: AnyType
[src]

impl Type<Postgres> for Vec<u8, Global>[src]

impl Type<Postgres> for [f32][src]

impl Type<MySql> for f64[src]

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

impl Type<Postgres> for Vec<PrimitiveDateTime, Global>[src]

impl Type<Postgres> for [PgRange<PrimitiveDateTime>][src]

impl Type<Postgres> for Vec<Uuid, Global>[src]

impl Type<Sqlite> for f64[src]

impl Type<Postgres> for str[src]

impl Type<Postgres> for [Time][src]

impl Type<Postgres> for Vec<String, Global>[src]

impl Type<Sqlite> for i32[src]

impl<T1> Type<Postgres> for Vec<(T1,), Global>[src]

impl Type<Postgres> for [i16][src]

impl Type<Postgres> for f32[src]

impl Type<MySql> for u32[src]

impl Type<Postgres> for Vec<IpNetwork, Global>[src]

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

impl Type<Postgres> for Vec<PgRange<i32>, Global>[src]

impl<T1, T2, T3, T4, T5, T6, T7> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6, T7), Global>[src]

impl<DB> Type<DB> for Vec<Value, Global> where
    DB: Database,
    Vec<Json<Value>, Global>: Type<DB>, 
[src]

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

impl Type<Postgres> for [i8][src]

impl Type<Sqlite> for i8[src]

impl<'_, T, DB> Type<DB> for &'_ T where
    T: Type<DB> + ?Sized,
    DB: Database
[src]

impl Type<Mssql> for i32[src]

impl Type<Postgres> for Vec<PgRange<NaiveDate>, Global>[src]

impl Type<MySql> for i64[src]

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

impl Type<Postgres> for [PgRange<BigDecimal>][src]

impl Type<Postgres> for [i32][src]

impl Type<MySql> for u8[src]

impl Type<Postgres> for [f64][src]

impl Type<Postgres> for Vec<i32, Global>[src]

impl Type<Postgres> for [PgInterval][src]

impl Type<Mssql> for f64[src]

impl<T1, T2> Type<Postgres> for Vec<(T1, T2), Global>[src]

impl Type<MySql> for Vec<u8, Global>[src]

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

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

impl Type<Postgres> for [bool][src]

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

impl Type<Postgres> for Vec<Time, Global>[src]

impl Type<Postgres> for [Duration][src]

impl<Tz> Type<Postgres> for Vec<PgRange<DateTime<Tz>>, Global> where
    Tz: TimeZone
[src]

impl Type<Postgres> for [PgRange<i64>][src]

impl Type<Postgres> for bool[src]

impl Type<Postgres> for Duration[src]

impl<Tz> Type<Postgres> for [DateTime<Tz>] where
    Tz: TimeZone
[src]

impl Type<Postgres> for [Decimal][src]

impl<T1, T2, T3, T4> Type<Postgres> for Vec<(T1, T2, T3, T4), Global>[src]

impl<'_> Type<Postgres> for Vec<&'_ str, Global>[src]

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

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

impl Type<Postgres> for Vec<NaiveDate, Global>[src]

impl<T1, T2> Type<Postgres> for (T1, T2)[src]

impl Type<Postgres> for Vec<u32, Global>[src]

impl Type<Postgres> for [PgMoney][src]

impl<Time, Offset> Type<Postgres> for [PgTimeTz<Time, Offset>] where
    PgTimeTz<Time, Offset>: Type<Postgres>, 
[src]

impl Type<Postgres> for [u32][src]

impl Type<Postgres> for [BigDecimal][src]

impl<T1, T2, T3> Type<Postgres> for Vec<(T1, T2, T3), Global>[src]

impl Type<Any> for String where
    String: AnyType
[src]

impl Type<Postgres> for i16[src]

impl Type<Postgres> for [PgRange<NaiveDate>][src]

impl<T> Type<Postgres> for Vec<Option<T>, Global> where
    Vec<T, Global>: Type<Postgres>, 
[src]

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

impl Type<Postgres> for [PrimitiveDateTime][src]

impl Type<Postgres> for Duration[src]

impl Type<Mssql> for i8[src]

impl Type<Any> for str where
    str: AnyType
[src]

impl<Tz> Type<Postgres> for Vec<DateTime<Tz>, Global> where
    Tz: TimeZone
[src]

impl Type<Postgres> for Vec<Date, Global>[src]

impl<'_> Type<Postgres> for Vec<&'_ [u8], Global>[src]

impl Type<Postgres> for Vec<i16, Global>[src]

impl Type<Postgres> for Vec<PgRange<PrimitiveDateTime>, Global>[src]

impl Type<Sqlite> for Vec<u8, Global>[src]

impl Type<Postgres> for Vec<i64, Global>[src]

impl Type<Postgres> for Vec<Vec<u8, Global>, Global>[src]

impl Type<Postgres> for [NaiveTime][src]

impl Type<Postgres> for [NaiveDate][src]

impl Type<Postgres> for [OffsetDateTime][src]

impl Type<Postgres> for Vec<OffsetDateTime, Global>[src]

impl Type<Mssql> for bool[src]

impl Type<Postgres> for [PgRange<Date>][src]

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

impl Type<Postgres> for Vec<f64, Global>[src]

impl Type<Postgres> for Vec<i8, Global>[src]

impl Type<Postgres> for [Uuid][src]

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6, T7, T8, T9), Global>[src]

impl Type<MySql> for i8[src]

impl<T1, T2, T3, T4, T5, T6, T7, T8> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6, T7, T8), Global>[src]

impl Type<Postgres> for f64[src]

impl Type<Sqlite> for bool[src]

impl Type<Postgres> for [IpNetwork][src]

impl Type<Postgres> for Vec<bool, Global>[src]

impl Type<Postgres> for Vec<PgRange<i64>, Global>[src]

impl Type<Mssql> for f32[src]

impl Type<Postgres> for [i64][src]

impl Type<MySql> for f32[src]

impl Type<Postgres> for [PgRange<NaiveDateTime>][src]

impl Type<Postgres> for String[src]

impl Type<Postgres> for Vec<BigDecimal, Global>[src]

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

impl Type<Sqlite> for str[src]

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

impl Type<MySql> for i16[src]

impl Type<Postgres> for [PgRange<OffsetDateTime>][src]

impl Type<MySql> for String[src]

impl<T1> Type<Postgres> for (T1,)[src]

impl Type<Sqlite> for String[src]

impl Type<Postgres> for [PgRange<i32>][src]

impl Type<Any> for bool where
    bool: AnyType
[src]

impl<T> Type<Postgres> for Vec<Json<T>, Global>[src]

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

impl Type<Any> for i32 where
    i32: AnyType
[src]

impl<T1, T2> Type<Postgres> for [(T1, T2)][src]

impl Type<Mssql> for String[src]

impl Type<Postgres> for Vec<NaiveDateTime, Global>[src]

impl Type<Postgres> for ()[src]

impl Type<Any> for f64 where
    f64: AnyType
[src]

impl Type<Postgres> for u32[src]

impl Type<Postgres> for [NaiveDateTime][src]

impl Type<Postgres> for Vec<f32, Global>[src]

impl Type<Postgres> for [Date][src]

impl Type<Postgres> for Vec<Decimal, Global>[src]

impl<Time, Offset> Type<Postgres> for Vec<PgTimeTz<Time, Offset>, Global> where
    PgTimeTz<Time, Offset>: Type<Postgres>, 
[src]

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

impl<T1> Type<Postgres> for [(T1,)][src]

Loading content...

Implementors

impl Type<MySql> for DateTime<Utc>[src]

impl Type<MySql> for NaiveDate[src]

impl Type<MySql> for NaiveDateTime[src]

impl Type<MySql> for NaiveTime[src]

impl Type<MySql> for BigDecimal[src]

impl Type<MySql> for Decimal[src]

impl Type<MySql> for Uuid[src]

impl Type<MySql> for Date[src]

impl Type<MySql> for OffsetDateTime[src]

impl Type<MySql> for PrimitiveDateTime[src]

impl Type<MySql> for Time[src]

impl Type<MySql> for Hyphenated[src]

impl Type<Postgres> for IpNetwork[src]

impl Type<Postgres> for PgInterval[src]

impl Type<Postgres> for PgMoney[src]

impl Type<Postgres> for PgRange<i32>[src]

impl Type<Postgres> for PgRange<i64>[src]

impl Type<Postgres> for PgRange<NaiveDate>[src]

impl Type<Postgres> for PgRange<NaiveDateTime>[src]

impl Type<Postgres> for PgRange<BigDecimal>[src]

impl Type<Postgres> for PgRange<Date>[src]

impl Type<Postgres> for PgRange<OffsetDateTime>[src]

impl Type<Postgres> for PgRange<PrimitiveDateTime>[src]

impl Type<Postgres> for PgTimeTz<NaiveTime, FixedOffset>[src]

impl Type<Postgres> for PgTimeTz<Time, UtcOffset>[src]

impl Type<Postgres> for NaiveDate[src]

impl Type<Postgres> for NaiveDateTime[src]

impl Type<Postgres> for NaiveTime[src]

impl Type<Postgres> for BigDecimal[src]

impl Type<Postgres> for BitVec<u32>[src]

impl Type<Postgres> for Decimal[src]

impl Type<Postgres> for Uuid[src]

impl Type<Postgres> for Date[src]

impl Type<Postgres> for OffsetDateTime[src]

impl Type<Postgres> for PrimitiveDateTime[src]

impl Type<Postgres> for Time[src]

impl Type<Sqlite> for NaiveDate[src]

impl Type<Sqlite> for NaiveDateTime[src]

impl Type<Sqlite> for NaiveTime[src]

impl Type<Sqlite> for Uuid[src]

impl Type<Sqlite> for Hyphenated[src]

impl<DB> Type<DB> for BString where
    DB: Database,
    [u8]: Type<DB>, 
[src]

impl<DB> Type<DB> for Oid where
    DB: Database,
    [u8]: Type<DB>, 
[src]

impl<T> Type<MySql> for Json<T>[src]

impl<T> Type<Postgres> for Json<T>[src]

impl<T> Type<Sqlite> for Json<T>[src]

impl<Tz> Type<Postgres> for PgRange<DateTime<Tz>> where
    Tz: TimeZone
[src]

impl<Tz> Type<Postgres> for DateTime<Tz> where
    Tz: TimeZone
[src]

impl<Tz> Type<Sqlite> for DateTime<Tz> where
    Tz: TimeZone
[src]

Loading content...