Prepare a statement and cache its metadata for future use
(
&self,
conn: &'conn Connection,
query: &str,
)
| 71 | |
| 72 | /// Prepare a statement and cache its metadata for future use |
| 73 | pub fn prepare_and_cache<'conn>( |
| 74 | &self, |
| 75 | conn: &'conn Connection, |
| 76 | query: &str, |
| 77 | ) -> Result<(Statement<'conn>, StatementMetadata), rusqlite::Error> { |
| 78 | // For batch INSERTs, use a normalized fingerprint for caching |
| 79 | let cache_key = if let Some(fingerprint) = Self::batch_insert_fingerprint(query) { |
| 80 | fingerprint |
| 81 | } else { |
| 82 | query.to_string() |
| 83 | }; |
| 84 | |
| 85 | // Check if we have cached metadata for this query |
| 86 | if let Some(metadata) = self.get_metadata(&cache_key) { |
| 87 | // We have metadata, prepare the statement with that info |
| 88 | let stmt = conn.prepare(query)?; |
| 89 | return Ok((stmt, metadata)); |
| 90 | } |
| 91 | |
| 92 | // First time seeing this query, prepare it and extract metadata |
| 93 | let stmt = conn.prepare(query)?; |
| 94 | let metadata = self.extract_metadata(&stmt, query)?; |
| 95 | |
| 96 | // Cache the metadata |
| 97 | self.cache_metadata(cache_key, metadata.clone()); |
| 98 | |
| 99 | Ok((stmt, metadata)) |
| 100 | } |
| 101 | |
| 102 | /// Execute a cached statement with parameters |
| 103 | pub fn execute_cached<P: Params>( |