Search in sources :

Example 1 with DbmsConnection

use of org.verdictdb.connection.DbmsConnection in project traindb by traindb-project.

the class TrainDBQueryEngine method loadSynopsisIntoTable.

private void loadSynopsisIntoTable(DbmsConnection dbmsConn, String synopsisName, MModelInstance mModelInstance, String synopsisFile) throws Exception {
    Connection origConn = null;
    if (dbmsConn instanceof CachedDbmsConnection) {
        loadSynopsisIntoTable(((CachedDbmsConnection) dbmsConn).getOriginalConnection(), synopsisName, mModelInstance, synopsisFile);
        return;
    } else if (dbmsConn instanceof JdbcConnection) {
        origConn = ((JdbcConnection) dbmsConn).getConnection();
    } else {
        throw new TrainDBException("cannot load synopsis data into table");
    }
    StringBuilder sb = new StringBuilder();
    sb.append("INSERT INTO ");
    sb.append(mModelInstance.getSchemaName());
    sb.append(".");
    sb.append(synopsisName);
    sb.append(" VALUES (");
    for (String columnName : mModelInstance.getColumnNames()) {
        sb.append("?,");
    }
    sb.deleteCharAt(sb.lastIndexOf(","));
    sb.append(")");
    CSVReader csvReader = new CSVReaderBuilder(new FileReader(synopsisFile)).withSkipLines(1).build();
    String sql = sb.toString();
    PreparedStatement pstmt = origConn.prepareStatement(sql);
    int collen = mModelInstance.getColumnNames().size();
    String[] row;
    try {
        while ((row = csvReader.readNext()) != null) {
            for (int i = 1; i <= collen; i++) {
                pstmt.setObject(i, row[i - 1]);
            }
            pstmt.addBatch();
        }
        pstmt.executeBatch();
    } catch (Exception e) {
        throw e;
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
    }
}
Also used : CachedDbmsConnection(org.verdictdb.connection.CachedDbmsConnection) CSVReader(com.opencsv.CSVReader) Connection(java.sql.Connection) CachedDbmsConnection(org.verdictdb.connection.CachedDbmsConnection) JdbcConnection(org.verdictdb.connection.JdbcConnection) DbmsConnection(org.verdictdb.connection.DbmsConnection) CSVReaderBuilder(com.opencsv.CSVReaderBuilder) JdbcConnection(org.verdictdb.connection.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) TrainDBException(traindb.common.TrainDBException) SQLException(java.sql.SQLException) CatalogException(traindb.catalog.CatalogException) TrainDBException(traindb.common.TrainDBException) FileReader(java.io.FileReader)

Example 2 with DbmsConnection

use of org.verdictdb.connection.DbmsConnection in project traindb by traindb-project.

the class TrainDBConnection method getDatabaseMetaData.

private java.sql.DatabaseMetaData getDatabaseMetaData() throws SQLException {
    DbmsConnection conn = vc.getConnection();
    java.sql.DatabaseMetaData metaData = getDatabaseMetaDataFromConnection(conn);
    if (metaData == null) {
        throw new SQLException("Unexpected underlying connection: " + conn);
    } else {
        return metaData;
    }
}
Also used : SQLException(java.sql.SQLException) DbmsConnection(org.verdictdb.connection.DbmsConnection) CachedDbmsConnection(org.verdictdb.connection.CachedDbmsConnection) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

SQLException (java.sql.SQLException)2 CachedDbmsConnection (org.verdictdb.connection.CachedDbmsConnection)2 DbmsConnection (org.verdictdb.connection.DbmsConnection)2 CSVReader (com.opencsv.CSVReader)1 CSVReaderBuilder (com.opencsv.CSVReaderBuilder)1 FileReader (java.io.FileReader)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 JdbcConnection (org.verdictdb.connection.JdbcConnection)1 CatalogException (traindb.catalog.CatalogException)1 TrainDBException (traindb.common.TrainDBException)1