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

Feature flag to have more parentheses #723

Merged
merged 5 commits into from
Dec 13, 2023
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
5 changes: 3 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
with:
toolchain: stable
components: clippy
- run: cargo clippy --all-features --workspace -- -D warnings
- run: cargo clippy --features=all-features --workspace -- -D warnings
- run: cargo clippy --manifest-path sea-query-binder/Cargo.toml --workspace --features runtime-async-std-rustls --features=with-chrono,with-json,with-rust_decimal,with-bigdecimal,with-uuid,with-time,with-ipnetwork,with-mac_address,postgres-array -- -D warnings
- run: cargo clippy --manifest-path sea-query-rusqlite/Cargo.toml --all-features --workspace -- -D warnings
- run: cargo clippy --manifest-path sea-query-postgres/Cargo.toml --all-features --workspace -- -D warnings
Expand Down Expand Up @@ -143,7 +143,8 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --all-features
- run: cargo test --features=all-features
- run: cargo test --test test-more-parentheses --features=tests-cfg,option-more-parentheses

derive-test:
name: Derive Tests
Expand Down
28 changes: 28 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ with-time = ["time"]
with-ipnetwork = ["ipnetwork"]
with-mac_address = ["mac_address"]
tests-cfg = []
all-features = [
"backend-mysql",
"backend-postgres",
"backend-sqlite",
"derive",
"attr",
"hashable-value",
"thread-safe",
"all-types",
] # everything except option-*
all-types = [
"postgres-array",
"postgres-interval",
"with-chrono",
"with-json",
"with-rust_decimal",
"with-bigdecimal",
"with-uuid",
"with-time",
"with-ipnetwork",
"with-mac_address",
]
option-more-parentheses = []

[[test]]
name = "test-derive"
Expand All @@ -95,6 +118,11 @@ name = "test-sqlite"
path = "tests/sqlite/mod.rs"
required-features = ["tests-cfg", "backend-sqlite"]

[[test]]
name = "test-more-parentheses"
path = "tests/more-parentheses.rs"
required-features = ["tests-cfg", "option-more-parentheses", "backend-mysql"]

[[bench]]
name = "basic"
harness = false
4 changes: 4 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,10 @@ pub(crate) fn common_inner_expr_well_known_greater_precedence(
| SimpleExpr::Case(_)
| SimpleExpr::SubQuery(_, _) => true,
SimpleExpr::Binary(_, inner_oper, _) => {
#[cfg(feature = "option-more-parentheses")]
{
return false;
}
let inner_oper: Oper = (*inner_oper).into();
if inner_oper.is_arithmetic() || inner_oper.is_shift() {
outer_oper.is_comparison()
Expand Down
59 changes: 59 additions & 0 deletions tests/more-parentheses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use sea_query::{tests_cfg::Glyph, Cond, Expr, MysqlQueryBuilder, Query};

#[test]
fn test_more_parentheses() {
let query = Query::select()
.column(Glyph::Image)
.from(Glyph::Table)
.cond_where(Cond::all())
.cond_where(Expr::val(1).eq(1))
.cond_where(Expr::val(2).eq(2))
.cond_where(Cond::any().add(Expr::val(3).eq(3)).add(Expr::val(4).eq(4)))
.to_owned();

assert_eq!(
query.to_string(MysqlQueryBuilder),
"SELECT `image` FROM `glyph` WHERE (1 = 1) AND (2 = 2) AND ((3 = 3) OR (4 = 4))"
);
}

#[test]
fn test_more_parentheses_complex() {
// Add pagination
let mut pagination = Cond::all();
let lt_value = Expr::col(Glyph::Aspect)
.lt(1)
.or(Expr::col(Glyph::Aspect).is_null());
let lt_id = Expr::col(Glyph::Aspect)
.is(1)
.and(Expr::col(Glyph::Id).lt(10));
pagination = pagination.add(lt_value.or(lt_id));

// Add filtering
let mut all = Cond::all();
all = all.add(Expr::col(Glyph::Image).eq("png"));

let mut nested = Cond::all();
nested = nested.add(Expr::col(Glyph::Table).gte(5));
nested = nested.add(Expr::col(Glyph::Tokens).lte(3));
all = all.add(nested);

let mut any = Cond::any();
any = any.add(Expr::col(Glyph::Image).like("%.jpg"));
any = any.add(all);
let filtering = any;

// Query
let query = Query::select()
.column(Glyph::Id)
.from(Glyph::Table)
.cond_where(Cond::all())
.cond_where(pagination)
.cond_where(filtering)
.to_owned();

assert_eq!(
query.to_string(MysqlQueryBuilder),
"SELECT `id` FROM `glyph` WHERE ((`aspect` < 1) OR (`aspect` IS NULL) OR ((`aspect` IS 1) AND (`id` < 10))) AND ((`image` LIKE '%.jpg') OR ((`image` = 'png') AND ((`glyph` >= 5) AND (`tokens` <= 3))))"
);
}