@@ -10,6 +10,7 @@ const HISTORY_FILE: &str = ".duckdb_rs_history";
1010struct SqlRepl {
1111 conn : Connection ,
1212 editor : Editor < ( ) , DefaultHistory > ,
13+ debug : bool ,
1314}
1415
1516impl SqlRepl {
@@ -22,10 +23,14 @@ impl SqlRepl {
2223 let editor = {
2324 let config = Config :: builder ( ) . auto_add_history ( true ) . build ( ) ;
2425 let mut editor = Editor :: with_config ( config) . expect ( "Failed to create editor" ) ;
25- let _ = editor. load_history ( HISTORY_FILE ) ; // Load history, might not exist yet
26+ let _ = editor. load_history ( HISTORY_FILE ) ; // History might not exist yet
2627 editor
2728 } ;
28- Ok ( SqlRepl { conn, editor } )
29+ Ok ( SqlRepl {
30+ conn,
31+ editor,
32+ debug : false ,
33+ } )
2934 }
3035
3136 fn run ( & mut self ) -> DuckResult < ( ) > {
@@ -52,6 +57,7 @@ impl SqlRepl {
5257 eprintln ! ( "Error showing tables: {e}" ) ;
5358 }
5459 }
60+ ".debug" => self . debug = !self . debug ,
5561 _ => {
5662 if let Err ( e) = self . execute_sql ( line) {
5763 eprintln ! ( "{e}" ) ;
@@ -85,6 +91,7 @@ impl SqlRepl {
8591 println ! ( " .quit - Exit the REPL" ) ;
8692 println ! ( " .schema - Show database schema" ) ;
8793 println ! ( " .tables - Show all tables" ) ;
94+ println ! ( " .debug - Toggle debug output" ) ;
8895 println ! ( ) ;
8996 println ! ( "Keyboard shortcuts:" ) ;
9097 println ! ( " Up/Down - Navigate command history" ) ;
@@ -108,7 +115,7 @@ impl SqlRepl {
108115 if rbs. is_empty ( ) || rbs[ 0 ] . num_rows ( ) == 0 {
109116 println ! ( "No tables found in database." ) ;
110117 } else {
111- print_records ( & rbs) ;
118+ self . print_records ( & rbs) ;
112119 }
113120
114121 Ok ( ( ) )
@@ -121,7 +128,7 @@ impl SqlRepl {
121128 if rbs. is_empty ( ) || rbs[ 0 ] . num_rows ( ) == 0 {
122129 println ! ( "No tables found in database." ) ;
123130 } else {
124- print_records ( & rbs) ;
131+ self . print_records ( & rbs) ;
125132 }
126133
127134 Ok ( ( ) )
@@ -145,7 +152,7 @@ impl SqlRepl {
145152 if rbs. is_empty ( ) || rbs[ 0 ] . num_rows ( ) == 0 {
146153 println ! ( "No results returned." ) ;
147154 } else {
148- print_records ( & rbs) ;
155+ self . print_records ( & rbs) ;
149156 }
150157 } else {
151158 // Execute non-query statements
@@ -154,14 +161,18 @@ impl SqlRepl {
154161
155162 Ok ( ( ) )
156163 }
157- }
158164
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+ fn print_records ( & self , rbs : & [ RecordBatch ] ) {
166+ let options = arrow:: util:: display:: FormatOptions :: default ( )
167+ . with_display_error ( true )
168+ . with_types_info ( true ) ;
169+ let table = arrow:: util:: pretty:: pretty_format_batches_with_options ( rbs, & options) . unwrap ( ) ;
170+ println ! ( "{table}" ) ;
171+
172+ if self . debug {
173+ dbg ! ( rbs) ;
174+ }
175+ }
165176}
166177
167178fn main ( ) -> DuckResult < ( ) > {
0 commit comments