GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #2: send GO batch text verbatim, like sqlcmd; rele… · jwcraig/sql-server-cli@c6583c1

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Cargo.lock
  • Cargo.toml
  • README.md
  • src/commands
    • admin.rs
    • sql.rs
    • sql_utils.rs
  • tests
    • batch_semantics_test.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Cargo.lock‎

Copy file name to clipboardExpand all lines: Cargo.lock
+1-1Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎Cargo.toml‎

Copy file name to clipboardExpand all lines: Cargo.toml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sscli"
3-
version = "0.1.16"
3+
version = "0.1.17"
44
edition = "2024"
55
rust-version = "1.85"
66
description = "Agent-friendly token efficient SQL Server CLI tool for database inspection"
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+3Lines changed: 3 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ Errors (stderr):
430430
- A line holding only `GO` (any case, optionally `GO n` to repeat the batch, or a
431431
trailing `--` comment) ends a batch. `GO` inside a string, bracketed
432432
identifier or comment is left alone.
433+
- Each batch is sent byte for byte, line endings and blank lines included, so
434+
a `CREATE TRIGGER` or `CREATE PROCEDURE` stores the same definition `sqlcmd`
435+
would. A leading UTF-8 byte order mark is ignored.
433436
- Every batch runs in order on one session. A transaction opened in one batch
434437
can be committed in a later one, and `SET` options, `USE` and `#temp` tables
435438
carry over, as they do in `sqlcmd`.
Collapse file

‎src/commands/admin.rs‎

Copy file name to clipboardExpand all lines: src/commands/admin.rs
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ fn execute_admin_sql(
236236
sql: &str,
237237
verify: Option<&VerifyRequest>,
238238
) -> Result<AdminExecution> {
239-
let mut batches = sql_utils::split_batches(sql);
240-
batches.retain(|batch| !batch.trim().is_empty());
239+
let batches = sql_utils::split_batches(sql);
241240
if batches.is_empty() {
242241
return Err(anyhow!("No SQL batches found"));
243242
}
Collapse file

‎src/commands/sql.rs‎

Copy file name to clipboardExpand all lines: src/commands/sql.rs
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ pub fn run(args: &CliArgs, cmd: &SqlArgs) -> Result<()> {
5858
let params = sql_utils::parse_params(&cmd.params)
5959
.map_err(|err| AppError::new(ErrorKind::Query, err.to_string()))?;
6060

61-
let mut batches = sql_utils::split_batches(&sql_text);
62-
batches.retain(|batch| !batch.trim().is_empty());
61+
let batches = sql_utils::split_batches(&sql_text);
6362

6463
if batches.is_empty() {
6564
return Err(anyhow!("No SQL batches found"));
Collapse file

‎src/commands/sql_utils.rs‎

Copy file name to clipboardExpand all lines: src/commands/sql_utils.rs
+60-34Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,50 @@ pub fn replace_named_params(sql: &str, params: &[SqlParam], start_index: usize)
7373
(out, matched)
7474
}
7575

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.
7690
pub fn split_batches(script: &str) -> Vec<String> {
91+
let script = script.strip_prefix('\u{feff}').unwrap_or(script);
7792
let mut batches = Vec::new();
78-
let mut current = Vec::new();
7993
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;
92105
}
106+
line_start = line_end;
93107
}
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);
98109

