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   * PostGreSQL Database Model implementation
37   * @author Frederic Bregier
38   *
39   */
40  public class DbModelPostgresql extends goldengate.common.database.model.DbModelPostgresql {
41      /**
42       * Create the object and initialize if necessary the driver
43       * @throws GoldenGateDatabaseNoConnectionException
44       */
45      public DbModelPostgresql() throws GoldenGateDatabaseNoConnectionException {
46          super();
47      }
48  
49      @Override
50      public void createTables(DbSession session) throws GoldenGateDatabaseNoConnectionException {
51          // Create tables: configuration, hosts, rules, runner, cptrunner
52          String createTableH2 = "CREATE TABLE ";
53          String primaryKey = " PRIMARY KEY ";
54          String notNull = " NOT NULL ";
55  
56          DbRequest request = new DbRequest(session);
57          // TRANSLOG
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          // Several columns for primary key
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          // Index TRANSLOG
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         // cptrunner
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      * (non-Javadoc)
120      *
121      * @see openr66.databaseold.model.DbModel#resetSequence()
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      * (non-Javadoc)
144      *
145      * @see openr66.databaseold.model.DbModel#nextSequence()
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             // Limit the search
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 }