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 */
}
}
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();
}
}
});
}
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();
}
}
Aggregations