-
Notifications
You must be signed in to change notification settings - Fork 26
/
run_sql.rs
120 lines (102 loc) · 2.94 KB
/
run_sql.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
extern crate snowflake_api;
use anyhow::Result;
use arrow::util::pretty::pretty_format_batches;
use clap::Parser;
use std::fs;
use snowflake_api::{QueryResult, SnowflakeApi};
#[derive(clap::ValueEnum, Clone, Debug)]
enum Output {
Arrow,
Json,
Query,
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path to RSA PEM private key
#[arg(long)]
private_key: Option<String>,
/// Password if certificate is not present
#[arg(long)]
password: Option<String>,
/// <account_identifier> in Snowflake format, uppercase
#[arg(short, long)]
account_identifier: String,
/// Database name
#[arg(short, long)]
database: Option<String>,
/// Schema name
#[arg(long)]
schema: Option<String>,
/// Warehouse
#[arg(short, long)]
warehouse: Option<String>,
/// username to whom the private key belongs to
#[arg(short, long)]
username: String,
/// role which user will assume
#[arg(short, long)]
role: Option<String>,
/// sql statement to execute and print result from
#[arg(long)]
sql: String,
#[arg(long)]
#[arg(value_enum, default_value_t = Output::Arrow)]
output: Output,
}
#[tokio::main]
async fn main() -> Result<()> {
pretty_env_logger::init();
let args = Args::parse();
let mut api = match (&args.private_key, &args.password) {
(Some(pkey), None) => {
let pem = fs::read_to_string(pkey)?;
SnowflakeApi::with_certificate_auth(
&args.account_identifier,
args.warehouse.as_deref(),
args.database.as_deref(),
args.schema.as_deref(),
&args.username,
args.role.as_deref(),
&pem,
)?
}
(None, Some(pwd)) => SnowflakeApi::with_password_auth(
&args.account_identifier,
args.warehouse.as_deref(),
args.database.as_deref(),
args.schema.as_deref(),
&args.username,
args.role.as_deref(),
pwd,
)?,
_ => {
panic!("Either private key path or password must be set")
}
};
match args.output {
Output::Arrow => {
let res = api.exec(&args.sql).await?;
match res {
QueryResult::Arrow(a) => {
println!("{}", pretty_format_batches(&a).unwrap());
}
QueryResult::Json(j) => {
println!("{j}");
}
QueryResult::Empty => {
println!("Query finished successfully")
}
}
}
Output::Json => {
let res = api.exec_json(&args.sql).await?;
println!("{res}");
}
Output::Query => {
let res = api.exec_response(&args.sql).await?;
println!("{:?}", res);
}
}
Ok(())
}