View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, 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 Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.gateway.kernel.database.model;
21  
22  import org.waarp.common.database.DbAdmin;
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.common.database.model.DbModel;
30  import org.waarp.common.database.model.DbModelFactory;
31  import org.waarp.common.database.model.DbType;
32  import org.waarp.common.logging.SysErrLogger;
33  import org.waarp.gateway.kernel.database.DbConstantGateway;
34  import org.waarp.gateway.kernel.database.data.DbTransferLog;
35  
36  import java.sql.SQLException;
37  
38  /**
39   * Factory to store the Database Model object
40   */
41  public class DbModelFactoryGateway extends DbModelFactory {
42  
43    /**
44     * Initialize the Database Model according to arguments.
45     *
46     * @param dbdriver
47     * @param dbserver
48     * @param dbuser
49     * @param dbpasswd
50     * @param write
51     *
52     * @throws WaarpDatabaseNoConnectionException
53     */
54    public static DbAdmin initialize(final String dbdriver, final String dbserver,
55                                     final String dbuser, final String dbpasswd,
56                                     final boolean write)
57        throws WaarpDatabaseNoConnectionException {
58      final DbType type = DbType.getFromDriver(dbdriver);
59      final DbModel dbModel;
60      switch (type) {
61        case H2:
62          dbModel = new DbModelH2Kernel(dbserver, dbuser, dbpasswd);
63          break;
64        case Oracle:
65          dbModel = new DbModelOracleKernel(dbserver, dbuser, dbpasswd);
66          break;
67        case PostGreSQL:
68          dbModel = new DbModelPostgresqlKernel();
69          break;
70        case MySQL:
71          dbModel = new DbModelMysqlKernel(dbserver, dbuser, dbpasswd);
72          break;
73        case MariaDB:
74          dbModel = new DbModelMariaDbKernel(dbserver, dbuser, dbpasswd);
75          break;
76        default:
77          throw new WaarpDatabaseNoConnectionException(
78              "TypeDriver unknown: " + type);
79      }
80      return new DbAdmin(dbModel, dbserver, dbuser, dbpasswd, write);
81    }
82  
83    public static void resetSequenceMonitoring(final DbSession session,
84                                               final long newvalue)
85        throws WaarpDatabaseNoConnectionException {
86      final String action =
87          "ALTER SEQUENCE " + DbTransferLog.fieldseq + " MINVALUE " +
88          (DbConstantGateway.ILLEGALVALUE + 1) + " RESTART WITH " + newvalue;
89      final DbRequest request = new DbRequest(session);
90      try {
91        request.query(action);
92      } catch (final WaarpDatabaseNoConnectionException e) {
93        SysErrLogger.FAKE_LOGGER.ignoreLog(e);
94        return;
95      } catch (final WaarpDatabaseSqlException e) {
96        SysErrLogger.FAKE_LOGGER.ignoreLog(e);
97        return;
98      } finally {
99        request.close();
100     }
101     SysErrLogger.FAKE_LOGGER.sysout(action);
102   }
103 
104   public static long nextSequenceMonitoring(final DbSession dbSession)
105       throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException,
106              WaarpDatabaseNoDataException {
107     long result;
108     final String action = "SELECT NEXTVAL('" + DbTransferLog.fieldseq + "')";
109     final DbPreparedStatement preparedStatement =
110         new DbPreparedStatement(dbSession);
111     try {
112       preparedStatement.createPrepareStatement(action);
113       // Limit the search
114       preparedStatement.executeQuery();
115       if (preparedStatement.getNext()) {
116         try {
117           result = preparedStatement.getResultSet().getLong(1);
118         } catch (final SQLException e) {
119           throw new WaarpDatabaseSqlException(e);
120         }
121         return result;
122       } else {
123         throw new WaarpDatabaseNoDataException(
124             "No sequence found. Must be initialized first");
125       }
126     } finally {
127       preparedStatement.realClose();
128     }
129   }
130 }