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   * PostGreSQL Database Model implementation
33   * 
34   * @author Frederic Bregier
35   * 
36   */
37  public class DbModelPostgresql extends org.waarp.common.database.model.DbModelPostgresql {
38      /**
39       * Create the object and initialize if necessary the driver
40       * 
41       * @throws WaarpDatabaseNoConnectionException
42       */
43      public DbModelPostgresql() throws WaarpDatabaseNoConnectionException {
44          super();
45      }
46  
47      @Override
48      public void createTables(DbSession session) throws WaarpDatabaseNoConnectionException {
49          // Create tables: configuration, hosts, rules, runner, cptrunner
50          String createTableH2 = "CREATE TABLE ";
51          String primaryKey = " PRIMARY KEY ";
52          String notNull = " NOT NULL ";
53  
54          DbRequest request = new DbRequest(session);
55          // TRANSLOG
56          String action = createTableH2 + DbTransferLog.table + "(";
57          DbTransferLog.Columns[] acolumns = DbTransferLog.Columns.values();
58          for (int i = 0; i < acolumns.length; i++) {
59              action += acolumns[i].name() +
60                      DBType.getType(DbTransferLog.dbTypes[i]) + notNull + ", ";
61          }
62          // Several columns for primary key
63          action += " CONSTRAINT TRANSLOG_PK " + primaryKey + "(";
64          for (int i = DbTransferLog.NBPRKEY; i > 1; i--) {
65              action += acolumns[acolumns.length - i].name() + ",";
66          }
67          action += acolumns[acolumns.length - 1].name() + "))";
68          System.out.println(action);
69          try {
70              request.query(action);
71          } catch (WaarpDatabaseNoConnectionException e) {
72              e.printStackTrace();
73              return;
74          } catch (WaarpDatabaseSqlException e) {
75              e.printStackTrace();
76              return;
77          } finally {
78              request.close();
79          }
80          // Index TRANSLOG
81          action = "CREATE INDEX IDX_TRANSLOG ON " + DbTransferLog.table + "(";
82          DbTransferLog.Columns[] icolumns = DbTransferLog.indexes;
83          for (int i = 0; i < icolumns.length - 1; i++) {
84              action += icolumns[i].name() + ", ";
85          }
86          action += icolumns[icolumns.length - 1].name() + ")";
87          System.out.println(action);
88          try {
89              request.query(action);
90          } catch (WaarpDatabaseNoConnectionException e) {
91              e.printStackTrace();
92              return;
93          } catch (WaarpDatabaseSqlException e) {
94              return;
95          } finally {
96              request.close();
97          }
98  
99          // cptrunner
100         action = "CREATE SEQUENCE " + DbTransferLog.fieldseq +
101                 " MINVALUE " + (DbConstant.ILLEGALVALUE + 1) +
102                 " RESTART WITH " + (DbConstant.ILLEGALVALUE + 1);
103         System.out.println(action);
104         try {
105             request.query(action);
106         } catch (WaarpDatabaseNoConnectionException e) {
107             e.printStackTrace();
108             return;
109         } catch (WaarpDatabaseSqlException e) {
110             e.printStackTrace();
111             return;
112         } finally {
113             request.close();
114         }
115     }
116 
117     @Override
118     public void resetSequence(DbSession session, long newvalue)
119             throws WaarpDatabaseNoConnectionException {
120         String action = "ALTER SEQUENCE " + DbTransferLog.fieldseq +
121                 " MINVALUE " + (DbConstant.ILLEGALVALUE + 1) +
122                 " RESTART WITH " + newvalue;
123         DbRequest request = new DbRequest(session);
124         try {
125             request.query(action);
126         } catch (WaarpDatabaseNoConnectionException e) {
127             e.printStackTrace();
128             return;
129         } catch (WaarpDatabaseSqlException e) {
130             e.printStackTrace();
131             return;
132         } finally {
133             request.close();
134         }
135         System.out.println(action);
136     }
137 
138     @Override
139     public long nextSequence(DbSession dbSession)
140             throws WaarpDatabaseNoConnectionException,
141             WaarpDatabaseSqlException, WaarpDatabaseNoDataException {
142         long result = DbConstant.ILLEGALVALUE;
143         String action = "SELECT NEXTVAL('" + DbTransferLog.fieldseq + "')";
144         DbPreparedStatement preparedStatement = new DbPreparedStatement(
145                 dbSession);
146         try {
147             preparedStatement.createPrepareStatement(action);
148             // Limit the search
149             preparedStatement.executeQuery();
150             if (preparedStatement.getNext()) {
151                 try {
152                     result = preparedStatement.getResultSet().getLong(1);
153                 } catch (SQLException e) {
154                     throw new WaarpDatabaseSqlException(e);
155                 }
156                 return result;
157             } else {
158                 throw new WaarpDatabaseNoDataException(
159                         "No sequence found. Must be initialized first");
160             }
161         } finally {
162             preparedStatement.realClose();
163         }
164     }
165 
166     @Override
167     public boolean upgradeDb(DbSession session, String version)
168             throws WaarpDatabaseNoConnectionException {
169         return true;
170     }
171 
172     @Override
173     public boolean needUpgradeDb(DbSession session, String version, boolean tryFix)
174             throws WaarpDatabaseNoConnectionException {
175         return false;
176     }
177 }