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  import java.util.concurrent.locks.ReentrantLock;
22  
23  import org.waarp.common.database.DbPreparedStatement;
24  import org.waarp.common.database.DbRequest;
25  import org.waarp.common.database.DbSession;
26  import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
27  import org.waarp.common.database.exception.WaarpDatabaseNoDataException;
28  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
29  import org.waarp.gateway.ftp.database.DbConstant;
30  import org.waarp.gateway.ftp.database.data.DbTransferLog;
31  
32  /**
33   * MySQL Database Model implementation
34   * 
35   * @author Frederic Bregier
36   * 
37   */
38  public class DbModelMysql extends org.waarp.common.database.model.DbModelMysql {
39      /**
40       * Create the object and initialize if necessary the driver
41       * 
42       * @param dbserver
43       * @param dbuser
44       * @param dbpasswd
45       * @throws WaarpDatabaseNoConnectionException
46       */
47      public DbModelMysql(String dbserver,
48              String dbuser, String dbpasswd) throws WaarpDatabaseNoConnectionException {
49          super(dbserver, dbuser, dbpasswd);
50      }
51  
52      private final ReentrantLock lock = new ReentrantLock();
53  
54      @Override
55      public void createTables(DbSession session) throws WaarpDatabaseNoConnectionException {
56          // Create tables: configuration, hosts, rules, runner, cptrunner
57          String createTableH2 = "CREATE TABLE IF NOT EXISTS ";
58          String primaryKey = " PRIMARY KEY ";
59          String notNull = " NOT NULL ";
60  
61          DbRequest request = new DbRequest(session);
62          // TRANSLOG
63          String action = createTableH2 + DbTransferLog.table + "(";
64          DbTransferLog.Columns[] acolumns = DbTransferLog.Columns.values();
65          for (int i = 0; i < acolumns.length; i++) {
66              action += acolumns[i].name() +
67                      DBType.getType(DbTransferLog.dbTypes[i]) + notNull + ", ";
68          }
69          // Several columns for primary key
70          action += " CONSTRAINT TRANSLOG_PK " + primaryKey + "(";
71          for (int i = DbTransferLog.NBPRKEY; i > 1; i--) {
72              action += acolumns[acolumns.length - i].name() + ",";
73          }
74          action += acolumns[acolumns.length - 1].name() + "))";
75          System.out.println(action);
76          try {
77              request.query(action);
78          } catch (WaarpDatabaseNoConnectionException e) {
79              e.printStackTrace();
80              return;
81          } catch (WaarpDatabaseSqlException e) {
82              e.printStackTrace();
83              return;
84          } finally {
85              request.close();
86          }
87          // Index TRANSLOG
88          action = "CREATE INDEX IDX_TRANSLOG ON " + DbTransferLog.table + "(";
89          DbTransferLog.Columns[] icolumns = DbTransferLog.indexes;
90          for (int i = 0; i < icolumns.length - 1; i++) {
91              action += icolumns[i].name() + ", ";
92          }
93          action += icolumns[icolumns.length - 1].name() + ")";
94          System.out.println(action);
95          try {
96              request.query(action);
97          } catch (WaarpDatabaseNoConnectionException e) {
98              e.printStackTrace();
99              return;
100         } catch (WaarpDatabaseSqlException e) {
101             return;
102         } finally {
103             request.close();
104         }
105 
106         // cptrunner
107         /*
108          * # Table to handle any number of sequences: CREATE TABLE Sequences ( name VARCHAR(22) NOT
109          * NULL, seq INT UNSIGNED NOT NULL, # (or BIGINT) PRIMARY KEY name ); # Create a Sequence:
110          * INSERT INTO Sequences (name, seq) VALUES (?, 0); # Drop a Sequence: DELETE FROM Sequences
111          * WHERE name = ?; # Get a sequence number: UPDATE Sequences SET seq = LAST_INSERT_ID(seq +
112          * 1) WHERE name = ?; $seq = $db->LastInsertId();
113          */
114         action = "CREATE TABLE Sequences (name VARCHAR(22) NOT NULL PRIMARY KEY," +
115                 "seq BIGINT NOT NULL)";
116         System.out.println(action);
117         try {
118             request.query(action);
119         } catch (WaarpDatabaseNoConnectionException e) {
120             e.printStackTrace();
121             return;
122         } catch (WaarpDatabaseSqlException e) {
123             e.printStackTrace();
124             return;
125         } finally {
126             request.close();
127         }
128         action = "INSERT INTO Sequences (name, seq) VALUES ('" + DbTransferLog.fieldseq + "', " +
129                 (DbConstant.ILLEGALVALUE + 1) + ")";
130         System.out.println(action);
131         try {
132             request.query(action);
133         } catch (WaarpDatabaseNoConnectionException e) {
134             e.printStackTrace();
135             return;
136         } catch (WaarpDatabaseSqlException e) {
137             e.printStackTrace();
138             return;
139         } finally {
140             request.close();
141         }
142     }
143 
144     @Override
145     public void resetSequence(DbSession session, long newvalue)
146             throws WaarpDatabaseNoConnectionException {
147         String action = "UPDATE Sequences SET seq = " + newvalue +
148                 " WHERE name = '" + DbTransferLog.fieldseq + "'";
149         DbRequest request = new DbRequest(session);
150         try {
151             request.query(action);
152         } catch (WaarpDatabaseNoConnectionException e) {
153             e.printStackTrace();
154             return;
155         } catch (WaarpDatabaseSqlException e) {
156             e.printStackTrace();
157             return;
158         } finally {
159             request.close();
160         }
161         System.out.println(action);
162     }
163 
164     @Override
165     public synchronized long nextSequence(DbSession dbSession)
166             throws WaarpDatabaseNoConnectionException,
167             WaarpDatabaseSqlException, WaarpDatabaseNoDataException {
168         lock.lock();
169         try {
170             long result = DbConstant.ILLEGALVALUE;
171             String action = "SELECT seq FROM Sequences WHERE name = '" +
172                     DbTransferLog.fieldseq + "' FOR UPDATE";
173             DbPreparedStatement preparedStatement = new DbPreparedStatement(
174                     dbSession);
175             try {
176                 dbSession.getConn().setAutoCommit(false);
177             } catch (SQLException e1) {
178             }
179             try {
180                 preparedStatement.createPrepareStatement(action);
181                 // Limit the search
182                 preparedStatement.executeQuery();
183                 if (preparedStatement.getNext()) {
184                     try {
185                         result = preparedStatement.getResultSet().getLong(1);
186                     } catch (SQLException e) {
187                         throw new WaarpDatabaseSqlException(e);
188                     }
189                 } else {
190                     throw new WaarpDatabaseNoDataException(
191                             "No sequence found. Must be initialized first");
192                 }
193             } finally {
194                 preparedStatement.realClose();
195             }
196             action = "UPDATE Sequences SET seq = " + (result + 1) +
197                     " WHERE name = '" + DbTransferLog.fieldseq + "'";
198             try {
199                 preparedStatement.createPrepareStatement(action);
200                 // Limit the search
201                 preparedStatement.executeUpdate();
202             } finally {
203                 preparedStatement.realClose();
204             }
205             return result;
206         } finally {
207             try {
208                 dbSession.getConn().setAutoCommit(true);
209             } catch (SQLException e1) {
210             }
211             lock.unlock();
212         }
213     }
214 
215     @Override
216     public boolean upgradeDb(DbSession session, String version)
217             throws WaarpDatabaseNoConnectionException {
218         return true;
219     }
220 
221     @Override
222     public boolean needUpgradeDb(DbSession session, String version, boolean tryFix)
223             throws WaarpDatabaseNoConnectionException {
224         return false;
225     }
226 }