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 or wrappers around SQL types. The generated implementations directly delegate to the implementation of the inner type.
#[derive(sqlx::Type)]
#[sqlx(transparent)]
struct UserId(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 toVARCHAR
). Affects Postgres only.#[sqlx(rename_all = "<strategy>")]
on struct definition: Seederive docs in FromRow
ยง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ยง
Sourcefn type_info() -> <DB as Database>::TypeInfo
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ยง
Sourcefn compatible(ty: &<DB as Database>::TypeInfo) -> bool
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.
Dyn Compatibilityยง
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Typesยง
Sourceยงimpl Type<Any> for bool
impl Type<Any> for bool
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for f32
impl Type<Any> for f32
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for f64
impl Type<Any> for f64
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for i16
impl Type<Any> for i16
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for i32
impl Type<Any> for i32
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for i64
impl Type<Any> for i64
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for str
impl Type<Any> for str
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for u16
impl Type<Any> for u16
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for u32
impl Type<Any> for u32
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for u64
impl Type<Any> for u64
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for String
impl Type<Any> for String
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for Vec<u8>
impl Type<Any> for Vec<u8>
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for [u8]
impl Type<Any> for [u8]
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for Cow<'_, str>
impl Type<Mssql> for Cow<'_, str>
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for bool
impl Type<Mssql> for bool
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for f32
impl Type<Mssql> for f32
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for f64
impl Type<Mssql> for f64
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for i8
impl Type<Mssql> for i8
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for i16
impl Type<Mssql> for i16
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for i32
impl Type<Mssql> for i32
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for i64
impl Type<Mssql> for i64
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for str
impl Type<Mssql> for str
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for u8
impl Type<Mssql> for u8
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for u16
impl Type<Mssql> for u16
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for u32
impl Type<Mssql> for u32
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for u64
impl Type<Mssql> for u64
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for String
impl Type<Mssql> for String
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for Vec<u8>
impl Type<Mssql> for Vec<u8>
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<Mssql> for [u8]
impl Type<Mssql> for [u8]
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for Cow<'_, str>
impl Type<MySql> for Cow<'_, str>
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for bool
impl Type<MySql> for bool
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for f32
impl Type<MySql> for f32
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for f64
impl Type<MySql> for f64
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for i8
impl Type<MySql> for i8
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for i16
impl Type<MySql> for i16
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for i32
impl Type<MySql> for i32
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for i64
impl Type<MySql> for i64
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for str
impl Type<MySql> for str
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for u8
impl Type<MySql> for u8
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for u16
impl Type<MySql> for u16
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for u32
impl Type<MySql> for u32
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for u64
impl Type<MySql> for u64
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for String
impl Type<MySql> for String
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for Vec<u8>
impl Type<MySql> for Vec<u8>
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for [u8]
impl Type<MySql> for [u8]
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for Cow<'_, str>
impl Type<Postgres> for Cow<'_, str>
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for IpAddr
impl Type<Postgres> for IpAddr
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for f64
impl Type<Postgres> for f64
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for str
impl Type<Postgres> for str
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for ()
impl Type<Postgres> for ()
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for String
impl Type<Postgres> for String
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for Cow<'_, str>
impl Type<Sqlite> for Cow<'_, str>
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for bool
impl Type<Sqlite> for bool
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for i8
impl Type<Sqlite> for i8
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for i16
impl Type<Sqlite> for i16
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for i32
impl Type<Sqlite> for i32
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for i64
impl Type<Sqlite> for i64
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for u8
impl Type<Sqlite> for u8
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for u16
impl Type<Sqlite> for u16
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for u32
impl Type<Sqlite> for u32
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for u64
impl Type<Sqlite> for u64
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for Vec<u8>
impl Type<Sqlite> for Vec<u8>
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for [u8]
impl Type<Sqlite> for [u8]
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl<'r> Type<Mssql> for Cow<'r, [u8]>
impl<'r> Type<Mssql> for Cow<'r, [u8]>
fn type_info() -> MssqlTypeInfo
fn compatible(ty: &MssqlTypeInfo) -> bool
Sourceยงimpl<T1, T2, T3> Type<Postgres> for (T1, T2, T3)
impl<T1, T2, T3> Type<Postgres> for (T1, T2, T3)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4> Type<Postgres> for (T1, T2, T3, T4)
impl<T1, T2, T3, T4> Type<Postgres> for (T1, T2, T3, T4)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5> Type<Postgres> for (T1, T2, T3, T4, T5)
impl<T1, T2, T3, T4, T5> Type<Postgres> for (T1, T2, T3, T4, T5)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5, T6> Type<Postgres> for (T1, T2, T3, T4, T5, T6)
impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for (T1, T2, T3, T4, T5, T6)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5, T6, T7> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7)
impl<T1, T2, T3, T4, T5, T6, T7> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5, T6, T7, T8> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8)
impl<T1, T2, T3, T4, T5, T6, T7, T8> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T> Type<Postgres> for [T]where
T: PgHasArrayType,
impl<T> Type<Postgres> for [T]where
T: PgHasArrayType,
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl<T> Type<Postgres> for Vec<T>where
T: PgHasArrayType,
impl<T> Type<Postgres> for Vec<T>where
T: PgHasArrayType,
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl<T, const N: usize> Type<Postgres> for [T; N]where
T: PgHasArrayType,
impl<T, const N: usize> Type<Postgres> for [T; N]where
T: PgHasArrayType,
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl<Time, Offset> Type<Postgres> for Vec<PgTimeTz<Time, Offset>>
impl<Time, Offset> Type<Postgres> for Vec<PgTimeTz<Time, Offset>>
fn type_info() -> PgTypeInfo
Sourceยงimpl<Time, Offset> Type<Postgres> for [PgTimeTz<Time, Offset>]
impl<Time, Offset> Type<Postgres> for [PgTimeTz<Time, Offset>]
fn type_info() -> PgTypeInfo
Implementorsยง
impl Type<Any> for Value
impl Type<Any> for DateTime<FixedOffset>
impl Type<Any> for DateTime<Local>
impl Type<Any> for DateTime<Utc>
impl Type<Any> for NaiveDate
impl Type<Any> for NaiveDateTime
impl Type<Any> for NaiveTime
impl Type<Any> for BigDecimal
impl Type<Any> for Decimal
impl Type<Mssql> for NaiveDate
Provides conversion of chrono::NaiveDate to MS SQL Date
impl Type<Mssql> for NaiveDateTime
Provides conversion of chrono::DateTime (UTC) to MS SQL DateTime2N
Note that MS SQL has a number of DateTime-related types and conversion might not work. During encoding, values are always encoded with the best possible precision, which uses 7 digits for nanoseconds.
impl Type<Mssql> for NaiveTime
Provides conversion of chrono::NaiveTime to MS SQL Time