Search in sources :

Example 96 with TdExpression

use of org.talend.cwm.relational.TdExpression in project tdq-studio-se by Talend.

the class TextIndicatorForNetezzaTest method testPatternLow.

@Test
public void testPatternLow() {
    TdExpression expression = findExpressionForNetezza(PATTERN_LOW_FREQUENCY);
    Assert.assertNotNull(expression);
    Assert.assertEquals(PATTERN_LOW_FREQUENCY_SQL, expression.getBody());
}
Also used : TdExpression(org.talend.cwm.relational.TdExpression) Test(org.junit.Test)

Example 97 with TdExpression

use of org.talend.cwm.relational.TdExpression in project tdq-studio-se by Talend.

the class UDIUtilsTest method testIsCurrentLanguageAndVersion.

/**
 * Test method for
 * {@link org.talend.dataprofiler.core.ui.utils.UDIUtils#isCurrentLanguageAndVersion(org.talend.cwm.relational.TdExpression, java.lang.String, java.lang.String)}
 * .
 */
@Test
public void testIsCurrentLanguageAndVersion() {
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    TdExpression tdExpression = UDIUtils.createNewTdExpression("MySQL", "5.0.2", "select * from test");
    // $NON-NLS-1$ //$NON-NLS-2$
    assertTrue(UDIUtils.isCurrentLanguageAndVersion(tdExpression, "MySQL", "5.0.2"));
    // $NON-NLS-1$ //$NON-NLS-2$
    assertFalse(UDIUtils.isCurrentLanguageAndVersion(tdExpression, "MySQL", "5.1.2"));
    // $NON-NLS-1$ //$NON-NLS-2$
    assertFalse(UDIUtils.isCurrentLanguageAndVersion(tdExpression, "Oracle", "5.0.2"));
    // $NON-NLS-1$
    assertFalse(UDIUtils.isCurrentLanguageAndVersion(tdExpression, "Oracle", null));
}
Also used : TdExpression(org.talend.cwm.relational.TdExpression) Test(org.junit.Test)

Example 98 with TdExpression

use of org.talend.cwm.relational.TdExpression in project tdq-studio-se by Talend.

the class ColumnAnalysisSqlExecutor method createSqlQuery.

/**
 * Method "createSqlQuery".
 *
 * @param dataFilterExpression
 * @param analysis
 * @param indicator
 *
 * @throws ParseException
 * @throws AnalysisExecutionException
 */
