Search in sources :

Example 56 with Expression

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

the class ColumnAnalysisSqlExecutor method runAnalysisIndicators.

/**
 * DOC xqliu Comment method "runAnalysisIndicators".
 *
 * @param connection
 * @param elementToIndicator
 * @param indicators
 * @return
 * @throws SQLException
 */
private boolean runAnalysisIndicators(Connection connection, Map<ModelElement, List<Indicator>> elementToIndicator, Collection<Indicator> indicators) throws SQLException {
    boolean runStatus = Boolean.TRUE;
    for (Indicator indicator : indicators) {
        // skip composite indicators that do not require a sql execution
        if (indicator instanceof CompositeIndicator) {
            // options of composite indicators are handled elsewhere
            continue;
        }
        // set the connection's catalog
        if (needChangeCatalog(connection)) {
            String catalogName = getCatalogOrSchemaName(indicator.getAnalyzedElement());
            if (catalogName != null) {
                // check whether null argument can be given
                changeCatalog(catalogName, connection);
            }
        }
        Expression query = dbms().getInstantiatedExpression(indicator);
        if (query == null || !executeQuery(indicator, connection, query.getBody())) {
            traceError(// $NON-NLS-1$//$NON-NLS-2$
            "Query not executed for indicator: \"" + AnalysisExecutorHelper.getIndicatorName(indicator) + "\" " + // $NON-NLS-1$//$NON-NLS-2$
            ((query == null) ? "query is null" : "SQL query: " + query.getBody()));
            runStatus = Boolean.FALSE;
        } else {
            // set computation done
            indicator.setComputed(true);
        }
        // add mapping of analyzed elements to their indicators
        addElements2IndicatorsMapping(elementToIndicator, indicator);
    }
    return runStatus;
}
Also used : TdExpression(org.talend.cwm.relational.TdExpression) Expression(orgomg.cwm.objectmodel.core.Expression) CompositeIndicator(org.talend.dataquality.indicators.CompositeIndicator) RowCountIndicator(org.talend.dataquality.indicators.RowCountIndicator) NullCountIndicator(org.talend.dataquality.indicators.NullCountIndicator) Indicator(org.talend.dataquality.indicators.Indicator) CompositeIndicator(org.talend.dataquality.indicators.CompositeIndicator)

Example 57 with Expression

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

the class ColumnAnalysisSqlExecutor method getPatterns.

/**
 * DOC scorreia Comment method "getPatterns".
 *
 * @param indicator
 * @return the patterns or null if none has been found
 */
protected List<String> getPatterns(Indicator indicator) {
    List<String> patternStrings = new ArrayList<String>();
    Domain dataValidDomain = indicator.getParameters().getDataValidDomain();
    if (dataValidDomain == null) {
        return patternStrings;
    }
    EList<Pattern> patterns = dataValidDomain.getPatterns();
    for (Pattern pattern : patterns) {
        Expression expression = this.dbms().getRegexp(pattern);
        String regexp = expression == null ? null : expression.getBody();
        if (regexp != null) {
            patternStrings.add(regexp);
        }
    }
    return patternStrings;
}
Also used : Pattern(org.talend.dataquality.domain.pattern.Pattern) TdExpression(org.talend.cwm.relational.TdExpression) Expression(orgomg.cwm.objectmodel.core.Expression) ArrayList(java.util.ArrayList) Domain(org.talend.dataquality.domain.Domain)

Example 58 with Expression

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

the class MultiColumnAnalysisExecutor method runAnalysis.

/**
 * DOC scorreia Comment method "getTableName".
 *
 * @param analyzedColumns
 * @return the quoted table name
 */
/*
     * tableName = columnSetOwner.getName(); Package pack = PackageHelper.getCatalogOrSchema(columnSetOwner); if (pack
     * == null) { log.error("No Catalog or Schema found for column set owner: " + tableName); continue; // do not break
     * until we find the owner } this.catalogOrSchema = pack.getName(); break; // all columns should belong to the same
     * table
     */
