Skip to content

Commit 3ae6a0d

Browse files
committed
Fancy new methods due to MultiSQL 0.0.3
1 parent afee0bf commit 3ae6a0d

File tree

4 files changed

+46
-89
lines changed

4 files changed

+46
-89
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ Cargo.lock
1313
# Added by cargo
1414

1515
/target
16+
17+
18+
/test.csv

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "multisql-cli"
3-
version = "0.0.0"
3+
version = "0.0.2"
44
authors = ["Kyran Gostelow <[email protected]>"]
55
edition = "2021"
66
description = "MultiSQL CLI"
@@ -10,6 +10,7 @@ repository = "https://github.com/KyGost/multisql-cli"
1010
[dependencies]
1111
dialoguer = "^0"
1212
indicatif = "^0"
13-
multisql = "0.0.2"
13+
multisql = "0.0.3"
1414
lazy_static = "^1"
15-
console = "^0"
15+
console = "^0"
16+
cli-table = "^0"

src/main.rs

Lines changed: 35 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use cli_table::{print_stdout, Cell, Table};
2+
use multisql::{Cast, Payload};
13
use {
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-
99
lazy_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-
2420
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")
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
}

test.csv

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
a,b
1+
val,text
2+
0.4,
3+
99999.999999,!!!!
4+
124.0,Heya!

0 commit comments

Comments
 (0)