Search in sources :

Example 11 with DatabaseServiceException

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

the class MySQLDatabaseService method getRows.

@Override
public List<DatabaseRow> getRows(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, false);
        Statement statement = connection.createStatement();
        statement.setFetchSize(10);
        ResultSet queryResult = statement.executeQuery(query);
        java.sql.ResultSetMetaData metadata = 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 : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DatabaseRow(com.google.refine.extension.database.model.DatabaseRow) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) ResultSet(java.sql.ResultSet)

Example 12 with DatabaseServiceException

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

the class MySQLDatabaseService method testQuery.

@Override
public DatabaseInfo testQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    Statement statement = null;
    ResultSet queryResult = null;
    try {
        Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true);
        statement = connection.createStatement();
        queryResult = statement.executeQuery(query);
        DatabaseInfo dbInfo = new DatabaseInfo();
        return dbInfo;
    } catch (SQLException e) {
        logger.error("SQLException::", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    } finally {
        try {
            if (queryResult != null) {
                queryResult.close();
            }
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        MySQLConnectionManager.getInstance().shutdown();
    }
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection)

Example 13 with DatabaseServiceException

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

the class MySQLDatabaseService method getColumns.

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

Example 14 with DatabaseServiceException

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

the class PgSQLDatabaseService method getMetadata.

/**
 * @param connectionInfo
 * @return
 * @throws DatabaseServiceException
 */
private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException {
    try {
        Connection connection = PgSQLConnectionManager.getInstance().getConnection(connectionInfo, true);
        if (connection != null) {
            java.sql.DatabaseMetaData metadata = connection.getMetaData();
            int dbMajorVersion = metadata.getDatabaseMajorVersion();
            int dbMinorVersion = metadata.getDatabaseMinorVersion();
            String dbProductVersion = metadata.getDatabaseProductVersion();
            String dbProductName = metadata.getDatabaseProductName();
            DatabaseInfo dbInfo = new DatabaseInfo();
            dbInfo.setDatabaseMajorVersion(dbMajorVersion);
            dbInfo.setDatabaseMinorVersion(dbMinorVersion);
            dbInfo.setDatabaseProductVersion(dbProductVersion);
            dbInfo.setDatabaseProductName(dbProductName);
            return dbInfo;
        }
    } catch (SQLException e) {
        logger.error("SQLException::", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    }
    return null;
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 15 with DatabaseServiceException

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

the class PgSQLDatabaseService method testQuery.

@Override
public DatabaseInfo testQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    Statement statement = null;
    ResultSet queryResult = null;
    try {
        Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, true);
        statement = connection.createStatement();
        queryResult = statement.executeQuery(query);
        DatabaseInfo dbInfo = new DatabaseInfo();
        return dbInfo;
    } catch (SQLException e) {
        logger.error("SQLException::", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    } finally {
        try {
            if (queryResult != null) {
                queryResult.close();
            }
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PgSQLConnectionManager.getInstance().shutdown();
    }
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection)

Aggregations

DatabaseServiceException (com.google.refine.extension.database.DatabaseServiceException)27 SQLException (java.sql.SQLException)23 Connection (java.sql.Connection)19 DatabaseInfo (com.google.refine.extension.database.model.DatabaseInfo)12 ResultSet (java.sql.ResultSet)12 Statement (java.sql.Statement)12 ArrayList (java.util.ArrayList)9 DatabaseColumn (com.google.refine.extension.database.model.DatabaseColumn)6 DatabaseRow (com.google.refine.extension.database.model.DatabaseRow)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 DatabaseConfiguration (com.google.refine.extension.database.DatabaseConfiguration)4 IOException (java.io.IOException)4 Writer (java.io.Writer)4 ServletException (javax.servlet.ServletException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 MariaDbResultSetMetaData (org.mariadb.jdbc.MariaDbResultSetMetaData)3 PgResultSetMetaData (org.postgresql.jdbc.PgResultSetMetaData)3