/*
     * (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 = 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();
        for (Indicator indicator : indicators) {
            indicator.prepare();
            // 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 isExeSuccess = executeQuery(indicator, connection, query);
                if (!isExeSuccess) {
                    traceError(// $NON-NLS-1$//$NON-NLS-2$
                    "Query not executed for indicator: \"" + AnalysisExecutorHelper.getIndicatorName(indicator) + "\" " + // $NON-NLS-1$//$NON-NLS-2$
                    ((query == null) ? "query is null" : "SQL query: " + query.getBody()));
                    isSuccess = Boolean.FALSE;
                    continue;
                }
            } catch (Exception e) {
                traceError(e.getMessage());
                isSuccess = Boolean.FALSE;
                continue;
            }
            indicator.setComputed(true);
        }
    } 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) Connection(java.sql.Connection) ColumnSetMultiValueIndicator(org.talend.dataquality.indicators.columnset.ColumnSetMultiValueIndicator) Indicator(org.talend.dataquality.indicators.Indicator) AllMatchIndicator(org.talend.dataquality.indicators.columnset.AllMatchIndicator) SimpleStatIndicator(org.talend.dataquality.indicators.columnset.SimpleStatIndicator) SQLException(java.sql.SQLException)

Example 59 with Expression

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

the class TableAnalysisSqlExecutor method createSqlQuery.

private boolean createSqlQuery(String dataFilterAsString, Indicator indicator, boolean withWhereOfRule) throws AnalysisExecutionException {
    if (!isAnalyzedElementValid(indicator)) {
        return Boolean.FALSE;
    }
    IndicatorDefinition indicatorDefinition = indicator.getIndicatorDefinition();
    if (!isIndicatorDefinitionValid(indicatorDefinition, AnalysisExecutorHelper.getIndicatorName(indicator))) {
        return Boolean.FALSE;
    }
    Expression sqlGenericExpression = dbms().getSqlExpression(indicatorDefinition);
    if (!isExpressionValid(sqlGenericExpression, indicator)) {
        return Boolean.FALSE;
    }
    // --- get indicator parameters and convert them into sql expression
    List<String> whereExpressionDQRule = new ArrayList<String>();
    final EList<JoinElement> joinConditions = indicator.getJoinConditions();
    if (RulesPackage.eINSTANCE.getWhereRule().equals(indicatorDefinition.eClass())) {
        WhereRule wr = (WhereRule) indicatorDefinition;
        if (withWhereOfRule) {
            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);
            }
        }
    }
    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;
    }
    // --- default case
    // allow join
    String joinclause = (!joinConditions.isEmpty()) ? dbms().createJoinConditionAsString(set, joinConditions, catalogName, schemaName) : PluginConstant.EMPTY_STRING;
    String setName = dbms().toQualifiedName(catalogName, schemaName, quote(set.getName()));
    String completedSqlString = dbms().fillGenericQueryWithJoin(sqlGenericExpression.getBody(), setName, joinclause);
    // ~
    List<String> whereExpressionAnalysis = new ArrayList<String>();
    if (StringUtils.isNotBlank(dataFilterAsString)) {
        whereExpressionAnalysis.add(dataFilterAsString);
    }
    completedSqlString = addWhereToSqlStringStatement(whereExpressionAnalysis, whereExpressionDQRule, completedSqlString, true);
    // completedSqlString is the final query
    String finalQuery = completedSqlString;
    TdExpression instantiateSqlExpression = BooleanExpressionHelper.createTdExpression(dbms().getDbmsName(), finalQuery);
    indicator.setInstantiatedExpression(instantiateSqlExpression);
    return true;
}
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) TdExpression(org.talend.cwm.relational.TdExpression) 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 60 with Expression

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

the class FunctionalDependencyExecutor method instantiateQuery.

private boolean instantiateQuery(Indicator indicator, DbmsLanguage dbmsLanguage) {
    if (ColumnsetPackage.eINSTANCE.getColumnDependencyIndicator().equals(indicator.eClass())) {
        ColumnDependencyIndicator rowMatchingIndicator = (ColumnDependencyIndicator) indicator;
        TdColumn columnA = rowMatchingIndicator.getColumnA();
        TdColumn columnB = rowMatchingIndicator.getColumnB();
        IndicatorDefinition indicatorDefinition = indicator.getIndicatorDefinition();
        // SystemIndicator),the IndicatorDefinition maybe a proxy,should reset it.
        if (indicatorDefinition == null || indicatorDefinition.eIsProxy()) {
            // $NON-NLS-1$
            indicatorDefinition = DefinitionHandler.getInstance().getIndicatorDefinition("Functional Dependency");
            indicator.setIndicatorDefinition(indicatorDefinition);
        }
        Expression sqlGenericExpression = dbms().getSqlExpression(indicatorDefinition);
        Expression instantiatedSqlExpression = createInstantiatedSqlExpression(sqlGenericExpression, columnA, columnB, dbmsLanguage);
        indicator.setInstantiatedExpression(instantiatedSqlExpression);
        return true;
    }
    traceError(Messages.getString("FunctionalDependencyExecutor.UNHANDLEDGIVENINDICATOR", // $NON-NLS-1$
    AnalysisExecutorHelper.getIndicatorName(indicator)));
    return Boolean.FALSE;
}
Also used : ColumnDependencyIndicator(org.talend.dataquality.indicators.columnset.ColumnDependencyIndicator) TdColumn(org.talend.cwm.relational.TdColumn) Expression(orgomg.cwm.objectmodel.core.Expression) IndicatorDefinition(org.talend.dataquality.indicators.definition.IndicatorDefinition)

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