1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.common.database.data;
22
23 import java.sql.Types;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import goldengate.common.database.DbPreparedStatement;
27 import goldengate.common.database.DbSession;
28 import goldengate.common.database.exception.GoldenGateDatabaseException;
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 DbDataModel extends AbstractDbData {
40 public static enum Columns {
41 READGLOBALLIMIT,
42 WRITEGLOBALLIMIT,
43 READSESSIONLIMIT,
44 WRITESESSIONLIMIT,
45 DELAYLIMIT,
46 UPDATEDINFO,
47 HOSTID
48 }
49
50 public static final int[] dbTypes = {
51 Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT,
52 Types.BIGINT, Types.INTEGER, Types.VARCHAR };
53
54 public static final String table = " CONFIGURATION ";
55 public static final String fieldseq = "RUNSEQ";
56 public static final Columns [] indexes = {
57 Columns.READGLOBALLIMIT, Columns.READSESSIONLIMIT, Columns.WRITEGLOBALLIMIT,
58 Columns.WRITESESSIONLIMIT, Columns.HOSTID
59 };
60
61
62
63
64 private static final ConcurrentHashMap<String, DbDataModel> dbR66ConfigurationHashMap =
65 new ConcurrentHashMap<String, DbDataModel>();
66
67 private String hostid;
68
69 private long readgloballimit;
70
71 private long writegloballimit;
72
73 private long readsessionlimit;
74
75 private long writesessionlimit;
76
77 private long delayllimit;
78
79 private int updatedInfo = UpdatedInfo.UNKNOWN.ordinal();
80
81
82 public static final int NBPRKEY = 1;
83
84 protected static final String selectAllFields = Columns.READGLOBALLIMIT
85 .name() +
86 "," +
87 Columns.WRITEGLOBALLIMIT.name() +
88 "," +
89 Columns.READSESSIONLIMIT.name() +
90 "," +
91 Columns.WRITESESSIONLIMIT.name() +
92 "," +
93 Columns.DELAYLIMIT.name() +
94 "," + Columns.UPDATEDINFO.name() + "," + Columns.HOSTID.name();
95
96 protected static final String updateAllFields = Columns.READGLOBALLIMIT
97 .name() +
98 "=?," +
99 Columns.WRITEGLOBALLIMIT.name() +
100 "=?," +
101 Columns.READSESSIONLIMIT.name() +
102 "=?," +
103 Columns.WRITESESSIONLIMIT.name() +
104 "=?," +
105 Columns.DELAYLIMIT.name() +
106 "=?," +
107 Columns.UPDATEDINFO.name() +
108 "=?";
109
110 protected static final String insertAllValues = " (?,?,?,?,?,?,?) ";
111
112
113
114
115 @Override
116 protected void initObject() {
117 primaryKey = new DbValue[]{new DbValue(hostid, Columns.HOSTID
118 .name())};
119 otherFields = new DbValue[]{
120 new DbValue(readgloballimit, Columns.READGLOBALLIMIT.name()),
121 new DbValue(writegloballimit, Columns.WRITEGLOBALLIMIT.name()),
122 new DbValue(readsessionlimit, Columns.READSESSIONLIMIT.name()),
123 new DbValue(writesessionlimit, Columns.WRITESESSIONLIMIT.name()),
124 new DbValue(delayllimit, Columns.DELAYLIMIT.name()),
125 new DbValue(updatedInfo, Columns.UPDATEDINFO.name()) };
126 allFields = new DbValue[]{
127 otherFields[0], otherFields[1], otherFields[2], otherFields[3],
128 otherFields[4], otherFields[5], primaryKey[0] };
129 }
130
131 @Override
132 protected void setToArray() {
133 allFields[Columns.HOSTID.ordinal()].setValue(hostid);
134 allFields[Columns.READGLOBALLIMIT.ordinal()].setValue(readgloballimit);
135 allFields[Columns.WRITEGLOBALLIMIT.ordinal()]
136 .setValue(writegloballimit);
137 allFields[Columns.READSESSIONLIMIT.ordinal()]
138 .setValue(readsessionlimit);
139 allFields[Columns.WRITESESSIONLIMIT.ordinal()]
140 .setValue(writesessionlimit);
141 allFields[Columns.DELAYLIMIT.ordinal()].setValue(delayllimit);
142 allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
143 }
144
145 @Override
146 protected void setFromArray() throws GoldenGateDatabaseSqlException {
147 hostid = (String) allFields[Columns.HOSTID.ordinal()].getValue();
148 readgloballimit = (Long) allFields[Columns.READGLOBALLIMIT.ordinal()]
149 .getValue();
150 writegloballimit = (Long) allFields[Columns.WRITEGLOBALLIMIT.ordinal()]
151 .getValue();
152 readsessionlimit = (Long) allFields[Columns.READSESSIONLIMIT.ordinal()]
153 .getValue();
154 writesessionlimit = (Long) allFields[Columns.WRITESESSIONLIMIT
155 .ordinal()].getValue();
156 delayllimit = (Long) allFields[Columns.DELAYLIMIT.ordinal()].getValue();
157 updatedInfo = (Integer) allFields[Columns.UPDATEDINFO.ordinal()]
158 .getValue();
159 }
160
161
162
163
164 @Override
165 protected String getSelectAllFields() {
166 return selectAllFields;
167 }
168
169
170
171
172 @Override
173 protected String getTable() {
174 return table;
175 }
176
177
178
179
180 @Override
181 protected String getInsertAllValues() {
182 return insertAllValues;
183 }
184
185
186
187
188 @Override
189 protected String getUpdateAllFields() {
190 return updateAllFields;
191 }
192
193
194
195
196 @Override
197 protected String getWherePrimaryKey() {
198 return primaryKey[0].column + " = ? ";
199 }
200
201
202
203 protected void setPrimaryKey() {
204 primaryKey[0].setValue(hostid);
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 public DbDataModel(DbSession dbSession, String hostid, long rg, long wg, long rs,
222 long ws, long del) {
223 super(dbSession);
224 this.hostid = hostid;
225 readgloballimit = rg;
226 writegloballimit = wg;
227 readsessionlimit = rs;
228 writesessionlimit = ws;
229 delayllimit = del;
230 setToArray();
231 isSaved = false;
232 }
233
234
235
236
237
238
239 public DbDataModel(DbSession dbSession, String hostid) throws GoldenGateDatabaseException {
240 super(dbSession);
241 this.hostid = hostid;
242
243 select();
244 }
245
246
247
248
249
250
251 @Override
252 public void delete() throws GoldenGateDatabaseException {
253 if (dbSession == null) {
254 dbR66ConfigurationHashMap.remove(this.hostid);
255 isSaved = false;
256 return;
257 }
258 DbPreparedStatement preparedStatement = new DbPreparedStatement(
259 dbSession);
260 try {
261 preparedStatement.createPrepareStatement("DELETE FROM " + table +
262 " WHERE " + getWherePrimaryKey());
263 setPrimaryKey();
264 setValues(preparedStatement, primaryKey);
265 int count = preparedStatement.executeUpdate();
266 if (count <= 0) {
267 throw new GoldenGateDatabaseNoDataException("No row found");
268 }
269 isSaved = false;
270 } finally {
271 preparedStatement.realClose();
272 }
273 }
274
275
276
277
278
279
280 @Override
281 public void insert() throws GoldenGateDatabaseException {
282 if (isSaved) {
283 return;
284 }
285 if (dbSession == null) {
286 dbR66ConfigurationHashMap.put(this.hostid, this);
287 isSaved = true;
288 return;
289 }
290 DbPreparedStatement preparedStatement = new DbPreparedStatement(
291 dbSession);
292 try {
293 preparedStatement.createPrepareStatement("INSERT INTO " + table +
294 " (" + selectAllFields + ") VALUES " + insertAllValues);
295 setValues(preparedStatement, allFields);
296 int count = preparedStatement.executeUpdate();
297 if (count <= 0) {
298 throw new GoldenGateDatabaseNoDataException("No row found");
299 }
300 isSaved = true;
301 } finally {
302 preparedStatement.realClose();
303 }
304 }
305
306
307
308
309 @Override
310 public boolean exist() throws GoldenGateDatabaseException {
311 if (dbSession == null) {
312 return dbR66ConfigurationHashMap.containsKey(hostid);
313 }
314 DbPreparedStatement preparedStatement = new DbPreparedStatement(
315 dbSession);
316 try {
317 preparedStatement.createPrepareStatement("SELECT " +
318 primaryKey[0].column + " FROM " + table + " WHERE " +
319 getWherePrimaryKey());
320 setPrimaryKey();
321 setValues(preparedStatement, primaryKey);
322 preparedStatement.executeQuery();
323 return preparedStatement.getNext();
324 } finally {
325 preparedStatement.realClose();
326 }
327 }
328
329
330
331
332
333 @Override
334 public void select() throws GoldenGateDatabaseException {
335 if (dbSession == null) {
336 DbDataModel conf = dbR66ConfigurationHashMap.get(this.hostid);
337 if (conf == null) {
338 throw new GoldenGateDatabaseNoDataException("No row found");
339 } else {
340
341 for (int i = 0; i < allFields.length; i++){
342 allFields[i].value = conf.allFields[i].value;
343 }
344 setFromArray();
345 isSaved = true;
346 return;
347 }
348 }
349 DbPreparedStatement preparedStatement = new DbPreparedStatement(
350 dbSession);
351 try {
352 preparedStatement.createPrepareStatement("SELECT " +
353 selectAllFields + " FROM " + table + " WHERE " +
354 getWherePrimaryKey());
355 setPrimaryKey();
356 setValues(preparedStatement, primaryKey);
357 preparedStatement.executeQuery();
358 if (preparedStatement.getNext()) {
359 getValues(preparedStatement, allFields);
360 setFromArray();
361 isSaved = true;
362 } else {
363 throw new GoldenGateDatabaseNoDataException("No row found");
364 }
365 } finally {
366 preparedStatement.realClose();
367 }
368 }
369
370
371
372
373
374 @Override
375 public void update() throws GoldenGateDatabaseException {
376 if (isSaved) {
377 return;
378 }
379 if (dbSession == null) {
380 dbR66ConfigurationHashMap.put(this.hostid, this);
381 isSaved = true;
382 return;
383 }
384 DbPreparedStatement preparedStatement = new DbPreparedStatement(
385 dbSession);
386 try {
387 preparedStatement.createPrepareStatement("UPDATE " + table +
388 " SET " + updateAllFields + " WHERE " +
389 getWherePrimaryKey());
390 setValues(preparedStatement, allFields);
391 int count = preparedStatement.executeUpdate();
392 if (count <= 0) {
393 throw new GoldenGateDatabaseNoDataException("No row found");
394 }
395 isSaved = true;
396 } finally {
397 preparedStatement.realClose();
398 }
399 }
400
401
402
403 private DbDataModel(DbSession session) {
404 super(session);
405 }
406
407
408
409
410
411
412
413 public static DbDataModel getFromStatement(DbPreparedStatement preparedStatement) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
414 DbDataModel dbDataModel = new DbDataModel(preparedStatement.getDbSession());
415 dbDataModel.getValues(preparedStatement, dbDataModel.allFields);
416 dbDataModel.setFromArray();
417 dbDataModel.isSaved = true;
418 return dbDataModel;
419 }
420
421
422
423
424
425
426 public static DbPreparedStatement getUpdatedPrepareStament(DbSession session) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
427 String request = "SELECT " +selectAllFields;
428 request += " FROM "+table+
429 " WHERE "+Columns.UPDATEDINFO.name()+" = "+
430 AbstractDbData.UpdatedInfo.TOSUBMIT.ordinal();
431 DbPreparedStatement prep = new DbPreparedStatement(session, request);
432 session.addLongTermPreparedStatement(prep);
433 return prep;
434 }
435
436
437
438
439
440 @Override
441 public void changeUpdatedInfo(UpdatedInfo info) {
442 if (updatedInfo != info.ordinal()) {
443 updatedInfo = info.ordinal();
444 allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
445 isSaved = false;
446 }
447 }
448
449 }