Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions src/webserver/database/sql/rewrite/projection_partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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"]);
}
}
Original file line number Diff line number Diff line change
@@ -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
;