44mod cli;
55mod database;
66mod highlighter;
7+ mod output;
78mod repl;
89mod table;
910
@@ -15,44 +16,54 @@ fn main() {
1516 let config = parse_args ( ) ;
1617 let query_source = config. query_source . clone ( ) ;
1718 let table_mode = config. table_mode ;
19+ let output_path = config. output_path . clone ( ) ;
1820
19- match query_source {
20- QuerySource :: Query ( sql) => {
21- let mut connection = database:: initialize_connection ( config) ;
22- if let Err ( e) = database:: execute_query ( & mut connection, & sql, table_mode) {
23- eprintln ! ( "{e}" ) ;
24- exit ( 1 ) ;
25- }
26- }
27- QuerySource :: File ( path) => {
28- let sql = match std:: fs:: read_to_string ( & path) {
29- Ok ( content) => content,
30- Err ( e) => {
31- eprintln ! ( "Failed to read file {}: {e}" , path. display( ) ) ;
32- exit ( 1 ) ;
33- }
34- } ;
35- let mut connection = database:: initialize_connection ( config) ;
36- if let Err ( e) = database:: execute_query ( & mut connection, & sql, table_mode) {
37- eprintln ! ( "{e}" ) ;
38- exit ( 1 ) ;
39- }
40- }
21+ if matches ! ( query_source, QuerySource :: Interactive ) {
22+ let connection = database:: initialize_connection ( config) ;
23+ repl:: run_repl ( connection, table_mode) ;
24+ return ;
25+ }
26+
27+ let sql = match query_source {
28+ QuerySource :: Query ( sql) => sql,
29+ QuerySource :: File ( path) => std:: fs:: read_to_string ( & path) . unwrap_or_else ( |e| {
30+ eprintln ! ( "Failed to read file {}: {e}" , path. display( ) ) ;
31+ exit ( 1 ) ;
32+ } ) ,
4133 QuerySource :: Stdin => {
4234 let mut sql = String :: new ( ) ;
43- if let Err ( e) = std:: io:: stdin ( ) . read_to_string ( & mut sql) {
44- eprintln ! ( "Failed to read from stdin: {e}" ) ;
45- exit ( 1 ) ;
46- }
47- let mut connection = database:: initialize_connection ( config) ;
48- if let Err ( e) = database:: execute_query ( & mut connection, & sql, table_mode) {
49- eprintln ! ( "{e}" ) ;
50- exit ( 1 ) ;
51- }
52- }
53- QuerySource :: Interactive => {
54- let connection = database:: initialize_connection ( config) ;
55- repl:: run_repl ( connection, table_mode) ;
35+ std:: io:: stdin ( )
36+ . read_to_string ( & mut sql)
37+ . unwrap_or_else ( |e| {
38+ eprintln ! ( "Failed to read from stdin: {e}" ) ;
39+ exit ( 1 ) ;
40+ } ) ;
41+ sql
5642 }
43+ QuerySource :: Interactive => unreachable ! ( ) ,
44+ } ;
45+
46+ let mut connection = database:: initialize_connection ( config) ;
47+ let batches = database:: execute_query ( & mut connection, & sql) . unwrap_or_else ( |e| {
48+ eprintln ! ( "{e}" ) ;
49+ exit ( 1 ) ;
50+ } ) ;
51+ if let Err ( e) = output_results ( & batches, table_mode, output_path. as_deref ( ) ) {
52+ eprintln ! ( "{e}" ) ;
53+ exit ( 1 ) ;
54+ }
55+ }
56+
57+ fn output_results (
58+ batches : & [ arrow_array:: RecordBatch ] ,
59+ table_mode : table:: TableMode ,
60+ output_path : Option < & std:: path:: Path > ,
61+ ) -> Result < ( ) , String > {
62+ if let Some ( path) = output_path {
63+ output:: write_batches_to_file ( batches, path)
64+ . map_err ( |e| format ! ( "Failed to write output file: {e}" ) )
65+ } else {
66+ table:: print_batches ( batches, table_mode)
67+ . map_err ( |e| format ! ( "Failed to print batches: {e}" ) )
5768 }
5869}
0 commit comments