use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.
the class ProgramSqlGeneratorFunctionsTest method test.
private Object test(String expression, AntlrExprLiteral exprLiteral, ExpressionItemMethod itemMethod) {
Set<String> dataElementsAndAttributesIdentifiers = new LinkedHashSet<>();
dataElementsAndAttributesIdentifiers.add(BASE_UID + "a");
dataElementsAndAttributesIdentifiers.add(BASE_UID + "b");
dataElementsAndAttributesIdentifiers.add(BASE_UID + "c");
ProgramExpressionParams params = ProgramExpressionParams.builder().programIndicator(programIndicator).reportingStartDate(startDate).reportingEndDate(endDate).dataElementAndAttributeIdentifiers(dataElementsAndAttributesIdentifiers).build();
CommonExpressionVisitor visitor = CommonExpressionVisitor.builder().idObjectManager(idObjectManager).dimensionService(dimensionService).programIndicatorService(programIndicatorService).programStageService(programStageService).statementBuilder(statementBuilder).i18n(new I18n(null, null)).itemMap(PROGRAM_INDICATOR_ITEMS).itemMethod(itemMethod).progParams(params).build();
visitor.setExpressionLiteral(exprLiteral);
return Parser.visit(expression, visitor);
}
use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.
the class ProgramMinMaxFunction method getSql.
@Override
public Object getSql(ExprContext ctx, CommonExpressionVisitor visitor) {
StatementBuilder sb = visitor.getStatementBuilder();
ProgramExpressionParams params = visitor.getProgParams();
ProgramIndicator pi = params.getProgramIndicator();
String columnName = "";
if (// arg: PS_EVENTDATE:programStageUid
ctx.uid1 == null) {
columnName = "\"executiondate\"";
} else // arg: #{programStageUid.dataElementUid}
{
String dataElement = ctx.uid1.getText();
columnName = "\"" + dataElement + "\"";
}
if (AnalyticsType.EVENT == pi.getAnalyticsType()) {
return columnName;
}
Date startDate = params.getReportingStartDate();
Date endDate = params.getReportingEndDate();
String eventTableName = "analytics_event_" + pi.getProgram().getUid();
String programStage = ctx.uid0.getText();
return "(select " + getAggregationOperator() + "(" + columnName + ") from " + eventTableName + " where " + eventTableName + ".pi = " + StatementBuilder.ANALYTICS_TBL_ALIAS + ".pi " + (pi.getEndEventBoundary() != null ? ("and " + sb.getBoundaryCondition(pi.getEndEventBoundary(), pi, startDate, endDate) + " ") : "") + (pi.getStartEventBoundary() != null ? ("and " + sb.getBoundaryCondition(pi.getStartEventBoundary(), pi, startDate, endDate) + " ") : "") + "and ps = '" + programStage + "')";
}
use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.
the class D2CountIfCondition method getConditionSql.
@Override
public String getConditionSql(ExprContext ctx, CommonExpressionVisitor visitor) {
String conditionExpression = getConditionalExpression(ctx);
ProgramExpressionParams params = visitor.getProgParams();
String conditionSql = visitor.getProgramIndicatorService().getAnalyticsSql(conditionExpression, params.getProgramIndicator(), params.getReportingStartDate(), params.getReportingEndDate());
return conditionSql.substring(1);
}
use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.
the class ProgramCountFunction method getSql.
@Override
public final Object getSql(ExprContext ctx, CommonExpressionVisitor visitor) {
validateCountFunctionArgs(ctx);
StatementBuilder sb = visitor.getStatementBuilder();
ProgramExpressionParams params = visitor.getProgParams();
ProgramIndicator pi = params.getProgramIndicator();
Date startDate = params.getReportingStartDate();
Date endDate = params.getReportingEndDate();
String programStage = ctx.uid0.getText();
String dataElement = ctx.uid1.getText();
String eventTableName = "analytics_event_" + pi.getProgram().getUid();
String columnName = "\"" + dataElement + "\"";
String conditionSql = getConditionSql(ctx, visitor);
return "(select count(" + columnName + ") from " + eventTableName + " where " + eventTableName + ".pi = " + StatementBuilder.ANALYTICS_TBL_ALIAS + ".pi and " + columnName + " is not null and " + columnName + conditionSql + " " + (pi.getEndEventBoundary() != null ? ("and " + sb.getBoundaryCondition(pi.getEndEventBoundary(), pi, startDate, endDate) + " ") : "") + (pi.getStartEventBoundary() != null ? ("and " + sb.getBoundaryCondition(pi.getStartEventBoundary(), pi, startDate, endDate) + " ") : "") + "and ps = '" + programStage + "')";
}
use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.
the class DefaultProgramIndicatorService method _getAnalyticsSql.
private String _getAnalyticsSql(String expression, ProgramIndicator programIndicator, Date startDate, Date endDate, String tableAlias) {
Set<String> uids = getDataElementAndAttributeIdentifiers(expression, programIndicator.getAnalyticsType());
ProgramExpressionParams params = ProgramExpressionParams.builder().programIndicator(programIndicator).reportingStartDate(startDate).reportingEndDate(endDate).dataElementAndAttributeIdentifiers(uids).build();
CommonExpressionVisitor visitor = newVisitor(ITEM_GET_SQL, params);
visitor.setExpressionLiteral(new SqlLiteral());
String sql = castString(Parser.visit(expression, visitor));
return (tableAlias != null ? sql.replaceAll(ANALYTICS_TBL_ALIAS + "\\.", tableAlias + "\\.") : sql);
}
Aggregations