All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mkolisnyk.aerial.util.HsqlDBWrapper Maven / Gradle / Ivy

There is a newer version: 0.0.6.1
Show newest version
/**
 * .
 */
package com.github.mkolisnyk.aerial.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hsqldb.Server;

/**
 * @author Myk Kolisnyk
 *
 */
public class HsqlDBWrapper {

    private static final Logger LOG = LoggerFactory.create(HsqlDBWrapper.class);

    private String dbName;
    private Server hsqlServer = null;
    private Connection connection;
    /**
     * .
     */
    public HsqlDBWrapper(String dbNameValue) {
        LOG.info("Initializing DB: " + dbNameValue);
        this.dbName = dbNameValue;
    }

    public void startServer() throws Exception {
        LOG.info("Starting local DB server");
        connection = null;
        hsqlServer = new Server();
        hsqlServer.setLogWriter(null);
        hsqlServer.setSilent(true);
        hsqlServer.setDatabaseName(0, this.dbName);
        hsqlServer.setDatabasePath(0, "file:" + this.dbName);
        hsqlServer.start();
        LOG.info("Started");
    }

    public void openConnection() throws Exception {
        LOG.info("Creating new DB connection");
        Class.forName("org.hsqldb.jdbcDriver");
        connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/" + this.dbName, "sa", "");
        LOG.info("Created");
    }

    public void closeConnection() throws Exception {
        LOG.info("Closing DB connection");
        connection.close();
        connection = null;
        LOG.info("Closed");
    }

    public void stopServer() throws Exception {
        LOG.info("Stopping local DB server");
        hsqlServer.stop();
        hsqlServer = null;
        LOG.info("Stopped");
    }

    public void execute(String query) throws SQLException {
        LOG.info("Running query: " + query);
        PreparedStatement statement = connection.prepareStatement(query);
        try {
            statement.execute();
        } catch (SQLException e) {
            LOG.error(e.getMessage(), e);
        }
        statement.close();
    }

    public List getColumnNames(ResultSet result) throws SQLException {
        List names = new ArrayList();
        int count = result.getMetaData().getColumnCount();
        for (int i = 1; i <= count; i++) {
            names.add(result.getMetaData().getColumnLabel(i));
        }
        return names;
    }

    public Map> executeQuery(String query) throws SQLException {
        Map> output = new HashMap>();
        LOG.info("Running query: " + query);
        PreparedStatement statement = connection.prepareStatement(query);
        ResultSet result = null;
        try {
            result = statement.executeQuery();
            List names = this.getColumnNames(result);
            result.next();
            while (!result.isAfterLast()) {
                for (String name: names) {
                    List column = new ArrayList();
                    if (output.containsKey(name)) {
                        column = output.get(name);
                    }
                    column.add(result.getString(name));
                    output.put(name, column);
                }
                result.next();
            }
        } catch (SQLException e) {
            LOG.error(e.getMessage(), e);
            e.printStackTrace();
        }
        statement.close();
        return output;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy