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

Function extract_sql_queries_from_file

src/parser/sql_parser.rs:30–65  ·  view source on GitHub ↗

Extract SQL queries from raw SQL file content Supports multiple queries separated by semicolons and annotations

(content: &str, file_path: &Path)

Source from the content-addressed store, hash-verified

28/// Extract SQL queries from raw SQL file content
29/// Supports multiple queries separated by semicolons and annotations
30fn extract_sql_queries_from_file(content: &str, file_path: &Path) -> Result<Vec<SQL>> {
31 let mut queries = Vec::new();
32
33 // Split content by semicolons to handle multiple queries
34 let query_blocks = split_sql_queries(content);
35
36 for (index, block) in query_blocks.iter().enumerate() {
37 let trimmed_block = block.trim();
38
39 // Skip empty blocks or comment-only blocks
40 if trimmed_block.is_empty() || is_comment_only(trimmed_block) {
41 continue;
42 }
43
44 // Extract annotations and clean query
45 let (query_name, _db_connection, cleaned_query) = extract_annotations_from_sql(trimmed_block);
46
47 // Skip if no actual SQL content after cleaning
48 if cleaned_query.trim().is_empty() {
49 continue;
50 }
51
52 // Generate default name if not provided via annotation
53 let var_decl_name = query_name.or_else(|| generate_default_query_name(file_path, index));
54
55 let sql = SQL {
56 query: cleaned_query,
57 var_decl_name,
58 span: DUMMY_SP.into(),
59 };
60
61 queries.push(sql);
62 }
63
64 Ok(queries)
65}
66
67/// Split SQL content into individual queries
68/// Handles semicolons inside strings and comments properly

Callers 1

parse_sql_fileFunction · 0.85

Calls 4

split_sql_queriesFunction · 0.85
is_comment_onlyFunction · 0.85

Tested by

no test coverage detected