diff --git a/sqlx-core/src/types/mod.rs b/sqlx-core/src/types/mod.rs index 549e99a8..20350b97 100644 --- a/sqlx-core/src/types/mod.rs +++ b/sqlx-core/src/types/mod.rs @@ -105,11 +105,11 @@ pub use json::Json; /// /// ##### Attributes /// -/// * `#[sqlx(rename = "")]` on struct definition: instead of inferring the SQL type name from the inner -/// field (in the above case, `BIGINT`), explicitly set it to `` 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(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 +/// `` 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 /// diff --git a/sqlx-macros/src/derives/attributes.rs b/sqlx-macros/src/derives/attributes.rs index fe67a17f..ba1b382b 100644 --- a/sqlx-macros/src/derives/attributes.rs +++ b/sqlx-macros/src/derives/attributes.rs @@ -1,5 +1,7 @@ -use proc_macro2::Ident; +use proc_macro2::{Ident, Span, TokenStream}; +use quote::{quote, quote_spanned}; use syn::punctuated::Punctuated; +use syn::spanned::Spanned; use syn::token::Comma; use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Variant}; @@ -26,6 +28,27 @@ macro_rules! try_set { }; } +pub struct TypeName { + pub val: String, + pub span: Span, + /// Whether the old sqlx(rename) syntax was used instead of sqlx(type_name) + pub deprecated_rename: bool, +} + +impl TypeName { + pub fn get(&self) -> TokenStream { + let val = &self.val; + if self.deprecated_rename { + quote_spanned!(self.span=> { + sqlx::_rename(); + #val + }) + } else { + quote! { #val } + } + } +} + #[derive(Copy, Clone)] pub enum RenameAll { LowerCase, @@ -39,7 +62,7 @@ pub enum RenameAll { pub struct SqlxContainerAttributes { pub transparent: bool, - pub type_name: Option, + pub type_name: Option, pub rename_all: Option, pub repr: Option, } @@ -95,7 +118,31 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result { - try_set!(type_name, val.value(), value) + try_set!( + type_name, + TypeName { + val: val.value(), + span: value.span(), + deprecated_rename: false + }, + value + ) + } + + Meta::NameValue(MetaNameValue { + path, + lit: Lit::Str(val), + .. + }) if path.is_ident("rename") => { + try_set!( + type_name, + TypeName { + val: val.value(), + span: value.span(), + deprecated_rename: true + }, + value + ) } u => fail!(u, "unexpected attribute"), diff --git a/sqlx-macros/src/derives/type.rs b/sqlx-macros/src/derives/type.rs index d44c13bf..143b9c7b 100644 --- a/sqlx-macros/src/derives/type.rs +++ b/sqlx-macros/src/derives/type.rs @@ -83,7 +83,10 @@ fn expand_derive_has_sql_type_transparent( let mut tts = proc_macro2::TokenStream::new(); if cfg!(feature = "postgres") { - let ty_name = attr.type_name.unwrap_or_else(|| ident.to_string()); + let ty_name = attr + .type_name + .map(|tn| tn.get()) + .unwrap_or_else(|| quote! { #ident }); tts.extend(quote!( impl sqlx::Type< sqlx::postgres::Postgres > for #ident #ty_generics { @@ -142,7 +145,10 @@ fn expand_derive_has_sql_type_strong_enum( } if cfg!(feature = "postgres") { - let ty_name = attributes.type_name.unwrap_or_else(|| ident.to_string()); + let ty_name = attributes + .type_name + .map(|tn| tn.get()) + .unwrap_or_else(|| quote! { #ident }); tts.extend(quote!( impl sqlx::Type< sqlx::Postgres > for #ident { @@ -180,7 +186,10 @@ fn expand_derive_has_sql_type_struct( let mut tts = proc_macro2::TokenStream::new(); if cfg!(feature = "postgres") { - let ty_name = attributes.type_name.unwrap_or_else(|| ident.to_string()); + let ty_name = attributes + .type_name + .map(|tn| tn.get()) + .unwrap_or_else(|| quote! { #ident }); tts.extend(quote!( impl sqlx::Type< sqlx::Postgres > for #ident { diff --git a/src/lib.rs b/src/lib.rs index e3bb60a0..97f74294 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,3 +149,8 @@ pub mod prelude { pub use super::Statement; pub use super::Type; } + +#[doc(hidden)] +#[inline(always)] +#[deprecated = "sqlx(rename) is now called sqlx(type_name)"] +pub fn _rename() {}