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