Skip to content

Commit

Permalink
WIP - Fix sql gen
Browse files Browse the repository at this point in the history
  • Loading branch information
luciano-fiandesio committed Dec 18, 2024
1 parent 3e070ce commit 5170115
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,19 @@ private String addCteFiltersToWhereClause(EventQueryParams params, CTEContext ct
}
}
}

// Handle the row context case
List<String> rowContextColumns = RowContextUtils.getRowContextWhereClauses(cteContext);
if (!StringUtils.isEmpty(whereClause)) {
whereClause.append(" AND ");
if (rowContextColumns.isEmpty()) {
return whereClause.toString();
} else {
if (whereClause.isEmpty()) {
whereClause.append(" where ");
} else {
whereClause.append(" AND ");
}
whereClause.append(String.join(" AND ", rowContextColumns));
}
whereClause.append(String.join(" AND ", rowContextColumns));

return whereClause.toString();
}
Expand Down Expand Up @@ -1041,14 +1048,14 @@ private CTEContext getCteDefinitions(EventQueryParams params) {
String cteSql =
"""
-- Generate CTE for program stage items
SELECT DISTINCT ON (enrollment) enrollment, %s as value, %s
SELECT DISTINCT ON (enrollment) enrollment, %s as value%s
FROM %s
WHERE eventstatus != 'SCHEDULE'
AND ps = '%s'
ORDER BY enrollment, occurreddate DESC, created DESC"""
.formatted(
colName,
rowContextAllowedAndNeeded(params, queryItem) ? "true as exists_flag" : "",
rowContextAllowedAndNeeded(params, queryItem) ? " ,true as exists_flag" : "",
eventTableName,
queryItem.getProgramStage().getUid());

Expand Down

0 comments on commit 5170115

Please sign in to comment.