Skip to content

Commit

Permalink
fix(driver): use macro_internal mod for import conflict (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Aug 23, 2023
1 parent 73866a8 commit c1dc353
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
7 changes: 6 additions & 1 deletion driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ mod rest_api;
pub use conn::{Client, Connection, ConnectionInfo};

// pub use for convenience
pub use databend_sql::error::Error;
pub use databend_sql::error::{Error, Result};
pub use databend_sql::rows::{
QueryProgress, Row, RowIterator, RowProgressIterator, RowWithProgress,
};
pub use databend_sql::schema::{DataType, DecimalSize, Field, Schema, SchemaRef};
pub use databend_sql::value::{NumberValue, Value};

pub use databend_driver_macros::TryFromRow;

#[doc(hidden)]
pub mod _macro_internal {
pub use databend_sql::_macro_internal::*;
}
17 changes: 7 additions & 10 deletions macros/src/from_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub fn from_row_derive(tokens_input: TokenStream) -> TokenStream {
let struct_name = &item.ident;
let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();

let path = quote!(databend_driver::_macro_internal);

let set_fields_code = struct_fields.named.iter().map(|field| {
let field_name = &field.ident;
let field_type = &field.ty;
Expand All @@ -35,26 +37,21 @@ pub fn from_row_derive(tokens_input: TokenStream) -> TokenStream {
.unwrap(); // vals_iter size is checked before this code is reached, so
// it is safe to unwrap
let t = col_value.get_type();

<#field_type>::try_from(col_value)
.map_err(|_| Error::InvalidResponse(format!("failed converting column {} from type({:?}) to type({})", col_ix, t, std::any::type_name::<#field_type>())))?
.map_err(|_| #path::Error::InvalidResponse(format!("failed converting column {} from type({:?}) to type({})", col_ix, t, std::any::type_name::<#field_type>())))?
},
}
});

let fields_count = struct_fields.named.len();
let generated = quote! {
use databend_sql::rows::Row;
use databend_sql::error::{Error, Result};

impl #impl_generics TryFrom<Row> for #struct_name #ty_generics #where_clause {
type Error = Error;
fn try_from(row: Row) -> Result<Self> {
impl #impl_generics TryFrom<#path::Row> for #struct_name #ty_generics #where_clause {
type Error = #path::Error;
fn try_from(row: #path::Row) -> #path::Result<Self> {
if #fields_count != row.len() {
return Err(Error::InvalidResponse(format!("row size mismatch: expected {} columns, got {}", #fields_count, row.len())));
return Err(#path::Error::InvalidResponse(format!("row size mismatch: expected {} columns, got {}", #fields_count, row.len())));
}
let mut vals_iter = row.into_iter().enumerate();

Ok(#struct_name {
#(#set_fields_code)*
})
Expand Down

0 comments on commit c1dc353

Please sign in to comment.