view geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorBase.java @ 271:8aad9d098b08

Integrated Patch of issue57 to get some Memoryusage-improvements geo-backend/trunk@248 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 20 Oct 2009 09:57:52 +0000
parents 56655046194f
children 3cbf11c67fdc
line wrap: on
line source
/**
 *
 */
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 Tim Englich <tim.englich@intevation.de>
 *
 */
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;
    }

}

http://dive4elements.wald.intevation.org