View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
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   * Oracle Database Model implementation
37   * @author Frederic Bregier
38   *
39   */
40  public class DbModelOracle extends goldengate.common.database.model.DbModelOracle {
41      /**
42       * Create the object and initialize if necessary the driver
43       * @param dbserver
44       * @param dbuser
45       * @param dbpasswd
46       * @throws GoldenGateDatabaseNoConnectionException
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          // Create tables: configuration, hosts, rules, runner, cptrunner
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          // TRANSLOG
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          // Several columns for primary key
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          // Index TRANSLOG
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         // cptrunner
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      * (non-Javadoc)
124      *
125      * @see openr66.databaseold.model.DbModel#resetSequence()
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      * (non-Javadoc)
152      *
153      * @see openr66.databaseold.model.DbModel#nextSequence()
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             // Limit the search
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 }