Search in sources :

Example 1 with ProgramExpressionParams

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor) AntlrParserUtils.castString(org.hisp.dhis.antlr.AntlrParserUtils.castString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) I18n(org.hisp.dhis.i18n.I18n)

Example 2 with ProgramExpressionParams

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 + "')";
}
Also used : ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Date(java.util.Date)

Example 3 with ProgramExpressionParams

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);
}
Also used : ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams)

Example 4 with ProgramExpressionParams

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 + "')";
}
Also used : ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Date(java.util.Date)

Example 5 with ProgramExpressionParams

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);
}
Also used : ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor) AntlrParserUtils.castString(org.hisp.dhis.antlr.AntlrParserUtils.castString) SqlLiteral(org.hisp.dhis.parser.expression.literal.SqlLiteral)

Aggregations

ProgramExpressionParams (org.hisp.dhis.parser.expression.ProgramExpressionParams)9 AntlrParserUtils.castString (org.hisp.dhis.antlr.AntlrParserUtils.castString)4 LinkedHashSet (java.util.LinkedHashSet)3 I18n (org.hisp.dhis.i18n.I18n)3 CommonExpressionVisitor (org.hisp.dhis.parser.expression.CommonExpressionVisitor)3 Date (java.util.Date)2 StatementBuilder (org.hisp.dhis.jdbc.StatementBuilder)2 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)2 ParserException (org.hisp.dhis.antlr.ParserException)1 ParserExceptionWithoutContext (org.hisp.dhis.antlr.ParserExceptionWithoutContext)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 SqlLiteral (org.hisp.dhis.parser.expression.literal.SqlLiteral)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1