Skip to content

Commit

Permalink
WIP - spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
luciano-fiandesio committed Jan 6, 2025
1 parent 10956a5 commit eb9f2d7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,7 @@ private String addCteFiltersToWhereClause(EventQueryParams params, CTEContext ct
for (QueryFilter filter : item.getFilters()) {
if (IN.equals(filter.getOperator())) {
InQueryCteFilter inQueryCteFilter =
new InQueryCteFilter(
"value",
filter.getFilter(),
item.isText(),
cteDef);
new InQueryCteFilter("value", filter.getFilter(), item.isText(), cteDef);
whereClause
.append(" and ")
.append(inQueryCteFilter.getSqlFilter(createOffset2(item.getProgramStageOffset())));
Expand Down Expand Up @@ -755,8 +751,7 @@ protected String getColumnWithCte(QueryItem item, String suffix, CTEContext cteC
CteDefinition cteDef = cteContext.getDefinitionByItemUid(computeKey(item));
int programStageOffset = createOffset2(item.getProgramStageOffset());
String alias = getAlias(item).orElse(null);
columns.add(
"%s.value as %s".formatted(cteDef.getAlias(programStageOffset), quote(alias)));
columns.add("%s.value as %s".formatted(cteDef.getAlias(programStageOffset), quote(alias)));
if (cteDef.isRowContext()) {
// Add additional status and exists columns for row context
columns.add(
Expand Down Expand Up @@ -1025,7 +1020,7 @@ private CTEContext getCteDefinitions(EventQueryParams params) {
String colName = quote(item.getItemName());

String cteSql =
"""
"""
select
enrollment,
%s as value,
Expand All @@ -1037,10 +1032,7 @@ private CTEContext getCteDefinitions(EventQueryParams params) {
WHERE eventstatus != 'SCHEDULE'
AND ps = '%s'
"""
.formatted(
colName,
eventTableName,
item.getProgramStage().getUid());
.formatted(colName, eventTableName, item.getProgramStage().getUid());
cteContext.addCTE(
item.getProgramStage(),
item,
Expand Down Expand Up @@ -1136,13 +1128,13 @@ private String buildEnrollmentQueryWithCte(EventQueryParams params) {
CteDefinition cteDef = cteContext.getDefinitionByItemUid(itemUid);
if (cteDef.isProgramStage()) {
List<Integer> offsets = cteDef.getOffsets();
for (Integer offset : offsets) {
String alias = cteDef.getAlias(offset);
String join = "%s %s ON %s.enrollment = ax.enrollment and %s.rn = %s"
.formatted(cteDef.asCteName(itemUid), alias, alias, alias, offset + 1);
sql.append(LEFT_JOIN).append(join);
}

for (Integer offset : offsets) {
String alias = cteDef.getAlias(offset);
String join =
"%s %s ON %s.enrollment = ax.enrollment and %s.rn = %s"
.formatted(cteDef.asCteName(itemUid), alias, alias, alias, offset + 1);
sql.append(LEFT_JOIN).append(join);
}
}
if (cteDef.isExists()) {
sql.append(LEFT_JOIN)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
/*
* Copyright (c) 2004-2025, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.analytics.util.sql;

public class SqlConditionJoiner {

public static String joinSqlConditions(String... conditions) {
if (conditions == null || conditions.length == 0) {
return "";
}
public static String joinSqlConditions(String... conditions) {
if (conditions == null || conditions.length == 0) {
return "";
}

StringBuilder result = new StringBuilder("where ");
boolean firstCondition = true;
StringBuilder result = new StringBuilder("where ");
boolean firstCondition = true;

for (String condition : conditions) {
if (condition == null || condition.trim().isEmpty()) {
continue;
}
for (String condition : conditions) {
if (condition == null || condition.trim().isEmpty()) {
continue;
}

// Remove leading "where" or " where" and trim
String cleanedCondition = condition.trim();
if (cleanedCondition.toLowerCase().startsWith("where")) {
cleanedCondition = cleanedCondition.substring(5).trim();
}
// Remove leading "where" or " where" and trim
String cleanedCondition = condition.trim();
if (cleanedCondition.toLowerCase().startsWith("where")) {
cleanedCondition = cleanedCondition.substring(5).trim();
}

if (!cleanedCondition.isEmpty()) {
if (!firstCondition) {
result.append(" and ");
}
result.append(cleanedCondition);
firstCondition = false;
}
if (!cleanedCondition.isEmpty()) {
if (!firstCondition) {
result.append(" and ");
}

return result.toString();
result.append(cleanedCondition);
firstCondition = false;
}
}

return result.toString();
}
}

0 comments on commit eb9f2d7

Please sign in to comment.