Subversion Repositories DevTools

Rev

Rev 7033 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.erggroup.buildtool.ripple;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** 
 * Class to simplify error handling when accessing ReseultSet data
 * Mostly simple wrappers with error detection or default data
 */
class RmResultSet 
{
    ResultSet rset;
    String eBase;
    String eSuffix;
    String eMessage;

    private static final Logger mLogger = LoggerFactory.getLogger(RmResultSet.class);
    
    /**
     * Create a new instance of the class
     * @param rs        - ResultSet to handle
     * @param eName     - Base of all error messages
     */
    RmResultSet(ResultSet rs, String eName)
    {
        rset = rs;
        eBase = eName;
        eSuffix = "";
    }
    
    /**
     * ResultSet MUST have an value of the specified name
     * Will throw an exception if one does not exist
     * Will use name and data to improve the error message
     * 
     * @param name
     * @return
     * @throws Exception
     */
    int mustGetKeyInt(String name) throws Exception
    {
        int rv = mustGetInt(name);
        eSuffix = " [" + name + "=" + rv + "] ";
        return rv;
    }
    
    /**
     * ResultSet MUST have an value of the specified name
     * Will through an exception if one does not exist
     * 
     * @param name
     * @return
     * @throws Exception
     */
    int mustGetInt(String name) throws Exception
    {
        int rv = rset.getInt(name);
        if ( rset.wasNull() )
        {
            eMessage = eBase + eSuffix + " is null: " + name;
            mLogger.error(eMessage);
            throw new Exception(eMessage);
        }
        return rv;
    }
    
    /**
     * ResultSet may have the named item. A default will be used if not available
     * 
     * @param name
     * @param def
     * @return
     * @throws Exception
     */
    int getInt(String name, int def) throws Exception
    {
        int rv = rset.getInt(name);
        if ( rset.wasNull() )
        {
            rv = def;
        }
        return rv;
    }
    
    /**
     * ResultSet MUST have an value of the specified name
     * Will through an exception if one does not exist
     * 
     * @param name
     * @return
     * @throws Exception
     */
    String mustGetString(String name) throws Exception
    {
        String rv = rset.getString(name);
        if ( rset.wasNull() )
        {
            eMessage = eBase + eSuffix + " is null: " + name;
            mLogger.error(eMessage);
            throw new Exception(eMessage);
        }
        return rv;
    }
    
    /**
     * ResultSet MUST have an value of the specified name
     * Will through an exception if one does not exist
     * Will use name and data to improve the error message
     * 
     * @param name
     * @return
     * @throws Exception
     */
    String mustGetKeyString(String name) throws Exception
    {
        String rv = mustGetString(name);
        eSuffix = " [" + name + "=" + rv + "] ";
        return rv;
    }
    
    /**
     * ResultSet may have the named item. A default will be used if not available
     * 
     * @param name
     * @param def
     * @return
     * @throws Exception
     */
    String getString(String name, String def) throws SQLException
    {
        String rv = rset.getString(name);
        if ( rset.wasNull() || rv.length() == 0 )
        {
            rv = def;
        }
        return rv;
    }
    
    /**
     * ResultSet may have the indexed  item. A default will be used if not available
     * 
     * @param column - Column idex to use
     * @param def
     * @return
     * @throws Exception
     */
    public String getString(int column, String def) throws SQLException {
        String rv = rset.getString(column);
        if ( rset.wasNull() || rv.length() == 0 )
        {
            rv = def;
        }
        return rv;
    }

    /**
     * Next item in the ResultSet
     * @return
     * @throws SQLException
     */
    public boolean next() throws SQLException {
        return rset.next();
    }

    /**
     * Close the result set. Must be called as the ResultSet will not close itelf
     * @throws SQLException
     */
    public void close() throws SQLException {
        rset.close();
    }

}