Search in sources :

Example 1 with PgResultSetMetaData

use of org.postgresql.jdbc.PgResultSetMetaData in project OpenRefine by OpenRefine.

the class PgSQLDatabaseService method getColumns.

@Override
public ArrayList<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, true);
        Statement statement = connection.createStatement();
        ResultSet queryResult = statement.executeQuery(query);
        PgResultSetMetaData metadata = (PgResultSetMetaData) 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);
        }
        return columns;
    } catch (SQLException e) {
        logger.error("SQLException::", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    }
}
Also used : DatabaseColumn(com.google.refine.extension.database.model.DatabaseColumn) 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)

Example 2 with PgResultSetMetaData

use of org.postgresql.jdbc.PgResultSetMetaData in project OpenRefine by OpenRefine.

the class PgSQLDatabaseService method executeQuery.

@Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false);
        Statement statement = connection.createStatement();
        ResultSet queryResult = statement.executeQuery(query);
        PgResultSetMetaData metadata = (PgResultSetMetaData) 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 {
        PgSQLConnectionManager.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) PgResultSetMetaData(org.postgresql.jdbc.PgResultSetMetaData) 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 3 with PgResultSetMetaData

use of org.postgresql.jdbc.PgResultSetMetaData 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

DatabaseServiceException (com.google.refine.extension.database.DatabaseServiceException)3 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 PgResultSetMetaData (org.postgresql.jdbc.PgResultSetMetaData)3 DatabaseColumn (com.google.refine.extension.database.model.DatabaseColumn)2 DatabaseRow (com.google.refine.extension.database.model.DatabaseRow)2 DatabaseInfo (com.google.refine.extension.database.model.DatabaseInfo)1