GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor(api): split runtime entity CRUD traits · zerodaycode/Canyon-SQL@1c81b3b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • canyon_crud/src
    • entity.rs
  • canyon_macros/src
    • lib.rs
    • query_operations
      • delete
        • entity.rs
      • insert
        • entity.rs
      • mod.rs
      • update
        • entity.rs
  • src
    • lib.rs
  • tests/crud
    • hex_arch_example.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎canyon_crud/src/entity.rs‎

Copy file name to clipboardExpand all lines: canyon_crud/src/entity.rs
+35-7Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ use canyon_core::error::CanyonResult;
33
use canyon_core::mapper::RowMapper;
44
use canyon_core::query::bounds::EntityRuntimeInfo;
55

6-
/// CRUD operations over an entity supplied to the operation.
7-
///
8-
/// It is intended for repository adapters and layered architectures where the persistence type is
9-
/// not the entity being persisted.
10-
pub trait EntityCrud: Send {
6+
/// Inserts an entity supplied to a repository adapter.
7+
pub trait EntityInsert: Send {
118
type Entity: RowMapper + EntityRuntimeInfo + Sync;
129

1310
fn insert_entity<'entity>(
@@ -23,20 +20,30 @@ pub trait EntityCrud: Send {
2320
where
2421
Self::Entity: 'entity,
2522
I: DbConnection + Send + 'entity;
23+
}
24+
25+
/// Updates an entity supplied to a repository adapter.
26+
pub trait EntityUpdate: Send {
27+
type Entity: RowMapper + EntityRuntimeInfo + Sync;
2628

2729
fn update_entity<'entity>(
2830
entity: &'entity Self::Entity,
29-
) -> impl Future<Output = CanyonResult<()>>
31+
) -> impl Future<Output = CanyonResult<u64>>
3032
where
3133
Self::Entity: 'entity;
3234

3335
fn update_entity_with<'entity, I>(
3436
entity: &'entity Self::Entity,
3537
input: I,
36-
) -> impl Future<Output = CanyonResult<()>>
38+
) -> impl Future<Output = CanyonResult<u64>>
3739
where
3840
Self::Entity: 'entity,
3941
I: DbConnection + Send + 'entity;
42+
}
43+
44+
/// Deletes an entity supplied to a repository adapter.
45+
pub trait EntityDelete: Send {
46+
type Entity: RowMapper + EntityRuntimeInfo + Sync;
4047

4148
fn delete_entity<'entity>(
4249
entity: &'entity Self::Entity,
@@ -52,3 +59,24 @@ pub trait EntityCrud: Send {
5259
Self::Entity: 'entity,
5360
I: DbConnection + Send + 'entity;
5461
}
62+
63+
/// Complete runtime entity CRUD contract.
64+
///
65+
/// This trait is implemented automatically for adapters that implement
66+
/// [`EntityInsert`], [`EntityUpdate`] and [`EntityDelete`] for the same entity.
67+
pub trait EntityCrud:
68+
EntityInsert<Entity = <Self as EntityCrud>::Entity>
69+
+ EntityUpdate<Entity = <Self as EntityCrud>::Entity>
70+
+ EntityDelete<Entity = <Self as EntityCrud>::Entity>
71+
{
72+
type Entity: RowMapper + EntityRuntimeInfo + Sync;
73+
}
74+
75+
impl<T> EntityCrud for T
76+
where
77+
T: EntityInsert,
78+
T: EntityUpdate<Entity = <T as EntityInsert>::Entity>,
79+
T: EntityDelete<Entity = <T as EntityInsert>::Entity>,
80+
{
81+
type Entity = <T as EntityInsert>::Entity;
82+
}
Collapse file

‎canyon_macros/src/lib.rs‎

Copy file name to clipboardExpand all lines: canyon_macros/src/lib.rs
+24-4Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ use crate::{
2424
canyon_tokio_test::generate_canyon_tokio_test_tokens,
2525
foreignkeyable_macro::foreignkeyable_tokens,
2626
query_operations::{
27-
impl_crud_entity_operations_trait_for_struct, impl_crud_operations_trait_for_struct,
28-
impl_delete_operations_trait_for_struct, impl_insert_operations_trait_for_struct,
29-
impl_read_operations_trait_for_struct, impl_update_operations_trait_for_struct,
27+
impl_crud_operations_trait_for_struct, impl_delete_operations_trait_for_struct,
28+
impl_entity_crud_traits_for_struct, impl_entity_delete_trait_for_struct,
29+
impl_entity_insert_trait_for_struct, impl_entity_update_trait_for_struct,
30+
impl_insert_operations_trait_for_struct, impl_read_operations_trait_for_struct,
31+
impl_update_operations_trait_for_struct,
3032
},
3133
utils::{function_parser::FunctionParser, helpers, macro_tokens::MacroTokens},
3234
};
@@ -177,7 +179,25 @@ pub fn delete(input: CompilerTokenStream) -> CompilerTokenStream {
177179
/// the entity to persist as an argument instead of operating on `self`.
178180
#[proc_macro_derive(EntityCrud, attributes(canyon_crud))]
179181
pub fn entity_crud(input: CompilerTokenStream) -> CompilerTokenStream {
180-
derive_operations(input, impl_crud_entity_operations_trait_for_struct)
182+
derive_operations(input, impl_entity_crud_traits_for_struct)
183+
}
184+
185+
/// Derives runtime insertion for an entity supplied to a repository adapter.
186+
#[proc_macro_derive(EntityInsert, attributes(canyon_crud))]
187+
pub fn entity_insert(input: CompilerTokenStream) -> CompilerTokenStream {
188+
derive_operations(input, impl_entity_insert_trait_for_struct)
189+
}
190+
191+
/// Derives runtime updates for an entity supplied to a repository adapter.
192+
#[proc_macro_derive(EntityUpdate, attributes(canyon_crud))]
193+
pub fn entity_update(input: CompilerTokenStream) -> CompilerTokenStream {
194+
derive_operations(input, impl_entity_update_trait_for_struct)
195+
}
196+
197+
/// Derives runtime deletion for an entity supplied to a repository adapter.
198+
#[proc_macro_derive(EntityDelete, attributes(canyon_crud))]
199+
pub fn entity_delete(input: CompilerTokenStream) -> CompilerTokenStream {
200+
derive_operations(input, impl_entity_delete_trait_for_struct)
181201
}
182202

183203
/// Derives the metadata required to navigate foreign-key relationships.
Collapse file

‎canyon_macros/src/query_operations/delete/entity.rs‎

Copy file name to clipboardExpand all lines: canyon_macros/src/query_operations/delete/entity.rs
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ mod __detail {
6262
};
6363

6464
let primary_key_name =
65-
<Self::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
65+
<<Self as canyon_sql::crud::EntityDelete>::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
6666
::primary_key_name()
6767
.ok_or(canyon_sql::error::QueryBuilderError::MissingPrimaryKey)?;
6868

6969
let primary_key_value =
70-
<Self::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
70+
<<Self as canyon_sql::crud::EntityDelete>::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
7171
::primary_key_value(entity)
7272
.ok_or(canyon_sql::error::QueryBuilderError::MissingPrimaryKeyValue)?;
7373

@@ -94,21 +94,21 @@ mod __detail {
9494
pub(crate) fn generate_delete_entity_signature() -> TokenStream {
9595
quote! {
9696
async fn delete_entity<'canyon_lt>(
97-
entity: &'canyon_lt Self::Entity,
97+
entity: &'canyon_lt <Self as canyon_sql::crud::EntityDelete>::Entity,
9898
) -> canyon_sql::CanyonResult<()>
9999
where
100-
Self::Entity: 'canyon_lt
100+
<Self as canyon_sql::crud::EntityDelete>::Entity: 'canyon_lt
101101
}
102102
}
103103

104104
pub(crate) fn generate_delete_entity_with_signature() -> TokenStream {
105105
quote! {
106106
async fn delete_entity_with<'canyon_lt, Input>(
107-
entity: &'canyon_lt Self::Entity,
107+
entity: &'canyon_lt <Self as canyon_sql::crud::EntityDelete>::Entity,
108108
input: Input,
109109
) -> canyon_sql::CanyonResult<()>
110110
where
111-
Self::Entity: 'canyon_lt,
111+
<Self as canyon_sql::crud::EntityDelete>::Entity: 'canyon_lt,
112112
Input: canyon_sql::connection::DbConnection
113113
+ Send
114114
+ 'canyon_lt
Collapse file

‎canyon_macros/src/query_operations/insert/entity.rs‎

Copy file name to clipboardExpand all lines: canyon_macros/src/query_operations/insert/entity.rs
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ mod __detail {
6565
};
6666

6767
let columns =
68-
<Self::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
68+
<<Self as canyon_sql::crud::EntityInsert>::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
6969
::field_columns();
7070

7171
if columns.is_empty() {
7272
return #no_fields_to_insert_err;
7373
}
7474

7575
let values =
76-
<Self::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
76+
<<Self as canyon_sql::crud::EntityInsert>::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
7777
::field_values(entity);
7878

7979
let statement =
@@ -84,7 +84,7 @@ mod __detail {
8484
.with_known_columns(columns);
8585

8686
if let Some(primary_key_column) =
87-
<Self::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
87+
<<Self as canyon_sql::crud::EntityInsert>::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
8888
::primary_key_column()
8989
{
9090
let statement = statement
@@ -95,15 +95,15 @@ mod __detail {
9595

9696
let primary_key = #connection
9797
.query_one_for::<
98-
<Self::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
98+
<<Self as canyon_sql::crud::EntityInsert>::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
9999
::PrimaryKey
100100
>(
101101
statement.sql(),
102102
&values,
103103
)
104104
.await?;
105105

106-
<Self::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
106+
<<Self as canyon_sql::crud::EntityInsert>::Entity as canyon_sql::query::bounds::EntityRuntimeInfo>
107107
::set_primary_key(entity, primary_key)?;
108108
} else {
109109
let statement = statement.build()?;
@@ -118,21 +118,21 @@ mod __detail {
118118
pub(crate) fn generate_insert_entity_signature() -> TokenStream {
119119
quote! {
120120
async fn insert_entity<'canyon_lt>(
121-
entity: &'canyon_lt mut Self::Entity,
121+
entity: &'canyon_lt mut <Self as canyon_sql::crud::EntityInsert>::Entity,
122122
) -> canyon_sql::CanyonResult<()>
123123
where
124-
Self::Entity: 'canyon_lt
124+
<Self as canyon_sql::crud::EntityInsert>::Entity: 'canyon_lt
125125
}
126126
}
127127

128128
pub(crate) fn generate_insert_entity_with_signature() -> TokenStream {
129129
quote! {
130130
async fn insert_entity_with<'canyon_lt, Input>(
131-
entity: &'canyon_lt mut Self::Entity,
131+
entity: &'canyon_lt mut <Self as canyon_sql::crud::EntityInsert>::Entity,
132132
input: Input,
133133
) -> canyon_sql::CanyonResult<()>
134134
where
135-
Self::Entity: 'canyon_lt,
135+
<Self as canyon_sql::crud::EntityInsert>::Entity: 'canyon_lt,
136136
Input: canyon_sql::connection::DbConnection
137137
+ Send
138138
+ 'canyon_lt
Collapse file

‎canyon_macros/src/query_operations/mod.rs‎

Copy file name to clipboardExpand all lines: canyon_macros/src/query_operations/mod.rs
+67-13Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,36 +123,90 @@ pub fn impl_delete_operations_trait_for_struct(
123123
})
124124
}
125125

126-
/// Generates the runtime entity CRUD implementation.
127-
///
128-
/// This contract is completely separate from `Crud`: its methods
129-
/// receive the entity to persist instead of operating on `self`.
130-
pub fn impl_crud_entity_operations_trait_for_struct(
126+
/// Generates every runtime entity CRUD implementation.
127+
pub fn impl_entity_crud_traits_for_struct(
128+
macro_data: &MacroTokens<'_>,
129+
table_schema_data: &str,
130+
) -> syn::Result<TokenStream> {
131+
let insert = impl_entity_insert_trait_for_struct(macro_data, table_schema_data)?;
132+
let update = impl_entity_update_trait_for_struct(macro_data, table_schema_data)?;
133+
let delete = impl_entity_delete_trait_for_struct(macro_data, table_schema_data)?;
134+
135+
Ok(quote! {
136+
#insert
137+
#update
138+
#delete
139+
})
140+
}
141+
142+
/// Generates runtime insertion for an externally supplied entity.
143+
pub fn impl_entity_insert_trait_for_struct(
131144
macro_data: &MacroTokens<'_>,
132145
table_schema_data: &str,
133146
) -> syn::Result<TokenStream> {
134147
let ty = macro_data.ty;
135148
let (impl_generics, ty_generics, where_clause) = macro_data.generics.split_for_impl();
136-
let entity_ty = compute_crud_ops_mapping_target_type_with_generics(
137-
ty,
138-
&ty_generics,
139-
macro_data.retrieve_mapping_target_type().as_ref(),
140-
);
149+
let entity_ty = runtime_entity_ty(macro_data, &ty_generics);
141150
let insert_operations = generate_insert_entity_function_tokens(table_schema_data)?;
142-
let update_operations = generate_update_entity_tokens(table_schema_data)?;
143-
let delete_operations = generate_delete_entity_tokens(table_schema_data)?;
144151

145152
Ok(quote! {
146-
impl #impl_generics canyon_sql::crud::EntityCrud for #ty #ty_generics #where_clause {
153+
impl #impl_generics canyon_sql::crud::EntityInsert for #ty #ty_generics #where_clause {
147154
type Entity = #entity_ty;
148155

149156
#insert_operations
157+
}
158+
})
159+
}
160+
161+
/// Generates runtime updates for an externally supplied entity.
162+
pub fn impl_entity_update_trait_for_struct(
163+
macro_data: &MacroTokens<'_>,
164+
table_schema_data: &str,
165+
) -> syn::Result<TokenStream> {
166+
let ty = macro_data.ty;
167+
let (impl_generics, ty_generics, where_clause) = macro_data.generics.split_for_impl();
168+
let entity_ty = runtime_entity_ty(macro_data, &ty_generics);
169+
let update_operations = generate_update_entity_tokens(table_schema_data)?;
170+
171+
Ok(quote! {
172+
impl #impl_generics canyon_sql::crud::EntityUpdate for #ty #ty_generics #where_clause {
173+
type Entity = #entity_ty;
174+
150175
#update_operations
176+
}
177+
})
178+
}
179+
180+
/// Generates runtime deletion for an externally supplied entity.
181+
pub fn impl_entity_delete_trait_for_struct(
182+
macro_data: &MacroTokens<'_>,
183+
table_schema_data: &str,
184+
) -> syn::Result<TokenStream> {
185+
let ty = macro_data.ty;
186+
let (impl_generics, ty_generics, where_clause) = macro_data.generics.split_for_impl();
187+
let entity_ty = runtime_entity_ty(macro_data, &ty_generics);
188+
let delete_operations = generate_delete_entity_tokens(table_schema_data)?;
189+
190+
Ok(quote! {
191+
impl #impl_generics canyon_sql::crud::EntityDelete for #ty #ty_generics #where_clause {
192+
type Entity = #entity_ty;
193+
151194
#delete_operations
152195
}
153196
})
154197
}
155198

199+
fn runtime_entity_ty(
200+
macro_data: &MacroTokens<'_>,
201+
ty_generics: &syn::TypeGenerics<'_>,
202+
) -> TokenStream {
203+
compute_crud_ops_mapping_target_type_with_generics(
204+
macro_data.ty,
205+
ty_generics,
206+
macro_data.retrieve_mapping_target_type().as_ref(),
207+
)
208+
}
209+
156210
fn impl_transaction_trait_for_struct(macro_data: &MacroTokens<'_>) -> TokenStream {
157211
let ty = macro_data.ty;
158212

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.