99110
batches
100111
}
101112

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+
102120
#[derive(Debug, Clone, Copy, Default)]
103121
struct ScanState {
104122
in_single_quote: bool,
@@ -140,11 +158,7 @@ fn go_repeat_count(line: &str, state: &mut ScanState) -> Option<usize> {
140158

141159
fn visible_sql_text(line: &str, state: &mut ScanState) -> String {
142160
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();
148162

149163
while let Some(ch) = chars.next() {
150164
if state.block_comment_depth > 0 {
@@ -283,62 +297,62 @@ mod tests {
283297
let script = "SELECT 1\nGO\nSELECT 2\nGO\nSELECT 3";
284298
let batches = split_batches(script);
285299
assert_eq!(batches.len(), 3);
286-
assert_eq!(batches[0], "SELECT 1");
300+
assert_eq!(batches[0], "SELECT 1\n");
287301
}
288302

289303
#[test]
290304
fn splits_batches_on_go_with_repeat_count() {
291305
let script = "SELECT 1\nGO 2\nSELECT 3";
292306
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"]);
294308
}
295309

296310
#[test]
297311
fn ignores_go_inside_single_quoted_string() {
298312
let script = "SELECT 'GO'\nGO\nSELECT 2";
299313
let batches = split_batches(script);
300-
assert_eq!(batches, vec!["SELECT 'GO'", "SELECT 2"]);
314+
assert_eq!(batches, vec!["SELECT 'GO'\n", "SELECT 2"]);
301315
}
302316

303317
#[test]
304318
fn ignores_go_inside_comments() {
305319
let script = "SELECT 1\n-- GO\n/* GO */\nGO\nSELECT 2";
306320
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"]);
308322
}
309323

310324
#[test]
311325
fn supports_go_followed_by_comment() {
312326
let script = "SELECT 1\nGO -- split here\nSELECT 2";
313327
let batches = split_batches(script);
314-
assert_eq!(batches, vec!["SELECT 1", "SELECT 2"]);
328+
assert_eq!(batches, vec!["SELECT 1\n", "SELECT 2"]);
315329
}
316330

317331
#[test]
318332
fn ignores_go_inside_multiline_block_comment() {
319333
let script = "/*\nGO\n*/\nSELECT 1\nGO\nSELECT 2";
320334
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"]);
322336
}
323337

324338
#[test]
325339
fn splits_on_lowercase_indented_go_with_trailing_space() {
326340
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"]);
328342
}
329343

330344
#[test]
331345
fn splits_crlf_scripts() {
332346
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"]);
334348
}
335349

336350
#[test]
337351
fn ignores_go_line_inside_multiline_string() {
338352
let script = "SELECT 'first\nGO\nlast'\nGO\nSELECT 2";
339353
assert_eq!(
340354
split_batches(script),
341-
vec!["SELECT 'first\nGO\nlast'", "SELECT 2"]
355+
vec!["SELECT 'first\nGO\nlast'\n", "SELECT 2"]
342356
);
343357
}
344358

@@ -347,7 +361,7 @@ mod tests {
347361
let script = "SELECT 1 AS [\nGO\n]\nGOTO done\ndone:\nGO\nSELECT 2";
348362
assert_eq!(
349363
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"]
351365
);
352366
}
353367

@@ -360,7 +374,19 @@ mod tests {
360374
#[test]
361375
fn handles_final_go_and_consecutive_separators() {
362376
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+
);
364390
}
365391

366392
#[test]
@@ -369,7 +395,7 @@ mod tests {
369395
let batches = split_batches(script);
370396
assert_eq!(
371397
batches,
372-
vec!["/* outer\n/* inner */\nGO\n*/\nSELECT 1", "SELECT 2"]
398+
vec!["/* outer\n/* inner */\nGO\n*/\nSELECT 1\n", "SELECT 2"]
373399
);
374400
}
375401
}
Collapse file

‎tests/batch_semantics_test.rs‎

Copy file name to clipboardExpand all lines: tests/batch_semantics_test.rs
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,29 @@ fn sql_stops_at_first_failing_batch_and_rolls_back_open_transaction() {
106106
serde_json::Value::Null
107107
);
108108
}
109+
110+
#[test]
111+
fn sql_sends_batch_text_verbatim_like_sqlcmd() {
112+
if !common::integration_enabled() {
113+
return;
114+
}
115+
// The server stores a module's batch text as its definition, so trailing
116+
// blank lines and whitespace must survive the GO split.
117+
let body = "\r\nCREATE PROCEDURE #sscli_def_probe\r\nAS\r\n\tSELECT 1; \r\n\r\n";
118+
let script = format!(
119+
"\u{feff}{body}GO\r\nSELECT m.definition FROM tempdb.sys.sql_modules m \
120+
WHERE m.object_id = OBJECT_ID(N'tempdb..#sscli_def_probe');\r\n"
121+
);
122+
123+
let output = cargo_bin_cmd!("sscli")
124+
.args(["sql", "--json", "--stdin"])
125+
.write_stdin(script)
126+
.assert()
127+
.success()
128+
.get_output()
129+
.stdout
130+
.clone();
131+
132+
let value: serde_json::Value = serde_json::from_slice(&output).expect("json");
133+
assert_eq!(value["resultSets"][0]["rows"][0][0], body);
134+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.