Process the query lazily - only do the work when needed
(&mut self, conn: &Connection, _schema_cache: &SchemaCache)
| 142 | |
| 143 | /// Process the query lazily - only do the work when needed |
| 144 | pub fn process(&mut self, conn: &Connection, _schema_cache: &SchemaCache) -> Result<&str, rusqlite::Error> { |
| 145 | // Re-enable translations now that wire protocol cache is disabled |
| 146 | // tracing::info!("TESTING: All query translations disabled - returning original query: {}", self.original_query); |
| 147 | // return Ok(self.original_query); |
| 148 | |
| 149 | // If already processed, return the result |
| 150 | if let Some(ref translated) = self.translated_query { |
| 151 | return Ok(translated.as_ref()); |
| 152 | } |
| 153 | |
| 154 | // Fast path - if no translation is needed, return original query directly |
| 155 | if !self.needs_cast_translation && !self.needs_regex_translation && |
| 156 | !self.needs_schema_translation && !self.needs_numeric_cast_translation && |
| 157 | !self.needs_array_translation && !self.needs_delete_using_translation && |
| 158 | !self.needs_batch_update_translation && !self.needs_datetime_translation && |
| 159 | !self.needs_pg_table_is_visible_translation && !self.needs_session_identifier_translation { |
| 160 | // Check if this is an INSERT that might need decimal rewrite |
| 161 | if matches!(QueryTypeDetector::detect_query_type(self.original_query), QueryType::Insert) { |
| 162 | if let Some(table_name) = extract_insert_table_name(self.original_query) |
| 163 | && !_schema_cache.has_decimal_columns(&table_name) { |
| 164 | return Ok(self.original_query); |
| 165 | } |
| 166 | } else if !matches!(QueryTypeDetector::detect_query_type(self.original_query), QueryType::Select) { |
| 167 | // Not a SELECT or INSERT that needs decimal handling |
| 168 | return Ok(self.original_query); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | let mut current_query = Cow::Borrowed(self.original_query); |
| 173 | |
| 174 | // Step 1: pg_table_is_visible removal if needed (must come before array translation) |
| 175 | if self.needs_pg_table_is_visible_translation { |
| 176 | tracing::debug!("Before pg_table_is_visible translation: {}", current_query); |
| 177 | let translated = crate::translator::PgTableIsVisibleTranslator::translate(¤t_query); |
| 178 | tracing::debug!("After pg_table_is_visible translation: {}", translated); |
| 179 | current_query = Cow::Owned(translated); |
| 180 | } |
| 181 | |
| 182 | // Step 1.5: Session identifier translation if needed (add parentheses to current_user, session_user) |
| 183 | if self.needs_session_identifier_translation { |
| 184 | tracing::debug!("Before session identifier translation: {}", current_query); |
| 185 | let translated = crate::translator::SessionIdentifierTranslator::translate_query(¤t_query); |
| 186 | tracing::debug!("After session identifier translation: {}", translated); |
| 187 | current_query = Cow::Owned(translated); |
| 188 | } |
| 189 | |
| 190 | // Step 2: Schema prefix removal if needed |
| 191 | if self.needs_schema_translation { |
| 192 | tracing::debug!("Before schema translation: {}", current_query); |
| 193 | let translated = crate::translator::SchemaPrefixTranslator::translate_query(¤t_query); |
| 194 | tracing::debug!("After schema translation: {}", translated); |
| 195 | current_query = Cow::Owned(translated); |
| 196 | } |
| 197 | |
| 198 | // Step 3: Numeric cast translation MUST come before general cast translation |
| 199 | // to ensure CAST(x AS NUMERIC(p,s)) is handled properly |
| 200 | if self.needs_numeric_cast_translation { |
| 201 | tracing::debug!("Before numeric cast translation: {}", current_query); |
no test coverage detected