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   * H2 Database Model implementation
37   * @author Frederic Bregier
38   *
39   */
40  public class DbModelH2 extends goldengate.common.database.model.DbModelH2 {
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 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          // Create tables: configuration, hosts, rules, runner, cptrunner
55          String createTableH2 = "CREATE TABLE IF NOT EXISTS ";
56          String primaryKey = " PRIMARY KEY ";
57          String notNull = " NOT NULL ";
58  
59          // log
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          // Several columns for primary key
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          // Index Runner
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         // cptrunner
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      * (non-Javadoc)
123      *
124      * @see openr66.databaseold.model.DbModel#resetSequence()
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      * (non-Javadoc)
147      *
148      * @see openr66.databaseold.model.DbModel#nextSequence()
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             // Limit the search
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 }