GeezerVison is showing github.com — refactor(api): split runtime entity CRUD traits · zerodaycode/Canyon-SQL@1c81b3b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: canyon_crud/src/entity.rs +35-7Lines changed: 35 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,11 +3,8 @@ use canyon_core::error::CanyonResult; | 3 | 3 | use canyon_core::mapper::RowMapper; | 4 | 4 | use canyon_core::query::bounds::EntityRuntimeInfo; | 5 | 5 |
| 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 { | 11 | 8 | type Entity: RowMapper + EntityRuntimeInfo + Sync; | 12 | 9 |
| 13 | 10 | fn insert_entity<'entity>( | @@ -23,20 +20,30 @@ pub trait EntityCrud: Send { | 23 | 20 | where | 24 | 21 | Self::Entity: 'entity, | 25 | 22 | 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; | 26 | 28 |
| 27 | 29 | fn update_entity<'entity>( | 28 | 30 | entity: &'entity Self::Entity, | 29 | | - ) -> impl Future<Output = CanyonResult<()>> | | 31 | + ) -> impl Future<Output = CanyonResult<u64>> | 30 | 32 | where | 31 | 33 | Self::Entity: 'entity; | 32 | 34 |
| 33 | 35 | fn update_entity_with<'entity, I>( | 34 | 36 | entity: &'entity Self::Entity, | 35 | 37 | input: I, | 36 | | - ) -> impl Future<Output = CanyonResult<()>> | | 38 | + ) -> impl Future<Output = CanyonResult<u64>> | 37 | 39 | where | 38 | 40 | Self::Entity: 'entity, | 39 | 41 | 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; | 40 | 47 |
| 41 | 48 | fn delete_entity<'entity>( | 42 | 49 | entity: &'entity Self::Entity, | @@ -52,3 +59,24 @@ pub trait EntityCrud: Send { | 52 | 59 | Self::Entity: 'entity, | 53 | 60 | I: DbConnection + Send + 'entity; | 54 | 61 | } | | 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 Copy file name to clipboardExpand all lines: canyon_macros/src/lib.rs +24-4Lines changed: 24 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,9 +24,11 @@ use crate::{ | 24 | 24 | canyon_tokio_test::generate_canyon_tokio_test_tokens, | 25 | 25 | foreignkeyable_macro::foreignkeyable_tokens, | 26 | 26 | 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, | 30 | 32 | }, | 31 | 33 | utils::{function_parser::FunctionParser, helpers, macro_tokens::MacroTokens}, | 32 | 34 | }; | @@ -177,7 +179,25 @@ pub fn delete(input: CompilerTokenStream) -> CompilerTokenStream { | 177 | 179 | /// the entity to persist as an argument instead of operating on `self`. | 178 | 180 | #[proc_macro_derive(EntityCrud, attributes(canyon_crud))] | 179 | 181 | 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) | 181 | 201 | } | 182 | 202 |
| 183 | 203 | /// Derives the metadata required to navigate foreign-key relationships. | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -62,12 +62,12 @@ mod __detail { | 62 | 62 | }; | 63 | 63 |
| 64 | 64 | 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> | 66 | 66 | ::primary_key_name() | 67 | 67 | .ok_or(canyon_sql::error::QueryBuilderError::MissingPrimaryKey)?; | 68 | 68 |
| 69 | 69 | 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> | 71 | 71 | ::primary_key_value(entity) | 72 | 72 | .ok_or(canyon_sql::error::QueryBuilderError::MissingPrimaryKeyValue)?; | 73 | 73 |
| @@ -94,21 +94,21 @@ mod __detail { | 94 | 94 | pub(crate) fn generate_delete_entity_signature() -> TokenStream { | 95 | 95 | quote! { | 96 | 96 | async fn delete_entity<'canyon_lt>( | 97 | | - entity: &'canyon_lt Self::Entity, | | 97 | + entity: &'canyon_lt <Self as canyon_sql::crud::EntityDelete>::Entity, | 98 | 98 | ) -> canyon_sql::CanyonResult<()> | 99 | 99 | where | 100 | | - Self::Entity: 'canyon_lt | | 100 | + <Self as canyon_sql::crud::EntityDelete>::Entity: 'canyon_lt | 101 | 101 | } | 102 | 102 | } | 103 | 103 |
| 104 | 104 | pub(crate) fn generate_delete_entity_with_signature() -> TokenStream { | 105 | 105 | quote! { | 106 | 106 | 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, | 108 | 108 | input: Input, | 109 | 109 | ) -> canyon_sql::CanyonResult<()> | 110 | 110 | where | 111 | | - Self::Entity: 'canyon_lt, | | 111 | + <Self as canyon_sql::crud::EntityDelete>::Entity: 'canyon_lt, | 112 | 112 | Input: canyon_sql::connection::DbConnection | 113 | 113 | + Send | 114 | 114 | + 'canyon_lt | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -65,15 +65,15 @@ mod __detail { | 65 | 65 | }; | 66 | 66 |
| 67 | 67 | 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> | 69 | 69 | ::field_columns(); | 70 | 70 |
| 71 | 71 | if columns.is_empty() { | 72 | 72 | return #no_fields_to_insert_err; | 73 | 73 | } | 74 | 74 |
| 75 | 75 | 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> | 77 | 77 | ::field_values(entity); | 78 | 78 |
| 79 | 79 | let statement = | @@ -84,7 +84,7 @@ mod __detail { | 84 | 84 | .with_known_columns(columns); | 85 | 85 |
| 86 | 86 | 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> | 88 | 88 | ::primary_key_column() | 89 | 89 | { | 90 | 90 | let statement = statement | @@ -95,15 +95,15 @@ mod __detail { | 95 | 95 |
| 96 | 96 | let primary_key = #connection | 97 | 97 | .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> | 99 | 99 | ::PrimaryKey | 100 | 100 | >( | 101 | 101 | statement.sql(), | 102 | 102 | &values, | 103 | 103 | ) | 104 | 104 | .await?; | 105 | 105 |
| 106 | | - <Self::Entity as canyon_sql::query::bounds::EntityRuntimeInfo> | | 106 | + <<Self as canyon_sql::crud::EntityInsert>::Entity as canyon_sql::query::bounds::EntityRuntimeInfo> | 107 | 107 | ::set_primary_key(entity, primary_key)?; | 108 | 108 | } else { | 109 | 109 | let statement = statement.build()?; | @@ -118,21 +118,21 @@ mod __detail { | 118 | 118 | pub(crate) fn generate_insert_entity_signature() -> TokenStream { | 119 | 119 | quote! { | 120 | 120 | 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, | 122 | 122 | ) -> canyon_sql::CanyonResult<()> | 123 | 123 | where | 124 | | - Self::Entity: 'canyon_lt | | 124 | + <Self as canyon_sql::crud::EntityInsert>::Entity: 'canyon_lt | 125 | 125 | } | 126 | 126 | } | 127 | 127 |
| 128 | 128 | pub(crate) fn generate_insert_entity_with_signature() -> TokenStream { | 129 | 129 | quote! { | 130 | 130 | 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, | 132 | 132 | input: Input, | 133 | 133 | ) -> canyon_sql::CanyonResult<()> | 134 | 134 | where | 135 | | - Self::Entity: 'canyon_lt, | | 135 | + <Self as canyon_sql::crud::EntityInsert>::Entity: 'canyon_lt, | 136 | 136 | Input: canyon_sql::connection::DbConnection | 137 | 137 | + Send | 138 | 138 | + 'canyon_lt | |
Collapse file Copy file name to clipboardExpand all lines: canyon_macros/src/query_operations/mod.rs +67-13Lines changed: 67 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -123,36 +123,90 @@ pub fn impl_delete_operations_trait_for_struct( | 123 | 123 | }) | 124 | 124 | } | 125 | 125 |
| 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( | 131 | 144 | macro_data: &MacroTokens<'_>, | 132 | 145 | table_schema_data: &str, | 133 | 146 | ) -> syn::Result<TokenStream> { | 134 | 147 | let ty = macro_data.ty; | 135 | 148 | 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); | 141 | 150 | 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)?; | 144 | 151 |
| 145 | 152 | 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 { | 147 | 154 | type Entity = #entity_ty; | 148 | 155 |
| 149 | 156 | #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 | + | 150 | 175 | #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 | + | 151 | 194 | #delete_operations | 152 | 195 | } | 153 | 196 | }) | 154 | 197 | } | 155 | 198 |
| | 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 | + | 156 | 210 | fn impl_transaction_trait_for_struct(macro_data: &MacroTokens<'_>) -> TokenStream { | 157 | 211 | let ty = macro_data.ty; | 158 | 212 |
| |
|