Subversion Repositories DevTools

Rev

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