Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normalize_with_options function #3

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading