Search in sources :

Example 11 with DbmsLanguage

use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.

the class DatabaseSQLExecutor method createSqlStatement.

/**
 * createSqlStatement: if has limit, add it, else do not use limit
 *
 * @param connection
 * @param analysedElements
 * @return
 */
private String createSqlStatement(DataManager connection, List<ModelElement> analysedElements, String where) {
    DbmsLanguage dbms = DbmsLanguageFactory.createDbmsLanguage(connection);
    TdColumn col = null;
    // $NON-NLS-1$
    StringBuilder sql = new StringBuilder("SELECT ");
    final Iterator<ModelElement> iterator = analysedElements.iterator();
    while (iterator.hasNext()) {
        ModelElement modelElement = iterator.next();
        col = SwitchHelpers.COLUMN_SWITCH.doSwitch(modelElement);
        sql.append(dbms.quote(col.getName()));
        // append comma if more columns exist
        if (iterator.hasNext()) {
            sql.append(',');
        }
    }
    sql.append(dbms.from());
    sql.append(dbms.getQueryColumnSetWithPrefix(col));
    if (where != null && where.length() > 0) {
        sql.append(dbms.where());
        sql.append(where);
    }
    String finalQuery = sql.toString();
    if (isShowRandomData()) {
        finalQuery = dbms.getRandomQuery(finalQuery);
    }
    if (getLimit() > 0) {
        return dbms.getTopNQuery(finalQuery, getLimit());
    } else {
        return finalQuery;
    }
}
Also used : DbmsLanguage(org.talend.dq.dbms.DbmsLanguage) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) TdColumn(org.talend.cwm.relational.TdColumn)

Example 12 with DbmsLanguage

use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.

the class ResultSetHelper method getResultSet.

public static ResultSet getResultSet(MetadataTable metadataTable, java.sql.Connection sqlConn, String whereExpression, int maxRows) throws SQLException {
    Connection tdDataProvider = ConnectionHelper.getTdDataProvider(metadataTable);
    if (sqlConn == null) {
        IMetadataConnection metadataBean = ConvertionHelper.convert(tdDataProvider);
        TypedReturnCode<java.sql.Connection> createConnection = MetadataConnectionUtils.createConnection(metadataBean, false);
        if (!createConnection.isOk()) {
            return null;
        }
        sqlConn = createConnection.getObject();
    }
    DbmsLanguage dbmsLanguage = DbmsLanguageFactory.createDbmsLanguage(tdDataProvider);
    Statement createStatement = null;
    if (maxRows != 0) {
        // TOPN algorithm, it has row limited,no need the fetch size.
        createStatement = dbmsLanguage.createStatement(sqlConn);
    } else {
        // Resevoir Sample algorithm
        createStatement = dbmsLanguage.createStatement(sqlConn, 1000);
    }
    createStatement.setMaxRows(maxRows);
    Expression columnQueryExpression = dbmsLanguage.getTableQueryExpression(metadataTable, whereExpression);
    return createStatement.executeQuery(columnQueryExpression.getBody());
}
Also used : DbmsLanguage(org.talend.dq.dbms.DbmsLanguage) Expression(orgomg.cwm.objectmodel.core.Expression) Statement(java.sql.Statement) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection)

Example 13 with DbmsLanguage

use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.

the class PreviewColumnAction method run.

@Override
public void run() {
    if (modelElements[0] instanceof TdColumn) {
        TdColumn[] columns = new TdColumn[modelElements.length];
        int i = 0;
        for (ModelElement me : modelElements) {
            columns[i] = (TdColumn) me;
            ++i;
        }
        if (ColumnHelper.isFromSameTable(Arrays.asList(columns))) {
            TdColumn oneColumn = columns[0];
            Connection dataprovider = ConnectionHelper.getTdDataProvider(oneColumn);
            ColumnSet columnSetOwner = ColumnHelper.getColumnOwnerAsColumnSet(oneColumn);
            DbmsLanguage language = DbmsLanguageFactory.createDbmsLanguage(dataprovider);
            String columnsWithPrefix = language.getQueryColumnsWithPrefix(columns);
            String columnSetName = language.getQueryColumnSetWithPrefix(columnSetOwner);
            // $NON-NLS-1$ //$NON-NLS-2$
            String query = "select " + columnsWithPrefix + " from " + columnSetName;
            SqlExplorerUtils.getDefault().runInDQViewer(dataprovider, query, columnSetName);
        } else {
            MessageDialogWithToggle.openWarning(null, DefaultMessagesImpl.getString("PreviewColumnAction.Warning"), // $NON-NLS-1$ //$NON-NLS-2$
            DefaultMessagesImpl.getString("PreviewColumnAction.previewColumns"));
        }
    }
}
Also used : DbmsLanguage(org.talend.dq.dbms.DbmsLanguage) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) TdColumn(org.talend.cwm.relational.TdColumn) Connection(org.talend.core.model.metadata.builder.connection.Connection) ColumnSet(orgomg.cwm.resource.relational.ColumnSet)

