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 DbModelOracle extends goldengate.common.database.model.DbModelOracle {
41
42
43
44
45
46
47
48 public DbModelOracle(String dbserver,
49 String dbuser, String dbpasswd) throws GoldenGateDatabaseNoConnectionException {
50 super(dbserver, dbuser, dbpasswd);
51 }
52
53 @Override
54 public void createTables(DbSession session) throws GoldenGateDatabaseNoConnectionException {
55
56 String createTableH2 = "CREATE TABLE ";
57 String constraint = " CONSTRAINT ";
58 String primaryKey = " PRIMARY KEY ";
59 String notNull = " NOT NULL ";
60
61 DbRequest request = new DbRequest(session);
62
63 String action = createTableH2 + DbTransferLog.table + "(";
64 DbTransferLog.Columns[] acolumns = DbTransferLog.Columns.values();
65 for (int i = 0; i < acolumns.length; i ++) {
66 action += acolumns[i].name() +
67 DBType.getType(DbTransferLog.dbTypes[i]) + notNull + ", ";
68 }
69
70 action += constraint+" TRANSLOG_PK " + primaryKey + "(";
71 for (int i = DbTransferLog.NBPRKEY; i > 1; i--) {
72 action += acolumns[acolumns.length - i].name() + ",";
73 }
74 action += acolumns[acolumns.length - 1].name() + "))";
75 System.out.println(action);
76 try {
77 request.query(action);
78 } catch (GoldenGateDatabaseNoConnectionException e) {
79 e.printStackTrace();
80 return;
81 } catch (GoldenGateDatabaseSqlException e) {
82 return;
83 } finally {
84 request.close();
85 }
86
87 action = "CREATE INDEX IDX_TRANSLOG ON "+ DbTransferLog.table + "(";
88 DbTransferLog.Columns[] icolumns = DbTransferLog.indexes;
89 for (int i = 0; i < icolumns.length-1; i ++) {
90 action += icolumns[i].name()+ ", ";
91 }
92 action += icolumns[icolumns.length-1].name()+ ")";
93 System.out.println(action);
94 try {
95 request.query(action);
96 } catch (GoldenGateDatabaseNoConnectionException e) {
97 e.printStackTrace();
98 return;
99 } catch (GoldenGateDatabaseSqlException e) {
100 return;
101 } finally {
102 request.close();
103 }
104
105
106 action = "CREATE SEQUENCE " + DbTransferLog.fieldseq +
107 " MINVALUE " + (DbConstant.ILLEGALVALUE + 1)+
108 " START WITH " + (DbConstant.ILLEGALVALUE + 1);
109 System.out.println(action);
110 try {
111 request.query(action);
112 } catch (GoldenGateDatabaseNoConnectionException e) {
113 e.printStackTrace();
114 return;
115 } catch (GoldenGateDatabaseSqlException e) {
116 return;
117 } finally {
118 request.close();
119 }
120 }
121
122
123
124
125
126
127 @Override
128 public void resetSequence(DbSession session, long newvalue) throws GoldenGateDatabaseNoConnectionException {
129 String action = "DROP SEQUENCE " + DbTransferLog.fieldseq;
130 String action2 = "CREATE SEQUENCE " + DbTransferLog.fieldseq +
131 " MINVALUE " + (DbConstant.ILLEGALVALUE + 1)+
132 " START WITH " + (newvalue);
133 DbRequest request = new DbRequest(session);
134 try {
135 request.query(action);
136 request.query(action2);
137 } catch (GoldenGateDatabaseNoConnectionException e) {
138 e.printStackTrace();
139 return;
140 } catch (GoldenGateDatabaseSqlException e) {
141 e.printStackTrace();
142 return;
143 } finally {
144 request.close();
145 }
146
147 System.out.println(action);
148 }
149
150
151
152
153
154
155 @Override
156 public long nextSequence(DbSession dbSession)
157 throws GoldenGateDatabaseNoConnectionException,
158 GoldenGateDatabaseSqlException, GoldenGateDatabaseNoDataException {
159 long result = DbConstant.ILLEGALVALUE;
160 String action = "SELECT " + DbTransferLog.fieldseq + ".NEXTVAL FROM DUAL";
161 DbPreparedStatement preparedStatement = new DbPreparedStatement(
162 dbSession);
163 try {
164 preparedStatement.createPrepareStatement(action);
165
166 preparedStatement.executeQuery();
167 if (preparedStatement.getNext()) {
168 try {
169 result = preparedStatement.getResultSet().getLong(1);
170 } catch (SQLException e) {
171 throw new GoldenGateDatabaseSqlException(e);
172 }
173 return result;
174 } else {
175 throw new GoldenGateDatabaseNoDataException(
176 "No sequence found. Must be initialized first");
177 }
178 } finally {
179 preparedStatement.realClose();
180 }
181 }
182 }