Search in sources :

Example 26 with Expression

use of orgomg.cwm.objectmodel.core.Expression in project tdq-studio-se by Talend.

the class TableAnalysisSqlExecutor method getValidStatement.

/**
 * DOC xqliu Comment method "getValidStatement". 2009-10-29 bug 9702
 *
 * @param dataFilterAsString
 * @param indicator
 * @param valid
 * @return
 */
public String getValidStatement(String dataFilterAsString, Indicator indicator, boolean valid) {
    if (!isAnalyzedElementValid(indicator)) {
        return PluginConstant.EMPTY_STRING;
    }
    IndicatorDefinition indicatorDefinition = indicator.getIndicatorDefinition();
    if (!isIndicatorDefinitionValid(indicatorDefinition, AnalysisExecutorHelper.getIndicatorName(indicator))) {
        return PluginConstant.EMPTY_STRING;
    }
    Expression sqlGenericExpression = dbms().getSqlExpression(indicatorDefinition);
    if (!isExpressionValid(sqlGenericExpression, indicator)) {
        return PluginConstant.EMPTY_STRING;
    }
    // --- get indicator parameters and convert them into sql expression
    List<String> whereExpressionAnalysis = new ArrayList<String>();
    if (StringUtils.isNotBlank(dataFilterAsString)) {
        whereExpressionAnalysis.add(dataFilterAsString);
    }
    List<String> whereExpressionDQRule = new ArrayList<String>();
    String setAliasA = PluginConstant.EMPTY_STRING;
    final EList<JoinElement> joinConditions = indicator.getJoinConditions();
    if (RulesPackage.eINSTANCE.getWhereRule().equals(indicatorDefinition.eClass())) {
        WhereRule wr = (WhereRule) indicatorDefinition;
        whereExpressionDQRule.add(wr.getWhereExpression());
        // MOD scorreia 2009-03-13 copy joins conditions into the indicator
        joinConditions.clear();
        if (!isJoinConditionEmpty(indicator)) {
            for (JoinElement joinelt : wr.getJoins()) {
                JoinElement joinCopy = EcoreUtil.copy(joinelt);
                joinConditions.add(joinCopy);
                setAliasA = PluginConstant.EMPTY_STRING.equals(setAliasA) ? joinCopy.getTableAliasA() : setAliasA;
            }
        }
    }
    NamedColumnSet set = SwitchHelpers.NAMED_COLUMN_SET_SWITCH.doSwitch(indicator.getAnalyzedElement());
    String schemaName = getQuotedSchemaName(set);
    // --- normalize table name
    String catalogName = getQuotedCatalogName(set);
    if (catalogName == null && schemaName != null) {
        // try to get catalog above schema
        final Schema parentSchema = SchemaHelper.getParentSchema(set);
        final Catalog parentCatalog = CatalogHelper.getParentCatalog(parentSchema);
        catalogName = parentCatalog != null ? parentCatalog.getName() : null;
    }
    String setName = dbms().toQualifiedName(catalogName, schemaName, quote(set.getName()));
    // ### evaluate SQL Statement depending on indicators ###
    String completedSqlString = null;
    // --- default case
    // allow join
    String joinclause = (!joinConditions.isEmpty()) ? dbms().createJoinConditionAsString(set, joinConditions, catalogName, schemaName) : PluginConstant.EMPTY_STRING;
    String genericSql = sqlGenericExpression.getBody();
    // $NON-NLS-1$//$NON-NLS-2$
    setAliasA = PluginConstant.EMPTY_STRING.equals(setAliasA) ? "*" : setAliasA + ".*";
    // $NON-NLS-1$
    genericSql = genericSql.replace("COUNT(*)", setAliasA);
    completedSqlString = dbms().fillGenericQueryWithJoin(genericSql, setName, joinclause);
    // ~
    completedSqlString = addWhereToSqlStringStatement(whereExpressionAnalysis, whereExpressionDQRule, completedSqlString, valid);
    return completedSqlString;
}
Also used : JoinElement(org.talend.dataquality.rules.JoinElement) WhereRule(org.talend.dataquality.rules.WhereRule) TdExpression(org.talend.cwm.relational.TdExpression) Expression(orgomg.cwm.objectmodel.core.Expression) Schema(orgomg.cwm.resource.relational.Schema) ArrayList(java.util.ArrayList) IndicatorDefinition(org.talend.dataquality.indicators.definition.IndicatorDefinition) NamedColumnSet(orgomg.cwm.resource.relational.NamedColumnSet) Catalog(orgomg.cwm.resource.relational.Catalog)

Example 27 with Expression

use of orgomg.cwm.objectmodel.core.Expression in project tdq-studio-se by Talend.

the class RowMatchingAnalysisExecutor method createInstantiatedSqlExpression.

/**
 * DOC scorreia Comment method "createInstantiatedSqlExpression".
 *
 * @param sqlGenericExpression
 * @param columnSetA
 * @param columnSetB
 * @param useNulls
 * @return
 */
private Expression createInstantiatedSqlExpression(Expression sqlGenericExpression, EList<TdColumn> columnSetA, EList<TdColumn> columnSetB, boolean useNulls, Indicator indicator) {
    // MOD scorreia 2009-05-25 allow to compare elements from the same table
    // aliases of tables
    // $NON-NLS-1$
    String aliasA = "A";
    // $NON-NLS-1$
    String aliasB = "B";
    // MOD xqliu 2009-06-16 bug 7334
    String dataFilterA = ContextHelper.getDataFilterWithoutContext(this.cachedAnalysis, AnalysisHelper.DATA_FILTER_A);
    String dataFilterB = ContextHelper.getDataFilterWithoutContext(this.cachedAnalysis, AnalysisHelper.DATA_FILTER_B);
    // MOD qiongli 2011-12-28 TDQ-4240.get the reversion value from a Map before generating sqlExpression.
    reversion = indiReversionMap != null && indiReversionMap.get(indicator) != null ? indiReversionMap.get(indicator).booleanValue() : false;
    if (reversion) {
        dataFilterA = ContextHelper.getDataFilterWithoutContext(this.cachedAnalysis, AnalysisHelper.DATA_FILTER_B);
        dataFilterB = ContextHelper.getDataFilterWithoutContext(this.cachedAnalysis, AnalysisHelper.DATA_FILTER_A);
    }
    // $NON-NLS-1$
    String tableNameA = addDataFilterWithTableName(getTableName(columnSetA), dataFilterA) + " " + aliasA;
    // $NON-NLS-1$
    String tableNameB = addDataFilterWithTableName(getTableName(columnSetB), dataFilterB) + " " + aliasB;
    // ~
    // Generic SQL expression is something like:
    // SELECT COUNT(*) FROM <%=__TABLE_NAME__%> LEFT JOIN <%=__TABLE_NAME_2__%> ON (<%=__JOIN_CLAUSE__%>) WHERE
    // (<%=__WHERE_CLAUSE__%>)
    String genericSQL = sqlGenericExpression.getBody();
    String joinClause = createJoinClause(aliasA, columnSetA, aliasB, columnSetB, useNulls);
    String whereClause = createWhereClause(aliasB, columnSetB);
    if (useNulls) {
        // add a where clause to avoid the equality of rows fully null (i.e. rows like "null,null,null"
        whereClause += dbms().and() + '(' + createNotNullCondition(aliasA, columnSetA) + ')';
    }
    String instantiatedSQL = dbms().fillGenericQueryWithJoin(genericSQL, tableNameA, tableNameB, joinClause, whereClause);
    Expression instantiatedExpression = CoreFactory.eINSTANCE.createExpression();
    instantiatedExpression.setLanguage(sqlGenericExpression.getLanguage());
    instantiatedExpression.setBody(instantiatedSQL);
    return instantiatedExpression;
}
Also used : Expression(orgomg.cwm.objectmodel.core.Expression)

Example 28 with Expression

use of orgomg.cwm.objectmodel.core.Expression in project tdq-studio-se by Talend.

the class RowMatchingAnalysisExecutor method runAnalysis.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.dq.analysis.AnalysisExecutor#runAnalysis(org.talend.dataquality.analysis.Analysis,
     * java.lang.String)
     */
