From ce5b5dc34c49d9d9fbda059d43a095fe9a430f13 Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Fri, 4 Dec 2026 11:26:14 +0900 Subject: [PATCH 1/3] Parse CASE expressions returning JSON as JSON columns --- .../sql/rewrite/projection_partitioning.rs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/webserver/database/sql/rewrite/projection_partitioning.rs b/src/webserver/database/sql/rewrite/projection_partitioning.rs index 84747f728..01d2bb79b 100644 --- a/src/webserver/database/sql/rewrite/projection_partitioning.rs +++ b/src/webserver/database/sql/rewrite/projection_partitioning.rs @@ -222,6 +222,59 @@ fn is_json_expression(expression: &SqlExpr) -> bool { ) } SqlExpr::Cast { data_type, .. } => matches!(data_type, DataType::JSON | DataType::JSONB), + SqlExpr::Nested(inner) => is_json_expression(inner), + SqlExpr::Case { + conditions, + else_result, + .. + } => { + // `CASE WHEN ... THEN json_object(...) ELSE NULL END` yields JSON text or NULL. + let results = conditions + .iter() + .map(|when| &when.result) + .chain(else_result.as_deref()); + let mut has_json = false; + for result in results { + if is_json_expression(result) { + has_json = true; + } else if !is_null_literal(result) { + return false; + } + } + has_json + } + _ => false, + } +} + +fn is_null_literal(expression: &SqlExpr) -> bool { + match expression { + SqlExpr::Value(value) => matches!(value.value, sqlparser::ast::Value::Null), + SqlExpr::Nested(inner) => is_null_literal(inner), _ => false, } } + +#[cfg(test)] +mod tests { + use super::*; + use sqlparser::dialect::SQLiteDialect; + use sqlparser::parser::Parser; + + fn json_columns(sql: &str) -> Vec { + let statement = Parser::parse_sql(&SQLiteDialect {}, sql).unwrap().remove(0); + detect_public_json_columns(&statement, SupportedDatabase::Sqlite).into_vec() + } + + #[test] + fn json_object_inside_case_is_a_json_column() { + let cols = json_columns( + "SELECT CASE WHEN a != '' THEN json_object('icon', 'clock') ELSE NULL END AS item, \ + (CASE WHEN a != '' THEN json_array(1) END) AS other, \ + CASE WHEN a THEN 'text' ELSE NULL END AS plain, \ + CASE WHEN a THEN json_object('x', 1) ELSE 'text' END AS mixed \ + FROM t", + ); + assert_eq!(cols, ["item", "other"]); + } +} From 5930f5d5bba9fbb800407e1c53d9fcd1b42c4eb0 Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Fri, 4 Dec 2026 11:26:36 +0900 Subject: [PATCH 2/3] Add a rendering test for CASE expressions returning JSON --- ...s_component_case_json_nomssql_nopostgres_nooracle.sql | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/sql_test_files/component_rendering/columns_component_case_json_nomssql_nopostgres_nooracle.sql diff --git a/tests/sql_test_files/component_rendering/columns_component_case_json_nomssql_nopostgres_nooracle.sql b/tests/sql_test_files/component_rendering/columns_component_case_json_nomssql_nopostgres_nooracle.sql new file mode 100644 index 000000000..192d66248 --- /dev/null +++ b/tests/sql_test_files/component_rendering/columns_component_case_json_nomssql_nopostgres_nooracle.sql @@ -0,0 +1,9 @@ +-- this test is skipped on MSSQL and postgres because their json_object function has a different syntax +-- The unused `note` key only matters if the JSON is printed as text instead of being parsed: +-- the page would then contain the word "error", which fails the test. +select 'columns' as component; +select + 'Case' as title, + case when 'x' != '' then json_object('description', 'It works !', 'note', 'error') else null end as item, + case when '' != '' then json_object('description', 'hidden', 'note', 'error') else null end as item +; From 3d49d454250e11d3ca3047e84250e5b6b0fe3068 Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Fri, 4 Dec 2026 11:26:54 +0900 Subject: [PATCH 3/3] Add changelog entry for CASE JSON columns --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69f3bc898..c67d179dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Standard alert descriptions have higher text contrast. - An `alert` without `color` uses the info color again, and its `role` attribute no longer starts with a space. ([#1697](https://github.com/sqlpage/SQLPage/issues/1697)) - New `create_new_text` and `no_results_text` properties on `select` fields translate the dropdown texts "Add …" and "No results found". The row that creates a new value is now announced by screen readers when it is the active option. ([#1699](https://github.com/sqlpage/SQLPage/issues/1699)) +- A `CASE` expression that returns `json_object(...)` or `json_array(...)` (or `NULL`) is now parsed as JSON, so it can be used for component properties such as `item` in `columns`. ([#1703](https://github.com/sqlpage/SQLPage/issues/1703)) - The `table` component can fetch and display HTML fragments in selected columns using `embed`. - New `navbar_toggle_label` property on the `shell` and `close_label` property on the `alert` component, to translate the accessible names of the menu toggle button and of the alert close button. ([#1632](https://github.com/sqlpage/SQLPage/issues/1632)) - Charts with `time` enabled now show both occurrences of the repeated local hour when daylight saving time ends.