forked from puzzleYOU/sqlquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.rs
More file actions
26 lines (24 loc) · 901 Bytes
/
Copy pathcommon.rs
File metadata and controls
26 lines (24 loc) · 901 Bytes
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
use {
crate::error::QueryCompilerError,
sqlparser::{dialect::GenericDialect, parser::Parser},
};
/// Reformats (i.e. indents and normalizes) a given SQL string to make
/// it more human-readable.
///
/// This also ensures the query is valid SQL as far the `sqlparser`
/// crate can tell. In case the passed SQL string is invalid, an
/// according error is returned.
pub fn format_query_prettily(query: &str)
-> Result<String, QueryCompilerError> {
let parser = Parser::new(&GenericDialect {});
let parsed =
parser.try_with_sql(query)
.map_err(|e| {
QueryCompilerError::ResultingQueryInvalid(query.into(), e)
})?
.parse_query()
.map_err(|e| {
QueryCompilerError::ResultingQueryInvalid(query.into(), e)
})?;
Ok(format!("{:#}", parsed))
}