From 0341d560755b5ef985a6acae46a58ad76c428480 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 28 Sep 2024 10:10:40 +0200 Subject: [PATCH] Add test triggered by #604 --- include/sqlpp11/core/to_sql_string.h | 2 ++ tests/core/serialize/operator/logical_expression.cpp | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/sqlpp11/core/to_sql_string.h b/include/sqlpp11/core/to_sql_string.h index 0fab6144..895c5e2d 100644 --- a/include/sqlpp11/core/to_sql_string.h +++ b/include/sqlpp11/core/to_sql_string.h @@ -256,6 +256,8 @@ namespace sqlpp template auto operand_to_sql_string(Context& context, const T& t) -> std::string { +#warning: For expressions with dynamic components this needs to be dynamic, e.g. +#warning: `A and dynamic(false, B)` would require parenthesis if and only if A requires parentheses if (requires_parentheses::value) { return "(" + to_sql_string(context, t) + ")"; diff --git a/tests/core/serialize/operator/logical_expression.cpp b/tests/core/serialize/operator/logical_expression.cpp index 7c98106c..db4200ed 100644 --- a/tests/core/serialize/operator/logical_expression.cpp +++ b/tests/core/serialize/operator/logical_expression.cpp @@ -62,7 +62,7 @@ int main(int, char* []) // NOT is not chained gracefully, but hey, don't do that anyways. SQLPP_COMPARE(not not not val, "NOT (NOT (NOT 1))"); - // Operands are enclosed in parenheses where required or completely dropped if inactive + // Operands are enclosed in parentheses where required or completely dropped if inactive SQLPP_COMPARE(val and dynamic(true, val), "1 AND 1"); SQLPP_COMPARE(val and dynamic(true, expr), "1 AND (17 > 15)"); SQLPP_COMPARE(expr and dynamic(true, val), "(17 > 15) AND 1"); @@ -90,5 +90,12 @@ int main(int, char* []) SQLPP_COMPARE(val or dynamic(true, val) or expr, "1 OR 1 OR (17 > 15)"); SQLPP_COMPARE(val or dynamic(false, val) or expr, "1 OR (17 > 15)"); + // More complex expressions + SQLPP_COMPARE((val and dynamic(true, expr)) or dynamic(true, val), "(1 AND (17 > 15)) OR 1"); +#warning: Remove paretheses! + SQLPP_COMPARE((val and dynamic(false, expr)) or dynamic(true, val), "(1) OR 1"); + SQLPP_COMPARE((val and dynamic(true, expr)) or dynamic(false, val), "1 AND (17 > 15)"); + SQLPP_COMPARE((val and dynamic(false, expr)) or dynamic(false, val), "1") + return 0; }