@@ -49,6 +49,7 @@ mod unpack_columns_vtab;
4949mod util;
5050
5151use alloc:: format;
52+ use alloc:: string:: String ;
5253use alloc:: string:: ToString ;
5354use alloc:: { borrow:: Cow , boxed:: Box , collections:: BTreeMap , vec:: Vec } ;
5455use core:: ffi:: c_char;
@@ -453,19 +454,30 @@ pub extern "C" fn sqlite3_crsqlcore_init(
453454 }
454455
455456 #[ cfg( feature = "test" ) ]
456- let rc = db
457- . create_function_v2 (
458- "crsql_cache_site_ordinal" ,
459- 1 ,
460- sqlite:: UTF8 | sqlite:: DETERMINISTIC ,
461- Some ( ext_data as * mut c_void ) ,
462- Some ( x_crsql_cache_site_ordinal) ,
463- None ,
464- None ,
465- None ,
466- )
467- . unwrap_or ( ResultCode :: ERROR ) ;
468- if rc != ResultCode :: OK {
457+ if let Err ( _) = db. create_function_v2 (
458+ "crsql_cache_site_ordinal" ,
459+ 1 ,
460+ sqlite:: UTF8 | sqlite:: DETERMINISTIC ,
461+ Some ( ext_data as * mut c_void ) ,
462+ Some ( x_crsql_cache_site_ordinal) ,
463+ None ,
464+ None ,
465+ None ,
466+ ) {
467+ unsafe { crsql_freeExtData ( ext_data) } ;
468+ return null_mut ( ) ;
469+ }
470+
471+ if let Err ( _) = db. create_function_v2 (
472+ "crsql_cache_pk_cl" ,
473+ 2 ,
474+ sqlite:: UTF8 | sqlite:: DETERMINISTIC ,
475+ Some ( ext_data as * mut c_void ) ,
476+ Some ( x_crsql_cache_pk_cl) ,
477+ None ,
478+ None ,
479+ None ,
480+ ) {
469481 unsafe { crsql_freeExtData ( ext_data) } ;
470482 return null_mut ( ) ;
471483 }
@@ -975,6 +987,39 @@ unsafe extern "C" fn x_crsql_cache_site_ordinal(
975987 sqlite:: result_int64 ( ctx, res) ;
976988}
977989
990+ /**
991+ * Get the pk cl cached in the ext data for the current transaction.
992+ * only used for test to inspect the cl cache.
993+ */
994+ unsafe extern "C" fn x_crsql_cache_pk_cl (
995+ ctx : * mut sqlite:: context ,
996+ argc : i32 ,
997+ argv : * mut * mut sqlite:: value ,
998+ ) {
999+ if argc < 2 {
1000+ ctx. result_error (
1001+ "Wrong number of args provided to crsql_cache_pk_cl. Provide the table name and pk key." ,
1002+ ) ;
1003+ return ;
1004+ }
1005+
1006+ let ext_data = ctx. user_data ( ) as * mut c:: crsql_ExtData ;
1007+ let args = sqlite:: args!( argc, argv) ;
1008+ let table_name = args[ 0 ] . text ( ) ;
1009+ let pk_key = args[ 1 ] . int64 ( ) ;
1010+
1011+ let cl_cache = mem:: ManuallyDrop :: new ( Box :: from_raw (
1012+ ( * ext_data) . clCache as * mut BTreeMap < String , BTreeMap < i64 , i64 > > ,
1013+ ) ) ;
1014+
1015+ let table_map = cl_cache. get ( table_name) ;
1016+ let cl = table_map
1017+ . and_then ( |x| x. get ( & pk_key) )
1018+ . cloned ( )
1019+ . unwrap_or ( -1 ) ;
1020+ sqlite:: result_int64 ( ctx, cl) ;
1021+ }
1022+
9781023/**
9791024 * Return the timestamp for the current transaction.
9801025 */
0 commit comments