private boolean createSqlQuery(String dataFilterAsString, Indicator indicator) throws AnalysisExecutionException {
    TypedReturnCode<TdColumn> checkResult = getTdColumn(indicator);
    if (!checkResult.isOk()) {
        return false;
    }
    TdColumn tdColumn = checkResult.getObject();
    if (tdColumn.eIsProxy()) {
        tdColumn = (TdColumn) EObjectHelper.resolveObject(tdColumn);
    }
    TypedReturnCode<String> columnName = getColumnName(indicator, tdColumn);
    if (!columnName.isOk()) {
        return false;
    }
    String colName = columnName.getObject();
    TypedReturnCode<IndicatorDefinition> id = getIndicatorDefinition(indicator);
    if (!id.isOk()) {
        return false;
    }
    IndicatorDefinition indicatorDefinition = id.getObject();
    // get correct language for current database
    String language = dbms().getDbmsName();
    // --- create select statement
    // get indicator's sql columnS (generate the real SQL statement from its definition)
    Expression sqlGenericExpression = dbms().getSqlExpression(indicatorDefinition);
    final EClass indicatorEclass = indicator.eClass();
    if (sqlGenericExpression == null || sqlGenericExpression.getBody() == null) {
        // analysis, will not check the sql expression and create again(from the definition).
        if (UDIHelper.isUDI(indicator) && indicator.getInstantiatedExpressions().size() > 0) {
            return Boolean.TRUE;
        }
        // expressions.
        if (IndicatorsPackage.eINSTANCE.getRegexpMatchingIndicator().equals(indicatorEclass)) {
            // $NON-NLS-1$
            traceError(Messages.getString("ColumnAnalysisSqlExecutor.PLEASEREMOVEALLPATTEN"));
            return Boolean.FALSE;
        }
        traceError(Messages.getString(// $NON-NLS-1$
        "ColumnAnalysisSqlExecutor.UNSUPPORTEDINDICATOR", (indicator.getName() != null ? AnalysisExecutorHelper.getIndicatorName(indicator) : indicatorEclass.getName())));
        return Boolean.FALSE;
    }
    // --- get indicator parameters and convert them into sql expression
    List<String> whereExpression = new ArrayList<String>();
    if (StringUtils.isNotBlank(dataFilterAsString)) {
        whereExpression.add(dataFilterAsString);
    }
    List<String> rangeStrings = null;
    DateGrain dateAggregationType = null;
    IndicatorParameters parameters = indicator.getParameters();
    if (parameters != null) {
        // handle bins
        Domain bins = parameters.getBins();
        if (bins != null) {
            rangeStrings = getBinsAsGenericString(bins.getRanges(), colName);
        }
        DateParameters dateParameters = parameters.getDateParameters();
        if (dateParameters != null) {
            dateAggregationType = dateParameters.getDateAggregationType();
        }
        TextParameters textParameter = parameters.getTextParameter();
        if (textParameter != null) {
            if (textParameter.isIgnoreCase()) {
                colName = dbms().toUpperCase(colName);
            }
            if (!textParameter.isUseBlank() && IndicatorsPackage.eINSTANCE.getLengthIndicator().isSuperTypeOf(indicatorEclass)) {
                String tdColName = getQuotedColumnName(tdColumn);
                // $NON-NLS-1$
                tdColName = dbms().replaceNullsWithString(tdColName, "'NULL TALEND'");
            } else if (textParameter.isUseBlank() && IndicatorsPackage.eINSTANCE.getFrequencyIndicator().isSuperTypeOf(indicatorEclass)) {
                colName = dbms().trim(colName);
            }
        }
    }
    TypedReturnCode<String> completedQuery = getCompletedQuery(indicator, tdColumn, colName, indicatorDefinition, language, sqlGenericExpression, indicatorEclass, whereExpression, rangeStrings, dateAggregationType);
    if (!completedQuery.isOk()) {
        return false;
    }
    String finalQuery = completedQuery.getObject();
    if (finalQuery != null) {
        TdExpression instantiateSqlExpression = BooleanExpressionHelper.createTdExpression(language, finalQuery);
        indicator.setInstantiatedExpression(instantiateSqlExpression);
        return true;
    }
    return false;
}
Also used : DateGrain(org.talend.dataquality.indicators.DateGrain) TdExpression(org.talend.cwm.relational.TdExpression) IndicatorParameters(org.talend.dataquality.indicators.IndicatorParameters) ArrayList(java.util.ArrayList) DateParameters(org.talend.dataquality.indicators.DateParameters) IndicatorDefinition(org.talend.dataquality.indicators.definition.IndicatorDefinition) TextParameters(org.talend.dataquality.indicators.TextParameters) EClass(org.eclipse.emf.ecore.EClass) TdColumn(org.talend.cwm.relational.TdColumn) TdExpression(org.talend.cwm.relational.TdExpression) Expression(orgomg.cwm.objectmodel.core.Expression) Domain(org.talend.dataquality.domain.Domain)

Example 99 with TdExpression

use of org.talend.cwm.relational.TdExpression 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 100 with TdExpression

use of org.talend.cwm.relational.TdExpression in project tdq-studio-se by Talend.

the class FrequencyStatisticsExplorer method getQueryForUDIndicator.

/**
 * get Query For User define indicator.
 *
 * @param indicatorDefinition
 * @return
 */
