Search in sources :

Example 11 with DatabaseColumn

use of com.google.refine.extension.database.model.DatabaseColumn in project OpenRefine by OpenRefine.

the class MariaDBDatabaseService method executeQuery.

@Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, false);
        Statement statement = connection.createStatement();
        ResultSet queryResult = statement.executeQuery(query);
        MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData) queryResult.getMetaData();
        int columnCount = metadata.getColumnCount();
        ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount);
        for (int i = 1; i <= columnCount; i++) {
            DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i), DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i));
            columns.add(dc);
        }
        int index = 0;
        List<DatabaseRow> rows = new ArrayList<DatabaseRow>();
        while (queryResult.next()) {
            DatabaseRow row = new DatabaseRow();
            row.setIndex(index);
            List<String> values = new ArrayList<String>(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                values.add(queryResult.getString(i));
            }
            row.setValues(values);
            rows.add(row);
            index++;
        }
        DatabaseInfo dbInfo = new DatabaseInfo();
        dbInfo.setColumns(columns);
        dbInfo.setRows(rows);
        return dbInfo;
    } catch (SQLException e) {
        logger.error("SQLException::", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    } finally {
        MariaDBConnectionManager.getInstance().shutdown();
    }
}
Also used : DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DatabaseRow(com.google.refine.extension.database.model.DatabaseRow) DatabaseColumn(com.google.refine.extension.database.model.DatabaseColumn) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) ResultSet(java.sql.ResultSet) MariaDbResultSetMetaData(org.mariadb.jdbc.MariaDbResultSetMetaData)

Example 12 with DatabaseColumn

use of com.google.refine.extension.database.model.DatabaseColumn in project OpenRefine by OpenRefine.

the class MySQLDatabaseService method executeQuery.

@Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, false);
        Statement statement = connection.createStatement();
        ResultSet queryResult = statement.executeQuery(query);
        java.sql.ResultSetMetaData metadata = queryResult.getMetaData();
        int columnCount = metadata.getColumnCount();
        ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount);
        for (int i = 1; i <= columnCount; i++) {
            DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i), DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i));
            columns.add(dc);
        }
        int index = 0;
        List<DatabaseRow> rows = new ArrayList<DatabaseRow>();
        while (queryResult.next()) {
            DatabaseRow row = new DatabaseRow();
            row.setIndex(index);
            List<String> values = new ArrayList<String>(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                values.add(queryResult.getString(i));
            }
            row.setValues(values);
            rows.add(row);
            index++;
        }
        DatabaseInfo dbInfo = new DatabaseInfo();
        dbInfo.setColumns(columns);
        dbInfo.setRows(rows);
        return dbInfo;
    } catch (SQLException e) {
        logger.error("SQLException::", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    } finally {
        MySQLConnectionManager.getInstance().shutdown();
    }
}
Also used : DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DatabaseRow(com.google.refine.extension.database.model.DatabaseRow) DatabaseColumn(com.google.refine.extension.database.model.DatabaseColumn) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) ResultSet(java.sql.ResultSet)

Example 13 with DatabaseColumn

use of com.google.refine.extension.database.model.DatabaseColumn in project OpenRefine by OpenRefine.

the class DBQueryResultImportReader method getNextRowOfCells.

@Override
public List<Object> getNextRowOfCells() throws IOException {
    try {
        if (!usedHeaders) {
            List<Object> row = new ArrayList<Object>(dbColumns.size());
            for (DatabaseColumn cd : dbColumns) {
                row.add(cd.getName());
            }
            usedHeaders = true;
            // logger.info("Exit::getNextRowOfCells return header::row:" +  row);
            return row;
        }
        if (rowsOfCells == null || (nextRow >= batchRowStart + rowsOfCells.size() && !end)) {
            int newBatchRowStart = batchRowStart + (rowsOfCells == null ? 0 : rowsOfCells.size());
            rowsOfCells = getRowsOfCells(newBatchRowStart);
            processedRows = processedRows + rowsOfCells.size();
            batchRowStart = newBatchRowStart;
            setProgress(job, querySource, -1);
        }
        if (rowsOfCells != null && nextRow - batchRowStart < rowsOfCells.size()) {
            List<Object> result = rowsOfCells.get(nextRow++ - batchRowStart);
            if (nextRow >= batchSize) {
                rowsOfCells = getRowsOfCells(processedRows);
                processedRows = processedRows + rowsOfCells.size();
                if (logger.isDebugEnabled()) {
                    logger.debug("[[ Returning last row in batch:nextRow::{}, processedRows:{} ]]", nextRow, processedRows);
                }
                nextRow = 0;
                if (processedRows % 100 == 0) {
                    setProgress(job, querySource, progress++);
                }
                if (processedRows % 10000 == 0) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("[[ {} rows processed... ]]", processedRows);
                    }
                }
            }
            return result;
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("[[processedRows:{} ]]", processedRows);
            }
            return null;
        }
    } catch (DatabaseServiceException e) {
        logger.error("DatabaseServiceException::{}", e);
        throw new IOException(e);
    }
}
Also used : DatabaseColumn(com.google.refine.extension.database.model.DatabaseColumn) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

DatabaseColumn (com.google.refine.extension.database.model.DatabaseColumn)13 ArrayList (java.util.ArrayList)12 DatabaseServiceException (com.google.refine.extension.database.DatabaseServiceException)6 DatabaseRow (com.google.refine.extension.database.model.DatabaseRow)6 Connection (java.sql.Connection)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 Statement (java.sql.Statement)6 DatabaseInfo (com.google.refine.extension.database.model.DatabaseInfo)4 IOException (java.io.IOException)2 List (java.util.List)2 MariaDbResultSetMetaData (org.mariadb.jdbc.MariaDbResultSetMetaData)2 PgResultSetMetaData (org.postgresql.jdbc.PgResultSetMetaData)2 MariaDBDatabaseService (com.google.refine.extension.database.mariadb.MariaDBDatabaseService)1 MySQLDatabaseService (com.google.refine.extension.database.mysql.MySQLDatabaseService)1 PgSQLDatabaseService (com.google.refine.extension.database.pgsql.PgSQLDatabaseService)1 SQLiteDatabaseService (com.google.refine.extension.database.sqlite.SQLiteDatabaseService)1 BeforeTest (org.testng.annotations.BeforeTest)1 Test (org.testng.annotations.Test)1