Search in sources :

Example 6 with ProgramExpressionParams

use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.

the class ProgramItemStageElement method getSql.

@Override
public Object getSql(ExprContext ctx, CommonExpressionVisitor visitor) {
    assumeStageElementSyntax(ctx);
    String programStageId = ctx.uid0.getText();
    String dataElementId = ctx.uid1.getText();
    ProgramExpressionParams params = visitor.getProgParams();
    int stageOffset = visitor.getState().getStageOffset();
    String column;
    if (isNonDefaultStageOffset(stageOffset)) {
        if (isRepeatableStage(visitor.getProgramStageService(), programStageId)) {
            column = visitor.getStatementBuilder().getProgramIndicatorEventColumnSql(programStageId, Integer.valueOf(stageOffset).toString(), SqlUtils.quote(dataElementId), params.getReportingStartDate(), params.getReportingEndDate(), params.getProgramIndicator());
        } else {
            throw new ParserException(getErrorMessage(programStageId));
        }
    } else {
        column = visitor.getStatementBuilder().getProgramIndicatorDataValueSelectSql(programStageId, dataElementId, params.getReportingStartDate(), params.getReportingEndDate(), params.getProgramIndicator());
    }
    if (visitor.getState().isReplaceNulls()) {
        DataElement dataElement = visitor.getIdObjectManager().get(DataElement.class, dataElementId);
        if (dataElement == null) {
            throw new ParserExceptionWithoutContext("Data element " + dataElementId + " not found during SQL generation.");
        }
        column = replaceNullSqlValues(column, dataElement.getValueType());
    }
    return column;
}
Also used : ParserExceptionWithoutContext(org.hisp.dhis.antlr.ParserExceptionWithoutContext) ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) ParserException(org.hisp.dhis.antlr.ParserException) DataElement(org.hisp.dhis.dataelement.DataElement)

Example 7 with ProgramExpressionParams

use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.

the class ProgramSqlGeneratorItemsTest 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)).constantMap(constantMap).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) I18n(org.hisp.dhis.i18n.I18n)

Example 8 with ProgramExpressionParams

use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.

the class ProgramSqlGeneratorVariablesTest method test.

private Object test(String expression, AntlrExprLiteral exprLiteral, ProgramIndicator programIndicator) {
    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();
    subject = CommonExpressionVisitor.builder().idObjectManager(idObjectManager).dimensionService(dimensionService).programIndicatorService(programIndicatorService).programStageService(programStageService).statementBuilder(statementBuilder).i18n(new I18n(null, null)).itemMap(PROGRAM_INDICATOR_ITEMS).itemMethod(ITEM_GET_SQL).progParams(params).build();
    subject.setExpressionLiteral(exprLiteral);
    return Parser.visit(expression, subject);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) AntlrParserUtils.castString(org.hisp.dhis.antlr.AntlrParserUtils.castString) I18n(org.hisp.dhis.i18n.I18n)

Example 9 with ProgramExpressionParams

use of org.hisp.dhis.parser.expression.ProgramExpressionParams in project dhis2-core by dhis2.

the class D2Condition method getSql.

@Override
public Object getSql(ExprContext ctx, CommonExpressionVisitor visitor) {
    String testExpression = trimQuotes(ctx.stringLiteral().getText());
    ProgramExpressionParams params = visitor.getProgParams();
    String testSql = visitor.getProgramIndicatorService().getAnalyticsSql(testExpression, params.getProgramIndicator(), params.getReportingStartDate(), params.getReportingEndDate());
    String valueIfTrue = visitor.castStringVisit(ctx.expr(0));
    String valueIfFalse = visitor.castStringVisit(ctx.expr(1));
    return "case when (" + testSql + ") then " + valueIfTrue + " else " + valueIfFalse + " end";
}
Also used : ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams)

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