Search in sources :

Example 1 with TdTable

use of org.talend.cwm.relational.TdTable in project tdi-studio-se by Talend.

the class QueryGuessCommandTest method generateNewQuery.

@Test
public void generateNewQuery() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    connection = ConnectionFactory.eINSTANCE.createConnection();
    node = Mockito.mock(INode.class);
    connection.setContextMode(true);
    TdTable table = RelationalFactory.eINSTANCE.createTdTable();
    table.setName("tableName");
    table.setLabel("tableLabel");
    TdColumn column1 = RelationalFactory.eINSTANCE.createTdColumn();
    column1.setId("ID1");
    column1.setName("cName1");
    TdColumn column2 = RelationalFactory.eINSTANCE.createTdColumn();
    column2.setId("ID2");
    column2.setName("cName2");
    table.getColumns().add(column1);
    table.getColumns().add(column2);
    metadataTable = ConvertionHelper.convert(table);
    ElementParameter parameter = new ElementParameter(node);
    parameter.setName("DBTABLE");
    parameter.setValue(table.getName());
    Mockito.when(node.getPropertyValue(EParameterName.PROPERTY_TYPE.getName())).thenReturn("REPOSITORY");
    Mockito.when(node.getElementParameterFromField(EParameterFieldType.DBTABLE)).thenReturn(parameter);
    String schema = "schema";
    // add test for PostgreSQL
    String dbType = EDatabaseTypeName.PSQL.getDisplayName();
    QueryGuessCommand command = new QueryGuessCommand(node, metadataTable, schema, dbType, connection);
    Method method = command.getClass().getDeclaredMethod("generateNewQuery");
    method.setAccessible(true);
    String queryString = (String) method.invoke(command);
    Assert.assertNotNull(queryString);
    Assert.assertTrue(queryString.contains(schema));
    Assert.assertTrue(queryString.contains(metadataTable.getTableName()));
    Assert.assertTrue(queryString.contains("cName1"));
    Assert.assertTrue(queryString.contains("cName2"));
    // add test for MySQL
    dbType = EDatabaseTypeName.MYSQL.getDisplayName();
    command = new QueryGuessCommand(node, metadataTable, schema, dbType, connection);
    method = command.getClass().getDeclaredMethod("generateNewQuery");
    method.setAccessible(true);
    queryString = (String) method.invoke(command);
    Assert.assertNotNull(queryString);
    Assert.assertFalse(queryString.contains(schema));
    Assert.assertTrue(queryString.contains(metadataTable.getTableName()));
    Assert.assertTrue(queryString.contains("cName1"));
    Assert.assertTrue(queryString.contains("cName2"));
}
Also used : INode(org.talend.core.model.process.INode) ElementParameter(org.talend.designer.core.model.components.ElementParameter) TdTable(org.talend.cwm.relational.TdTable) TdColumn(org.talend.cwm.relational.TdColumn) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 2 with TdTable

use of org.talend.cwm.relational.TdTable in project tdi-studio-se by Talend.

the class QueryGuessCommandTest method generateNewTeradataQuery.

@Test
public void generateNewTeradataQuery() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, PersistenceException {
    connection = ConnectionFactory.eINSTANCE.createConnection();
    node = Mockito.mock(INode.class);
    connection.setContextMode(true);
    TdTable table = RelationalFactory.eINSTANCE.createTdTable();
    table.setName("tableName");
    table.setLabel("tableLabel");
    TdColumn column1 = RelationalFactory.eINSTANCE.createTdColumn();
    column1.setName("id");
    TdColumn column2 = RelationalFactory.eINSTANCE.createTdColumn();
    column2.setName("name");
    table.getColumns().add(column1);
    table.getColumns().add(column2);
    metadataTable = ConvertionHelper.convert(table);
    ElementParameter parameter = new ElementParameter(node);
    parameter.setName("DBTABLE");
    parameter.setValue(table.getName());
    Mockito.when(node.getPropertyValue(EParameterName.PROPERTY_TYPE.getName())).thenReturn("REPOSITORY");
    Mockito.when(node.getElementParameterFromField(EParameterFieldType.DBTABLE)).thenReturn(parameter);
    // test case 1
    String schema = "myschema";
    String dbType = EDatabaseTypeName.TERADATA.getDisplayName();
    String expectedQuery = "\"SELECT myschema.tableName.\\\"id\\\", myschema.tableName.name FROM myschema.tableName\"";
    QueryGuessCommand command = new QueryGuessCommand(node, metadataTable, schema, dbType, connection);
    Method method = command.getClass().getDeclaredMethod("generateNewQuery");
    method.setAccessible(true);
    String queryString = (String) method.invoke(command);
    Assert.assertEquals(expectedQuery, queryString);
}
Also used : INode(org.talend.core.model.process.INode) ElementParameter(org.talend.designer.core.model.components.ElementParameter) TdTable(org.talend.cwm.relational.TdTable) TdColumn(org.talend.cwm.relational.TdColumn) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 3 with TdTable

use of org.talend.cwm.relational.TdTable in project tdi-studio-se by Talend.

the class FillParametersForDatabaseConnectionMigrationTask method fillParametersForColumns.

