Search in sources :

Example 6 with DatabaseInfo

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

the class SQLiteDatabaseService method executeQuery.

@Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    try {
        Connection connection = SQLiteConnectionManager.getInstance().getConnection(dbConfig);
        Statement statement = connection.createStatement();
        ResultSet queryResult = statement.executeQuery(query);
        ResultSetMetaData metadata = queryResult.getMetaData();
        int columnCount = metadata.getColumnCount();
        ArrayList<DatabaseColumn> columns = new ArrayList<>(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<>();
        while (queryResult.next()) {
            DatabaseRow row = new DatabaseRow();
            row.setIndex(index);
            List<String> values = new ArrayList<>(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 {
        SQLiteConnectionManager.getInstance().shutdown();
    }
}
Also used : DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) ArrayList(java.util.ArrayList) DatabaseRow(com.google.refine.extension.database.model.DatabaseRow) DatabaseColumn(com.google.refine.extension.database.model.DatabaseColumn)

Example 7 with DatabaseInfo

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

the class TestQueryCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!hasValidCSRFToken(request)) {
        respondCSRFError(response);
        return;
    }
    DatabaseConfiguration dbConfig = getJdbcConfiguration(request);
    String query = request.getParameter("query");
    if (logger.isDebugEnabled()) {
        logger.debug("TestQueryCommand::Post::DatabaseConfiguration::{}::Query::{} ", dbConfig, query);
    }
    // ProjectManager.singleton.setBusy(true);
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        Writer w = response.getWriter();
        JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
        try {
            DatabaseInfo databaseInfo = DatabaseService.get(dbConfig.getDatabaseType()).testQuery(dbConfig, query);
            ObjectMapper mapperObj = new ObjectMapper();
            response.setStatus(HttpStatus.SC_OK);
            String jsonStr = mapperObj.writeValueAsString(databaseInfo);
            if (logger.isDebugEnabled()) {
                logger.debug("TestQueryCommand::Post::Result::{} ", jsonStr);
            }
            writer.writeStartObject();
            writer.writeStringField("code", "ok");
            writer.writeStringField("QueryResult", jsonStr);
            writer.writeEndObject();
        } catch (DatabaseServiceException e) {
            logger.error("TestQueryCommand::Post::DatabaseServiceException::{}", e);
            sendError(HttpStatus.SC_BAD_REQUEST, response, e);
        } catch (Exception e) {
            logger.error("TestQueryCommand::Post::Exception::{}", e);
            sendError(HttpStatus.SC_BAD_REQUEST, response, e);
        } finally {
            writer.flush();
            writer.close();
            w.close();
        }
    } catch (Exception e) {
        logger.error("TestQueryCommand::Post::Exception::{}", e);
        throw new ServletException(e);
    }
// finally {
// // ProjectManager.singleton.setBusy(false);
// }
}
Also used : ServletException(javax.servlet.ServletException) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) DatabaseConfiguration(com.google.refine.extension.database.DatabaseConfiguration) DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServletException(javax.servlet.ServletException) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) IOException(java.io.IOException)

Example 8 with DatabaseInfo

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

the class MariaDBDatabaseService method getMetadata.

/**
 * @param connectionInfo
 * @return
 * @throws DatabaseServiceException
 */
private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException {
    try {
        Connection connection = MariaDBConnectionManager.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 9 with DatabaseInfo

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

the class MariaDBDatabaseService method testQuery.

@Override
public DatabaseInfo testQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
    Statement statement = null;
    ResultSet queryResult = null;
    try {
        Connection connection = MariaDBConnectionManager.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();
        }
        MariaDBConnectionManager.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 10 with DatabaseInfo

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

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