Skip to content

Commit

Permalink
add normalize_with_options function (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
takaebato authored Feb 12, 2024
1 parent 9ca906b commit 0924fef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion sql-insight-cli/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl NormalizeExecutor {

impl CliExecutable for NormalizeExecutor {
fn execute(&self) -> Result<Vec<String>, Error> {
sql_insight::normalize(
sql_insight::normalize_with_options(
get_dialect(self.dialect_name.as_deref())?.as_ref(),
self.sql.as_ref(),
self.options.clone(),
Expand Down
6 changes: 5 additions & 1 deletion sql-insight/src/normalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use sqlparser::ast::{Expr, VisitMut, VisitorMut};
use sqlparser::dialect::Dialect;
use sqlparser::parser::Parser;

pub fn normalize(
pub fn normalize(dialect: &dyn Dialect, sql: &str) -> Result<Vec<String>, Error> {
Normalizer::normalize(dialect, sql, NormalizerOptions::new())
}

pub fn normalize_with_options(
dialect: &dyn Dialect,
sql: &str,
options: NormalizerOptions,
Expand Down
22 changes: 19 additions & 3 deletions sql-insight/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,32 @@ mod integration {
fn test_normalize() {
let sql = "SELECT a FROM t1 WHERE b = 1 AND c in (2, 3) AND d LIKE '%foo'";
for dialect in all_dialects() {
let result =
sql_insight::normalize(dialect.as_ref(), sql, NormalizerOptions::new())
.unwrap();
let result = sql_insight::normalize(dialect.as_ref(), sql).unwrap();
assert_eq!(
result,
["SELECT a FROM t1 WHERE b = ? AND c IN (?, ?) AND d LIKE ?"],
"Failed for dialect: {dialect:?}"
)
}
}

#[test]
fn test_normalize_with_options() {
let sql = "SELECT a FROM t1 WHERE b = 1 AND c in (2, 3, 4)";
for dialect in all_dialects() {
let result = sql_insight::normalize_with_options(
dialect.as_ref(),
sql,
NormalizerOptions::new().with_unify_in_list(true),
)
.unwrap();
assert_eq!(
result,
["SELECT a FROM t1 WHERE b = ? AND c IN (...)"],
"Failed for dialect: {dialect:?}"
)
}
}
}

mod extract_crud_tables {
Expand Down

0 comments on commit 0924fef

Please sign in to comment.