private String getQueryForUDIndicator(IndicatorDefinition indicatorDefinition) {
    String sql = PluginConstant.EMPTY_STRING;
    EList<TdExpression> list = ((UDIndicatorDefinition) indicatorDefinition).getViewRowsExpression();
    TdExpression tdExp = DbmsLanguage.getSqlExpression(indicatorDefinition, dbmsLanguage.getDbmsName(), list, dbmsLanguage.getDbVersion());
    sql = tdExp.getBody();
    String dataFilterClause = getDataFilterClause();
    if (!dataFilterClause.equals(PluginConstant.EMPTY_STRING)) {
        // $NON-NLS-1$ //$NON-NLS-2$
        sql = sql.replace(GenericSQLHandler.WHERE_CLAUSE, dbmsLanguage.where() + "(" + dataFilterClause + ")");
        // $NON-NLS-1$ //$NON-NLS-2$
        sql = sql.replace(GenericSQLHandler.AND_WHERE_CLAUSE, dbmsLanguage.and() + "(" + dataFilterClause + ")");
    } else {
        sql = sql.replace(GenericSQLHandler.WHERE_CLAUSE, PluginConstant.EMPTY_STRING);
        sql = sql.replace(GenericSQLHandler.AND_WHERE_CLAUSE, PluginConstant.EMPTY_STRING);
    }
    ModelElement analyzedElement = this.indicator.getAnalyzedElement();
    String tableName = getFullyQualifiedTableName(analyzedElement);
    sql = sql.replace(GenericSQLHandler.TABLE_NAME, tableName);
    sql = sql.replace(GenericSQLHandler.COLUMN_NAMES, dbmsLanguage.quote(analyzedElement.getName()));
    if (sql.indexOf(GenericSQLHandler.UDI_INDICATOR_VALUE) != -1) {
        Object key = entity.getKey();
        if (key == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            sql = sql.replace("= " + GenericSQLHandler.UDI_INDICATOR_VALUE, "IS NULL");
        } else {
            // $NON-NLS-1$ //$NON-NLS-2$
            sql = sql.replace(GenericSQLHandler.UDI_INDICATOR_VALUE, "'" + key + "'");
        }
    }
    if (sql.indexOf(GenericSQLHandler.GROUP_BY_ALIAS) != -1) {
        sql = sql.replace(GenericSQLHandler.GROUP_BY_ALIAS, dbmsLanguage.quote(analyzedElement.getName()));
    }
    return sql;
}
Also used : ModelElement(orgomg.cwm.objectmodel.core.ModelElement) TdExpression(org.talend.cwm.relational.TdExpression) UDIndicatorDefinition(org.talend.dataquality.indicators.definition.userdefine.UDIndicatorDefinition)

Aggregations

TdExpression (org.talend.cwm.relational.TdExpression)121 Test (org.junit.Test)51 IndicatorDefinition (org.talend.dataquality.indicators.definition.IndicatorDefinition)30 UDIndicatorDefinition (org.talend.dataquality.indicators.definition.userdefine.UDIndicatorDefinition)29 ArrayList (java.util.ArrayList)19 RegularExpression (org.talend.dataquality.domain.pattern.RegularExpression)19 Pattern (org.talend.dataquality.domain.pattern.Pattern)16 TdColumn (org.talend.cwm.relational.TdColumn)12 Expression (orgomg.cwm.objectmodel.core.Expression)12 BasicEList (org.eclipse.emf.common.util.BasicEList)11 IndicatorParameters (org.talend.dataquality.indicators.IndicatorParameters)11 Domain (org.talend.dataquality.domain.Domain)10 TdTable (org.talend.cwm.relational.TdTable)9 ProductVersion (org.talend.utils.ProductVersion)9 Analysis (org.talend.dataquality.analysis.Analysis)8 PatternComponent (org.talend.dataquality.domain.pattern.PatternComponent)8 ChartDataEntity (org.talend.dq.indicators.preview.table.ChartDataEntity)8 File (java.io.File)7 IFile (org.eclipse.core.resources.IFile)6 IFolder (org.eclipse.core.resources.IFolder)6