Search in sources :

Example 6 with DatabaseColumn

use of com.google.refine.extension.database.model.DatabaseColumn 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 7 with DatabaseColumn

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

the class DBQueryResultPreviewReader method getRowsOfCells.

/**
 * @param startRow
 * @return
 * @throws IOException
 * @throws DatabaseServiceException
 */
private List<List<Object>> getRowsOfCells(int startRow) throws IOException, DatabaseServiceException {
    // logger.info("Entry getRowsOfCells::startRow:" + startRow);
    List<List<Object>> rowsOfCells = new ArrayList<List<Object>>(batchSize);
    String query = databaseService.buildLimitQuery(batchSize, startRow, dbQueryInfo.getQuery());
    if (logger.isDebugEnabled()) {
        logger.debug("batchSize::" + batchSize + " startRow::" + startRow + " query::" + query);
    }
    List<DatabaseRow> dbRows = databaseService.getRows(dbQueryInfo.getDbConfig(), query);
    if (dbRows != null && !dbRows.isEmpty() && dbRows.size() > 0) {
        for (DatabaseRow dbRow : dbRows) {
            List<String> row = dbRow.getValues();
            List<Object> rowOfCells = new ArrayList<Object>(row.size());
            for (int j = 0; j < row.size() && j < dbColumns.size(); j++) {
                String text = row.get(j);
                if (text == null || text.isEmpty()) {
                    rowOfCells.add(null);
                } else {
                    DatabaseColumn col = dbColumns.get(j);
                    if (col.getType() == DatabaseColumnType.NUMBER) {
                        try {
                            rowOfCells.add(Long.parseLong(text));
                            continue;
                        } catch (NumberFormatException e) {
                        }
                    } else if (col.getType() == DatabaseColumnType.DOUBLE || col.getType() == DatabaseColumnType.FLOAT) {
                        try {
                            double d = Double.parseDouble(text);
                            if (!Double.isInfinite(d) && !Double.isNaN(d)) {
                                rowOfCells.add(d);
                                continue;
                            }
                        } catch (NumberFormatException e) {
                        }
                    }
                    rowOfCells.add(text);
                }
            }
            rowsOfCells.add(rowOfCells);
        }
    }
    end = dbRows.size() < batchSize + 1;
    // logger.info("Exit::getRowsOfCells::rowsOfCells:{}", rowsOfCells);
    return rowsOfCells;
}
Also used : DatabaseColumn(com.google.refine.extension.database.model.DatabaseColumn) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) DatabaseRow(com.google.refine.extension.database.model.DatabaseRow)

Example 8 with DatabaseColumn

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

the class DBQueryResultPreviewReader 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.debug("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);
            batchRowStart = newBatchRowStart;
            setProgress(job, querySource, -1);
        // logger.info("getNextRowOfCells:: rowsOfCellsIsNull::rowsOfCells size:" + rowsOfCells.size() + ":batchRowStart:" + batchRowStart + " ::nextRow:" + nextRow);
        }
        if (rowsOfCells != null && nextRow - batchRowStart < rowsOfCells.size()) {
            // logger.info("Exit::getNextRowOfCells :rowsOfCellsNotNull::rowsOfCells size:" + rowsOfCells.size() + ":batchRowStart:" + batchRowStart + " ::nextRow:" + nextRow);
            return rowsOfCells.get(nextRow++ - batchRowStart);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("nextRow:{}, batchRowStart:{}", nextRow, batchRowStart);
            }
            return null;
        }
    } catch (DatabaseServiceException e) {
        logger.error("DatabaseServiceException::preview:{}", e.getMessage());
        IOException ioEx = new IOException(e.getMessage(), e);
        throw ioEx;
    }
}
Also used : DatabaseColumn(com.google.refine.extension.database.model.DatabaseColumn) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 9 with DatabaseColumn

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

the class DatabaseServiceTest method testGetColumns.

@Test(groups = { "requiresMySQL" })
public void testGetColumns() throws DatabaseServiceException {
    List<DatabaseColumn> dbColumns;
    DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
    dbColumns = dbService.getColumns(testDbConfig, "SELECT * FROM " + testTable);
    Assert.assertNotNull(dbColumns);
    int cols = dbColumns.size();
    Assert.assertEquals(cols, 10);
}
Also used : DatabaseColumn(com.google.refine.extension.database.model.DatabaseColumn) MariaDBDatabaseService(com.google.refine.extension.database.mariadb.MariaDBDatabaseService) MySQLDatabaseService(com.google.refine.extension.database.mysql.MySQLDatabaseService) SQLiteDatabaseService(com.google.refine.extension.database.sqlite.SQLiteDatabaseService) PgSQLDatabaseService(com.google.refine.extension.database.pgsql.PgSQLDatabaseService) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 10 with DatabaseColumn

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

the class MariaDBDatabaseService method getColumns.

@Override
public ArrayList<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, true);
        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);
        }
        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) MariaDbResultSetMetaData(org.mariadb.jdbc.MariaDbResultSetMetaData)

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