@Override
protected boolean runAnalysis(Analysis analysis, String sqlStatement) {
    boolean isSuccess = Boolean.TRUE;
    TypedReturnCode<java.sql.Connection> trc = this.getConnectionBeforeRun(analysis);
    if (!trc.isOk()) {
        log.error(trc.getMessage());
        setError(trc.getMessage());
        traceError(Messages.getString("FunctionalDependencyExecutor.CANNOTEXECUTEANALYSIS", analysis.getName(), // $NON-NLS-1$
        trc.getMessage()));
        return Boolean.FALSE;
    }
    Connection connection = trc.getObject();
    try {
        // execute the sql statement for each indicator
        EList<Indicator> indicators = analysis.getResults().getIndicators();
        EList<Indicator> deactivatedIndicators = analysis.getParameters().getDeactivatedIndicators();
        for (Indicator indicator : indicators) {
            if (deactivatedIndicators.contains(indicator)) {
                // do not evaluate this indicator
                continue;
            }
            // set the connection's catalog
            if (this.catalogOrSchema != null && needChangeCatalog(connection)) {
                // check whether null argument can be given
                changeCatalog(this.catalogOrSchema, connection);
            }
            Expression query = dbms().getInstantiatedExpression(indicator);
            if (query == null) {
                traceError(// $NON-NLS-1$//$NON-NLS-2$
                "Query not executed for indicator: \"" + AnalysisExecutorHelper.getIndicatorName(indicator) + "\" " + // $NON-NLS-1$
                "query is null");
                isSuccess = Boolean.FALSE;
                continue;
            }
            try {
                Boolean isExecSuccess = executeQuery(indicator, connection, query);
                indicator.setComputed(true);
                if (!isExecSuccess) {
                    traceError(// $NON-NLS-1$//$NON-NLS-2$
                    "Query not executed for indicator: \"" + AnalysisExecutorHelper.getIndicatorName(indicator) + "\" " + "SQL query: " + // $NON-NLS-1$
                    query.getBody());
                    isSuccess = Boolean.FALSE;
                    continue;
                }
            } catch (AnalysisExecutionException e) {
                traceError(e.getMessage());
                isSuccess = Boolean.FALSE;
                continue;
            }
        }
    } finally {
        ReturnCode rc = closeConnection(analysis, connection);
        if (!rc.isOk()) {
            isSuccess = Boolean.FALSE;
        }
    }
    return isSuccess;
}
Also used : TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) ReturnCode(org.talend.utils.sugars.ReturnCode) Expression(orgomg.cwm.objectmodel.core.Expression) AnalysisExecutionException(org.talend.cwm.exception.AnalysisExecutionException) Connection(java.sql.Connection) Indicator(org.talend.dataquality.indicators.Indicator) RowMatchingIndicator(org.talend.dataquality.indicators.columnset.RowMatchingIndicator)

Example 29 with Expression

use of orgomg.cwm.objectmodel.core.Expression in project tdq-studio-se by Talend.

the class TableAnalysisSqlExecutor method executeRule.

/**
 * DOC yyin Comment method "executeRule".
 *
 * @param rule
 * @param connection
 * @return
 */
private boolean executeRule(WhereRuleIndicator rule, Connection connection) {
    // set the connection's catalog
    String catalogName = getCatalogOrSchemaName(rule.getAnalyzedElement());
    if (catalogName != null && needChangeCatalog(connection)) {
        // check whether null argument can be given
        changeCatalog(catalogName, connection);
    }
    Expression query = dbms().getInstantiatedExpression(rule);
    if (query == null) {
        traceError(// $NON-NLS-1$//$NON-NLS-2$
        "Query not executed for indicator: \"" + rule.getName() + "\" " + // $NON-NLS-1$
        "query is null");
        return Boolean.FALSE;
    } else {
        try {
            // and use this result as the count
            if (!isJoinConditionEmpty(rule)) {
                List<Object[]> myResultSet = executeQuery(catalogName, connection, query.getBody());
                rule.setCount(myResultSet);
            }
            // add the where condition to the query, and execute it
            createSqlQuery(stringDataFilter, rule, true);
            query = dbms().getInstantiatedExpression(rule);
            List<Object[]> myResultSet = executeQuery(catalogName, connection, query.getBody());
            // give result to indicator so that it handles the results
            Boolean isExecSuccess = rule.storeSqlResults(myResultSet);
            if (!isExecSuccess) {
                traceError(// $NON-NLS-1$//$NON-NLS-2$
                "Query not executed for indicator: \"" + rule.getName() + "\" " + "SQL query: " + // $NON-NLS-1$
                query.getBody());
                return Boolean.FALSE;
            }
        } catch (Exception e) {
            traceError(// $NON-NLS-1$//$NON-NLS-2$
            "Query not executed for indicator: \"" + rule.getName() + "\" " + "SQL query: " + query.getBody() + ". Exception: " + // $NON-NLS-1$ //$NON-NLS-2$
            e.getMessage());
            return Boolean.FALSE;
        }
    }
    rule.setComputed(true);
    return Boolean.TRUE;
}
Also used : TdExpression(org.talend.cwm.relational.TdExpression) Expression(orgomg.cwm.objectmodel.core.Expression) AnalysisExecutionException(org.talend.cwm.exception.AnalysisExecutionException) SQLException(java.sql.SQLException)

