1
+ use cli_table:: { print_stdout, Cell , Table } ;
2
+ use multisql:: { Cast , Payload } ;
1
3
use {
2
- console:: Style ,
3
- dialoguer:: { theme:: ColorfulTheme , Confirm , Editor , Input , Select } ,
4
+ dialoguer:: { theme:: ColorfulTheme , Input } ,
4
5
indicatif:: { ProgressBar , ProgressStyle } ,
5
6
lazy_static:: lazy_static,
6
- multisql:: { CSVStorage , Glue , SledStorage , Storage } ,
7
+ multisql:: Glue ,
7
8
} ;
8
-
9
9
lazy_static ! {
10
10
pub ( crate ) static ref PROGRESS_STYLE : ProgressStyle = ProgressStyle :: default_spinner( )
11
11
. template( "{spinner:.magenta} {elapsed:3.red} {msg:.green}" )
@@ -17,90 +17,40 @@ fn main() {
17
17
while prompt ( & mut glue) { }
18
18
}
19
19
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
-
24
20
fn 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" )
31
23
. 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 ( )
101
51
}
102
- _ => unreachable ! ( ) ,
103
- }
52
+ Ok ( payload ) => println ! ( "{:?}" , payload ) ,
53
+ } ;
104
54
105
55
true
106
56
}
0 commit comments