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.openr66.database.model;
21  
22  import org.waarp.common.database.DbRequest;
23  import org.waarp.common.database.DbSession;
24  import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
25  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
26  import org.waarp.common.database.model.DbModelH2;
27  import org.waarp.common.logging.SysErrLogger;
28  import org.waarp.openr66.database.data.DbHostAuth;
29  import org.waarp.openr66.database.data.DbHostConfiguration;
30  import org.waarp.openr66.database.data.DbTaskRunner;
31  import org.waarp.openr66.protocol.configuration.Configuration;
32  import org.waarp.openr66.protocol.configuration.PartnerConfiguration;
33  import org.waarp.openr66.protocol.utils.R66Versions;
34  
35  import static org.waarp.common.database.DbConstant.*;
36  
37  /**
38   * H2 Database Model implementation
39   */
40  public class DbModelH2R66 extends DbModelH2 {
41    /**
42     * Create the object and initialize if necessary the driver
43     *
44     * @param dbserver
45     * @param dbuser
46     * @param dbpasswd
47     *
48     * @throws WaarpDatabaseNoConnectionException
49     */
50    public DbModelH2R66(final String dbserver, final String dbuser,
51                        final String dbpasswd)
52        throws WaarpDatabaseNoConnectionException {
53      super(dbserver, dbuser, dbpasswd);
54    }
55  
56    @Override
57    public final void createTables(final DbSession session)
58        throws WaarpDatabaseNoConnectionException {
59      // Create tables: configuration, hosts, rules, runner, cptrunner
60      final String createTableH2 = "CREATE TABLE IF NOT EXISTS ";
61      final String primaryKey = " PRIMARY KEY ";
62      final String notNull = " NOT NULL ";
63      final DbRequest request =
64          DbModelFactoryR66.subCreateTableMariaDbMySQLH2PostgreSQL(dbTypeResolver,
65                                                                   session,
66                                                                   createTableH2,
67                                                                   primaryKey,
68                                                                   notNull);
69      if (request == null) {
70        return;
71      }
72      StringBuilder action;
73  
74      // cptrunner
75      final long minimalValue = System.currentTimeMillis() + 1;
76      action = new StringBuilder(
77          "CREATE SEQUENCE IF NOT EXISTS " + DbTaskRunner.fieldseq +
78          " START WITH " + minimalValue + " MINVALUE " + (ILLEGALVALUE + 1));
79      SysErrLogger.FAKE_LOGGER.sysout(action);
80      try {
81        request.query(action.toString());
82      } catch (final WaarpDatabaseNoConnectionException e) {
83        SysErrLogger.FAKE_LOGGER.ignoreLog(e);
84        return;
85      } catch (final WaarpDatabaseSqlException e) {
86        // version <= 1.2.173
87        action = new StringBuilder(
88            "CREATE SEQUENCE IF NOT EXISTS " + DbTaskRunner.fieldseq +
89            " START WITH " + minimalValue);
90        SysErrLogger.FAKE_LOGGER.sysout(action);
91        try {
92          request.query(action.toString());
93        } catch (final WaarpDatabaseNoConnectionException e2) {
94          SysErrLogger.FAKE_LOGGER.ignoreLog(e2);
95          return;
96        } catch (final WaarpDatabaseSqlException e2) {
97          SysErrLogger.FAKE_LOGGER.ignoreLog(e2);
98          // XXX FIX no return
99        }
100       // XXX FIX no return
101     } finally {
102       request.close();
103     }
104 
105     DbHostConfiguration.updateVersionDb(Configuration.configuration.getHostId(),
106                                         R66Versions.V3_1_0.getVersion());
107   }
108 
109   @Override
110   public final boolean upgradeDb(final DbSession session, final String version)
111       throws WaarpDatabaseNoConnectionException {
112     if (PartnerConfiguration.isVersion2GEQVersion1(
113         R66Versions.V3_1_0.getVersion(), version)) {
114       return true;
115     }
116     if (PartnerConfiguration.isVersion2GEQVersion1(version,
117                                                    R66Versions.V2_4_13.getVersion())) {
118       SysErrLogger.FAKE_LOGGER.sysout(
119           version + " to " + R66Versions.V2_4_13.getVersion() + "? " + true);
120       final String createTableH2 = "CREATE TABLE IF NOT EXISTS ";
121       final String primaryKey = " PRIMARY KEY ";
122       final String notNull = " NOT NULL ";
123 
124       // HostConfiguration
125       final StringBuilder action =
126           new StringBuilder(createTableH2 + DbHostConfiguration.table + '(');
127       final DbHostConfiguration.Columns[] chcolumns =
128           DbHostConfiguration.Columns.values();
129       for (int i = 0; i < chcolumns.length - 1; i++) {
130         action.append(chcolumns[i].name())
131               .append(DBType.getType(DbHostConfiguration.dbTypes[i]))
132               .append(notNull).append(", ");
133       }
134       action.append(chcolumns[chcolumns.length - 1].name()).append(
135                 DBType.getType(DbHostConfiguration.dbTypes[chcolumns.length - 1]))
136             .append(primaryKey).append(')');
137       SysErrLogger.FAKE_LOGGER.sysout(action);
138       final DbRequest request = new DbRequest(session);
139       try {
140         request.query(action.toString());
141       } catch (final WaarpDatabaseSqlException e) {
142         SysErrLogger.FAKE_LOGGER.ignoreLog(e);
143         return false;
144       } finally {
145         request.close();
146       }
147     }
148     if (PartnerConfiguration.isVersion2GEQVersion1(version,
149                                                    R66Versions.V2_4_17.getVersion())) {
150       SysErrLogger.FAKE_LOGGER.sysout(
151           version + " to " + R66Versions.V2_4_17.getVersion() + "? " + true);
152       final String command =
153           "ALTER TABLE " + DbTaskRunner.table + " ADD COLUMN IF NOT EXISTS " +
154           DbTaskRunner.Columns.TRANSFERINFO.name() + ' ' + DBType.getType(
155               DbTaskRunner.dbTypes[DbTaskRunner.Columns.TRANSFERINFO.ordinal()]) +
156           " NOT NULL DEFAULT '{}' " + " AFTER " +
157           DbTaskRunner.Columns.FILEINFO.name();
158       final DbRequest request = new DbRequest(session);
159       try {
160         SysErrLogger.FAKE_LOGGER.sysout("Command: " + command);
161         request.query(command);
162       } catch (final WaarpDatabaseSqlException e) {
163         SysErrLogger.FAKE_LOGGER.ignoreLog(e);
164         return false;
165       } finally {
166         request.close();
167       }
168     }
169     if (PartnerConfiguration.isVersion2GEQVersion1(version,
170                                                    R66Versions.V2_4_23.getVersion())) {
171       SysErrLogger.FAKE_LOGGER.sysout(
172           version + " to " + R66Versions.V2_4_23.getVersion() + "? " + true);
173       String command =
174           "ALTER TABLE " + DbHostAuth.table + " ADD COLUMN IF NOT EXISTS " +
175           DbHostAuth.Columns.ISACTIVE.name() + ' ' + DBType.getType(
176               DbHostAuth.dbTypes[DbHostAuth.Columns.ISACTIVE.ordinal()]) +
177           " NOT NULL DEFAULT " + true + " AFTER " +
178           DbHostAuth.Columns.ISCLIENT.name();
179       DbRequest request = new DbRequest(session);
180       try {
181         SysErrLogger.FAKE_LOGGER.sysout("Command: " + command);
182         request.query(command);
183       } catch (final WaarpDatabaseSqlException e) {
184         SysErrLogger.FAKE_LOGGER.ignoreLog(e);
185         return false;
186       } finally {
187         request.close();
188       }
189       command =
190           "ALTER TABLE " + DbHostAuth.table + " ADD COLUMN IF NOT EXISTS " +
191           DbHostAuth.Columns.ISPROXIFIED.name() + ' ' + DBType.getType(
192               DbHostAuth.dbTypes[DbHostAuth.Columns.ISPROXIFIED.ordinal()]) +
193           " NOT NULL DEFAULT " + false + " AFTER " +
194           DbHostAuth.Columns.ISACTIVE.name();
195       request = new DbRequest(session);
196       try {
197         SysErrLogger.FAKE_LOGGER.sysout("Command: " + command);
198         request.query(command);
199       } catch (final WaarpDatabaseSqlException e) {
200         SysErrLogger.FAKE_LOGGER.ignoreLog(e);
201         return false;
202       } finally {
203         request.close();
204       }
205     }
206     if (PartnerConfiguration.isVersion2GTVersion1(version,
207                                                   R66Versions.V2_4_25.getVersion())) {
208       SysErrLogger.FAKE_LOGGER.sysout(
209           version + " to " + R66Versions.V2_4_25.getVersion() + "? " + true);
210       String command = "ALTER TABLE " + DbTaskRunner.table + " ALTER COLUMN " +
211                        DbTaskRunner.Columns.FILENAME.name() + ' ' +
212                        DBType.getType(
213                            DbTaskRunner.dbTypes[DbTaskRunner.Columns.FILENAME.ordinal()]) +
214                        " NOT NULL ";
215       DbRequest request = new DbRequest(session);
216       try {
217         SysErrLogger.FAKE_LOGGER.sysout("Command: " + command);
218         request.query(command);
219       } catch (final WaarpDatabaseSqlException e) {
220         SysErrLogger.FAKE_LOGGER.ignoreLog(e);
221         return false;
222       } finally {
223         request.close();
224       }
225       command = "ALTER TABLE " + DbTaskRunner.table + " ALTER COLUMN " +
226                 DbTaskRunner.Columns.ORIGINALNAME.name() + ' ' + DBType.getType(
227           DbTaskRunner.dbTypes[DbTaskRunner.Columns.ORIGINALNAME.ordinal()]) +
228                 " NOT NULL ";
229       request = new DbRequest(session);
230       try {
231         SysErrLogger.FAKE_LOGGER.sysout("Command: " + command);
232         request.query(command);
233       } catch (final WaarpDatabaseSqlException e) {
234         SysErrLogger.FAKE_LOGGER.ignoreLog(e);
235         return false;
236       } finally {
237         request.close();
238       }
239     }
240     if (PartnerConfiguration.isVersion2GTVersion1(version,
241                                                   R66Versions.V3_0_4.getVersion())) {
242       SysErrLogger.FAKE_LOGGER.sysout(
243           version + " to " + R66Versions.V3_0_4.getVersion() + "? " + true);
244       final DbRequest request = new DbRequest(session);
245       // Change Type for all Tables
246       DbModelFactoryR66.upgradeTable30(dbTypeResolver, request,
247                                        " ALTER COLUMN ", " ", " NOT NULL ");
248       try {
249         final String command = "DROP INDEX IF EXISTS IDX_RUNNER ";
250         try {
251           SysErrLogger.FAKE_LOGGER.sysout("Command: " + command);
252           request.query(command);
253         } catch (final WaarpDatabaseNoConnectionException e) {
254           SysErrLogger.FAKE_LOGGER.ignoreLog(e);
255           return false;
256         } catch (final WaarpDatabaseSqlException e) {
257           SysErrLogger.FAKE_LOGGER.ignoreLog(e);
258           // XXX FIX no return
259         }
260         DbModelFactoryR66.createIndex30(dbTypeResolver, request);
261       } finally {
262         request.close();
263       }
264     }
265     DbHostConfiguration.updateVersionDb(Configuration.configuration.getHostId(),
266                                         R66Versions.V3_1_0.getVersion());
267     return true;
268   }
269 
270   @Override
271   public final boolean needUpgradeDb(final DbSession session,
272                                      final String version, final boolean tryFix)
273       throws WaarpDatabaseNoConnectionException {
274     return DbModelFactoryR66.needUpgradeDbAllDb(dbTypeResolver, session,
275                                                 version);
276   }
277 
278 }