Search in sources :

Example 21 with DatabaseInfo

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

the class MariaDBDatabaseService method executeQuery.

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

Example 22 with DatabaseInfo

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

the class MySQLDatabaseService method executeQuery.

@Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, false);
        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);
        }
        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 {
        MySQLConnectionManager.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) 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 23 with DatabaseInfo

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

the class MySQLDatabaseService method getMetadata.

/**
 * @param connectionInfo
 * @return
 * @throws DatabaseServiceException
 */
private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException {
    try {
        Connection connection = MySQLConnectionManager.getInstance().getConnection(connectionInfo, true);
        if (connection != null) {
            java.sql.DatabaseMetaData metadata;
            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 24 with DatabaseInfo

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

the class SQLiteDatabaseService method getMetadata.

/**
 * @param dbConfig
 * @return
 * @throws DatabaseServiceException
 */
private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException {
    try {
        Connection connection = SQLiteConnectionManager.getInstance().getConnection(connectionInfo);
        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 : DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) java.sql(java.sql)

Aggregations

DatabaseInfo (com.google.refine.extension.database.model.DatabaseInfo)24 DatabaseServiceException (com.google.refine.extension.database.DatabaseServiceException)12 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9 BeforeTest (org.testng.annotations.BeforeTest)8 Test (org.testng.annotations.Test)8 ResultSet (java.sql.ResultSet)6 Statement (java.sql.Statement)6 DatabaseColumn (com.google.refine.extension.database.model.DatabaseColumn)4 DatabaseRow (com.google.refine.extension.database.model.DatabaseRow)4 ArrayList (java.util.ArrayList)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 DatabaseConfiguration (com.google.refine.extension.database.DatabaseConfiguration)3 IOException (java.io.IOException)3 Writer (java.io.Writer)3 ServletException (javax.servlet.ServletException)3 MariaDBDatabaseService (com.google.refine.extension.database.mariadb.MariaDBDatabaseService)2 MySQLDatabaseService (com.google.refine.extension.database.mysql.MySQLDatabaseService)2 PgSQLDatabaseService (com.google.refine.extension.database.pgsql.PgSQLDatabaseService)2