private void fillParametersForColumns(EList<orgomg.cwm.objectmodel.core.Package> pkgs) {
    for (orgomg.cwm.objectmodel.core.Package pkg : pkgs) {
        // handle tables
        List<TdTable> tables = PackageHelper.getTables(pkg);
        for (TdTable table : tables) {
            fillParamaters(TableHelper.getColumns(table));
        }
        // handle views
        List<TdView> views = PackageHelper.getViews(pkg);
        for (TdView view : views) {
            fillParamaters(ViewHelper.getColumns(view));
        }
    }
}
Also used : TdTable(org.talend.cwm.relational.TdTable) TdView(org.talend.cwm.relational.TdView)

Example 4 with TdTable

use of org.talend.cwm.relational.TdTable in project tdi-studio-se by Talend.

the class QueryGuessCommandTest method generateNewTeradataQuery2.

@Test
public void generateNewTeradataQuery2() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, PersistenceException {
    ProxyRepositoryFactory repFactory = ProxyRepositoryFactory.getInstance();
    String propertyId = repFactory.getNextId();
    try {
        DatabaseConnection connection = ConnectionFactory.eINSTANCE.createDatabaseConnection();
        connection.setSID("myschema");
        Property connectionProperty = PropertiesFactory.eINSTANCE.createProperty();
        connectionProperty.setAuthor(((RepositoryContext) CoreRuntimePlugin.getInstance().getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getUser());
        connectionProperty.setVersion(VersionUtils.DEFAULT_VERSION);
        //$NON-NLS-1$
        connectionProperty.setStatusCode("");
        connectionProperty.setId(propertyId);
        connectionProperty.setLabel("test_connection");
        ConnectionItem connectionItem = PropertiesFactory.eINSTANCE.createDatabaseConnectionItem();
        connectionItem.setProperty(connectionProperty);
        connectionItem.setConnection(connection);
        repFactory.create(connectionItem, new Path(""));
        node = Mockito.mock(INode.class);
        connection.setContextMode(true);
        TdTable table = RelationalFactory.eINSTANCE.createTdTable();
        table.setName("tableName");
        table.setLabel("tableLabel");
        TdColumn column1 = RelationalFactory.eINSTANCE.createTdColumn();
        column1.setName("id");
        TdColumn column2 = RelationalFactory.eINSTANCE.createTdColumn();
        column2.setName("name");
        table.getColumns().add(column1);
        table.getColumns().add(column2);
        metadataTable = ConvertionHelper.convert(table);
        ElementParameter parameter = new ElementParameter(node);
        parameter.setName("DBTABLE");
        parameter.setValue(table.getName());
        Mockito.when(node.getPropertyValue(EParameterName.PROPERTY_TYPE.getName())).thenReturn("REPOSITORY");
        Mockito.when(node.getElementParameterFromField(EParameterFieldType.DBTABLE)).thenReturn(parameter);
        ElementParameter connectionId = new ElementParameter(node);
        connectionId.setValue(connectionProperty.getId());
        Mockito.when(node.getElementParameter(EParameterName.REPOSITORY_PROPERTY_TYPE.getName())).thenReturn(connectionId);
        // test case 1
        String schema = "";
        String dbType = EDatabaseTypeName.TERADATA.getDisplayName();
        String expectedQuery = "\"SELECT myschema.tableName.\\\"id\\\", myschema.tableName.name FROM myschema.tableName\"";
        QueryGuessCommand command = new QueryGuessCommand(node, metadataTable, schema, dbType, connection);
        Method method = command.getClass().getDeclaredMethod("generateNewQuery");
        method.setAccessible(true);
        String queryString = (String) method.invoke(command);
        Assert.assertEquals(expectedQuery, queryString);
    } catch (Exception e) {
        throw e;
    } finally {
        IRepositoryViewObject lastVersion = repFactory.getLastVersion(propertyId);
        if (lastVersion != null) {
            repFactory.deleteObjectPhysical(lastVersion);
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) INode(org.talend.core.model.process.INode) TdTable(org.talend.cwm.relational.TdTable) ConnectionItem(org.talend.core.model.properties.ConnectionItem) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException) ElementParameter(org.talend.designer.core.model.components.ElementParameter) ProxyRepositoryFactory(org.talend.core.repository.model.ProxyRepositoryFactory) TdColumn(org.talend.cwm.relational.TdColumn) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) Property(org.talend.core.model.properties.Property) Test(org.junit.Test)

Aggregations

TdTable (org.talend.cwm.relational.TdTable)4 Method (java.lang.reflect.Method)3 Test (org.junit.Test)3 INode (org.talend.core.model.process.INode)3 TdColumn (org.talend.cwm.relational.TdColumn)3 ElementParameter (org.talend.designer.core.model.components.ElementParameter)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (org.eclipse.core.runtime.Path)1 PersistenceException (org.talend.commons.exception.PersistenceException)1 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)1 ConnectionItem (org.talend.core.model.properties.ConnectionItem)1 Property (org.talend.core.model.properties.Property)1 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)1 ProxyRepositoryFactory (org.talend.core.repository.model.ProxyRepositoryFactory)1 TdView (org.talend.cwm.relational.TdView)1