Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macros): prefix generated variable names in query_as!() #1336

Merged
merged 1 commit into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sqlx-macros/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ where
|&output::RustColumn {
ref ident,
ref type_,
..
}| quote!(#ident: #type_,),
);

Expand Down
17 changes: 11 additions & 6 deletions sqlx-macros/src/query/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use syn::Token;

pub struct RustColumn {
pub(super) ident: Ident,
pub(super) var_name: Ident,
pub(super) type_: ColumnType,
}

Expand Down Expand Up @@ -114,6 +115,9 @@ fn column_to_rust<DB: DatabaseExt>(describe: &Describe<DB>, i: usize) -> crate::
};

Ok(RustColumn {
// prefix the variable name we use in `quote_query_as!()` so it doesn't conflict
// https://github.com/launchbadge/sqlx/issues/1322
var_name: quote::format_ident!("sqlx_query_as_{}", decl.ident),
ident: decl.ident,
type_,
})
Expand All @@ -129,7 +133,7 @@ pub fn quote_query_as<DB: DatabaseExt>(
|(
i,
&RustColumn {
ref ident,
ref var_name,
ref type_,
..
},
Expand All @@ -140,20 +144,21 @@ pub fn quote_query_as<DB: DatabaseExt>(
// binding to a `let` avoids confusing errors about
// "try expression alternatives have incompatible types"
// it doesn't seem to hurt inference in the other branches
let #ident = row.try_get_unchecked::<#type_, _>(#i)?;
let #var_name = row.try_get_unchecked::<#type_, _>(#i)?;
},
// type was overridden to be a wildcard so we fallback to the runtime check
(true, ColumnType::Wildcard) => quote! ( let #ident = row.try_get(#i)?; ),
(true, ColumnType::Wildcard) => quote! ( let #var_name = row.try_get(#i)?; ),
(true, ColumnType::OptWildcard) => {
quote! ( let #ident = row.try_get::<::std::option::Option<_>, _>(#i)?; )
quote! ( let #var_name = row.try_get::<::std::option::Option<_>, _>(#i)?; )
}
// macro is the `_unchecked!()` variant so this will die in decoding if it's wrong
(false, _) => quote!( let #ident = row.try_get_unchecked(#i)?; ),
(false, _) => quote!( let #var_name = row.try_get_unchecked(#i)?; ),
}
},
);

let ident = columns.iter().map(|col| &col.ident);
let var_name = columns.iter().map(|col| &col.var_name);

let db_path = DB::db_path();
let row_path = DB::row_path();
Expand All @@ -165,7 +170,7 @@ pub fn quote_query_as<DB: DatabaseExt>(

#(#instantiations)*

Ok(#out_ty { #(#ident: #ident),* })
Ok(#out_ty { #(#ident: #var_name),* })
})
}
}
Expand Down