Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7023 dpurdie 1
package com.erggroup.buildtool.ripple;
2
 
3
import java.sql.ResultSet;
4
import java.sql.SQLException;
5
 
6
import org.apache.log4j.Logger;
7
/** 
8
 * Class to simplify error handling when accessing ReseultSet data
9
 * Mostly simple wrappers with error detection or default data
10
 */
11
class RmResultSet 
12
{
13
    ResultSet rset;
14
    String eBase;
15
    String eSuffix;
16
    String eMessage;
17
 
18
    private static final Logger mLogger = Logger.getLogger(RmResultSet.class);
19
 
20
    /**
21
     * Create a new instance of the class
22
     * @param rs        - ResultSet to handle
23
     * @param eName     - Base of all error messages
24
     */
25
    RmResultSet(ResultSet rs, String eName)
26
    {
27
        rset = rs;
28
        eBase = eName;
29
        eSuffix = "";
30
    }
31
 
32
    /**
33
     * ResultSet MUST have an value of the specified name
34
     * Will throw an exception if one does not exist
35
     * Will use name and data to improve the error message
36
     * 
37
     * @param name
38
     * @return
39
     * @throws Exception
40
     */
41
    int mustGetKeyInt(String name) throws Exception
42
    {
43
        int rv = mustGetInt(name);
44
        eSuffix = " [" + name + "=" + rv + "] ";
45
        return rv;
46
    }
47
 
48
    /**
49
     * ResultSet MUST have an value of the specified name
50
     * Will through an exception if one does not exist
51
     * 
52
     * @param name
53
     * @return
54
     * @throws Exception
55
     */
56
    int mustGetInt(String name) throws Exception
57
    {
58
        int rv = rset.getInt(name);
59
        if ( rset.wasNull() )
60
        {
61
            eMessage = eBase + eSuffix + " is null: " + name;
62
            mLogger.fatal(eMessage);
63
            throw new Exception(eMessage);
64
        }
65
        return rv;
66
    }
67
 
68
    /**
69
     * ResultSet may have the named item. A default will be used if not available
70
     * 
71
     * @param name
72
     * @param def
73
     * @return
74
     * @throws Exception
75
     */
76
    int getInt(String name, int def) throws Exception
77
    {
78
        int rv = rset.getInt(name);
79
        if ( rset.wasNull() )
80
        {
81
            rv = def;
82
        }
83
        return rv;
84
    }
85
 
86
    /**
87
     * ResultSet MUST have an value of the specified name
88
     * Will through an exception if one does not exist
89
     * 
90
     * @param name
91
     * @return
92
     * @throws Exception
93
     */
94
    String mustGetString(String name) throws Exception
95
    {
96
        String rv = rset.getString(name);
97
        if ( rset.wasNull() )
98
        {
99
            eMessage = eBase + eSuffix + " is null: " + name;
100
            mLogger.fatal(eMessage);
101
            throw new Exception(eMessage);
102
        }
103
        return rv;
104
    }
105
 
106
    /**
107
     * ResultSet MUST have an value of the specified name
108
     * Will through an exception if one does not exist
109
     * Will use name and data to improve the error message
110
     * 
111
     * @param name
112
     * @return
113
     * @throws Exception
114
     */
115
    String mustGetKeyString(String name) throws Exception
116
    {
117
        String rv = mustGetString(name);
118
        eSuffix = " [" + name + "=" + rv + "] ";
119
        return rv;
120
    }
121
 
122
    /**
123
     * ResultSet may have the named item. A default will be used if not available
124
     * 
125
     * @param name
126
     * @param def
127
     * @return
128
     * @throws Exception
129
     */
130
    String getString(String name, String def) throws SQLException
131
    {
132
        String rv = rset.getString(name);
133
        if ( rset.wasNull() || rv.length() == 0 )
134
        {
135
            rv = def;
136
        }
137
        return rv;
138
    }
139
 
140
    /**
141
     * ResultSet may have the indexed  item. A default will be used if not available
142
     * 
143
     * @param column - Column idex to use
144
     * @param def
145
     * @return
146
     * @throws Exception
147
     */
148
    public String getString(int column, String def) throws SQLException {
149
        String rv = rset.getString(column);
150
        if ( rset.wasNull() || rv.length() == 0 )
151
        {
152
            rv = def;
153
        }
154
        return rv;
155
    }
156
 
157
    /**
158
     * Next item in the ResultSet
159
     * @return
160
     * @throws SQLException
161
     */
162
    public boolean next() throws SQLException {
163
        return rset.next();
164
    }
165
 
166
    /**
167
     * Close the result set. Must be called as the ResultSet will not close itelf
168
     * @throws SQLException
169
     */
170
    public void close() throws SQLException {
171
        rset.close();
172
    }
173
 
174
}
175