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