Search in sources :

Example 11 with DatabaseRow

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

the class PgSQLDatabaseService method getRows.

@Override
public List<DatabaseRow> getRows(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false);
        Statement statement = connection.createStatement();
        statement.setFetchSize(10);
        ResultSet queryResult = statement.executeQuery(query);
        PgResultSetMetaData metadata = (PgResultSetMetaData) queryResult.getMetaData();
        int columnCount = metadata.getColumnCount();
        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++;
        }
        return rows;
    } catch (SQLException e) {
        logger.error("SQLException::{}::{}", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    }
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PgResultSetMetaData(org.postgresql.jdbc.PgResultSetMetaData) DatabaseRow(com.google.refine.extension.database.model.DatabaseRow)

Aggregations

DatabaseRow (com.google.refine.extension.database.model.DatabaseRow)11 ArrayList (java.util.ArrayList)10 DatabaseServiceException (com.google.refine.extension.database.DatabaseServiceException)6 DatabaseColumn (com.google.refine.extension.database.model.DatabaseColumn)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 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