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   * H2 Database Model implementation
33   * 
34   * @author Frederic Bregier
35   * 
36   */
37  public class DbModelH2 extends org.waarp.common.database.model.DbModelH2 {
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 DbModelH2(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 IF NOT EXISTS ";
55          String primaryKey = " PRIMARY KEY ";
56          String notNull = " NOT NULL ";
57  
58          // log
59          String action = createTableH2 + DbTransferLog.table + "(";
60          DbTransferLog.Columns[] acolumns = DbTransferLog.Columns.values();
61          for (int i = 0; i < acolumns.length; i++) {
62              action += acolumns[i].name() +
63                      DBType.getType(DbTransferLog.dbTypes[i]) + notNull + ", ";
64          }
65          // Several columns for primary key
66          action += " CONSTRAINT TRANSLOG_PK " + primaryKey + "(";
67          for (int i = DbTransferLog.NBPRKEY; i > 1; i--) {
68              action += acolumns[acolumns.length - i].name() + ",";
69          }
70          action += acolumns[acolumns.length - 1].name() + "))";
71          System.out.println(action);
72          DbRequest request = new DbRequest(session);
73          try {
74              request.query(action);
75          } catch (WaarpDatabaseNoConnectionException e) {
76              e.printStackTrace();
77              return;
78          } catch (WaarpDatabaseSqlException e) {
79              e.printStackTrace();
80              return;
81          } finally {
82              request.close();
83          }
84          // Index Runner
85          action = "CREATE INDEX IF NOT EXISTS 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 IF NOT EXISTS " + DbTransferLog.fieldseq +
105                 " START WITH " + (DbConstant.ILLEGALVALUE + 1) +
106                 " MINVALUE " + (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             // version <= 1.2.173
115             action = "CREATE SEQUENCE IF NOT EXISTS " + DbTransferLog.fieldseq +
116                     " START WITH " + (DbConstant.ILLEGALVALUE + 1);
117             System.out.println(action);
118             try {
119                 request.query(action);
120             } catch (WaarpDatabaseNoConnectionException e2) {
121                 e2.printStackTrace();
122                 return;
123             } catch (WaarpDatabaseSqlException e2) {
124                 e2.printStackTrace();
125                 return;
126             } finally {
127                 request.close();
128             }
129             return;
130         } finally {
131             request.close();
132         }
133     }
134 
135     @Override
136     public void resetSequence(DbSession session, long newvalue)
137             throws WaarpDatabaseNoConnectionException {
138         String action = "ALTER SEQUENCE " + DbTransferLog.fieldseq +
139                 " RESTART WITH " + newvalue;
140         DbRequest request = new DbRequest(session);
141         try {
142             request.query(action);
143         } catch (WaarpDatabaseNoConnectionException e) {
144             e.printStackTrace();
145             return;
146         } catch (WaarpDatabaseSqlException e) {
147             e.printStackTrace();
148             return;
149         } finally {
150             request.close();
151         }
152         System.out.println(action);
153     }
154 
155     @Override
156     public long nextSequence(DbSession dbSession)
157             throws WaarpDatabaseNoConnectionException,
158             WaarpDatabaseSqlException, WaarpDatabaseNoDataException {
159         long result = DbConstant.ILLEGALVALUE;
160         String action = "SELECT NEXTVAL('" + DbTransferLog.fieldseq + "')";
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 WaarpDatabaseSqlException(e);
172                 }
173                 return result;
174             } else {
175                 throw new WaarpDatabaseNoDataException(
176                         "No sequence found. Must be initialized first");
177             }
178         } finally {
179             preparedStatement.realClose();
180         }
181     }
182 
183     @Override
184     public boolean upgradeDb(DbSession session, String version)
185             throws WaarpDatabaseNoConnectionException {
186         return true;
187     }
188 
189     @Override
190     public boolean needUpgradeDb(DbSession session, String version, boolean tryFix)
191             throws WaarpDatabaseNoConnectionException {
192         return false;
193     }
194 
195 }