Example 14 with DbmsLanguage

use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.

the class AnalysisExecutorHelperTest method testGetTableName_5.

/**
 * try the mysql db type. Test method for
 * {@link org.talend.dq.helper.AnalysisExecutorHelper#getTableName(orgomg.cwm.objectmodel.core.ModelElement, org.talend.dq.dbms.DbmsLanguage)}
 * .
 */
@Test
public void testGetTableName_5() {
    TdColumn tdColumn = RelationalFactory.eINSTANCE.createTdColumn();
    TdTable tdTable = RelationalFactory.eINSTANCE.createTdTable();
    // $NON-NLS-1$
    tdTable.setName("tableName");
    tdColumn.setOwner(tdTable);
    // $NON-NLS-1$
    tdColumn.setName("columnName");
    DbmsLanguage dbmsLanguage = DbmsLanguageFactory.createDbmsLanguage(SupportDBUrlType.MYSQLDEFAULTURL.getLanguage(), null);
    // mock(Catalog.class);
    Package catalog = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createCatalog();
    // $NON-NLS-1$
    catalog.setName("catalogName");
    tdTable.setNamespace(catalog);
    // $NON-NLS-1$
    assertEquals("`catalogName`.`tableName`", AnalysisExecutorHelper.getTableName(tdColumn, dbmsLanguage));
}
Also used : DbmsLanguage(org.talend.dq.dbms.DbmsLanguage) TdColumn(org.talend.cwm.relational.TdColumn) TdTable(org.talend.cwm.relational.TdTable) Package(orgomg.cwm.objectmodel.core.Package) Test(org.junit.Test)

Example 15 with DbmsLanguage

use of org.talend.dq.dbms.DbmsLanguage in project tdq-studio-se by Talend.

the class AnalysisExecutorHelperTest method testGetTableName_3.

/**
 * try the sybase db type. Test method for
 * {@link org.talend.dq.helper.AnalysisExecutorHelper#getTableName(orgomg.cwm.objectmodel.core.ModelElement, org.talend.dq.dbms.DbmsLanguage)}
 * .
 */
@Test
public void testGetTableName_3() {
    TdColumn tdColumn = RelationalFactory.eINSTANCE.createTdColumn();
    TdTable tdTable = RelationalFactory.eINSTANCE.createTdTable();
    // $NON-NLS-1$
    tdTable.setName("tableName");
    tdColumn.setOwner(tdTable);
    // $NON-NLS-1$
    tdColumn.setName("columnName");
    DbmsLanguage dbmsLanguage = DbmsLanguageFactory.createDbmsLanguage(SupportDBUrlType.SYBASEDEFAULTURL.getLanguage(), null);
    // mock(Schema.class);
    Package schema = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createSchema();
    schema.setName("schemaName");
    tdTable.setNamespace(schema);
    // mock(Catalog.class);
    Package catalog = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createCatalog();
    // $NON-NLS-1$
    catalog.setName("catalogName");
    schema.setNamespace(catalog);
    // $NON-NLS-1$
    assertEquals("catalogName.schemaName.tableName", AnalysisExecutorHelper.getTableName(tdColumn, dbmsLanguage));
}
Also used : DbmsLanguage(org.talend.dq.dbms.DbmsLanguage) TdColumn(org.talend.cwm.relational.TdColumn) TdTable(org.talend.cwm.relational.TdTable) Package(orgomg.cwm.objectmodel.core.Package) Test(org.junit.Test)

Aggregations

DbmsLanguage (org.talend.dq.dbms.DbmsLanguage)29 Test (org.junit.Test)10 Connection (org.talend.core.model.metadata.builder.connection.Connection)9 TdColumn (org.talend.cwm.relational.TdColumn)9 Expression (orgomg.cwm.objectmodel.core.Expression)7 TdTable (org.talend.cwm.relational.TdTable)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 ModelElementIndicator (org.talend.dataprofiler.core.model.ModelElementIndicator)5 IndicatorUnit (org.talend.dataprofiler.core.ui.editor.preview.IndicatorUnit)5 Indicator (org.talend.dataquality.indicators.Indicator)5 Package (orgomg.cwm.objectmodel.core.Package)4 ArrayList (java.util.ArrayList)3 IFolder (org.eclipse.core.resources.IFolder)3 Shell (org.eclipse.swt.widgets.Shell)3 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)3 CreatePatternAction (org.talend.dataprofiler.core.pattern.actions.CreatePatternAction)3 IRepositoryNode (org.talend.repository.model.IRepositoryNode)3 ModelElement (orgomg.cwm.objectmodel.core.ModelElement)3 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2