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 — Fix empty SQL result set rendering · jwcraig/sql-server-cli@97d191e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Cargo.lock
  • Cargo.toml
  • src/db
    • executor.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Cargo.lock‎

Copy file name to clipboardExpand all lines: Cargo.lock
+2-1Lines changed: 2 additions & 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
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sscli"
3-
version = "0.1.12"
3+
version = "0.1.13"
44
edition = "2024"
55
rust-version = "1.85"
66
description = "Agent-friendly token efficient SQL Server CLI tool for database inspection"
@@ -24,6 +24,7 @@ owo-colors = "4.0"
2424
dirs = "5.0"
2525
csv = "1.3"
2626
dotenvy = "0.15.7"
27+
futures-util = "0.3"
2728
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls"] }
2829
semver = "1.0"
2930
regex = "1.10"
Collapse file

‎src/db/executor.rs‎

Copy file name to clipboardExpand all lines: src/db/executor.rs
+114-27Lines changed: 114 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Result;
2+
use futures_util::TryStreamExt;
23

34
use crate::db::types::{Column, ResultSet, Value};
45
use crate::error::{AppError, ErrorKind};
@@ -15,39 +16,84 @@ pub async fn run_query(
1516
}
1617

1718
pub async fn collect_result_sets(stream: tiberius::QueryStream<'_>) -> Result<Vec<ResultSet>> {
18-
let result_sets = stream
19-
.into_results()
20-
.await
21-
.map_err(|err| AppError::new(ErrorKind::Query, err.to_string()))?;
22-
let mut output = Vec::new();
23-
24-
for rows in result_sets {
25-
let columns = rows
26-
.first()
27-
.map(|row| {
28-
row.columns()
29-
.iter()
30-
.map(|col| Column {
31-
name: col.name().to_string(),
32-
data_type: None,
33-
})
34-
.collect::<Vec<_>>()
35-
})
36-
.unwrap_or_default();
19+
let mut stream = stream;
20+
let mut collector = ResultSetCollector::default();
3721

38-
let mut converted_rows = Vec::new();
39-
for row in rows {
40-
let values = row.cells().map(|(_, data)| map_column_data(data)).collect();
41-
converted_rows.push(values);
22+
while let Some(item) = stream
23+
.try_next()
24+
.await
25+
.map_err(|err| AppError::new(ErrorKind::Query, err.to_string()))?
26+
{
27+
match item {
28+
tiberius::QueryItem::Metadata(metadata) => {
29+
collector.start_result_set(metadata.columns());
30+
}
31+
tiberius::QueryItem::Row(row) => {
32+
collector.push_row(&row);
33+
}
4234
}
35+
}
36+
37+
Ok(collector.finish())
38+
}
39+
40+
#[derive(Debug, Default)]
41+
struct ResultSetCollector {
42+
result_sets: Vec<ResultSet>,
43+
current: Option<ResultSet>,
44+
}
4345

44-
output.push(ResultSet {
45-
columns,
46-
rows: converted_rows,
46+
impl ResultSetCollector {
47+
fn start_result_set(&mut self, columns: &[tiberius::Column]) {
48+
self.finish_current();
49+
self.current = Some(ResultSet {
50+
columns: map_columns(columns),
51+
rows: Vec::new(),
4752
});
4853
}
4954

50-
Ok(output)
55+
fn push_row(&mut self, row: &tiberius::Row) {
56+
if self.current.is_none() {
57+
self.current = Some(ResultSet {
58+
columns: map_columns(row.columns()),
59+
rows: Vec::new(),
60+
});
61+
}
62+
let values = row.cells().map(|(_, data)| map_column_data(data)).collect();
63+
self.push_values(values);
64+
}
65+
66+
fn push_values(&mut self, values: Vec<Value>) {
67+
if self.current.is_none() {
68+
self.current = Some(ResultSet::default());
69+
}
70+
self.current
71+
.as_mut()
72+
.expect("current result set must exist before pushing row values")
73+
.rows
74+
.push(values);
75+
}
76+
77+
fn finish(mut self) -> Vec<ResultSet> {
78+
self.finish_current();
79+
self.result_sets
80+
}
81+
82+
fn finish_current(&mut self) {
83+
if let Some(result_set) = self.current.take() {
84+
self.result_sets.push(result_set);
85+
}
86+
}
87+
}
88+
89+
fn map_columns(columns: &[tiberius::Column]) -> Vec<Column> {
90+
columns
91+
.iter()
92+
.map(|col| Column {
93+
name: col.name().to_string(),
94+
data_type: None,
95+
})
96+
.collect()
5197
}
5298

5399
fn map_column_data(data: &tiberius::ColumnData<'_>) -> Value {
@@ -252,3 +298,44 @@ fn format_tds_time(time: tiberius::time::Time) -> String {
252298
format!("{:02}:{:02}:{:02}", hours, mins, secs)
253299
}
254300
}
301+
302+
#[cfg(test)]
303+
mod tests {
304+
use super::*;
305+
use tiberius::{Column as TdsColumn, ColumnType};
306+
307+
fn tds_column(name: &str) -> TdsColumn {
308+
TdsColumn::new(name.to_string(), ColumnType::Int4)
309+
}
310+
311+
#[test]
312+
fn collector_preserves_headers_for_empty_result_set_before_rows() {
313+
let mut collector = ResultSetCollector::default();
314+
collector.start_result_set(&[tds_column("empty_int")]);
315+
collector.start_result_set(&[tds_column("full_int")]);
316+
collector.push_values(vec![Value::Int(7)]);
317+
318+
let result_sets = collector.finish();
319+
320+
assert_eq!(result_sets.len(), 2);
321+
assert_eq!(result_sets[0].columns[0].name, "empty_int");
322+
assert!(result_sets[0].rows.is_empty());
323+
assert_eq!(result_sets[1].columns[0].name, "full_int");
324+
assert_eq!(result_sets[1].rows, vec![vec![Value::Int(7)]]);
325+
}
326+
327+
#[test]
328+
fn collector_keeps_consecutive_empty_result_sets() {
329+
let mut collector = ResultSetCollector::default();
330+
collector.start_result_set(&[tds_column("first_empty")]);
331+
collector.start_result_set(&[tds_column("second_empty")]);
332+
333+
let result_sets = collector.finish();
334+
335+
assert_eq!(result_sets.len(), 2);
336+
assert_eq!(result_sets[0].columns[0].name, "first_empty");
337+
assert!(result_sets[0].rows.is_empty());
338+
assert_eq!(result_sets[1].columns[0].name, "second_empty");
339+
assert!(result_sets[1].rows.is_empty());
340+
}
341+
}

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.