Analyze query and create execution plan
(
&self,
query: &str,
schema_cache: &SchemaCache,
conn: &Connection,
)
| 242 | |
| 243 | /// Analyze query and create execution plan |
| 244 | fn analyze_and_create_plan( |
| 245 | &self, |
| 246 | query: &str, |
| 247 | schema_cache: &SchemaCache, |
| 248 | conn: &Connection, |
| 249 | ) -> Result<Option<CachedQueryPlan>, rusqlite::Error> { |
| 250 | // Extract table name from query |
| 251 | let table_name = match self.extract_table_name(query) { |
| 252 | Some(name) => name, |
| 253 | None => return Ok(None), |
| 254 | }; |
| 255 | |
| 256 | // Prepare statement to get column information |
| 257 | let stmt = conn.prepare(query)?; |
| 258 | let column_count = stmt.column_count(); |
| 259 | |
| 260 | // Get column names |
| 261 | let mut columns = Vec::new(); |
| 262 | for i in 0..column_count { |
| 263 | columns.push(sanitize_column_name(stmt.column_name(i)?).to_string()); |
| 264 | } |
| 265 | |
| 266 | // Get column types from schema cache |
| 267 | let mut column_types = Vec::new(); |
| 268 | if let Ok(table_schema) = schema_cache.get_or_load(conn, &table_name) { |
| 269 | for col_name in &columns { |
| 270 | if let Some(col_info) = table_schema.column_map.get(&col_name.to_lowercase()) { |
| 271 | column_types.push(Some(col_info.pg_type.clone())); |
| 272 | } else { |
| 273 | column_types.push(None); |
| 274 | } |
| 275 | } |
| 276 | } else { |
| 277 | column_types.resize(columns.len(), None); |
| 278 | } |
| 279 | |
| 280 | // Determine query complexity |
| 281 | let complexity = self.classify_query_complexity(query); |
| 282 | |
| 283 | // Check for WHERE clause |
| 284 | let has_where_clause = query.to_uppercase().contains("WHERE"); |
| 285 | |
| 286 | Ok(Some(CachedQueryPlan { |
| 287 | query: query.to_string(), |
| 288 | table_name, |
| 289 | columns, |
| 290 | column_types, |
| 291 | has_where_clause, |
| 292 | complexity, |
| 293 | last_used: Instant::now(), |
| 294 | access_count: 0, |
| 295 | avg_execution_time: Duration::from_millis(0), |
| 296 | success_rate: 1.0, |
| 297 | })) |
| 298 | } |
| 299 | |
| 300 | /// Execute query using cached plan |
| 301 | fn execute_with_cached_plan( |
no test coverage detected