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

Function split_sql_queries

src/parser/sql_parser.rs:69–149  ·  view source on GitHub ↗

Split SQL content into individual queries Handles semicolons inside strings and comments properly

(content: &str)

Source from the content-addressed store, hash-verified

67/// Split SQL content into individual queries
68/// Handles semicolons inside strings and comments properly
69fn split_sql_queries(content: &str) -> Vec<String> {
70 let mut queries = Vec::new();
71 let mut current_query = String::new();
72 let mut in_string = false;
73 let mut in_comment = false;
74 let mut string_delimiter = '\0';
75 let mut chars = content.chars().peekable();
76
77 while let Some(ch) = chars.next() {
78 match ch {
79 // Handle string literals
80 '\'' | '"' if !in_comment => {
81 if !in_string {
82 in_string = true;
83 string_delimiter = ch;
84 } else if ch == string_delimiter {
85 // Check for escaped quotes
86 if chars.peek() == Some(&ch) {
87 current_query.push(ch);
88 current_query.push(chars.next().unwrap()); // consume the escaped quote
89 continue;
90 } else {
91 in_string = false;
92 }
93 }
94 current_query.push(ch);
95 }
96
97 // Handle single-line comments
98 '-' if !in_string && !in_comment && chars.peek() == Some(&'-') => {
99 in_comment = true;
100 current_query.push(ch);
101 current_query.push(chars.next().unwrap()); // consume second dash
102 }
103
104 // Handle multi-line comments
105 '/' if !in_string && !in_comment && chars.peek() == Some(&'*') => {
106 in_comment = true;
107 current_query.push(ch);
108 current_query.push(chars.next().unwrap()); // consume asterisk
109 }
110
111 // Non-comment dash or slash
112 '-' | '/' if !in_string && !in_comment => {
113 current_query.push(ch);
114 }
115
116 '*' if in_comment && !in_string => {
117 current_query.push(ch);
118 if chars.peek() == Some(&'/') {
119 current_query.push(chars.next().unwrap()); // consume slash
120 in_comment = false;
121 }
122 }
123
124 // Handle newlines (end single-line comments)
125 '\n' | '\r' if in_comment => {
126 in_comment = false;

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected