@@ -73,32 +73,50 @@ pub fn replace_named_params(sql: &str, params: &[SqlParam], start_index: usize) |
73 | 73 | (out, matched) |
74 | 74 | } |
75 | 75 |
|
| 76 | +/// Split a script into batches on `GO` lines, as `sqlcmd` does. |
| 77 | +/// |
| 78 | +/// Each batch is the script text between two `GO` lines, byte for byte, |
| 79 | +/// line endings and surrounding whitespace included. The server stores that |
| 80 | +/// text as a module definition, so trimming it would make a `CREATE TRIGGER` |
| 81 | +/// differ from the one `sqlcmd` creates. Whitespace-only batches are dropped. |
| 82 | +/// |
| 83 | +/// # Arguments |
| 84 | +/// |
| 85 | +/// * `script` - full script text; a leading UTF-8 byte order mark is ignored |
| 86 | +/// |
| 87 | +/// # Returns |
| 88 | +/// |
| 89 | +/// Batches in execution order, with `GO n` repeating its batch `n` times. |
76 | 90 | pub fn split_batches(script: &str) -> Vec<String> { |
| 91 | + let script = script.strip_prefix('\u{feff}').unwrap_or(script); |
77 | 92 | let mut batches = Vec::new(); |
78 | | - let mut current = Vec::new(); |
79 | 93 | let mut state = ScanState::default(); |
80 | | - |
81 | | - for line in script.lines() { |
82 | | - if let Some(repeat) = go_repeat_count(line, &mut state) { |
83 | | - if !current.is_empty() { |
84 | | - let batch = current.join("\n").trim().to_string(); |
85 | | - for _ in 0..repeat { |
86 | | - batches.push(batch.clone()); |
87 | | - } |
88 | | - current.clear(); |
89 | | - } |
90 | | - } else { |
91 | | - current.push(line.to_string()); |
| 94 | + let mut batch_start = 0; |
| 95 | + let mut line_start = 0; |
| 96 | + |
| 97 | + // `split_inclusive` keeps each line's `\n` (and any `\r` before it), so |
| 98 | + // byte offsets map straight back into `script`. |
| 99 | + for line in script.split_inclusive('\n') { |
| 100 | + let line_end = line_start + line.len(); |
| 101 | + let content = line.trim_end_matches(['\n', '\r']); |
| 102 | + if let Some(repeat) = go_repeat_count(content, &mut state) { |
| 103 | + push_batch(&mut batches, &script[batch_start..line_start], repeat); |
| 104 | + batch_start = line_end; |
92 | 105 | } |
| 106 | + line_start = line_end; |
93 | 107 | } |
94 | | - |
95 | | - if !current.is_empty() { |
96 | | - batches.push(current.join("\n").trim().to_string()); |
97 | | - } |
| 108 | + push_batch(&mut batches, &script[batch_start..], 1); |
98 | 109 |
|
99 | 110 | batches |
100 | 111 | } |
101 | 112 |
|
| 113 | +fn push_batch(batches: &mut Vec<String>, batch: &str, repeat: usize) { |
| 114 | + if batch.trim().is_empty() { |
| 115 | + return; |
| 116 | + } |
| 117 | + batches.extend(std::iter::repeat_n(batch, repeat).map(str::to_string)); |
| 118 | +} |
| 119 | + |
102 | 120 | #[derive(Debug, Clone, Copy, Default)] |
103 | 121 | struct ScanState { |
104 | 122 | in_single_quote: bool, |
@@ -140,11 +158,7 @@ fn go_repeat_count(line: &str, state: &mut ScanState) -> Option<usize> { |
140 | 158 |
|
141 | 159 | fn visible_sql_text(line: &str, state: &mut ScanState) -> String { |
142 | 160 | let mut visible = String::new(); |
143 | | - let mut chars = line |
144 | | - .strip_prefix('\u{feff}') |
145 | | - .unwrap_or(line) |
146 | | - .chars() |
147 | | - .peekable(); |
| 161 | + let mut chars = line.chars().peekable(); |
148 | 162 |
|
149 | 163 | while let Some(ch) = chars.next() { |
150 | 164 | if state.block_comment_depth > 0 { |
@@ -283,62 +297,62 @@ mod tests { |
283 | 297 | let script = "SELECT 1\nGO\nSELECT 2\nGO\nSELECT 3"; |
284 | 298 | let batches = split_batches(script); |
285 | 299 | assert_eq!(batches.len(), 3); |
286 | | - assert_eq!(batches[0], "SELECT 1"); |
| 300 | + assert_eq!(batches[0], "SELECT 1\n"); |
287 | 301 | } |
288 | 302 |
|
289 | 303 | #[test] |
290 | 304 | fn splits_batches_on_go_with_repeat_count() { |
291 | 305 | let script = "SELECT 1\nGO 2\nSELECT 3"; |
292 | 306 | let batches = split_batches(script); |
293 | | - assert_eq!(batches, vec!["SELECT 1", "SELECT 1", "SELECT 3"]); |
| 307 | + assert_eq!(batches, vec!["SELECT 1\n", "SELECT 1\n", "SELECT 3"]); |
294 | 308 | } |
295 | 309 |
|
296 | 310 | #[test] |
297 | 311 | fn ignores_go_inside_single_quoted_string() { |
298 | 312 | let script = "SELECT 'GO'\nGO\nSELECT 2"; |
299 | 313 | let batches = split_batches(script); |
300 | | - assert_eq!(batches, vec!["SELECT 'GO'", "SELECT 2"]); |
| 314 | + assert_eq!(batches, vec!["SELECT 'GO'\n", "SELECT 2"]); |
301 | 315 | } |
302 | 316 |
|
303 | 317 | #[test] |
304 | 318 | fn ignores_go_inside_comments() { |
305 | 319 | let script = "SELECT 1\n-- GO\n/* GO */\nGO\nSELECT 2"; |
306 | 320 | let batches = split_batches(script); |
307 | | - assert_eq!(batches, vec!["SELECT 1\n-- GO\n/* GO */", "SELECT 2"]); |
| 321 | + assert_eq!(batches, vec!["SELECT 1\n-- GO\n/* GO */\n", "SELECT 2"]); |
308 | 322 | } |
309 | 323 |
|
310 | 324 | #[test] |
311 | 325 | fn supports_go_followed_by_comment() { |
312 | 326 | let script = "SELECT 1\nGO -- split here\nSELECT 2"; |
313 | 327 | let batches = split_batches(script); |
314 | | - assert_eq!(batches, vec!["SELECT 1", "SELECT 2"]); |
| 328 | + assert_eq!(batches, vec!["SELECT 1\n", "SELECT 2"]); |
315 | 329 | } |
316 | 330 |
|
317 | 331 | #[test] |
318 | 332 | fn ignores_go_inside_multiline_block_comment() { |
319 | 333 | let script = "/*\nGO\n*/\nSELECT 1\nGO\nSELECT 2"; |
320 | 334 | let batches = split_batches(script); |
321 | | - assert_eq!(batches, vec!["/*\nGO\n*/\nSELECT 1", "SELECT 2"]); |
| 335 | + assert_eq!(batches, vec!["/*\nGO\n*/\nSELECT 1\n", "SELECT 2"]); |
322 | 336 | } |
323 | 337 |
|
324 | 338 | #[test] |
325 | 339 | fn splits_on_lowercase_indented_go_with_trailing_space() { |
326 | 340 | let script = "SELECT 1\n go \nSELECT 2"; |
327 | | - assert_eq!(split_batches(script), vec!["SELECT 1", "SELECT 2"]); |
| 341 | + assert_eq!(split_batches(script), vec!["SELECT 1\n", "SELECT 2"]); |
328 | 342 | } |
329 | 343 |
|
330 | 344 | #[test] |
331 | 345 | fn splits_crlf_scripts() { |
332 | 346 | let script = "SELECT 1\r\nGO\r\nSELECT 2\r\n"; |
333 | | - assert_eq!(split_batches(script), vec!["SELECT 1", "SELECT 2"]); |
| 347 | + assert_eq!(split_batches(script), vec!["SELECT 1\r\n", "SELECT 2\r\n"]); |
334 | 348 | } |
335 | 349 |
|
336 | 350 | #[test] |
337 | 351 | fn ignores_go_line_inside_multiline_string() { |
338 | 352 | let script = "SELECT 'first\nGO\nlast'\nGO\nSELECT 2"; |
339 | 353 | assert_eq!( |
340 | 354 | split_batches(script), |
341 | | - vec!["SELECT 'first\nGO\nlast'", "SELECT 2"] |
| 355 | + vec!["SELECT 'first\nGO\nlast'\n", "SELECT 2"] |
342 | 356 | ); |
343 | 357 | } |
344 | 358 |
|
@@ -347,7 +361,7 @@ mod tests { |
347 | 361 | let script = "SELECT 1 AS [\nGO\n]\nGOTO done\ndone:\nGO\nSELECT 2"; |
348 | 362 | assert_eq!( |
349 | 363 | split_batches(script), |
350 | | - vec!["SELECT 1 AS [\nGO\n]\nGOTO done\ndone:", "SELECT 2"] |
| 364 | + vec!["SELECT 1 AS [\nGO\n]\nGOTO done\ndone:\n", "SELECT 2"] |
351 | 365 | ); |
352 | 366 | } |
353 | 367 |
|
@@ -360,7 +374,19 @@ mod tests { |
360 | 374 | #[test] |
361 | 375 | fn handles_final_go_and_consecutive_separators() { |
362 | 376 | let script = "SELECT 1\nGO\nGO\nSELECT 2\nGO"; |
363 | | - assert_eq!(split_batches(script), vec!["SELECT 1", "SELECT 2"]); |
| 377 | + assert_eq!(split_batches(script), vec!["SELECT 1\n", "SELECT 2\n"]); |
| 378 | + } |
| 379 | + |
| 380 | + #[test] |
| 381 | + fn keeps_batch_text_verbatim_and_drops_byte_order_mark() { |
| 382 | + let script = "\u{feff}-- header\nCREATE PROC p\nAS\n\tSELECT 1; \n\nGO\n\n SELECT 2\n"; |
| 383 | + assert_eq!( |
| 384 | + split_batches(script), |
| 385 | + vec![ |
| 386 | + "-- header\nCREATE PROC p\nAS\n\tSELECT 1; \n\n", |
| 387 | + "\n SELECT 2\n" |
| 388 | + ] |
| 389 | + ); |
364 | 390 | } |
365 | 391 |
|
366 | 392 | #[test] |
@@ -369,7 +395,7 @@ mod tests { |
369 | 395 | let batches = split_batches(script); |
370 | 396 | assert_eq!( |
371 | 397 | batches, |
372 | | - vec!["/* outer\n/* inner */\nGO\n*/\nSELECT 1", "SELECT 2"] |
| 398 | + vec!["/* outer\n/* inner */\nGO\n*/\nSELECT 1\n", "SELECT 2"] |
373 | 399 | ); |
374 | 400 | } |
375 | 401 | } |