1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package goldengate.common.database;
21
22 import goldengate.common.logging.GgInternalLogger;
23 import goldengate.common.logging.GgInternalLoggerFactory;
24
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.sql.Statement;
28
29 import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
30 import goldengate.common.database.exception.GoldenGateDatabaseNoDataException;
31 import goldengate.common.database.exception.GoldenGateDatabaseSqlException;
32
33
34
35
36
37
38
39 public class DbRequest {
40
41
42
43 private static final GgInternalLogger logger = GgInternalLoggerFactory
44 .getLogger(DbRequest.class);
45
46
47
48
49 private Statement stmt = null;
50
51
52
53
54 private ResultSet rs = null;
55
56
57
58
59 private final DbSession ls;
60
61
62
63
64
65
66
67 public DbRequest(DbSession ls) throws GoldenGateDatabaseNoConnectionException {
68 if (ls.isDisconnected) {
69 ls.checkConnection();
70 }
71 this.ls = ls;
72 }
73
74
75
76
77
78
79
80
81 private Statement createStatement()
82 throws GoldenGateDatabaseNoConnectionException,
83 GoldenGateDatabaseSqlException {
84 if (ls == null) {
85 throw new GoldenGateDatabaseNoConnectionException("No connection");
86 }
87 if (ls.conn == null) {
88 throw new GoldenGateDatabaseNoConnectionException("No connection");
89 }
90 if (ls.isDisconnected) {
91 ls.checkConnection();
92 }
93 try {
94 return ls.conn.createStatement();
95 } catch (SQLException e) {
96 ls.checkConnection();
97 try {
98 return ls.conn.createStatement();
99 } catch (SQLException e1) {
100 throw new GoldenGateDatabaseSqlException(
101 "Error while Create Statement", e);
102 }
103 }
104 }
105
106
107
108
109
110
111
112
113
114 public void select(String select)
115 throws GoldenGateDatabaseNoConnectionException,
116 GoldenGateDatabaseSqlException {
117 close();
118 stmt = createStatement();
119
120
121
122 try {
123 if (stmt.execute(select)) {
124 rs = stmt.getResultSet();
125 }
126 } catch (SQLException e) {
127 logger.error("SQL Exception Request:" + select + "\n" +
128 e.getMessage());
129 DbSession.error(e);
130 ls.checkConnectionNoException();
131 throw new GoldenGateDatabaseSqlException("SQL Exception Request:" +
132 select, e);
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145 public int query(String query) throws GoldenGateDatabaseNoConnectionException,
146 GoldenGateDatabaseSqlException {
147 close();
148 stmt = createStatement();
149 try {
150 int rowcount = stmt.executeUpdate(query);
151 logger.debug("QUERY(" + rowcount + "): {}", query);
152 return rowcount;
153 } catch (SQLException e) {
154 logger.error("SQL Exception Request:" + query + "\n" +
155 e.getMessage());
156 DbSession.error(e);
157 ls.checkConnectionNoException();
158 throw new GoldenGateDatabaseSqlException("SQL Exception Request:" +
159 query, e);
160 }
161 }
162
163
164
165
166 public void close() {
167
168
169
170
171 if (rs != null) {
172 try {
173 rs.close();
174 } catch (SQLException sqlEx) {
175 ls.checkConnectionNoException();
176 }
177 rs = null;
178 }
179 if (stmt != null) {
180 try {
181 stmt.close();
182 } catch (SQLException sqlEx) {
183 ls.checkConnectionNoException();
184 }
185 stmt = null;
186 }
187 }
188
189
190
191
192
193
194
195
196 public long getLastId() throws GoldenGateDatabaseNoDataException {
197 ResultSet rstmp;
198 long result = DbConstant.ILLEGALVALUE;
199 try {
200 rstmp = stmt.getGeneratedKeys();
201 if (rstmp.next()) {
202 result = rstmp.getLong(1);
203 }
204 rstmp.close();
205 rstmp = null;
206 } catch (SQLException e) {
207 DbSession.error(e);
208 ls.checkConnectionNoException();
209 throw new GoldenGateDatabaseNoDataException("No data found", e);
210 }
211 return result;
212 }
213
214
215
216
217
218
219
220
221 public boolean getNext() throws GoldenGateDatabaseNoConnectionException,
222 GoldenGateDatabaseSqlException {
223 if (rs == null) {
224 logger.error("SQL ResultSet is Null into getNext");
225 throw new GoldenGateDatabaseNoConnectionException(
226 "SQL ResultSet is Null into getNext");
227 }
228 if (ls.isDisconnected) {
229 ls.checkConnection();
230 throw new GoldenGateDatabaseSqlException(
231 "Request cannot be executed since connection was recreated between");
232 }
233 try {
234 return rs.next();
235 } catch (SQLException e) {
236 logger.warn("SQL Exception to getNextRow" + "\n" + e.getMessage());
237 DbSession.error(e);
238 ls.checkConnectionNoException();
239 throw new GoldenGateDatabaseSqlException("SQL Exception to getNextRow",
240 e);
241 }
242 }
243
244
245
246
247
248
249 public ResultSet getResultSet() throws GoldenGateDatabaseNoConnectionException {
250 if (rs == null) {
251 throw new GoldenGateDatabaseNoConnectionException(
252 "SQL ResultSet is Null into getResultSet");
253 }
254 return rs;
255 }
256
257
258
259
260
261
262
263 public static String getIsNull(String value) {
264 return value == null? " is NULL" : " = '" + value + "'";
265 }
266 }