aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-06-30 00:27:49 -0600
committerMelody Horn <melody@boringcactus.com>2021-06-30 00:27:49 -0600
commit434b6d19a5a47e0d3b4399d9937565723328643e (patch)
treefef8238cbffe8f3b0188e85a80174c4337bb3e7f
parent9829bbfcd57c57e237c6aacb96a78d0b9f5bab68 (diff)
downloadtosin-434b6d19a5a47e0d3b4399d9937565723328643e.tar.gz
tosin-434b6d19a5a47e0d3b4399d9937565723328643e.zip
juggle Id so FromSql works out
-rw-r--r--src/db/models/mod.rs2
-rw-r--r--tosin-macros/src/lib.rs27
2 files changed, 22 insertions, 7 deletions
diff --git a/src/db/models/mod.rs b/src/db/models/mod.rs
index 4133cf2..ad60ac5 100644
--- a/src/db/models/mod.rs
+++ b/src/db/models/mod.rs
@@ -3,7 +3,7 @@ pub use tosin_macros::{gather_models as gather, Model};
mod meta;
pub use meta::*;
-pub type Id = usize;
+pub type Id = i64;
#[derive(Clone, Debug)]
pub enum Field {
diff --git a/tosin-macros/src/lib.rs b/tosin-macros/src/lib.rs
index 15a0266..0da725e 100644
--- a/tosin-macros/src/lib.rs
+++ b/tosin-macros/src/lib.rs
@@ -77,8 +77,8 @@ fn to_field_spec(field: &syn::Field) -> FieldInfo<impl quote::ToTokens> {
if field_type == &parse_type("Option<Id>") {
FieldInfo {
tosin_field: quote! { ::tosin::db::models::Field::IntField { name: stringify!(#field_name) } },
- diesel_column: quote! { #field_name -> Integer },
- diesel_type: quote! { ::tosin::db::sql_types::Integer },
+ diesel_column: quote! { #field_name -> BigInt },
+ diesel_type: quote! { ::tosin::db::sql_types::BigInt },
}
} else if field_type == &parse_type("Id") {
// TODO foreign key constraint
@@ -132,7 +132,22 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream {
} else {
panic!("not on a struct");
};
- let real_types: Vec<_> = ast_data.fields.iter().map(|field| &field.ty).collect();
+ let real_db_types: Vec<_> = ast_data
+ .fields
+ .iter()
+ .map(|field| {
+ if field
+ .ident
+ .as_ref()
+ .map_or(false, |name| name.to_string() == "id")
+ && field.ty == syn::parse_str("Option<Id>").unwrap()
+ {
+ syn::parse_str("Id").unwrap()
+ } else {
+ field.ty.clone()
+ }
+ })
+ .collect();
let FieldsInfo {
tosin_fields,
diesel_columns,
@@ -147,11 +162,11 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream {
}
impl<__DB: tosin::db::diesel_backend::Backend, __ST> tosin::db::Queryable<__ST, __DB> for #name
- where (#(#real_types),*): tosin::db::Queryable<__ST, __DB> {
- type Row = <(#(#real_types),*) as tosin::db::Queryable<__ST, __DB>>::Row;
+ where (#(#real_db_types),*): tosin::db::Queryable<__ST, __DB> {
+ type Row = <(#(#real_db_types),*) as tosin::db::Queryable<__ST, __DB>>::Row;
fn build(row: Self::Row) -> Self {
- let row: (#(#real_types),*) = tosin::db::Queryable::build(row);
+ let row: (#(#real_db_types),*) = tosin::db::Queryable::build(row);
todo!()
}
}