Search in sources :

Example 6 with BinaryObjectCellFactory

use of org.knime.core.data.blob.BinaryObjectCellFactory in project knime-core by knime.

the class DatabaseReaderConnection method createRowIteratorConnection.

/**
 * Read data from database using a {@link RowIterator}.
 * @param exec used for progress info
 * @param cp {@link CredentialsProvider} providing user/password
 * @param useDbRowId <code>true</code> if the row id returned by the database should be used to generate the
 * KNIME row id
 * @return an object that represents the open database connection. The individual entries are accessible by means of a {@link RowIterator}.
 * @throws CanceledExecutionException if canceled in between
 * @throws SQLException if the connection could not be opened
 * @since 3.1
 */
public RowIteratorConnection createRowIteratorConnection(final ExecutionContext exec, final CredentialsProvider cp, final boolean useDbRowId) throws SQLException {
    if (m_blobFactory == null) {
        m_blobFactory = new BinaryObjectCellFactory(exec);
    }
    // synchronized (m_conn.syncConnection(conn)) {
    return m_conn.execute(cp, conn -> {
        exec.setMessage("Start reading rows from database...");
        // remember auto-commit flag
        final boolean autoCommit = conn.getAutoCommit();
        final Statement stmt = initStatement(cp, conn);
        int fetchsize = (DatabaseConnectionSettings.FETCH_SIZE != null) ? DatabaseConnectionSettings.FETCH_SIZE : -1;
        m_conn.getUtility().getStatementManipulator().setFetchSize(stmt, fetchsize);
        final String[] oQueries = m_conn.getQuery().split(SQL_QUERY_SEPARATOR);
        // execute all except the last query
        for (int i = 0; i < oQueries.length - 1; i++) {
            LOGGER.debug("Executing SQL statement as execute: " + oQueries[i]);
            stmt.execute(oQueries[i]);
        }
        final String selectQuery = oQueries[oQueries.length - 1];
        LOGGER.debug("Executing SQL statement as executeQuery: " + selectQuery);
        final ResultSet result = stmt.executeQuery(selectQuery);
        LOGGER.debug("Reading meta data from database ResultSet...");
        m_spec = createTableSpec(result.getMetaData());
        LOGGER.debug("Parsing database ResultSet...");
        return new RowIteratorConnection(conn, stmt, result, m_spec, autoCommit, useDbRowId);
    });
}
Also used : Statement(java.sql.Statement) BinaryObjectCellFactory(org.knime.core.data.blob.BinaryObjectCellFactory) ResultSet(java.sql.ResultSet)

Aggregations

BinaryObjectCellFactory (org.knime.core.data.blob.BinaryObjectCellFactory)6 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)4 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 RowIterator (org.knime.core.data.RowIterator)3 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)3 DataCell (org.knime.core.data.DataCell)2 DataContainer (org.knime.core.data.container.DataContainer)2 InputStream (java.io.InputStream)1 TimeZone (java.util.TimeZone)1 Test (org.junit.Test)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataRow (org.knime.core.data.DataRow)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 RowKey (org.knime.core.data.RowKey)1 AppendedColumnRow (org.knime.core.data.append.AppendedColumnRow)1 BinaryObjectDataCell (org.knime.core.data.blob.BinaryObjectDataCell)1 DataCellToJavaConverter (org.knime.core.data.convert.java.DataCellToJavaConverter)1 DefaultRow (org.knime.core.data.def.DefaultRow)1