view geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorBase.java @ 1127:ebeb56428409

Added license headers and license file. geo-backend/trunk@1261 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:52:22 +0000
parents eb777022b628
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.geobackend.base.query;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import de.intevation.gnv.geobackend.base.DefaultResult;
import de.intevation.gnv.geobackend.base.DefaultResultDescriptor;
import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.geobackend.base.ResultDescriptor;

/**
 * This is an abstract Basicimplementation of the Interface
 * QueryExecutor providing several Helpermethods.
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
public abstract class QueryExecutorBase implements QueryExecutor {

    /**
     * Constructor
     */
    public QueryExecutorBase() {
        super();
    }

    /**
     * This Method puts the Filtervalues into the Querystring
     * @param queryString the Query which should be manipulated
     * @param filter the values which should be put into the Query
     * @return the manipulated Query
     */
    protected String setFilterValues(String queryString, String[] filter){
        String returnValue = queryString;
        for (int i = 0; i < filter.length; i++){
            returnValue = returnValue.replaceFirst("[?]", filter[i]);
        }
        return returnValue;
    }

    /**
     * This Methods converts the ResultSet into an Collection of Result-objects
     * @param resultSet the ResultSet which should be converted
     * @return an Collection containing Result-Objects
     * @throws SQLException
     */
    protected Collection<Result> createResultCollection(ResultSet resultSet) throws SQLException{
        Collection<Result> returnValue =  new ArrayList<Result>();

        ResultDescriptor resultDescriptor = null;
        int columns = -1;

        List<String> columnNames = null;

        while (resultSet.next()){
            if (resultDescriptor == null){
                resultDescriptor = new DefaultResultDescriptor();
                ResultSetMetaData rsmd = resultSet.getMetaData();
                columns = rsmd.getColumnCount();

                for (int i = 1; i <= columns; i++){
                    resultDescriptor.addColumn(rsmd.getColumnName(i), rsmd.getColumnClassName(i));
                }
            }

            Result result = convertResult(resultSet, resultDescriptor);

            returnValue.add(result);
        }
        return returnValue;
    }

    /**
     * This Method converts one Singel ResultSetEntry into an Result-Object
     * @param resultSet the ResultSet where the Entry should be took from.
     * @param resultDescriptor the ResultsetDescriptor which describes the Entry
     * @return an new Result-Objects containing the Values of the ResultSet-Entry
     * @throws SQLException
     */
    private Result convertResult(ResultSet resultSet, ResultDescriptor resultDescriptor)
    throws SQLException {

        Result result = new DefaultResult(resultDescriptor);

        for (int i = 0, N = resultDescriptor.getColumnCount(); i < N; i++) {
            result.addColumnValue(i, resultSet.getObject(i+1));
        }
        return result;
    }

    public Collection<Result> cachedResults(String query) {
        return null;
    }

    public void cacheResults(String query, Collection<Result> result) {
    }

    public void clearCache(String[] tableNames) {
    }

}

http://dive4elements.wald.intevation.org