Example 30 with Expression

use of orgomg.cwm.objectmodel.core.Expression in project tdq-studio-se by Talend.

the class DefinitionHandler method updateRegex.

public boolean updateRegex(String dbmsName, String regexpFunction) {
    boolean ok = true;
    boolean replaced = false;
    IndicatorDefinition regexIndDef = this.getIndicatorDefinition(REGULAR_EXPRESSION_MATCHING);
    EList<TdExpression> sqlGenericExpression = regexIndDef.getSqlGenericExpression();
    for (Expression expression : sqlGenericExpression) {
        // MOD qiongli 2011-4-18,bug 16723.data cleansing.
        if (DbmsLanguageFactory.compareDbmsLanguage(dbmsName, expression.getLanguage())) {
            replaced = replaceBodyWith(expression, regexpFunction);
        }
    }
    if (!replaced) {
        // add new expression
        String genericSQL = getGenericSQL(dbmsName, regexpFunction);
        TdExpression createdExpression = BooleanExpressionHelper.createTdExpression(dbmsName, genericSQL);
        sqlGenericExpression.add(createdExpression);
    }
    return ok;
}
Also used : TdExpression(org.talend.cwm.relational.TdExpression) TdExpression(org.talend.cwm.relational.TdExpression) RegularExpression(org.talend.dataquality.domain.pattern.RegularExpression) Expression(orgomg.cwm.objectmodel.core.Expression) IndicatorDefinition(org.talend.dataquality.indicators.definition.IndicatorDefinition) UDIndicatorDefinition(org.talend.dataquality.indicators.definition.userdefine.UDIndicatorDefinition)

Aggregations

Expression (orgomg.cwm.objectmodel.core.Expression)71 TdExpression (org.talend.cwm.relational.TdExpression)42 RegularExpression (org.talend.dataquality.domain.pattern.RegularExpression)37 Test (org.junit.Test)25 Domain (org.talend.dataquality.domain.Domain)14 ArrayList (java.util.ArrayList)13 TdColumn (org.talend.cwm.relational.TdColumn)11 Pattern (org.talend.dataquality.domain.pattern.Pattern)10 Catalog (orgomg.cwm.resource.relational.Catalog)10 Indicator (org.talend.dataquality.indicators.Indicator)9 IndicatorParameters (org.talend.dataquality.indicators.IndicatorParameters)8 Analysis (org.talend.dataquality.analysis.Analysis)7 PatternMatchingIndicator (org.talend.dataquality.indicators.PatternMatchingIndicator)7 UDIndicatorDefinition (org.talend.dataquality.indicators.definition.userdefine.UDIndicatorDefinition)7 DbmsLanguage (org.talend.dq.dbms.DbmsLanguage)7 Connection (org.talend.core.model.metadata.builder.connection.Connection)6 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)6 AnalysisContext (org.talend.dataquality.analysis.AnalysisContext)6 IndicatorDefinition (org.talend.dataquality.indicators.definition.IndicatorDefinition)6 ChartDataEntity (org.talend.dq.indicators.preview.table.ChartDataEntity)6