MCPcopy Create free account
hub / github.com/erans/pgsqlite / handle_table_existence_query

Method handle_table_existence_query

src/session/db_handler.rs:2711–2747  ·  view source on GitHub ↗

Handle SQLAlchemy table existence check query This optimizes the complex JOIN query by doing a simple table lookup

(&self, query: &str, session_id: &Uuid)

Source from the content-addressed store, hash-verified

2709 /// Handle SQLAlchemy table existence check query
2710 /// This optimizes the complex JOIN query by doing a simple table lookup
2711 async fn handle_table_existence_query(&self, query: &str, session_id: &Uuid) -> Result<DbResponse, PgSqliteError> {
2712 // Extract table name from the query
2713 // Look for patterns like "relname = 'table_name'" or "relname = $1"
2714 let table_name = if let Some(captures) = RELNAME_REGEX.as_ref()
2715 .ok()
2716 .and_then(|regex| regex.captures(query)) {
2717 captures[1].to_string()
2718 } else {
2719 // For parameterized queries, we need to look at the actual parameters
2720 // For now, return empty result to indicate table doesn't exist
2721 // This will cause SQLAlchemy to proceed with CREATE TABLE
2722 return Ok(DbResponse {
2723 columns: vec!["relname".to_string()],
2724 rows: vec![],
2725 rows_affected: 0,
2726 });
2727 };
2728
2729 debug!("Checking table existence for: {}", table_name);
2730
2731 // Simple table existence check
2732 let existence_query = "SELECT name FROM sqlite_master WHERE type IN ('table', 'view') AND name = ? AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '__pgsqlite_%'";
2733
2734 self.connection_manager.execute_with_session(session_id, |conn| {
2735 let mut stmt = conn.prepare(existence_query)?;
2736 let rows: Result<Vec<_>, _> = stmt.query_map([&table_name], |row| {
2737 let name: String = row.get(0)?;
2738 Ok(vec![Some(name.into_bytes())])
2739 })?.collect();
2740
2741 Ok(DbResponse {
2742 columns: vec!["relname".to_string()],
2743 rows: rows?,
2744 rows_affected: 0,
2745 })
2746 })
2747 }
2748
2749 /// Store CREATE TABLE metadata including type mappings and numeric constraints
2750 fn store_create_table_metadata(

Callers 2

query_with_sessionMethod · 0.80

Calls 2

execute_with_sessionMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected