View Javadoc
1   /**
2    * This file is part of Waarp Project.
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6    * 
7    * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8    * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9    * the License, or (at your option) any later version.
10   * 
11   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12   * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16   * <http://www.gnu.org/licenses/>.
17   */
18  package org.waarp.gateway.ftp.database.model;
19  
20  import java.sql.SQLException;
21  
22  import org.waarp.common.database.DbPreparedStatement;
23  import org.waarp.common.database.DbRequest;
24  import org.waarp.common.database.DbSession;
25  import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
26  import org.waarp.common.database.exception.WaarpDatabaseNoDataException;
27  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
28  import org.waarp.gateway.ftp.database.DbConstant;
29  import org.waarp.gateway.ftp.database.data.DbTransferLog;
30  
31  /**
32   * Oracle Database Model implementation
33   * 
34   * @author Frederic Bregier
35   * 
36   */
37  public class DbModelOracle extends org.waarp.common.database.model.DbModelOracle {
38      /**
39       * Create the object and initialize if necessary the driver
40       * 
41       * @param dbserver
42       * @param dbuser
43       * @param dbpasswd
44       * @throws WaarpDatabaseNoConnectionException
45       */
46      public DbModelOracle(String dbserver,
47              String dbuser, String dbpasswd) throws WaarpDatabaseNoConnectionException {
48          super(dbserver, dbuser, dbpasswd);
49      }
50  
51      @Override
52      public void createTables(DbSession session) throws WaarpDatabaseNoConnectionException {
53          // Create tables: configuration, hosts, rules, runner, cptrunner
54          String createTableH2 = "CREATE TABLE ";
55          String constraint = " CONSTRAINT ";
56          String primaryKey = " PRIMARY KEY ";
57          String notNull = " NOT NULL ";
58  
59          DbRequest request = new DbRequest(session);
60          // TRANSLOG
61          String action = createTableH2 + DbTransferLog.table + "(";
62          DbTransferLog.Columns[] acolumns = DbTransferLog.Columns.values();
63          for (int i = 0; i < acolumns.length; i++) {
64              action += acolumns[i].name() +
65                      DBType.getType(DbTransferLog.dbTypes[i]) + notNull + ", ";
66          }
67          // Several columns for primary key
68          action += constraint + " TRANSLOG_PK " + primaryKey + "(";
69          for (int i = DbTransferLog.NBPRKEY; i > 1; i--) {
70              action += acolumns[acolumns.length - i].name() + ",";
71          }
72          action += acolumns[acolumns.length - 1].name() + "))";
73          System.out.println(action);
74          try {
75              request.query(action);
76          } catch (WaarpDatabaseNoConnectionException e) {
77              e.printStackTrace();
78              return;
79          } catch (WaarpDatabaseSqlException e) {
80              return;
81          } finally {
82              request.close();
83          }
84          // Index TRANSLOG
85          action = "CREATE INDEX IDX_TRANSLOG ON " + DbTransferLog.table + "(";
86          DbTransferLog.Columns[] icolumns = DbTransferLog.indexes;
87          for (int i = 0; i < icolumns.length - 1; i++) {
88              action += icolumns[i].name() + ", ";
89          }
90          action += icolumns[icolumns.length - 1].name() + ")";
91          System.out.println(action);
92          try {
93              request.query(action);
94          } catch (WaarpDatabaseNoConnectionException e) {
95              e.printStackTrace();
96              return;
97          } catch (WaarpDatabaseSqlException e) {
98              return;
99          } finally {
100             request.close();
101         }
102 
103         // cptrunner
104         action = "CREATE SEQUENCE " + DbTransferLog.fieldseq +
105                 " MINVALUE " + (DbConstant.ILLEGALVALUE + 1) +
106                 " START WITH " + (DbConstant.ILLEGALVALUE + 1);
107         System.out.println(action);
108         try {
109             request.query(action);
110         } catch (WaarpDatabaseNoConnectionException e) {
111             e.printStackTrace();
112             return;
113         } catch (WaarpDatabaseSqlException e) {
114             return;
115         } finally {
116             request.close();
117         }
118     }
119 
120     @Override
121     public void resetSequence(DbSession session, long newvalue)
122             throws WaarpDatabaseNoConnectionException {
123         String action = "DROP SEQUENCE " + DbTransferLog.fieldseq;
124         String action2 = "CREATE SEQUENCE " + DbTransferLog.fieldseq +
125                 " MINVALUE " + (DbConstant.ILLEGALVALUE + 1) +
126                 " START WITH " + (newvalue);
127         DbRequest request = new DbRequest(session);
128         try {
129             request.query(action);
130             request.query(action2);
131         } catch (WaarpDatabaseNoConnectionException e) {
132             e.printStackTrace();
133             return;
134         } catch (WaarpDatabaseSqlException e) {
135             e.printStackTrace();
136             return;
137         } finally {
138             request.close();
139         }
140 
141         System.out.println(action);
142     }
143 
144     @Override
145     public long nextSequence(DbSession dbSession)
146             throws WaarpDatabaseNoConnectionException,
147             WaarpDatabaseSqlException, WaarpDatabaseNoDataException {
148         long result = DbConstant.ILLEGALVALUE;
149         String action = "SELECT " + DbTransferLog.fieldseq + ".NEXTVAL FROM DUAL";
150         DbPreparedStatement preparedStatement = new DbPreparedStatement(
151                 dbSession);
152         try {
153             preparedStatement.createPrepareStatement(action);
154             // Limit the search
155             preparedStatement.executeQuery();
156             if (preparedStatement.getNext()) {
157                 try {
158                     result = preparedStatement.getResultSet().getLong(1);
159                 } catch (SQLException e) {
160                     throw new WaarpDatabaseSqlException(e);
161                 }
162                 return result;
163             } else {
164                 throw new WaarpDatabaseNoDataException(
165                         "No sequence found. Must be initialized first");
166             }
167         } finally {
168             preparedStatement.realClose();
169         }
170     }
171 
172     @Override
173     public boolean upgradeDb(DbSession session, String version)
174             throws WaarpDatabaseNoConnectionException {
175         return true;
176     }
177 
178     @Override
179     public boolean needUpgradeDb(DbSession session, String version, boolean tryFix)
180             throws WaarpDatabaseNoConnectionException {
181         return false;
182     }
183 }