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.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.guid.LongUuid;
28  import org.waarp.common.logging.SysErrLogger;
29  import org.waarp.gateway.kernel.database.data.DbTransferLog;
30  
31  /**
32   * H2 Database Model implementation
33   */
34  public class DbModelH2Kernel extends DbModelH2 {
35    /**
36     * Create the object and initialize if necessary the driver
37     *
38     * @param dbserver
39     * @param dbuser
40     * @param dbpasswd
41     *
42     * @throws WaarpDatabaseNoConnectionException
43     */
44    public DbModelH2Kernel(final String dbserver, final String dbuser,
45                           final String dbpasswd)
46        throws WaarpDatabaseNoConnectionException {
47      super(dbserver, dbuser, dbpasswd);
48    }
49  
50    @Override
51    public final void createTables(final DbSession session)
52        throws WaarpDatabaseNoConnectionException {
53      createTableMonitoring(session);
54    }
55  
56    public static void createTableMonitoring(final DbSession session)
57        throws WaarpDatabaseNoConnectionException {
58      // Create tables: logs
59      final String createTableH2 = "CREATE TABLE IF NOT EXISTS ";
60      final String primaryKey = " PRIMARY KEY ";
61      final String notNull = " NOT NULL ";
62  
63      // log
64      StringBuilder action =
65          new StringBuilder(createTableH2 + DbTransferLog.table + '(');
66      final DbTransferLog.Columns[] acolumns = DbTransferLog.Columns.values();
67      for (int i = 0; i < acolumns.length; i++) {
68        action.append(acolumns[i].name())
69              .append(DBType.getType(DbTransferLog.dbTypes[i])).append(notNull)
70              .append(", ");
71      }
72      // Several columns for primary key
73      action.append(" CONSTRAINT TRANSLOG_PK " + primaryKey + '(');
74      for (int i = DbTransferLog.NBPRKEY; i > 1; i--) {
75        action.append(acolumns[acolumns.length - i].name()).append(',');
76      }
77      action.append(acolumns[acolumns.length - 1].name()).append("))");
78      SysErrLogger.FAKE_LOGGER.sysout(action);
79      final DbRequest request = new DbRequest(session);
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        SysErrLogger.FAKE_LOGGER.ignoreLog(e);
87        // XXX FIX No return
88      } finally {
89        request.close();
90      }
91      // Index Runner
92      action = new StringBuilder(
93          "CREATE INDEX IF NOT EXISTS IDX_TRANSLOG ON " + DbTransferLog.table +
94          "(");
95      final DbTransferLog.Columns[] icolumns = DbTransferLog.indexes;
96      for (int i = 0; i < icolumns.length - 1; i++) {
97        action.append(icolumns[i].name()).append(", ");
98      }
99      action.append(icolumns[icolumns.length - 1].name()).append(')');
100     SysErrLogger.FAKE_LOGGER.sysout(action);
101     try {
102       request.query(action.toString());
103     } catch (final WaarpDatabaseNoConnectionException e) {
104       SysErrLogger.FAKE_LOGGER.ignoreLog(e);
105       return;
106     } catch (final WaarpDatabaseSqlException e) {
107       SysErrLogger.FAKE_LOGGER.ignoreLog(e);
108       // XXX FIX No return
109     } finally {
110       request.close();
111     }
112 
113     // cptrunner
114     action =
115         new StringBuilder("DROP SEQUENCE IF EXISTS " + DbTransferLog.fieldseq);
116     SysErrLogger.FAKE_LOGGER.sysout(action);
117     try {
118       request.query(action.toString());
119     } catch (final WaarpDatabaseNoConnectionException e) {
120       SysErrLogger.FAKE_LOGGER.ignoreLog(e);
121     } catch (final WaarpDatabaseSqlException e) {
122       SysErrLogger.FAKE_LOGGER.ignoreLog(e);
123     } finally {
124       request.close();
125     }
126   }
127 
128   @Override
129   public final void resetSequence(final DbSession session,
130                                   final long newvalue) {
131     // Nothing since LongUuid
132   }
133 
134   @Override
135   public final long nextSequence(final DbSession dbSession) {
136     return LongUuid.getLongUuid();
137   }
138 
139   @Override
140   public final boolean upgradeDb(final DbSession session,
141                                  final String version) {
142     return true;
143   }
144 
145   @Override
146   public final boolean needUpgradeDb(final DbSession session,
147                                      final String version,
148                                      final boolean tryFix) {
149     return false;
150   }
151 
152 }