Skip to content

Commit

Permalink
FEAT: add trait ArelAttributeFromRow
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyuedefeng committed Aug 30, 2023
1 parent 67c17b6 commit 4f32117
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 183 deletions.
6 changes: 4 additions & 2 deletions arel-macros/src/arel/arel_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ fn impl_trait_sqlx_from_row(input: &crate::Input) -> syn::Result<proc_macro2::To
// arel(rename="x")
if let Some(rename) = crate::get_path_value(input, Some(&field), "rename", None)? {
build_assign_clauses.push(quote::quote!(
user.#ident = row.try_get::<#option_type, _>(#rename).unwrap_or_default();
// user.#ident = row.try_get::<#option_type, _>(#rename).unwrap_or_default();
user.#ident = <#option_type as arel::ArelAttributeFromRow>::from_row(&row, #rename).unwrap_or_default();
));
} else {
build_assign_clauses.push(quote::quote!(
user.#ident = row.try_get::<#option_type, _>(stringify!(#ident)).unwrap_or_default();
// user.#ident = row.try_get::<#option_type, _>(stringify!(#ident)).unwrap_or_default();
user.#ident = <#option_type as arel::ArelAttributeFromRow>::from_row(&row, stringify!(#ident)).unwrap_or_default();
));
}
}
Expand Down
6 changes: 4 additions & 2 deletions arel-macros/src/arel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ fn impl_trait_sqlx_from_row(input: &crate::Input) -> syn::Result<proc_macro2::To
// arel(rename="x")
if let Some(rename) = crate::get_path_value(input, Some(&field), "rename", None)? {
build_assign_clauses.push(quote::quote!(
user.#ident = row.try_get::<#r#type, _>(#rename).unwrap_or_default();
// user.#ident = row.try_get::<#r#type, _>(#rename).unwrap_or_default();
user.#ident = <#r#type as arel::ArelAttributeFromRow>::from_row(&row, #rename).unwrap_or_default();
));
} else {
build_assign_clauses.push(quote::quote!(
user.#ident = row.try_get::<#r#type, _>(stringify!(#ident)).unwrap_or_default();
// user.#ident = row.try_get::<#r#type, _>(stringify!(#ident)).unwrap_or_default();
user.#ident = <#r#type as arel::ArelAttributeFromRow>::from_row(&row, stringify!(#ident)).unwrap_or_default();
));
}
}
Expand Down
33 changes: 33 additions & 0 deletions example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
use arel::prelude::*;

#[derive(Debug, Clone, PartialEq)]
pub enum Gender {
Unknown = 0,
Male = 1,
Female = 2,
}
impl ArelAttributeFromRow for Gender {
fn from_row<'r, I>(row: &'r arel::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error>
where
Self: Sized,
I: sqlx::ColumnIndex<arel::DatabaseRow>,
{
let v: u8 = row.try_get(index)?;
let ret = match v {
1 => Gender::Male,
2 => Gender::Female,
_ => Gender::Unknown,
};
Ok(ret)
}
}
impl From<Gender> for arel::Value {
fn from(value: Gender) -> Self {
match value {
Gender::Male => 1.into(),
Gender::Female => 2.into(),
_ => 0.into(),
}
}
}

#[arel(table_name = "user")]
#[allow(dead_code)]
struct User {
Expand All @@ -8,6 +39,7 @@ struct User {
name: String,
#[arel(rename = "type")]
r#type: String,
gender: Option<Gender>,
desc: Option<String>,
done: Option<bool>,
lock_version: Option<i32>,
Expand All @@ -23,6 +55,7 @@ async fn init_db() -> anyhow::Result<()> {
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR(255),
type VARCHAR(255),
gender INT(1) NOT NULL DEFAULT 0,
desc TEXT,
done BOOLEAN NOT NULL DEFAULT 0,
lock_version INT(11) NOT NULL DEFAULT 0,
Expand Down
132 changes: 0 additions & 132 deletions src/active_model/active_record/mod.rs

This file was deleted.

36 changes: 0 additions & 36 deletions src/active_model/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod traits;
pub mod value;
pub mod visitor;

pub use crate::traits::{Arel, ArelPersisted, SuperArel};
pub use crate::traits::{Arel, ArelAttributeFromRow, ArelPersisted, SuperArel};
pub use bytes::Bytes;
pub use sql::Sql;
pub use value::{ActiveValue, Value};
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub use crate::arel;
pub use crate::statements::ArelStatement;
pub use crate::Set;
// pub use crate::{ActiveValue, Value};
pub use crate::{Arel, ArelPersisted, SuperArel};
pub use crate::{Arel, ArelAttributeFromRow, ArelPersisted, SuperArel};
8 changes: 4 additions & 4 deletions src/sql/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ impl<'a> QueryBuilder<'a> {
self.push_bind(Some(*val));
}
crate::Value::TinyUnsigned(val) => {
self.push_bind(Some(*val as i32));
self.push_bind(Some(*val as i64));
}
crate::Value::SmallUnsigned(val) => {
self.push_bind(Some(*val as i32));
self.push_bind(Some(*val as i64));
}
crate::Value::Unsigned(val) => {
self.push_bind(Some(*val as i32));
self.push_bind(Some(*val as i64));
}
crate::Value::BigUnsigned(val) => {
self.push_bind(Some(*val as i32));
self.push_bind(Some(*val as i64));
}
crate::Value::Float(val) => {
self.push_bind(Some(*val));
Expand Down

0 comments on commit 4f32117

Please sign in to comment.