1+ use cli_table:: { print_stdout, Cell , Table } ;
2+ use multisql:: { Cast , Payload } ;
13use {
2- console:: Style ,
3- dialoguer:: { theme:: ColorfulTheme , Confirm , Editor , Input , Select } ,
4+ dialoguer:: { theme:: ColorfulTheme , Input } ,
45 indicatif:: { ProgressBar , ProgressStyle } ,
56 lazy_static:: lazy_static,
6- multisql:: { CSVStorage , Glue , SledStorage , Storage } ,
7+ multisql:: Glue ,
78} ;
8-
99lazy_static ! {
1010 pub ( crate ) static ref PROGRESS_STYLE : ProgressStyle = ProgressStyle :: default_spinner( )
1111 . template( "{spinner:.magenta} {elapsed:3.red} {msg:.green}" )
@@ -17,90 +17,40 @@ fn main() {
1717 while prompt ( & mut glue) { }
1818}
1919
20- const PROMPT_ACTION : [ & str ; 2 ] = [ "Connect" , "Query" ] ;
21- const PROMPT_KIND : [ & str ; 2 ] = [ "Sled" , "CSV" ] ;
22- const QUERY_KIND : [ & str ; 3 ] = [ "Small" , "Big" , "File" ] ;
23-
2420fn prompt ( glue : & mut Glue ) -> bool {
25- let mut input_theme = ColorfulTheme :: default ( ) ;
26- input_theme. active_item_prefix = Style :: new ( ) . green ( ) . apply_to ( String :: from ( "•-" ) ) ;
27-
28- match Select :: with_theme ( & input_theme)
29- . items ( & PROMPT_ACTION )
30- . default ( 0 )
21+ let query: String = Input :: with_theme ( & ColorfulTheme :: default ( ) )
22+ . with_prompt ( "Query" )
3123 . interact ( )
32- . unwrap ( )
33- {
34- 0 => {
35- let name = Input :: with_theme ( & input_theme)
36- . with_prompt ( "Name" )
37- . interact ( )
38- . unwrap ( ) ;
39- let new_storage = match Select :: with_theme ( & input_theme)
40- . items ( & PROMPT_KIND )
41- . default ( 0 )
42- . interact ( )
43- . unwrap ( )
44- {
45- 0 => {
46- let path: String = Input :: with_theme ( & input_theme)
47- . with_prompt ( "Path" )
48- . interact ( )
49- . unwrap ( ) ;
50- Storage :: new_sled ( SledStorage :: new ( & path) . unwrap ( ) )
51- }
52- 1 => {
53- let path: String = Input :: with_theme ( & input_theme)
54- . with_prompt ( "Path" )
55- . interact ( )
56- . unwrap ( ) ;
57- Storage :: new_csv ( CSVStorage :: new ( & path) . unwrap ( ) )
58- }
59- _ => unreachable ! ( ) ,
60- } ;
61- glue. extend ( vec ! [ Glue :: new( name, new_storage) ] ) ;
62- }
63- 1 => {
64- let query = match Select :: with_theme ( & input_theme)
65- . items ( & QUERY_KIND )
66- . default ( 0 )
67- . interact ( )
68- . unwrap ( )
69- {
70- 0 => Input :: with_theme ( & input_theme)
71- . with_prompt ( "Query" )
72- . interact ( )
73- . unwrap ( ) ,
74- 1 => {
75- let text = Editor :: new ( )
76- . extension ( "sql" )
77- . require_save ( false )
78- . edit ( "" )
79- . unwrap ( )
80- . unwrap ( ) ;
81- println ! ( "{}" , text) ;
82- Confirm :: with_theme ( & input_theme)
83- . with_prompt ( "Run" )
84- . default ( true )
85- . interact ( )
86- . unwrap ( ) ;
87- text
88- }
89- 2 => unimplemented ! ( ) ,
90- _ => unreachable ! ( ) ,
91- } ;
92-
93- let progress = ProgressBar :: new_spinner ( )
94- . with_message ( format ! ( "Running Query" ) )
95- . with_style ( PROGRESS_STYLE . clone ( ) ) ;
96- progress. enable_steady_tick ( 200 ) ;
97-
98- glue. execute ( & query) . unwrap ( ) ;
99-
100- progress. finish ( ) ;
24+ . unwrap ( ) ;
25+
26+ let progress = ProgressBar :: new_spinner ( )
27+ . with_message ( format ! ( "Running Query" ) )
28+ . with_style ( PROGRESS_STYLE . clone ( ) ) ;
29+ progress. enable_steady_tick ( 200 ) ;
30+ let result = glue. execute ( & query) ;
31+ progress. finish ( ) ;
32+
33+ match result {
34+ Err ( err) => println ! ( "{:?}" , err) ,
35+ Ok ( Payload :: Select { labels, rows } ) => {
36+ let table = rows
37+ . into_iter ( )
38+ . map ( |row| {
39+ row. 0
40+ . into_iter ( )
41+ . map ( |value| {
42+ let string: String = value. cast ( ) . unwrap ( ) ;
43+ string. cell ( )
44+ } )
45+ . collect ( )
46+ } )
47+ . collect :: < Vec < Vec < _ > > > ( )
48+ . table ( )
49+ . title ( labels. into_iter ( ) . map ( |label| label. cell ( ) ) . collect :: < Vec < _ > > ( ) ) ;
50+ print_stdout ( table) . unwrap ( )
10151 }
102- _ => unreachable ! ( ) ,
103- }
52+ Ok ( payload ) => println ! ( "{:?}" , payload ) ,
53+ } ;
10454
10555 true
10656}
0 commit comments