1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.ftp.exec.database.model;
22
23 import goldengate.common.database.DbPreparedStatement;
24 import goldengate.common.database.DbRequest;
25 import goldengate.common.database.DbSession;
26 import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
27 import goldengate.common.database.exception.GoldenGateDatabaseNoDataException;
28 import goldengate.common.database.exception.GoldenGateDatabaseSqlException;
29
30 import java.sql.SQLException;
31
32 import goldengate.ftp.exec.database.DbConstant;
33 import goldengate.ftp.exec.database.data.DbTransferLog;
34
35
36
37
38
39
40 public class DbModelH2 extends goldengate.common.database.model.DbModelH2 {
41
42
43
44
45
46
47
48 public DbModelH2(String dbserver,
49 String dbuser, String dbpasswd) throws GoldenGateDatabaseNoConnectionException {
50 super(dbserver, dbuser, dbpasswd);
51 }
52 @Override
53 public void createTables(DbSession session) throws GoldenGateDatabaseNoConnectionException {
54
55 String createTableH2 = "CREATE TABLE IF NOT EXISTS ";
56 String primaryKey = " PRIMARY KEY ";
57 String notNull = " NOT NULL ";
58
59
60 String action = createTableH2 + DbTransferLog.table + "(";
61 DbTransferLog.Columns[] acolumns = DbTransferLog.Columns.values();
62 for (int i = 0; i < acolumns.length; i ++) {
63 action += acolumns[i].name() +
64 DBType.getType(DbTransferLog.dbTypes[i]) + notNull + ", ";
65 }
66
67 action += " CONSTRAINT TRANSLOG_PK " + primaryKey + "(";
68 for (int i = DbTransferLog.NBPRKEY; i > 1; i--) {
69 action += acolumns[acolumns.length - i].name() + ",";
70 }
71 action += acolumns[acolumns.length - 1].name() + "))";
72 System.out.println(action);
73 DbRequest request = new DbRequest(session);
74 try {
75 request.query(action);
76 } catch (GoldenGateDatabaseNoConnectionException e) {
77 e.printStackTrace();
78 return;
79 } catch (GoldenGateDatabaseSqlException e) {
80 e.printStackTrace();
81 return;
82 } finally {
83 request.close();
84 }
85
86 action = "CREATE INDEX IF NOT EXISTS IDX_TRANSLOG ON "+ DbTransferLog.table + "(";
87 DbTransferLog.Columns[] icolumns = DbTransferLog.indexes;
88 for (int i = 0; i < icolumns.length-1; i ++) {
89 action += icolumns[i].name()+ ", ";
90 }
91 action += icolumns[icolumns.length-1].name()+ ")";
92 System.out.println(action);
93 try {
94 request.query(action);
95 } catch (GoldenGateDatabaseNoConnectionException e) {
96 e.printStackTrace();
97 return;
98 } catch (GoldenGateDatabaseSqlException e) {
99 return;
100 } finally {
101 request.close();
102 }
103
104
105 action = "CREATE SEQUENCE IF NOT EXISTS " + DbTransferLog.fieldseq +
106 " START WITH " + (DbConstant.ILLEGALVALUE + 1);
107 System.out.println(action);
108 try {
109 request.query(action);
110 } catch (GoldenGateDatabaseNoConnectionException e) {
111 e.printStackTrace();
112 return;
113 } catch (GoldenGateDatabaseSqlException e) {
114 e.printStackTrace();
115 return;
116 } finally {
117 request.close();
118 }
119 }
120
121
122
123
124
125
126 @Override
127 public void resetSequence(DbSession session, long newvalue) throws GoldenGateDatabaseNoConnectionException {
128 String action = "ALTER SEQUENCE " + DbTransferLog.fieldseq +
129 " RESTART WITH " + newvalue;
130 DbRequest request = new DbRequest(session);
131 try {
132 request.query(action);
133 } catch (GoldenGateDatabaseNoConnectionException e) {
134 e.printStackTrace();
135 return;
136 } catch (GoldenGateDatabaseSqlException e) {
137 e.printStackTrace();
138 return;
139 } finally {
140 request.close();
141 }
142 System.out.println(action);
143 }
144
145
146
147
148
149
150 @Override
151 public long nextSequence(DbSession dbSession)
152 throws GoldenGateDatabaseNoConnectionException,
153 GoldenGateDatabaseSqlException, GoldenGateDatabaseNoDataException {
154 long result = DbConstant.ILLEGALVALUE;
155 String action = "SELECT NEXTVAL('" + DbTransferLog.fieldseq + "')";
156 DbPreparedStatement preparedStatement = new DbPreparedStatement(
157 dbSession);
158 try {
159 preparedStatement.createPrepareStatement(action);
160
161 preparedStatement.executeQuery();
162 if (preparedStatement.getNext()) {
163 try {
164 result = preparedStatement.getResultSet().getLong(1);
165 } catch (SQLException e) {
166 throw new GoldenGateDatabaseSqlException(e);
167 }
168 return result;
169 } else {
170 throw new GoldenGateDatabaseNoDataException(
171 "No sequence found. Must be initialized first");
172 }
173 } finally {
174 preparedStatement.realClose();
175 }
176 }
177
178 }