MCPcopy Create free account
hub / github.com/JasonShin/sqlx-ts / extract_annotations_from_sql

Function extract_annotations_from_sql

src/parser/sql_parser.rs:172–229  ·  view source on GitHub ↗

Extract annotations from SQL content and return cleaned query Supports @name and @db annotations

(content: &str)

Source from the content-addressed store, hash-verified

170/// Extract annotations from SQL content and return cleaned query
171/// Supports @name and @db annotations
172fn extract_annotations_from_sql(content: &str) -> (Option<String>, Option<String>, String) {
173 let mut query_name = None;
174 let mut db_connection = None;
175 let mut cleaned_lines = Vec::new();
176
177 // Regex patterns for annotations
178 let name_re = Regex::new(r"@name:\s*(.+)").unwrap();
179 let db_re = Regex::new(r"@db:\s*(.+)").unwrap();
180
181 for line in content.lines() {
182 let trimmed_line = line.trim();
183
184 // Skip empty lines
185 if trimmed_line.is_empty() {
186 cleaned_lines.push(line);
187 continue;
188 }
189
190 // Check for name annotation in comments
191 if let Some(stripped) = trimmed_line.strip_prefix("--") {
192 let comment_content = &stripped.trim();
193
194 if let Some(captures) = name_re.captures(comment_content) {
195 query_name = Some(captures.get(1).unwrap().as_str().trim().to_string());
196 continue; // Don't include annotation lines in final query
197 }
198
199 if let Some(captures) = db_re.captures(comment_content) {
200 db_connection = Some(captures.get(1).unwrap().as_str().trim().to_string());
201 continue; // Don't include annotation lines in final query
202 }
203 }
204
205 // Check for annotations in multi-line comments
206 if trimmed_line.starts_with("/*") || trimmed_line.contains("@name:") || trimmed_line.contains("@db:") {
207 if let Some(captures) = name_re.captures(trimmed_line) {
208 query_name = Some(captures.get(1).unwrap().as_str().trim().to_string());
209 }
210
211 if let Some(captures) = db_re.captures(trimmed_line) {
212 db_connection = Some(captures.get(1).unwrap().as_str().trim().to_string());
213 }
214
215 // Only skip pure annotation lines
216 if trimmed_line.starts_with("/*")
217 && (trimmed_line.contains("@name:") || trimmed_line.contains("@db:"))
218 && trimmed_line.ends_with("*/")
219 {
220 continue;
221 }
222 }
223
224 cleaned_lines.push(line);
225 }
226
227 let cleaned_query = cleaned_lines.join("\n");
228 (query_name, db_connection, cleaned_query)
229}

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected