Skip to content

Commit 8f954d2

Browse files
committed
Print records with type info
1 parent 3b6d96d commit 8f954d2

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

crates/duckdb/examples/repl.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// cargo run --example repl
33
// cargo run --example repl path/to/database.db
44

5-
use duckdb::{
6-
arrow::{record_batch::RecordBatch, util::pretty::print_batches},
7-
Connection, Result as DuckResult,
8-
};
5+
use duckdb::{arrow::record_batch::RecordBatch, Connection, Result as DuckResult};
96
use rustyline::{error::ReadlineError, history::DefaultHistory, Config, Editor};
107

118
const HISTORY_FILE: &str = ".duckdb_rs_history";
@@ -43,7 +40,7 @@ impl SqlRepl {
4340
continue;
4441
}
4542
match line {
46-
".quit" | ".exit" => break,
43+
".quit" => break,
4744
".help" => self.show_help(),
4845
".schema" => {
4946
if let Err(e) = self.show_schema() {
@@ -86,7 +83,6 @@ impl SqlRepl {
8683
println!("Available commands:");
8784
println!(" .help - Show this help message");
8885
println!(" .quit - Exit the REPL");
89-
println!(" .exit - Exit the REPL");
9086
println!(" .schema - Show database schema");
9187
println!(" .tables - Show all tables");
9288
println!();
@@ -112,7 +108,7 @@ impl SqlRepl {
112108
if rbs.is_empty() || rbs[0].num_rows() == 0 {
113109
println!("No tables found in database.");
114110
} else {
115-
print_batches(&rbs).unwrap();
111+
print_records(&rbs);
116112
}
117113

118114
Ok(())
@@ -125,7 +121,7 @@ impl SqlRepl {
125121
if rbs.is_empty() || rbs[0].num_rows() == 0 {
126122
println!("No tables found in database.");
127123
} else {
128-
print_batches(&rbs).unwrap();
124+
print_records(&rbs);
129125
}
130126

131127
Ok(())
@@ -149,7 +145,7 @@ impl SqlRepl {
149145
if rbs.is_empty() || rbs[0].num_rows() == 0 {
150146
println!("No results returned.");
151147
} else {
152-
print_batches(&rbs).unwrap();
148+
print_records(&rbs);
153149
}
154150
} else {
155151
// Execute non-query statements
@@ -160,6 +156,14 @@ impl SqlRepl {
160156
}
161157
}
162158

159+
fn print_records(rbs: &[RecordBatch]) {
160+
let options = arrow::util::display::FormatOptions::default()
161+
.with_display_error(true)
162+
.with_types_info(true);
163+
let str = arrow::util::pretty::pretty_format_batches_with_options(rbs, &options).unwrap();
164+
println!("{str}");
165+
}
166+
163167
fn main() -> DuckResult<()> {
164168
let args: Vec<String> = std::env::args().collect();
165169

0 commit comments

Comments
 (0)