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

com.technophobia.substeps.database.runner.DatabaseExecutionContext Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright Technophobia Ltd & Alan Raison 2013
 *
 *   This file is part of Substeps.
 *
 *    Substeps is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Substeps is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with Substeps.  If not, see .
 */

package com.technophobia.substeps.database.runner;

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 java.util.Set;

import junit.framework.Assert;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.technophobia.substeps.database.impl.Database;

public class DatabaseExecutionContext {

    private static final Logger logger = LoggerFactory.getLogger(DatabaseExecutionContext.class);

    public static final String EXECUTION_CONTEXT_KEY = "_db_context_key";

    public static final String DEFAULT_RESULT_NAMESPACE = "_default_results_namespace";

    private Map>> mapOfMultipleResults = null;


    public void clear() {

        this.mapOfMultipleResults = null;
    }


    private List> getDefaultStash() {

        Assert.assertNotNull("getting default stash when not initialised", this.mapOfMultipleResults);

        final List> results = this.mapOfMultipleResults.get(DEFAULT_RESULT_NAMESPACE);

        Assert.assertNotNull("no default stash", results);

        return results;
    }


    /**
     * @param rs
     */
    public void stashResultSet(final List> rs) {

        if (this.mapOfMultipleResults == null) {
            this.mapOfMultipleResults = new HashMap>>();
        }

        stashResultSetInternal(DEFAULT_RESULT_NAMESPACE, rs);
    }


    /**
     * @param rs
     */
    public void stashResultSet(final String key, final List> rs) {

        if (DEFAULT_RESULT_NAMESPACE.equals(key)) {

            throw new IllegalArgumentException("key cannot be the same as the default: " + DEFAULT_RESULT_NAMESPACE
                    + ", please use a different key name");
        }

        stashResultSetInternal(key, rs);
    }


    private void stashResultSetInternal(final String key, final List> rs) {

        if (this.mapOfMultipleResults == null) {
            this.mapOfMultipleResults = new HashMap>>();
        }

        if (this.mapOfMultipleResults.containsKey(key)) {

            logger.warn("stashing new query results over the top of existing results...");
        }

        this.mapOfMultipleResults.put(key, rs);
    }


    /**
     * 
     */
    public List> getResultSet() {

        return getResultSet(DEFAULT_RESULT_NAMESPACE);
    }


    public List> getResultSet(final String key) {

        List> rtn = null;

        if (this.mapOfMultipleResults != null) {
            rtn = this.mapOfMultipleResults.get(key);
        }

        return rtn;
    }


    public Set getColumns() {

        final List> defaultStash = getDefaultStash();

        Assert.assertFalse("expecting some results in the default stash", defaultStash.isEmpty());
        // return the columns from the first result
        return defaultStash.get(0).keySet();
    }


    public List getResultsForColumn(final String columnName) {

        List rtn = null;

        final List> defaultStash = getDefaultStash();

        for (final Map row : defaultStash) {

            final Object val = row.get(columnName);

            if (rtn == null) {
                rtn = new ArrayList();
            }
            rtn.add((String) val);
        }
        return rtn;
    }


    public void setQueryResult(final ResultSet rs) throws SQLException {

        final List> resultsMap = Database.buildResultsMap(rs);

        stashResultSet(resultsMap);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy