Trait sqlx::Type [−][src]
pub trait Type<DB> where
DB: Database, { fn type_info() -> <DB as Database>::TypeInfo; 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
fn type_info() -> <DB as Database>::TypeInfo
[src]
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!
.
Provided methods
fn compatible(ty: &<DB as Database>::TypeInfo) -> bool
[src]
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.
Implementations on Foreign Types
impl<T1, T2, T3> Type<Postgres> for Vec<(T1, T2, T3), Global>
[src]
impl<T1, T2, T3> Type<Postgres> for Vec<(T1, T2, T3), Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for Vec<PgRange<BigDecimal>, Global>
[src]
impl Type<Postgres> for Vec<PgRange<BigDecimal>, Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for (T1, T2, T3, T4, T5, T6)
[src]
impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for (T1, T2, T3, T4, T5, T6)
[src]pub fn type_info() -> PgTypeInfo
[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<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for [(T1, T2, T3, T4, T5, T6, T7, T8, T9)]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [PgRange<OffsetDateTime>]
[src]
impl Type<Postgres> for [PgRange<OffsetDateTime>]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4> Type<Postgres> for [(T1, T2, T3, T4)]
[src]
impl<T1, T2, T3, T4> Type<Postgres> for [(T1, T2, T3, T4)]
[src]pub fn type_info() -> PgTypeInfo
[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<T1, T2, T3, T4, T5, T6, T7, T8> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6, T7, T8), Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4, T5, T6, T7> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6, T7), Global>
[src]
impl<T1, T2, T3, T4, T5, T6, T7> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6, T7), Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6), Global>
[src]
impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6), Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [OffsetDateTime]
[src]
impl Type<Postgres> for [OffsetDateTime]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for Vec<PgRange<PrimitiveDateTime>, Global>
[src]
impl Type<Postgres> for Vec<PgRange<PrimitiveDateTime>, Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [PgInterval]
[src]
impl Type<Postgres> for [PgInterval]
[src]pub fn type_info() -> PgTypeInfo
[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<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for Vec<(T1, T2, T3, T4, T5, T6, T7, T8, T9), Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<Tz> Type<Postgres> for [PgRange<DateTime<Tz>>] where
Tz: TimeZone,
[src]
impl<Tz> Type<Postgres> for [PgRange<DateTime<Tz>>] where
Tz: TimeZone,
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4, T5> Type<Postgres> for [(T1, T2, T3, T4, T5)]
[src]
impl<T1, T2, T3, T4, T5> Type<Postgres> for [(T1, T2, T3, T4, T5)]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<Time, Offset> Type<Postgres> for [PgTimeTz<Time, Offset>] where
PgTimeTz<Time, Offset>: Type<Postgres>,
[src]
impl<Time, Offset> Type<Postgres> for [PgTimeTz<Time, Offset>] where
PgTimeTz<Time, Offset>: Type<Postgres>,
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for Vec<BigDecimal, Global>
[src]
impl Type<Postgres> for Vec<BigDecimal, Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for Vec<PrimitiveDateTime, Global>
[src]
impl Type<Postgres> for Vec<PrimitiveDateTime, Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [PgRange<PrimitiveDateTime>]
[src]
impl Type<Postgres> for [PgRange<PrimitiveDateTime>]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4, T5> Type<Postgres> for Vec<(T1, T2, T3, T4, T5), Global>
[src]
impl<T1, T2, T3, T4, T5> Type<Postgres> for Vec<(T1, T2, T3, T4, T5), Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for [(T1, T2, T3, T4, T5, T6)]
[src]
impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for [(T1, T2, T3, T4, T5, T6)]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [PrimitiveDateTime]
[src]
impl Type<Postgres> for [PrimitiveDateTime]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [PgRange<BigDecimal>]
[src]
impl Type<Postgres> for [PgRange<BigDecimal>]
[src]pub fn type_info() -> PgTypeInfo
[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<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for Vec<PgRange<OffsetDateTime>, Global>
[src]
impl Type<Postgres> for Vec<PgRange<OffsetDateTime>, Global>
[src]pub fn type_info() -> PgTypeInfo
[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, T6, T7, T8> Type<Postgres> for [(T1, T2, T3, T4, T5, T6, T7, T8)]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<DB> Type<DB> for Vec<Value, Global> where
DB: Database,
Vec<Json<Value>, Global>: Type<DB>,
[src]
impl<DB> Type<DB> for Vec<Value, Global> where
DB: Database,
Vec<Json<Value>, Global>: Type<DB>,
[src]impl<Tz> Type<Postgres> for Vec<DateTime<Tz>, Global> where
Tz: TimeZone,
[src]
impl<Tz> Type<Postgres> for Vec<DateTime<Tz>, Global> where
Tz: TimeZone,
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [BigDecimal]
[src]
impl Type<Postgres> for [BigDecimal]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for Vec<PgRange<NaiveDateTime>, Global>
[src]
impl Type<Postgres> for Vec<PgRange<NaiveDateTime>, Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for Vec<NaiveDateTime, Global>
[src]
impl Type<Postgres> for Vec<NaiveDateTime, Global>
[src]pub fn type_info() -> PgTypeInfo
[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> Type<Postgres> for [(T1, T2, T3, T4, T5, T6, T7)]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4, T5> Type<Postgres> for (T1, T2, T3, T4, T5)
[src]
impl<T1, T2, T3, T4, T5> Type<Postgres> for (T1, T2, T3, T4, T5)
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<T1, T2, T3, T4> Type<Postgres> for Vec<(T1, T2, T3, T4), Global>
[src]
impl<T1, T2, T3, T4> Type<Postgres> for Vec<(T1, T2, T3, T4), Global>
[src]pub fn type_info() -> PgTypeInfo
[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> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7)
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<Time, Offset> Type<Postgres> for Vec<PgTimeTz<Time, Offset>, Global> where
PgTimeTz<Time, Offset>: Type<Postgres>,
[src]
impl<Time, Offset> Type<Postgres> for Vec<PgTimeTz<Time, Offset>, Global> where
PgTimeTz<Time, Offset>: Type<Postgres>,
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<Tz> Type<Postgres> for Vec<PgRange<DateTime<Tz>>, Global> where
Tz: TimeZone,
[src]
impl<Tz> Type<Postgres> for Vec<PgRange<DateTime<Tz>>, Global> where
Tz: TimeZone,
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for Vec<OffsetDateTime, Global>
[src]
impl Type<Postgres> for Vec<OffsetDateTime, Global>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [PgRange<NaiveDateTime>]
[src]
impl Type<Postgres> for [PgRange<NaiveDateTime>]
[src]pub fn type_info() -> PgTypeInfo
[src]
impl<Tz> Type<Postgres> for [DateTime<Tz>] where
Tz: TimeZone,
[src]
impl<Tz> Type<Postgres> for [DateTime<Tz>] where
Tz: TimeZone,
[src]pub fn type_info() -> PgTypeInfo
[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, T6, T7, T8> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8)
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for [NaiveDateTime]
[src]
impl Type<Postgres> for [NaiveDateTime]
[src]pub fn type_info() -> PgTypeInfo
[src]
Implementors
impl Type<MySql> for NaiveDateTime
[src]
impl Type<MySql> for NaiveDateTime
[src]pub fn type_info() -> MySqlTypeInfo
[src]
impl Type<MySql> for BigDecimal
[src]
impl Type<MySql> for BigDecimal
[src]pub fn type_info() -> MySqlTypeInfo
[src]
impl Type<MySql> for OffsetDateTime
[src]
impl Type<MySql> for OffsetDateTime
[src]impl Type<MySql> for PrimitiveDateTime
[src]
impl Type<MySql> for PrimitiveDateTime
[src]pub fn type_info() -> MySqlTypeInfo
[src]
impl Type<MySql> for Hyphenated
[src]
impl Type<MySql> for Hyphenated
[src]impl Type<Postgres> for PgInterval
[src]
impl Type<Postgres> for PgInterval
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for PgRange<NaiveDateTime>
[src]
impl Type<Postgres> for PgRange<NaiveDateTime>
[src]impl Type<Postgres> for PgRange<BigDecimal>
[src]
impl Type<Postgres> for PgRange<BigDecimal>
[src]impl Type<Postgres> for PgRange<OffsetDateTime>
[src]
impl Type<Postgres> for PgRange<OffsetDateTime>
[src]impl Type<Postgres> for PgRange<PrimitiveDateTime>
[src]
impl Type<Postgres> for PgRange<PrimitiveDateTime>
[src]impl Type<Postgres> for PgTimeTz<NaiveTime, FixedOffset>
[src]
impl Type<Postgres> for PgTimeTz<NaiveTime, FixedOffset>
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for NaiveDateTime
[src]
impl Type<Postgres> for NaiveDateTime
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for BigDecimal
[src]
impl Type<Postgres> for BigDecimal
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for OffsetDateTime
[src]
impl Type<Postgres> for OffsetDateTime
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Postgres> for PrimitiveDateTime
[src]
impl Type<Postgres> for PrimitiveDateTime
[src]pub fn type_info() -> PgTypeInfo
[src]
impl Type<Sqlite> for NaiveDateTime
[src]
impl Type<Sqlite> for NaiveDateTime
[src]impl Type<Sqlite> for Hyphenated
[src]
impl Type<Sqlite> for Hyphenated
[src]pub fn type_info() -> SqliteTypeInfo
[src]
impl<Tz> Type<Postgres> for DateTime<Tz> where
Tz: TimeZone,
[src]
impl<Tz> Type<Postgres> for DateTime<Tz> where
Tz: TimeZone,
[src]