Search in sources :

Example 1 with JDBCRowSource

use of org.talend.components.jdbc.runtime.JDBCRowSource in project components by Talend.

the class JDBCRowTestIT method test_reject_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_reject_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from notexists");
    properties.dieOnError.setValue(false);
    randomCommit(properties);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    JDBCRowSource source = new JDBCRowSource();
    source.initialize(null, properties);
    ValidationResult result = source.validate(null);
    Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    Reader reader = source.createReader(null);
    try {
        reader.start();
        reader.getCurrent();
        // should go to the exception before current statement
        Assert.fail();
        reader.advance();
        reader.close();
    } catch (DataRejectException e) {
        Map<String, Object> info = e.getRejectInfo();
        IndexedRecord data = (IndexedRecord) info.get("talend_record");
        Assert.assertNull(data.get(0));
        Assert.assertNotNull(data.get(1));
        Assert.assertNotNull(data.get(2));
    } finally {
        reader.close();
    }
}
Also used : TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException) Schema(org.apache.avro.Schema) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) Map(java.util.Map) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 2 with JDBCRowSource

use of org.talend.components.jdbc.runtime.JDBCRowSource in project components by Talend.

the class JDBCRowTestIT method test_use_preparedstatement_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_use_preparedstatement_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from " + tablename + " where id = ?");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(1));
    JDBCRowSource source = new JDBCRowSource();
    source.initialize(null, properties);
    ValidationResult result = source.validate(null);
    Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    Reader reader = source.createReader(null);
    try {
        reader.start();
        IndexedRecord row = (IndexedRecord) reader.getCurrent();
        ResultSet resultSet = (ResultSet) row.get(0);
        resultSet.next();
        Assert.assertEquals(1, resultSet.getInt(1));
        Assert.assertEquals("wangwei", resultSet.getString(2));
        resultSet.close();
        // only output one row when it works as a input component
        Assert.assertFalse(reader.advance());
        reader.close();
    } finally {
        reader.close();
    }
}
Also used : TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 3 with JDBCRowSource

use of org.talend.components.jdbc.runtime.JDBCRowSource in project components by Talend.

the class JdbcRowTestIT method test_basic_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_basic_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from " + tablename);
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSource source = (JDBCRowSource) sandboxedInstance.getInstance();
        source.initialize(null, properties);
        ValidationResult result = source.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        Reader reader = source.createReader(null);
        try {
            reader.start();
            IndexedRecord row = (IndexedRecord) reader.getCurrent();
            ResultSet resultSet = (ResultSet) row.get(0);
            resultSet.next();
            Assert.assertEquals(1, resultSet.getInt(1));
            Assert.assertEquals("wangwei", resultSet.getString(2));
            resultSet.next();
            Assert.assertEquals(2, resultSet.getInt(1));
            Assert.assertEquals("gaoyan", resultSet.getString(2));
            resultSet.next();
            Assert.assertEquals(3, resultSet.getInt(1));
            Assert.assertEquals("dabao", resultSet.getString(2));
            resultSet.close();
            // only output one row when it works as a input component
            Assert.assertFalse(reader.advance());
            reader.close();
        } finally {
            reader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 4 with JDBCRowSource

use of org.talend.components.jdbc.runtime.JDBCRowSource in project components by Talend.

the class JdbcRowTestIT method test_use_preparedstatement_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_use_preparedstatement_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from " + tablename + " where id = ?");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(1));
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSource source = (JDBCRowSource) sandboxedInstance.getInstance();
        source.initialize(null, properties);
        ValidationResult result = source.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        Reader reader = source.createReader(null);
        try {
            reader.start();
            IndexedRecord row = (IndexedRecord) reader.getCurrent();
            ResultSet resultSet = (ResultSet) row.get(0);
            resultSet.next();
            Assert.assertEquals(1, resultSet.getInt(1));
            Assert.assertEquals("wangwei", resultSet.getString(2));
            resultSet.close();
            // only output one row when it works as a input component
            Assert.assertFalse(reader.advance());
            reader.close();
        } finally {
            reader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 5 with JDBCRowSource

use of org.talend.components.jdbc.runtime.JDBCRowSource in project components by Talend.

the class JdbcRowTestIT method test_reject_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_reject_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from notexists");
    properties.dieOnError.setValue(false);
    randomCommit(properties);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSource source = (JDBCRowSource) sandboxedInstance.getInstance();
        source.initialize(null, properties);
        ValidationResult result = source.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        Reader reader = source.createReader(null);
        try {
            reader.start();
            reader.getCurrent();
            // should go to the exception before current statement
            Assert.fail();
            reader.advance();
            reader.close();
        } catch (DataRejectException e) {
            Map<String, Object> info = e.getRejectInfo();
            IndexedRecord data = (IndexedRecord) info.get("talend_record");
            Assert.assertNull(data.get(0));
            Assert.assertNotNull(data.get(1));
            Assert.assertNotNull(data.get(2));
        } finally {
            reader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException) Schema(org.apache.avro.Schema) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) Map(java.util.Map) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Aggregations

Schema (org.apache.avro.Schema)6 IndexedRecord (org.apache.avro.generic.IndexedRecord)6 Test (org.junit.Test)6 Reader (org.talend.components.api.component.runtime.Reader)6 JDBCRowSource (org.talend.components.jdbc.runtime.JDBCRowSource)6 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)6 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)6 ValidationResult (org.talend.daikon.properties.ValidationResult)6 ResultSet (java.sql.ResultSet)4 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)3 Map (java.util.Map)2 DataRejectException (org.talend.components.api.exception.DataRejectException)2