11use crate :: constants;
2- use canyon_crud:: bounds:: QueryParameter ;
32use canyon_crud:: { bounds:: RowOperations , crud:: Transaction , DatabaseType , DatasourceConfig } ;
43use regex:: Regex ;
54use std:: collections:: HashMap ;
@@ -58,6 +57,7 @@ impl Transaction<Self> for CanyonMemory {}
5857impl CanyonMemory {
5958 /// Queries the database to retrieve internal data about the structures
6059 /// tracked by `CanyonSQL`
60+ #[ cfg( not( cargo_check) ) ]
6161 #[ allow( clippy:: nonminimal_bool) ]
6262 pub async fn remember (
6363 datasource : & DatasourceConfig < ' static > ,
@@ -67,7 +67,6 @@ impl CanyonMemory {
6767 Self :: create_memory ( datasource. name , & datasource. properties . db_type ) . await ;
6868
6969 // Retrieve the last status data from the `canyon_memory` table
70- // TODO still pending on the target schema, for now they are created on the default one
7170 let res = Self :: query ( "SELECT * FROM canyon_memory" , [ ] , datasource. name )
7271 . await
7372 . expect ( "Error querying Canyon Memory" ) ;
@@ -84,99 +83,71 @@ impl CanyonMemory {
8483 } ;
8584 db_rows. push ( db_row) ;
8685 }
87- println ! ( "Data in the canyon_memory table: {db_rows:?}" ) ;
8886
8987 // Parses the source code files looking for the #[canyon_entity] annotated classes
9088 let mut mem = Self {
9189 memory : Vec :: new ( ) ,
9290 renamed_entities : HashMap :: new ( ) ,
9391 } ;
9492 Self :: find_canyon_entity_annotated_structs ( & mut mem, canyon_entities) . await ;
95-
96- // Insert into the memory table the new discovered entities
97- // Care, insert the new ones, delete the olds
98- // Also, updates the registry when the fields changes
93+
9994 let mut updates = Vec :: new ( ) ;
10095
101- for _struct in & mem. memory {
102- // When the filepath and the struct hasn't been modified and are already on db
103- let already_in_db = db_rows. iter ( ) . any ( |el| {
104- ( el. filepath == _struct. filepath && el. struct_name == _struct. struct_name )
105- || ( ( el. filepath != _struct. filepath && el. struct_name == _struct. struct_name )
106- || ( el. filepath == _struct. filepath
107- && el. struct_name != _struct. struct_name ) )
108- } ) ;
109- if !already_in_db {
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- }
124- }
96+ for _struct in & mem. memory { // For every program entity detected
97+ let already_in_db = db_rows. iter ( ) . find ( |el|
98+ el. filepath == _struct. filepath || el. struct_name == _struct. struct_name || el. declared_table_name == _struct. declared_table_name
99+ ) ;
125100
126- // When the struct or the filepath it's already on db but one of the two has been modified
127- let need_to_update = db_rows. iter ( ) . find ( |el| {
128- ( el. filepath == _struct. filepath || el. struct_name == _struct. struct_name )
129- && !( el. filepath == _struct. filepath && el. struct_name == _struct. struct_name )
130- } ) ;
101+ if let Some ( old) = already_in_db {
102+ if !( old. filepath == _struct. filepath && old. struct_name == _struct. struct_name && old. declared_table_name == _struct. declared_table_name ) {
103+ updates. push ( old. struct_name ) ;
104+ let stmt = format ! (
105+ "UPDATE canyon_memory SET filepath = '{}', struct_name = '{}', declared_table_name = '{}' \
106+ WHERE id = {}",
107+ _struct. filepath, _struct. struct_name, _struct. declared_table_name, old. id
108+ ) ;
109+ save_canyon_memory_query ( stmt, datasource. name ) ;
131110
132- // updated means: the old one. The value to update
133- if let Some ( old) = need_to_update {
134- updates. push ( old. struct_name ) ;
111+ // if the updated element is the struct name, we add it to the table_rename Hashmap
112+ let rename_table = old. declared_table_name != _struct. declared_table_name ;
135113
136- match CanyonMemory :: query (
137- constants:: queries:: UPDATE_CANYON_MEMORY ,
138- [
139- & _struct. filepath as & dyn QueryParameter ,
140- & _struct. struct_name ,
141- & _struct. declared_table_name ,
142- & old. id ,
143- ] ,
144- datasource. name ,
145- )
146- . await
147- {
148- Ok ( v) => println ! ( "Query update CM OK: {v:?}" ) ,
149- Err ( e) => println ! ( "Error update CM: {e:?}" ) ,
114+ if rename_table {
115+ mem. renamed_entities . insert (
116+ _struct. declared_table_name . to_string ( ) , // The new one
117+ old. declared_table_name . to_string ( ) , // The old one
118+ ) ;
119+ }
150120 }
121+ }
151122
152- // if the updated element is the struct name, we add it to the table_rename Hashmap
153- let rename_table = old. struct_name != _struct. struct_name ;
154-
155- if rename_table {
156- mem. renamed_entities . insert (
157- _struct. struct_name . to_string ( ) , // The new one
158- old. struct_name . to_string ( ) , // The old one
159- ) ;
160- }
123+ if already_in_db. is_none ( ) {
124+ println ! ( "\t Insert action for: {_struct:?}" ) ;
125+ let stmt = format ! (
126+ "INSERT INTO canyon_memory (filepath, struct_name, declared_table_name) \
127+ VALUES ('{}', '{}', '{}')",
128+ _struct. filepath, _struct. struct_name, _struct. declared_table_name
129+ ) ;
130+ save_canyon_memory_query ( stmt, datasource. name )
161131 }
162132 }
163133
164- // Deletes the records when a table is dropped on the previous Canyon run
165- db_rows. into_iter ( ) . for_each ( |db_row| {
134+ // Deletes the records from canyon_memory, because they stopped to be tracked by Canyon
135+ for db_row in db_rows. into_iter ( ) {
166136 if !mem
167137 . memory
168138 . iter ( )
169139 . any ( |entity| entity. struct_name == db_row. struct_name )
170140 && !updates. contains ( & db_row. struct_name )
171141 {
172- // crate::add_cm_query_to_execute(stmt, datasource.name, &[& db_row.struct_name] );
142+ save_canyon_memory_query ( format ! ( "DELETE FROM canyon_memory WHERE struct_name = '{}'" , db_row. struct_name) , datasource . name ) ;
173143 }
174- } ) ;
144+ }
175145 mem
176146 }
177147
178148 /// Parses the Rust source code files to find the one who contains Canyon entities
179149 /// ie -> annotated with `#[canyon_entity]`
150+ #[ cfg( not( cargo_check) ) ]
180151 async fn find_canyon_entity_annotated_structs (
181152 & mut self ,
182153 canyon_entities : & [ CanyonRegisterEntity < ' _ > ] ,
@@ -234,6 +205,7 @@ impl CanyonMemory {
234205 }
235206
236207 /// Generates, if not exists the `canyon_memory` table
208+ #[ cfg( not( cargo_check) ) ]
237209 async fn create_memory ( datasource_name : & str , database_type : & DatabaseType ) {
238210 let query = if database_type == & DatabaseType :: PostgreSql {
239211 constants:: postgresql_queries:: CANYON_MEMORY_TABLE
@@ -247,6 +219,29 @@ impl CanyonMemory {
247219 }
248220}
249221
222+
223+ fn save_canyon_memory_query < ' a > ( stmt : String , ds_name : & ' static str ) {
224+ use crate :: CM_QUERIES_TO_EXECUTE ;
225+
226+ if CM_QUERIES_TO_EXECUTE
227+ . lock ( )
228+ . unwrap ( )
229+ . contains_key ( ds_name)
230+ {
231+ CM_QUERIES_TO_EXECUTE
232+ . lock ( )
233+ . unwrap ( )
234+ . get_mut ( ds_name)
235+ . unwrap ( )
236+ . push ( stmt) ;
237+ } else {
238+ CM_QUERIES_TO_EXECUTE
239+ . lock ( )
240+ . unwrap ( )
241+ . insert ( ds_name, vec ! [ stmt] ) ;
242+ }
243+ }
244+
250245/// Represents a single row from the `canyon_memory` table
251246#[ derive( Debug ) ]
252247struct CanyonMemoryRow < ' a > {
0 commit comments