1+ use crate :: constants;
12use canyon_crud:: bounds:: QueryParameter ;
23use canyon_crud:: { bounds:: RowOperations , crud:: Transaction , DatabaseType , DatasourceConfig } ;
34use regex:: Regex ;
45use std:: collections:: HashMap ;
56use std:: fs;
67use walkdir:: WalkDir ;
7- use crate :: constants;
88
99use super :: register_types:: CanyonRegisterEntity ;
1010
@@ -59,7 +59,10 @@ impl CanyonMemory {
5959 /// Queries the database to retrieve internal data about the structures
6060 /// tracked by `CanyonSQL`
6161 #[ allow( clippy:: nonminimal_bool) ]
62- pub async fn remember ( datasource : & DatasourceConfig < ' static > , canyon_entities : & Vec < CanyonRegisterEntity < ' _ > > ) -> Self {
62+ pub async fn remember (
63+ datasource : & DatasourceConfig < ' static > ,
64+ canyon_entities : & Vec < CanyonRegisterEntity < ' _ > > ,
65+ ) -> Self {
6366 // Creates the memory table if not exists
6467 Self :: create_memory ( datasource. name , & datasource. properties . db_type ) . await ;
6568
@@ -81,7 +84,7 @@ impl CanyonMemory {
8184 } ;
8285 db_rows. push ( db_row) ;
8386 }
84- println ! ( "Data in the canyon_memory table: {db_rows:?}" ) ;
87+ println ! ( "Data in the canyon_memory table: {db_rows:?}" ) ;
8588
8689 // Parses the source code files looking for the #[canyon_entity] annotated classes
8790 let mut mem = Self {
@@ -100,23 +103,25 @@ impl CanyonMemory {
100103 let already_in_db = db_rows. iter ( ) . any ( |el| {
101104 ( el. filepath == _struct. filepath && el. struct_name == _struct. struct_name )
102105 || ( ( el. filepath != _struct. filepath && el. struct_name == _struct. struct_name )
103- || ( el. filepath == _struct. filepath && el. struct_name != _struct. struct_name ) )
106+ || ( el. filepath == _struct. filepath
107+ && el. struct_name != _struct. struct_name ) )
104108 } ) ;
105109 if !already_in_db {
106- match CanyonMemory :: query (
107- constants:: queries:: INSERT_INTO_CANYON_MEMORY ,
108- & [
109- & _struct. filepath as & dyn QueryParameter ,
110- & _struct. struct_name ,
111- & _struct. declared_table_name
112- ] ,
113- datasource. name
114- ) . await {
115- Ok ( v) => println ! ( "Query insert CM OK: {v:?}" ) ,
116- Err ( e) => println ! ( "Error update CM: {e:?}" )
117- }
110+ match CanyonMemory :: query (
111+ constants:: queries:: INSERT_INTO_CANYON_MEMORY ,
112+ [
113+ & _struct. filepath as & dyn QueryParameter ,
114+ & _struct. struct_name ,
115+ & _struct. declared_table_name ,
116+ ] ,
117+ datasource. name ,
118+ )
119+ . await
120+ {
121+ Ok ( v) => println ! ( "Query insert CM OK: {v:?}" ) ,
122+ Err ( e) => println ! ( "Error update CM: {e:?}" ) ,
123+ }
118124 }
119-
120125
121126 // When the struct or the filepath it's already on db but one of the two has been modified
122127 let need_to_update = db_rows. iter ( ) . find ( |el| {
@@ -130,33 +135,38 @@ impl CanyonMemory {
130135
131136 match CanyonMemory :: query (
132137 constants:: queries:: UPDATE_CANYON_MEMORY ,
133- & [
138+ [
134139 & _struct. filepath as & dyn QueryParameter ,
135140 & _struct. struct_name ,
136141 & _struct. declared_table_name ,
137- & old. id
142+ & old. id ,
138143 ] ,
139- datasource. name
140- ) . await {
144+ datasource. name ,
145+ )
146+ . await
147+ {
141148 Ok ( v) => println ! ( "Query update CM OK: {v:?}" ) ,
142- Err ( e) => println ! ( "Error update CM: {e:?}" )
149+ Err ( e) => println ! ( "Error update CM: {e:?}" ) ,
143150 }
144151
145152 // if the updated element is the struct name, we add it to the table_rename Hashmap
146153 let rename_table = old. struct_name != _struct. struct_name ;
147154
148155 if rename_table {
149156 mem. renamed_entities . insert (
150- _struct. struct_name . to_string ( ) , // The new one
151- old. struct_name . to_string ( ) , // The old one
157+ _struct. struct_name . to_string ( ) , // The new one
158+ old. struct_name . to_string ( ) , // The old one
152159 ) ;
153160 }
154161 }
155162 }
156163
157164 // Deletes the records when a table is dropped on the previous Canyon run
158165 db_rows. into_iter ( ) . for_each ( |db_row| {
159- if !mem. memory . iter ( ) . any ( |entity| entity. struct_name == db_row. struct_name )
166+ if !mem
167+ . memory
168+ . iter ( )
169+ . any ( |entity| entity. struct_name == db_row. struct_name )
160170 && !updates. contains ( & db_row. struct_name )
161171 {
162172 // crate::add_cm_query_to_execute(stmt, datasource.name, &[&db_row.struct_name]);
@@ -167,7 +177,10 @@ impl CanyonMemory {
167177
168178 /// Parses the Rust source code files to find the one who contains Canyon entities
169179 /// ie -> annotated with `#[canyon_entity]`
170- async fn find_canyon_entity_annotated_structs ( & mut self , canyon_entities : & Vec < CanyonRegisterEntity < ' _ > > ) {
180+ async fn find_canyon_entity_annotated_structs (
181+ & mut self ,
182+ canyon_entities : & [ CanyonRegisterEntity < ' _ > ] ,
183+ ) {
171184 for file in WalkDir :: new ( "./src" )
172185 . into_iter ( )
173186 . filter_map ( |file| file. ok ( ) )
@@ -185,7 +198,9 @@ impl CanyonMemory {
185198 if line. contains ( "#[" ) // separated checks for possible different paths
186199 && line. contains ( "canyon_entity" )
187200 && !line. starts_with ( "//" )
188- { canyon_entity_macro_counter += 1 ; }
201+ {
202+ canyon_entity_macro_counter += 1 ;
203+ }
189204
190205 let re = Regex :: new ( r#"\bstruct\s+(\w+)"# ) . unwrap ( ) ;
191206 if let Some ( captures) = re. captures ( line) {
@@ -198,15 +213,15 @@ impl CanyonMemory {
198213 match canyon_entity_macro_counter {
199214 0 => ( ) ,
200215 1 => {
201- let canyon_entity = canyon_entities. iter ( )
216+ let canyon_entity = canyon_entities
217+ . iter ( )
202218 . find ( |ce| ce. entity_name == struct_name) ;
203219 if let Some ( c_entity) = canyon_entity {
204- self . memory . push (
205- CanyonMemoryAnalyzer {
206- filepath : file. path ( ) . display ( ) . to_string ( ) . replace ( '\\' , "/" ) ,
207- struct_name : struct_name. clone ( ) ,
208- declared_table_name : c_entity. entity_db_table_name . to_string ( ) }
209- )
220+ self . memory . push ( CanyonMemoryAnalyzer {
221+ filepath : file. path ( ) . display ( ) . to_string ( ) . replace ( '\\' , "/" ) ,
222+ struct_name : struct_name. clone ( ) ,
223+ declared_table_name : c_entity. entity_db_table_name . to_string ( ) ,
224+ } )
210225 }
211226 }
212227 _ => panic ! (
@@ -224,7 +239,7 @@ impl CanyonMemory {
224239 constants:: postgresql_queries:: CANYON_MEMORY_TABLE
225240 } else {
226241 constants:: mssql_queries:: CANYON_MEMORY_TABLE
227- } ;
242+ } ;
228243
229244 Self :: query ( query, [ ] , datasource_name)
230245 . await
@@ -238,13 +253,13 @@ struct CanyonMemoryRow<'a> {
238253 id : i32 ,
239254 filepath : & ' a str ,
240255 struct_name : & ' a str ,
241- declared_table_name : & ' a str
256+ declared_table_name : & ' a str ,
242257}
243258
244259/// Represents the data that will be serialized in the `canyon_memory` table
245260#[ derive( Debug ) ]
246261pub struct CanyonMemoryAnalyzer {
247262 pub filepath : String ,
248263 pub struct_name : String ,
249- pub declared_table_name : String
264+ pub declared_table_name : String ,
250265}
0 commit comments