-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlsonnet.rs
More file actions
266 lines (246 loc) · 8.32 KB
/
Copy pathsqlsonnet.rs
File metadata and controls
266 lines (246 loc) · 8.32 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use std::collections::BTreeMap;
use std::io::IsTerminal;
use std::path::PathBuf;
use clap::Parser;
use sqlsonnet_clickhouse_client as clickhouse_client;
use tracing::*;
use sqlsonnet::Queries;
lazy_static::lazy_static! {
static ref THEMES: Vec<String> =
bat::assets::HighlightingAssets::from_binary().themes().map(String::from).collect();
}
#[derive(Debug, miette::Diagnostic, thiserror::Error)]
enum Error {
#[error("Failed to read input")]
Input(#[from] clap_stdin::StdinError),
#[error(transparent)]
#[diagnostic(transparent)]
Inner(#[from] sqlsonnet::Error),
#[error("Failed to highlight SQL")]
Bat(#[from] bat::error::Error),
#[error(transparent)]
Miette(#[from] miette::InstallError),
#[error("Failed to execute query")]
Clickhouse(#[from] clickhouse_client::Error),
#[error(transparent)]
Watch(#[from] notify_debouncer_mini::notify::Error),
}
#[derive(Parser)]
#[clap(version)]
struct Flags {
/// Color theme for syntax highlighting
#[clap(long, env = "SQLSONNET_THEME",
value_parser=clap::builder::PossibleValuesParser::new(THEMES.iter().map(|s| s.as_str())))]
theme: Option<String>,
/// Compact SQL representation
#[clap(long, short)]
compact: bool,
/// Input file (path or - for stdin).
input: clap_stdin::FileOrStdin,
/// Convert an SQL file into Jsonnet.
#[clap(long, short)]
from_sql: bool,
/// With --from-sql: Convert back to SQL and print the differences with the original, if any.
#[clap(long, requires = "from_sql")]
diff: bool,
#[clap(long, value_delimiter = ',')]
display_format: Option<Vec<Language>>,
/// Clickhouse HTTP URL, to execute queries
#[clap(long, env = "SQLSONNET_CLICKHOUSE")]
clickhouse_url: Option<reqwest::Url>,
/// Send query to Clickhouse proxy (--proxy-url) for execution
#[clap(long, short, conflicts_with = "from_sql", requires = "clickhouse_url")]
execute: bool,
/// Output format for execution
#[clap(long, default_value = "PrettyMonoBlock")]
execute_format: String,
/// Watch for file changes
#[clap(long, short)]
watch: bool,
/// Library path
#[clap(long, short = 'J', env = "JSONNET_PATH", value_delimiter = ':')]
jpath: Option<Vec<PathBuf>>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
enum Language {
Sql,
Jsonnet,
Json,
}
impl Language {
fn as_str(&self) -> &'static str {
match self {
Self::Sql => "sql",
Self::Json => "json",
Self::Jsonnet => "jsonnet",
}
}
}
fn highlight<T: std::fmt::Display>(
snippet: T,
language: Language,
args: &Flags,
) -> Result<(), Error> {
if args.execute {
// TODO: Get `bat` to output on stderr. This currently cannot be configured with the
// PrettyPrinter
return Ok(());
}
let is_tty = std::io::stdout().is_terminal();
if is_tty {
let mut printer = bat::PrettyPrinter::new();
if let Some(theme) = &args.theme {
printer.theme(theme);
}
let sql = std::io::Cursor::new(snippet.to_string());
printer
.input(bat::Input::from_reader(sql).name(format!("queries.{}", language.as_str())))
.language(language.as_str())
.header(true);
printer.print()?;
} else {
println!("{}", snippet);
}
println!();
Ok(())
}
#[tokio::main]
async fn main() -> miette::Result<()> {
Ok(main_impl().await?)
}
async fn process_one(
args: &Flags,
client: &Option<clickhouse_client::HttpClient>,
) -> Result<(), Error> {
let start = std::time::Instant::now();
let display_format = args.display_format.clone().unwrap_or_else(|| {
vec![if args.from_sql {
Language::Jsonnet
} else {
Language::Sql
}]
});
let filename = args.input.filename();
let input = args.input.clone().contents()?;
if args.from_sql {
info!("Converting SQL file {}", filename);
let queries = Queries::from_sql(&input)?;
let has_df = |l| display_format.iter().any(|l2| l2 == &l);
let sql = queries.to_sql(args.compact);
if has_df(Language::Sql) {
highlight(&sql, Language::Sql, args)?;
}
if has_df(Language::Jsonnet) {
let jsonnet = queries.as_jsonnet();
highlight(jsonnet, Language::Jsonnet, args)?;
}
if args.diff && input != sql {
eprintln!("{}", pretty_assertions::StrComparison::new(&input, &sql));
}
} else {
let contents = sqlsonnet::jsonnet::import_utils() + &input;
info!("Converting Jsonnet file {} to SQL", filename);
let mut resolver = sqlsonnet::jsonnet::FsResolver::current_dir();
if let Some(jpath) = args.jpath.clone() {
for path in jpath {
resolver.add(path);
}
}
let queries_json = sqlsonnet::jsonnet::evaluate(
&contents,
sqlsonnet::jsonnet::Options::new(
resolver,
concat!(env!("CARGO_BIN_NAME"), " ", env!("CARGO_PKG_VERSION")),
),
)
.map_err(sqlsonnet::Error::from)?;
let queries = Queries::from_json(&queries_json).inspect_err(|_| {
// TODO: Print on stderr
highlight(&queries_json, Language::Json, args).unwrap();
})?;
let has_df = |l| display_format.iter().any(|l2| l2 == &l);
// Display queries
debug!("{:#?}", queries);
if has_df(Language::Jsonnet) {
highlight(&contents, Language::Jsonnet, args)?;
}
if has_df(Language::Json) {
highlight(&queries_json, Language::Json, args)?;
}
if has_df(Language::Sql) {
highlight(queries.to_sql(args.compact), Language::Sql, args)?;
}
if let Some(client) = client {
info!("Executing query on Clickhouse");
for query in queries {
let resp = client
.send_query(&clickhouse_client::ClickhouseQuery {
query: query.to_sql(false),
params: BTreeMap::from([(
"default_format".into(),
args.execute_format.clone(),
)]),
compression: clickhouse_client::Compression::Zstd,
..Default::default()
})
.await?
.text()
.await
.map_err(clickhouse_client::Error::from)?;
println!("{}", resp);
}
}
}
info!(elapsed=?start.elapsed(), "Done");
Ok(())
}
async fn main_impl() -> Result<(), Error> {
sqlsonnet::setup_logging();
let mut args = Flags::parse();
if !args.execute {
args.clickhouse_url = None;
}
let assets = bat::assets::HighlightingAssets::from_binary();
let theme = assets.get_theme(
args.theme
.as_deref()
.unwrap_or_else(|| bat::assets::HighlightingAssets::default_theme()),
);
let highlighter = miette::highlighters::SyntectHighlighter::new(
assets.get_syntax_set().unwrap().clone(),
theme.clone(),
false,
);
miette::set_hook(Box::new(move |_| {
Box::new(
miette::MietteHandlerOpts::new()
.context_lines(10)
.with_syntax_highlighting(highlighter.clone())
.build(),
)
}))?;
let client = args
.clickhouse_url
.clone()
.map(|url| clickhouse_client::HttpClient::new(url, true /* auto-decompress */));
if args.watch && args.input.is_file() {
// Watch mode
let (tx, rx) = std::sync::mpsc::channel();
let mut debouncer =
notify_debouncer_mini::new_debouncer(std::time::Duration::from_millis(500), tx)?;
debouncer.watcher().watch(
std::path::Path::new(args.input.filename()),
notify_debouncer_mini::notify::RecursiveMode::NonRecursive,
)?;
loop {
if let Err(e) = process_one(&args, &client).await {
error!("{}", e);
}
info!("Watching {} for changes", args.input.filename());
rx.recv().unwrap()?;
}
} else {
process_one(&args, &client).await?;
}
Ok(())
}