From 862c99229314de78b2df9937b0f618fd223a42f1 Mon Sep 17 00:00:00 2001 From: Takahiro Ebato Date: Mon, 12 Feb 2024 13:56:21 +0900 Subject: [PATCH] add normalize_with_options function --- sql-insight-cli/src/executor.rs | 2 +- sql-insight/src/normalizer.rs | 6 +++++- sql-insight/tests/integration.rs | 22 +++++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/sql-insight-cli/src/executor.rs b/sql-insight-cli/src/executor.rs index 1421aa1..c355803 100644 --- a/sql-insight-cli/src/executor.rs +++ b/sql-insight-cli/src/executor.rs @@ -55,7 +55,7 @@ impl NormalizeExecutor { impl CliExecutable for NormalizeExecutor { fn execute(&self) -> Result, 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(), diff --git a/sql-insight/src/normalizer.rs b/sql-insight/src/normalizer.rs index 5fb3f43..00c2614 100644 --- a/sql-insight/src/normalizer.rs +++ b/sql-insight/src/normalizer.rs @@ -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, Error> { + Normalizer::normalize(dialect, sql, NormalizerOptions::new()) +} + +pub fn normalize_with_options( dialect: &dyn Dialect, sql: &str, options: NormalizerOptions, diff --git a/sql-insight/tests/integration.rs b/sql-insight/tests/integration.rs index 40838e5..e839728 100644 --- a/sql-insight/tests/integration.rs +++ b/sql-insight/tests/integration.rs @@ -28,9 +28,7 @@ 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 ?"], @@ -38,6 +36,24 @@ mod integration { ) } } + + #[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 {