Move to separate DeriveActiveModel, cargo fmt
This commit is contained in:
parent
bbe80fa873
commit
7aaeef834c
@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
|
||||
#[table = "cake"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
|
@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
|
||||
#[table = "cake_filling"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub cake_id: i32,
|
||||
pub filling_id: i32,
|
||||
|
@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
|
||||
#[table = "filling"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
|
@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
|
||||
#[table = "fruit"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
|
75
sea-orm-macros/src/derives/active_model.rs
Normal file
75
sea-orm-macros/src/derives/active_model.rs
Normal file
@ -0,0 +1,75 @@
|
||||
use heck::CamelCase;
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::{format_ident, quote, quote_spanned};
|
||||
use syn::{Data, DataStruct, Field, Fields, Type};
|
||||
|
||||
pub fn expend_derive_active_model(ident: Ident, data: Data) -> syn::Result<TokenStream> {
|
||||
let fields = match data {
|
||||
Data::Struct(DataStruct {
|
||||
fields: Fields::Named(named),
|
||||
..
|
||||
}) => named.named,
|
||||
_ => {
|
||||
return Ok(quote_spanned! {
|
||||
ident.span() => compile_error!("you can only derive DeriveModel on structs");
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
let field: Vec<Ident> = fields
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string()))
|
||||
.collect();
|
||||
|
||||
let name: Vec<Ident> = fields
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case()))
|
||||
.collect();
|
||||
|
||||
let ty: Vec<Type> = fields.into_iter().map(|Field { ty, .. }| ty).collect();
|
||||
|
||||
Ok(quote!(
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ActiveModel {
|
||||
#(pub #field: sea_orm::Action<#ty>),*
|
||||
}
|
||||
|
||||
impl sea_orm::ActiveModelOf<#ident> for ActiveModel {
|
||||
fn from_model(m: #ident) -> Self {
|
||||
Self::from(m)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<#ident> for ActiveModel {
|
||||
fn from(m: #ident) -> Self {
|
||||
Self {
|
||||
#(#field: sea_orm::Action::Set(m.#field)),*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::ActiveModelTrait for ActiveModel {
|
||||
type Column = Column;
|
||||
|
||||
fn get(&self, c: Self::Column) -> sea_orm::Action<sea_orm::Value> {
|
||||
match c {
|
||||
#(Self::Column::#name => self.#field.clone().into_action_value()),*
|
||||
}
|
||||
}
|
||||
|
||||
fn set(&mut self, c: Self::Column, v: sea_orm::Value) {
|
||||
match c {
|
||||
#(Self::Column::#name => self.#field = sea_orm::Action::Set(v.unwrap())),*
|
||||
}
|
||||
}
|
||||
|
||||
fn unset(&mut self, c: Self::Column) {
|
||||
match c {
|
||||
#(Self::Column::#name => self.#field = sea_orm::Action::Unset),*
|
||||
}
|
||||
}
|
||||
}
|
||||
))
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
mod active_model;
|
||||
mod column;
|
||||
mod entity;
|
||||
mod from_query_result;
|
||||
mod model;
|
||||
mod primary_key;
|
||||
|
||||
pub use active_model::*;
|
||||
pub use column::*;
|
||||
pub use entity::*;
|
||||
pub use from_query_result::*;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use heck::CamelCase;
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::{format_ident, quote, quote_spanned};
|
||||
use syn::{Data, DataStruct, Field, Fields, Type};
|
||||
use syn::{Data, DataStruct, Field, Fields};
|
||||
|
||||
pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream> {
|
||||
let fields = match data {
|
||||
@ -23,16 +23,10 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
|
||||
.collect();
|
||||
|
||||
let name: Vec<Ident> = fields
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case()))
|
||||
.collect();
|
||||
|
||||
let ty: Vec<Type> = fields
|
||||
.into_iter()
|
||||
.map(|Field { ty, .. }| ty)
|
||||
.collect();
|
||||
|
||||
Ok(quote!(
|
||||
impl sea_orm::ModelTrait for #ident {
|
||||
type Column = Column;
|
||||
@ -55,46 +49,5 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ActiveModel {
|
||||
#(pub #field: sea_orm::Action<#ty>),*
|
||||
}
|
||||
|
||||
impl sea_orm::ActiveModelOf<#ident> for ActiveModel {
|
||||
fn from_model(m: #ident) -> Self {
|
||||
Self::from(m)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<#ident> for ActiveModel {
|
||||
fn from(m: #ident) -> Self {
|
||||
Self {
|
||||
#(#field: sea_orm::Action::Set(m.#field)),*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::ActiveModelTrait for ActiveModel {
|
||||
type Column = Column;
|
||||
|
||||
fn get(&self, c: Self::Column) -> sea_orm::Action<sea_orm::Value> {
|
||||
match c {
|
||||
#(Self::Column::#name => self.#field.clone().into_action_value()),*
|
||||
}
|
||||
}
|
||||
|
||||
fn set(&mut self, c: Self::Column, v: sea_orm::Value) {
|
||||
match c {
|
||||
#(Self::Column::#name => self.#field = sea_orm::Action::Set(v.unwrap())),*
|
||||
}
|
||||
}
|
||||
|
||||
fn unset(&mut self, c: Self::Column) {
|
||||
match c {
|
||||
#(Self::Column::#name => self.#field = sea_orm::Action::Unset),*
|
||||
}
|
||||
}
|
||||
}
|
||||
))
|
||||
}
|
||||
|
@ -45,6 +45,16 @@ pub fn derive_model(input: TokenStream) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro_derive(DeriveActiveModel)]
|
||||
pub fn derive_active_model(input: TokenStream) -> TokenStream {
|
||||
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
|
||||
|
||||
match derives::expend_derive_active_model(ident, data) {
|
||||
Ok(ts) => ts.into(),
|
||||
Err(e) => e.to_compile_error().into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro_derive(FromQueryResult)]
|
||||
pub fn derive_from_query_result(input: TokenStream) -> TokenStream {
|
||||
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
|
||||
|
@ -2,7 +2,7 @@ mod select;
|
||||
|
||||
pub use select::*;
|
||||
|
||||
use crate::{Statement, DatabaseConnection, QueryResult, TypeErr};
|
||||
use crate::{DatabaseConnection, QueryResult, Statement, TypeErr};
|
||||
use async_trait::async_trait;
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use sea_query::{inject_parameters, MySqlQueryBuilder, Values};
|
||||
use std::{fmt};
|
||||
use std::fmt;
|
||||
|
||||
pub struct Statement {
|
||||
pub sql: String,
|
||||
|
@ -4,7 +4,7 @@ use sqlx::{mysql::MySqlRow, MySqlPool};
|
||||
sea_query::sea_query_driver_mysql!();
|
||||
use sea_query_driver_mysql::bind_query;
|
||||
|
||||
use crate::{connector::*, debug_print, query::*, Statement, DatabaseConnection};
|
||||
use crate::{connector::*, debug_print, query::*, DatabaseConnection, Statement};
|
||||
|
||||
pub struct SqlxMySqlConnector;
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::fmt::Debug;
|
||||
use crate::{ColumnTrait, ModelTrait, Value};
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Action<V> {
|
||||
@ -7,7 +7,10 @@ pub enum Action<V> {
|
||||
Unset,
|
||||
}
|
||||
|
||||
impl<V> Action<V> where V: Into<Value> {
|
||||
impl<V> Action<V>
|
||||
where
|
||||
V: Into<Value>,
|
||||
{
|
||||
pub fn into_action_value(self) -> Action<Value> {
|
||||
match self {
|
||||
Self::Set(v) => Action::Set(v.into()),
|
||||
|
@ -1,5 +1,6 @@
|
||||
pub use crate::{
|
||||
Action, ActiveModelOf, ActiveModelTrait, ColumnTrait, ColumnType, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName,
|
||||
EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait,
|
||||
QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value,
|
||||
Action, ActiveModelOf, ActiveModelTrait, ColumnTrait, ColumnType, DeriveActiveModel,
|
||||
DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter,
|
||||
Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, QueryResult, Related,
|
||||
RelationDef, RelationTrait, Select, TypeErr, Value,
|
||||
};
|
||||
|
@ -13,7 +13,7 @@ pub use entity::*;
|
||||
pub use query::*;
|
||||
|
||||
pub use sea_orm_macros::{
|
||||
DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, FromQueryResult,
|
||||
DeriveActiveModel, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, FromQueryResult,
|
||||
};
|
||||
pub use sea_query;
|
||||
pub use sea_query::Iden;
|
||||
|
@ -5,7 +5,7 @@ use crate::entity::prelude::*;
|
||||
#[table = "cake"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
|
@ -5,7 +5,7 @@ use crate::entity::prelude::*;
|
||||
#[table = "cake_filling"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub cake_id: i32,
|
||||
pub filling_id: i32,
|
||||
|
@ -5,7 +5,7 @@ use crate::entity::prelude::*;
|
||||
#[table = "filling"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
|
@ -5,7 +5,7 @@ use crate::entity::prelude::*;
|
||||
#[table = "fruit"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
|
Loading…
x
Reference in New Issue
Block a user