mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 14:34:19 +00:00
fix(sqlx-macros): update trait definitions
This commit is contained in:
parent
9341cb8651
commit
9b68eb19ef
8 changed files with 41 additions and 39 deletions
|
@ -11,7 +11,7 @@ use crate::database::Database;
|
|||
pub type Result<T> = StdResult<T, Error>;
|
||||
|
||||
// Convenience type alias for usage within SQLx.
|
||||
pub(crate) type BoxDynError = Box<dyn StdError + 'static + Send + Sync>;
|
||||
pub type BoxDynError = Box<dyn StdError + 'static + Send + Sync>;
|
||||
|
||||
/// An unexpected `NULL` was encountered during decoding.
|
||||
///
|
||||
|
|
|
@ -62,7 +62,6 @@ mod private_column_index {
|
|||
/// This trait is sealed and cannot be implemented for types outside of SQLx.
|
||||
///
|
||||
/// [`FromRow`]: crate::row::FromRow
|
||||
/// [`Cursor`]: crate::cursor::Cursor
|
||||
/// [`Query::fetch`]: crate::query::Query::fetch
|
||||
pub trait Row: private_row::Sealed + Unpin + Send + Sync + 'static {
|
||||
type Database: Database;
|
||||
|
|
|
@ -71,7 +71,7 @@ fn expand_derive_decode_transparent(
|
|||
|
||||
let tts = quote!(
|
||||
impl #impl_generics sqlx::decode::Decode<'de, DB> for #ident #ty_generics #where_clause {
|
||||
fn decode(value: <DB as sqlx::value::HasRawValue<'de>>::RawValue) -> sqlx::Result<Self> {
|
||||
fn decode(value: <DB as sqlx::database::HasValueRef<'de>>::ValueRef) -> std::result::Result<Self, sqlx::BoxDynError> {
|
||||
<#ty as sqlx::decode::Decode<'de, DB>>::decode(value).map(Self)
|
||||
}
|
||||
}
|
||||
|
@ -100,13 +100,13 @@ fn expand_derive_decode_weak_enum(
|
|||
|
||||
Ok(quote!(
|
||||
impl<'de, DB: sqlx::Database> sqlx::decode::Decode<'de, DB> for #ident where #repr: sqlx::decode::Decode<'de, DB> {
|
||||
fn decode(value: <DB as sqlx::value::HasRawValue<'de>>::RawValue) -> sqlx::Result<Self> {
|
||||
fn decode(value: <DB as sqlx::database::HasValueRef<'de>>::ValueRef) -> std::result::Result<Self, sqlx::BoxDynError> {
|
||||
let value = <#repr as sqlx::decode::Decode<'de, DB>>::decode(value)?;
|
||||
|
||||
match value {
|
||||
#(#arms)*
|
||||
|
||||
_ => Err(sqlx::Error::Decode(format!("invalid value {:?} for enum {}", value, #ident_s).into()))
|
||||
_ => Err(Box::new(sqlx::Error::Decode(format!("invalid value {:?} for enum {}", value, #ident_s).into())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -140,12 +140,12 @@ fn expand_derive_decode_strong_enum(
|
|||
|
||||
Ok(quote!(
|
||||
impl<'de, DB: sqlx::Database> sqlx::decode::Decode<'de, DB> for #ident where &'de str: sqlx::decode::Decode<'de, DB> {
|
||||
fn decode(value: <DB as sqlx::value::HasRawValue<'de>>::RawValue) -> sqlx::Result<Self> {
|
||||
fn decode(value: <DB as sqlx::database::HasValueRef<'de>>::ValueRef) -> std::result::Result<Self, sqlx::BoxDynError> {
|
||||
let value = <&'de str as sqlx::decode::Decode<'de, DB>>::decode(value)?;
|
||||
match value {
|
||||
#(#value_arms)*
|
||||
|
||||
_ => Err(sqlx::Error::Decode(format!("invalid value {:?} for enum {}", value, #ident_s).into()))
|
||||
_ => Err(Box::new(sqlx::Error::Decode(format!("invalid value {:?} for enum {}", value, #ident_s).into())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,12 +3,13 @@ use super::attributes::{
|
|||
check_weak_enum_attributes, parse_child_attributes, parse_container_attributes,
|
||||
};
|
||||
use super::rename_all;
|
||||
use proc_macro2::Span;
|
||||
use quote::quote;
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::token::Comma;
|
||||
use syn::{
|
||||
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Expr, Field, Fields, FieldsNamed,
|
||||
FieldsUnnamed, Stmt, Variant,
|
||||
FieldsUnnamed, Lifetime, Stmt, Variant,
|
||||
};
|
||||
|
||||
pub fn expand_derive_encode(input: &DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
|
||||
|
@ -61,22 +62,25 @@ fn expand_derive_encode_transparent(
|
|||
let (_, ty_generics, _) = generics.split_for_impl();
|
||||
|
||||
// add db type for impl generics & where clause
|
||||
let lifetime = Lifetime::new("'q", Span::call_site());
|
||||
let mut generics = generics.clone();
|
||||
generics.params.insert(0, parse_quote!(DB: sqlx::Database));
|
||||
generics
|
||||
.make_where_clause()
|
||||
.predicates
|
||||
.push(parse_quote!(#ty: sqlx::encode::Encode<DB>));
|
||||
.push(parse_quote!(#ty: sqlx::encode::Encode<#lifetime, DB>));
|
||||
let (impl_generics, _, where_clause) = generics.split_for_impl();
|
||||
|
||||
Ok(quote!(
|
||||
impl #impl_generics sqlx::encode::Encode<DB> for #ident #ty_generics #where_clause {
|
||||
fn encode(&self, buf: &mut DB::RawBuffer) {
|
||||
sqlx::encode::Encode::encode(&self.0, buf)
|
||||
impl<#lifetime> #impl_generics sqlx::encode::Encode<#lifetime, DB> for #ident #ty_generics #where_clause {
|
||||
fn encode(self, buf: &mut <DB as sqlx::database::HasArguments<#lifetime>>::ArgumentBuffer) -> sqlx::encode::IsNull {
|
||||
sqlx::encode::Encode::encode(self.0, buf)
|
||||
}
|
||||
fn encode_nullable(&self, buf: &mut DB::RawBuffer) -> sqlx::encode::IsNull {
|
||||
sqlx::encode::Encode::encode_nullable(&self.0, buf)
|
||||
|
||||
fn encode_by_ref(&self, buf: &mut <DB as sqlx::database::HasArguments<#lifetime>>::ArgumentBuffer) -> sqlx::encode::IsNull {
|
||||
sqlx::encode::Encode::encode_by_ref(&self.0, buf)
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> usize {
|
||||
sqlx::encode::Encode::size_hint(&self.0)
|
||||
}
|
||||
|
@ -94,13 +98,13 @@ fn expand_derive_encode_weak_enum(
|
|||
let ident = &input.ident;
|
||||
|
||||
Ok(quote!(
|
||||
impl<DB: sqlx::Database> sqlx::encode::Encode<DB> for #ident where #repr: sqlx::encode::Encode<DB> {
|
||||
fn encode(&self, buf: &mut DB::RawBuffer) {
|
||||
sqlx::encode::Encode::encode(&(*self as #repr), buf)
|
||||
impl<'q, DB: sqlx::Database> sqlx::encode::Encode<'q, DB> for #ident where #repr: sqlx::encode::Encode<'q, DB> {
|
||||
fn encode(self, buf: &mut <DB as sqlx::database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
|
||||
sqlx::encode::Encode::encode((self as #repr), buf)
|
||||
}
|
||||
|
||||
fn encode_nullable(&self, buf: &mut DB::RawBuffer) -> sqlx::encode::IsNull {
|
||||
sqlx::encode::Encode::encode_nullable(&(*self as #repr), buf)
|
||||
fn encode_by_ref(&self, buf: &mut <DB as sqlx::database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
|
||||
sqlx::encode::Encode::encode_by_ref(&(*self as #repr), buf)
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> usize {
|
||||
|
@ -136,19 +140,20 @@ fn expand_derive_encode_strong_enum(
|
|||
}
|
||||
|
||||
Ok(quote!(
|
||||
impl<DB: sqlx::Database> sqlx::encode::Encode<DB> for #ident where str: sqlx::encode::Encode<DB> {
|
||||
fn encode(&self, buf: &mut DB::RawBuffer) {
|
||||
impl<'q, DB: sqlx::Database> sqlx::encode::Encode<'q, DB> for #ident where str: sqlx::encode::Encode<'q, DB> {
|
||||
fn encode_by_ref(&self, buf: &mut <DB as sqlx::database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
|
||||
let val = match self {
|
||||
#(#value_arms)*
|
||||
};
|
||||
<str as sqlx::encode::Encode<DB>>::encode(val, buf)
|
||||
|
||||
<str as sqlx::encode::Encode<'q, DB>>::encode_by_ref(val, buf)
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> usize {
|
||||
let val = match self {
|
||||
#(#value_arms)*
|
||||
};
|
||||
<str as sqlx::encode::Encode<DB>>::size_hint(val)
|
||||
<str as sqlx::encode::Encode<'q, DB>>::size_hint(val)
|
||||
}
|
||||
}
|
||||
))
|
||||
|
@ -177,8 +182,8 @@ fn expand_derive_encode_struct(
|
|||
for field in fields {
|
||||
let ty = &field.ty;
|
||||
|
||||
predicates.push(parse_quote!(#ty: sqlx::encode::Encode<sqlx::Postgres>));
|
||||
predicates.push(parse_quote!(#ty: sqlx::types::Type<sqlx::Postgres>));
|
||||
predicates.push(parse_quote!(#ty: sqlx::encode::Encode<'q, sqlx::Postgres>));
|
||||
predicates.push(parse_quote!(#ty: sqlx::types::Type<'q, sqlx::Postgres>));
|
||||
}
|
||||
|
||||
let (impl_generics, _, where_clause) = generics.split_for_impl();
|
||||
|
@ -202,13 +207,13 @@ fn expand_derive_encode_struct(
|
|||
});
|
||||
|
||||
tts.extend(quote!(
|
||||
impl #impl_generics sqlx::encode::Encode<sqlx::Postgres> for #ident #ty_generics #where_clause {
|
||||
fn encode(&self, buf: &mut sqlx::postgres::PgRawBuffer) {
|
||||
impl<'q> #impl_generics sqlx::encode::Encode<'q, sqlx::Postgres> for #ident #ty_generics #where_clause {
|
||||
fn encode_by_ref(&self, buf: &mut <sqlx::Postgres as sqlx::database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
|
||||
let mut encoder = sqlx::postgres::types::raw::PgRecordEncoder::new(buf);
|
||||
|
||||
#(#writes)*
|
||||
|
||||
encoder.finish();
|
||||
encoder.finish()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> usize {
|
||||
|
|
|
@ -53,9 +53,7 @@ fn expand_derive_from_row_struct(
|
|||
let (_, ty_generics, _) = generics.split_for_impl();
|
||||
|
||||
let mut generics = generics.clone();
|
||||
generics
|
||||
.params
|
||||
.insert(0, parse_quote!(R: sqlx::Row<#lifetime>));
|
||||
generics.params.insert(0, parse_quote!(R: sqlx::Row));
|
||||
|
||||
if provided {
|
||||
generics.params.insert(0, parse_quote!(#lifetime));
|
||||
|
@ -63,7 +61,7 @@ fn expand_derive_from_row_struct(
|
|||
|
||||
let predicates = &mut generics.make_where_clause().predicates;
|
||||
|
||||
predicates.push(parse_quote!(&#lifetime str: sqlx::row::ColumnIndex<#lifetime, R>));
|
||||
predicates.push(parse_quote!(&#lifetime str: sqlx::ColumnIndex<R>));
|
||||
|
||||
for field in fields {
|
||||
let ty = &field.ty;
|
||||
|
@ -91,8 +89,8 @@ fn expand_derive_from_row_struct(
|
|||
let names = fields.iter().map(|field| &field.ident);
|
||||
|
||||
Ok(quote!(
|
||||
impl #impl_generics sqlx::row::FromRow<#lifetime, R> for #ident #ty_generics #where_clause {
|
||||
fn from_row(row: &R) -> sqlx::Result<Self> {
|
||||
impl #impl_generics sqlx::FromRow<#lifetime, R> for #ident #ty_generics #where_clause {
|
||||
fn from_row(row: &#lifetime R) -> sqlx::Result<Self> {
|
||||
#(#reads)*
|
||||
|
||||
Ok(#ident {
|
||||
|
|
|
@ -64,12 +64,12 @@ fn expand_derive_has_sql_type_transparent(
|
|||
generics
|
||||
.make_where_clause()
|
||||
.predicates
|
||||
.push(parse_quote!(#ty: sqlx::types::Type<DB>));
|
||||
.push(parse_quote!(#ty: sqlx::Type<DB>));
|
||||
|
||||
let (impl_generics, _, where_clause) = generics.split_for_impl();
|
||||
|
||||
Ok(quote!(
|
||||
impl #impl_generics sqlx::types::Type< DB > for #ident #ty_generics #where_clause {
|
||||
impl #impl_generics sqlx::Type< DB > for #ident #ty_generics #where_clause {
|
||||
fn type_info() -> DB::TypeInfo {
|
||||
<#ty as sqlx::Type<DB>>::type_info()
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ fn expand_derive_has_sql_type_struct(
|
|||
let ty_name = attributes.rename.unwrap_or_else(|| ident.to_string());
|
||||
|
||||
tts.extend(quote!(
|
||||
impl sqlx::types::Type< sqlx::Postgres > for #ident {
|
||||
impl sqlx::Type< sqlx::Postgres > for #ident {
|
||||
fn type_info() -> sqlx::postgres::PgTypeInfo {
|
||||
sqlx::postgres::PgTypeInfo::with_name(#ty_name)
|
||||
}
|
||||
|
|
|
@ -226,7 +226,7 @@ where
|
|||
let ret_tokens = quote! {
|
||||
macro_rules! macro_result {
|
||||
(#($#arg_names:expr),*) => {{
|
||||
use sqlx::arguments::Arguments as _;
|
||||
use sqlx::Arguments as _;
|
||||
|
||||
#args_tokens
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ pub use sqlx_core::describe;
|
|||
pub use sqlx_core::types::{self, Type};
|
||||
|
||||
#[doc(inline)]
|
||||
pub use sqlx_core::error::{self, Error, Result};
|
||||
pub use sqlx_core::error::{self, BoxDynError, Error, Result};
|
||||
|
||||
#[cfg(feature = "mysql")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
|
||||
|
|
Loading…
Reference in a new issue