Search in sources :

Example 1 with DatabaseServiceException

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

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

the class MySQLConnectionManager method testConnection.

/**
 * testConnection
 * @param databaseConfiguration
 * @return
 */
public boolean testConnection(DatabaseConfiguration databaseConfiguration) throws DatabaseServiceException {
    try {
        boolean connResult = false;
        Connection conn = getConnection(databaseConfiguration, true);
        if (conn != null) {
            connResult = true;
            conn.close();
        }
        return connResult;
    } catch (SQLException e) {
        logger.error("Test connection Failed!", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    }
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 3 with DatabaseServiceException

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

the class MySQLConnectionManager method getConnection.

/**
 * Get a connection form the connection pool.
 *
 * @return connection from the pool
 */
public Connection getConnection(DatabaseConfiguration databaseConfiguration, boolean forceNewConnection) throws DatabaseServiceException {
    try {
        if (connection != null && !forceNewConnection) {
            // logger.info("connection closed::{}", connection.isClosed());
            if (!connection.isClosed()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Returning existing connection::{}", connection);
                }
                return connection;
            }
        }
        String dbURL = getDatabaseUrl(databaseConfiguration);
        Class.forName(type.getClassPath());
        // logger.info("*** type.getClassPath() ::{}, {}**** ", type.getClassPath());
        DriverManager.setLoginTimeout(10);
        connection = DriverManager.getConnection(dbURL, databaseConfiguration.getDatabaseUser(), databaseConfiguration.getDatabasePassword());
        if (logger.isDebugEnabled()) {
            logger.debug("*** Acquired New  connection for ::{} **** ", dbURL);
        }
        return connection;
    } catch (ClassNotFoundException e) {
        logger.error("Jdbc Driver not found", e);
        throw new DatabaseServiceException(e.getMessage());
    } catch (SQLException e) {
        logger.error("SQLException::Couldn't get a Connection!", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    }
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) SQLException(java.sql.SQLException)

Example 4 with DatabaseServiceException

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

the class PgSQLConnectionManager method getConnection.

/**
 * Get a connection form the connection pool.
 *
 * @return connection from the pool
 */
public Connection getConnection(DatabaseConfiguration databaseConfiguration, boolean forceNewConnection) throws DatabaseServiceException {
    try {
        if (connection != null && !forceNewConnection) {
            // logger.info("connection closed::{}", connection.isClosed());
            if (!connection.isClosed()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Returning existing connection::{}", connection);
                }
                return connection;
            }
        }
        Class.forName(type.getClassPath());
        DriverManager.setLoginTimeout(10);
        String dbURL = getDatabaseUrl(databaseConfiguration);
        connection = DriverManager.getConnection(dbURL, databaseConfiguration.getDatabaseUser(), databaseConfiguration.getDatabasePassword());
        logger.debug("*** Acquired New  connection for ::{} **** ", dbURL);
        return connection;
    } catch (ClassNotFoundException e) {
        logger.error("Jdbc Driver not found", e);
        throw new DatabaseServiceException(e.getMessage());
    } catch (SQLException e) {
        logger.error("SQLException::Couldn't get a Connection!", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    }
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) SQLException(java.sql.SQLException)

Example 5 with DatabaseServiceException

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

the class PgSQLConnectionManager method testConnection.

/**
 * testConnection
 * @param databaseConfiguration
 * @return
 */
public boolean testConnection(DatabaseConfiguration databaseConfiguration) throws DatabaseServiceException {
    try {
        boolean connResult = false;
        Connection conn = getConnection(databaseConfiguration, true);
        if (conn != null) {
            connResult = true;
            conn.close();
        }
        return connResult;
    } catch (SQLException e) {
        logger.error("Test connection Failed!", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    }
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) SQLException(java.sql.SQLException) 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