Skip to content

Commit

Permalink
fix: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge committed Jan 18, 2024
1 parent 2b16090 commit d8dc1b9
Showing 1 changed file with 0 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
import static org.hisp.dhis.program.AnalyticsPeriodBoundary.DB_SCHEDULED_DATE;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import org.hisp.dhis.analytics.AnalyticsConstants;
Expand All @@ -56,125 +54,6 @@ public abstract class AbstractStatementBuilder implements StatementBuilder {

protected static final String SINGLE_QUOTE = "'";

/**
* Generates a derived table containing one column of literal strings.
*
* <p>The generic implementation, which works in all supported database types, returns a subquery
* in the following form: <code>
* (select 's1' as column
* union select 's2'
* union select 's3') table
* </code>
*
* @param values (non-empty) String values for the derived table
* @param table the desired table name alias
* @param column the desired column name
* @return the derived literal table
*/
@Override
public String literalStringTable(Collection<String> values, String table, String column) {
StringBuilder sb = new StringBuilder();

String before = "(select '";
String after = "' as " + column;

for (String value : values) {
sb.append(before).append(value).append(after);

before = " union select '";
after = "'";
}

return sb.append(") ").append(table).toString();
}

/**
* Generates a derived table containing literals in two columns: integer and string.
*
* <p>The generic implementation, which works in all supported database types, returns a subquery
* in the following form: <code>
* (select i1 as intColumn, 's1' as stringColumn
* union select i2, 's2'
* union select i3, 's3') table
* </code>
*
* @param longValues (non-empty) Integer values for the derived table
* @param strValues (same size) String values for the derived table
* @param table the desired table name alias
* @param longColumn the desired integer column name
* @param strColumn the desired string column name
* @return the derived literal table
*/
@Override
public String literalLongStringTable(
List<Long> longValues,
List<String> strValues,
String table,
String longColumn,
String strColumn) {
StringBuilder sb = new StringBuilder();

String before = "(select ";
String afterInt = " as " + longColumn + ", '";
String afterStr = "' as " + strColumn;

for (int i = 0; i < longValues.size(); i++) {
sb.append(before)
.append(longValues.get(i))
.append(afterInt)
.append(strValues.get(i))
.append(afterStr);
before = " union select ";
afterInt = ", '";
afterStr = "'";
}

return sb.append(") ").append(table).toString();
}

/**
* Generates a derived table containing literals in two columns: integer and integer.
*
* @param long1Values (non-empty) 1st integer column values for the table
* @param long2Values (same size) 2nd integer column values for the table
* @param table the desired table name alias
* @param long1Column the desired 1st integer column name
* @param long2Column the desired 2nd integer column name
* @return the derived literal table
* <p>The generic implementation, which works in all supported database types, returns a
* subquery in the following form: <code>
* (select i1_1 as int1Column, i2_1 as int2Column
* union select i1_2, i2_2
* union select i1_3, i2_3) table
* </code>
*/
@Override
public String literalLongLongTable(
List<Long> long1Values,
List<Long> long2Values,
String table,
String long1Column,
String long2Column) {
StringBuilder sb = new StringBuilder();

String before = "(select ";
String afterInt1 = " as " + long1Column + ", ";
String afterInt2 = " as " + long2Column;

for (int i = 0; i < long1Values.size(); i++) {
sb.append(before)
.append(long1Values.get(i))
.append(afterInt1)
.append(long2Values.get(i))
.append(afterInt2);
before = " union select ";
afterInt1 = ", ";
afterInt2 = "";
}

return sb.append(") ").append(table).toString();
}

@Override
public String getProgramIndicatorDataValueSelectSql(
String programStageUid,
Expand Down

0 comments on commit d8dc1b9

Please sign in to comment.