Search in sources :

Example 56 with AllSetting

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

the class JDBCRowSourceOrSink method validate.

@Override
public ValidationResult validate(RuntimeContainer runtime) {
    if (runtime != null) {
        runtime.setComponentData(runtime.getCurrentComponentId(), CommonUtils.getStudioNameFromProperty(ComponentConstants.RETURN_QUERY), setting.getSql());
    }
    ValidationResultMutable vr = new ValidationResultMutable();
    AllSetting setting = properties.getRuntimeSetting();
    String sql = setting.getSql();
    boolean usePreparedStatement = setting.getUsePreparedStatement();
    boolean dieOnError = setting.getDieOnError();
    Connection conn = null;
    try {
        conn = connect(runtime);
    } catch (ClassNotFoundException | SQLException e) {
        throw CommonUtils.newComponentException(e);
    }
    try {
        if (usePreparedStatement) {
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                JdbcRuntimeUtils.setPreparedStatement(pstmt, setting.getIndexs(), setting.getTypes(), setting.getValues());
                pstmt.execute();
            }
        } else {
            try (Statement stmt = conn.createStatement()) {
                stmt.execute(sql);
            }
        }
        if (useCommit) {
            conn.commit();
        }
    } catch (Exception ex) {
        if (dieOnError) {
            vr.setStatus(Result.ERROR);
            vr.setMessage(CommonUtils.correctExceptionInfo(ex));
        } else {
            System.err.println(CommonUtils.correctExceptionInfo(ex));
        }
    } finally {
        if (!useExistedConnection) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw CommonUtils.newComponentException(e);
            }
        }
    }
    return vr;
}
Also used : AllSetting(org.talend.components.jdbc.runtime.setting.AllSetting) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ValidationResultMutable(org.talend.daikon.properties.ValidationResultMutable) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Aggregations

AllSetting (org.talend.components.jdbc.runtime.setting.AllSetting)56 Test (org.junit.Test)38 RuntimeInfo (org.talend.daikon.runtime.RuntimeInfo)11 ExecutionEngine (org.talend.components.api.component.runtime.ExecutionEngine)7 JDBCDatastoreProperties (org.talend.components.jdbc.datastore.JDBCDatastoreProperties)6 JDBCDatasetProperties (org.talend.components.jdbc.dataset.JDBCDatasetProperties)4 Connection (java.sql.Connection)3 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 JarInputStream (java.util.jar.JarInputStream)1 BeforeClass (org.junit.BeforeClass)1 JDBCAvroRegistryString (org.talend.components.jdbc.avro.JDBCAvroRegistryString)1 JDBCConnectionModule (org.talend.components.jdbc.module.JDBCConnectionModule)1 TJDBCConnectionDefinition (org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition)1