Search in sources :

Example 36 with DataContainer

use of org.knime.core.data.container.DataContainer in project knime-core by knime.

the class WindowCacheTableTest method testWindowCacheWithEmptyTable.

/**
 * Tests initialization of cache with an empty table (no rows)
 * @throws UnknownRowCountException
 * @throws CanceledExecutionException
 */
@Test
public void testWindowCacheWithEmptyTable() throws UnknownRowCountException, CanceledExecutionException {
    DataContainer emptyContainer = new DataContainer(TABLE_SPEC);
    emptyContainer.close();
    WindowCacheTable cache = new WindowCacheTable(emptyContainer.getTable());
    assertTrue("Cache should have data", cache.hasData());
    assertTrue("Cache should have row count", cache.hasRowCount());
    assertEquals("Cache should have no rows", 0, cache.getRowCount());
    assertEquals("Col count of table should equal col count of cache", TABLE_SPEC.getNumColumns(), cache.getColumnCount());
    assertEquals("Tables should be equal", emptyContainer.getTable(), cache.getDataTable());
    assertEquals("Table specs should be equal", emptyContainer.getTableSpec(), cache.getDataTableSpec());
    assertEquals("Table names should be equal", emptyContainer.getTableSpec().getName(), cache.getTableName());
    try {
        cache.getRows(0, 1, new ExecutionMonitor());
        fail("Should not be able to access an cache without data");
    } catch (IndexOutOfBoundsException e) {
    /* expected */
    }
}
Also used : DataContainer(org.knime.core.data.container.DataContainer) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) Test(org.junit.Test)

Example 37 with DataContainer

use of org.knime.core.data.container.DataContainer in project knime-core by knime.

the class DBReaderImpl method createTable.

/**
 * Called from the database port to read the first n-number of rows.
 *
 * @param useDbRowId <code>true</code> if the KNIME row id should based on the db row id
 * @param cachedNoRows number of rows cached for data preview
 * @param cp {@link CredentialsProvider} providing user/password
 * @return buffered data table read from database
 * @throws SQLException if the connection could not be opened
 */
DataTable createTable(final boolean useDbRowId, final int cachedNoRows, final CredentialsProvider cp) throws SQLException {
    if (m_blobFactory == null) {
        m_blobFactory = new BinaryObjectCellFactory();
    }
    // retrieve connection
    // final Connection conn = initConnection(cp);
    final DatabaseQueryConnectionSettings dbConn = getQueryConnection();
    // synchronized (dbConn.syncConnection(conn)) {
    return dbConn.execute(cp, conn -> {
        // remember auto-commit flag
        final boolean autoCommit = conn.getAutoCommit();
        final Statement stmt = initStatement(cp, conn);
        try {
            final String[] oQueries = dbConn.getQuery().split(SQL_QUERY_SEPARATOR);
            if (cachedNoRows < 0) {
                int fetchsize = (DatabaseConnectionSettings.FETCH_SIZE != null) ? DatabaseConnectionSettings.FETCH_SIZE : -1;
                dbConn.getUtility().getStatementManipulator().setFetchSize(stmt, fetchsize);
            } else {
                final int hashAlias = System.identityHashCode(this);
                final int selectIdx = oQueries.length - 1;
                // replace last element in statement(s) with wrapped SQL
                oQueries[selectIdx] = "SELECT * FROM (" + oQueries[selectIdx] + ") table_" + hashAlias;
                try {
                    // bugfix 2925: may fail, e.g. on sqlite
                    stmt.setMaxRows(cachedNoRows);
                } catch (SQLException ex) {
                    Throwable cause = ExceptionUtils.getRootCause(ex);
                    if (cause == null) {
                        cause = ex;
                    }
                    LOGGER.warn("Can't set max rows on statement, reason: " + cause.getMessage(), ex);
                }
            }
            // 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 lastQuery = oQueries[oQueries.length - 1];
            LOGGER.debug("Executing SQL statement as executeQuery: " + lastQuery);
            final ResultSet result = stmt.executeQuery(lastQuery);
            LOGGER.debug("Reading meta data from database ResultSet...");
            m_spec = createTableSpec(result.getMetaData());
            LOGGER.debug("Parsing database ResultSet...");
            // final DBRowIterator dbIt = createRowIterator(useDbRowId, result);
            final RowIterator it = createDBRowIterator(m_spec, dbConn, m_blobFactory, useDbRowId, result);
            DataContainer buf = new DataContainer(m_spec);
            while (it.hasNext()) {
                buf.addRowToTable(it.next());
            }
            buf.close();
            return buf.getTable();
        } finally {
            if (stmt != null) {
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
                DatabaseConnectionSettings.setAutoCommit(conn, autoCommit);
                stmt.close();
            }
        }
    });
}
Also used : DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataContainer(org.knime.core.data.container.DataContainer) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) BinaryObjectCellFactory(org.knime.core.data.blob.BinaryObjectCellFactory) RowIterator(org.knime.core.data.RowIterator) ResultSet(java.sql.ResultSet)

Example 38 with DataContainer

use of org.knime.core.data.container.DataContainer in project knime-core by knime.

the class DataTableSpecView method createTableSpecTable.

private DataTable createTableSpecTable(final DataTableSpec spec) {
    if (spec != null) {
        DataTableSpecExtractor e = new DataTableSpecExtractor();
        e.setExtractColumnNameAsColumn(false);
        e.setPossibleValueOutputFormat(PossibleValueOutputFormat.Columns);
        e.setPropertyHandlerOutputFormat(PropertyHandlerOutputFormat.ToString);
        return e.extract(spec);
    } else {
        String[] names = new String[] { "No outgoing table spec" };
        DataType[] types = new DataType[] { StringCell.TYPE };
        DataContainer result = new DataContainer(new DataTableSpec(names, types));
        result.close();
        return result.getTable();
    }
}
Also used : DataTableSpecExtractor(org.knime.core.data.util.DataTableSpecExtractor) DataContainer(org.knime.core.data.container.DataContainer) DataTableSpec(org.knime.core.data.DataTableSpec) DataType(org.knime.core.data.DataType)

Aggregations

DataContainer (org.knime.core.data.container.DataContainer)38 DataTableSpec (org.knime.core.data.DataTableSpec)25 DefaultRow (org.knime.core.data.def.DefaultRow)23 DataRow (org.knime.core.data.DataRow)19 DataCell (org.knime.core.data.DataCell)17 BufferedDataTable (org.knime.core.node.BufferedDataTable)17 RowKey (org.knime.core.data.RowKey)12 ArrayList (java.util.ArrayList)9 DoubleCell (org.knime.core.data.def.DoubleCell)9 IntCell (org.knime.core.data.def.IntCell)8 LinkedHashMap (java.util.LinkedHashMap)7 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)7 PortObject (org.knime.core.node.port.PortObject)7 HashSet (java.util.HashSet)6 DataColumnSpec (org.knime.core.data.DataColumnSpec)6 RowIterator (org.knime.core.data.RowIterator)6 StringCell (org.knime.core.data.def.StringCell)6 Map (java.util.Map)5 Set (java.util.Set)5 SortedTable (org.knime.base.data.sort.SortedTable)5