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.DbModelOracle;
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   * Oracle Database Model implementation
33   */
34  public class DbModelOracleKernel extends DbModelOracle {
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 DbModelOracleKernel(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 ";
60      final String constraint = " CONSTRAINT ";
61      final String primaryKey = " PRIMARY KEY ";
62      final String notNull = " NOT NULL ";
63  
64      final DbRequest request = new DbRequest(session);
65      // TRANSLOG
66      StringBuilder action =
67          new StringBuilder(createTableH2 + DbTransferLog.table + '(');
68      final DbTransferLog.Columns[] acolumns = DbTransferLog.Columns.values();
69      for (int i = 0; i < acolumns.length; i++) {
70        action.append(acolumns[i].name())
71              .append(DBType.getType(DbTransferLog.dbTypes[i])).append(notNull)
72              .append(", ");
73      }
74      // Several columns for primary key
75      action.append(constraint + " TRANSLOG_PK " + primaryKey + '(');
76      for (int i = DbTransferLog.NBPRKEY; i > 1; i--) {
77        action.append(acolumns[acolumns.length - i].name()).append(',');
78      }
79      action.append(acolumns[acolumns.length - 1].name()).append("))");
80      SysErrLogger.FAKE_LOGGER.sysout(action);
81      try {
82        request.query(action.toString());
83      } catch (final WaarpDatabaseNoConnectionException e) {
84        SysErrLogger.FAKE_LOGGER.ignoreLog(e);
85        return;
86      } catch (final WaarpDatabaseSqlException e) {
87        SysErrLogger.FAKE_LOGGER.ignoreLog(e);
88        // XXX FIX No return
89      } finally {
90        request.close();
91      }
92      // Index TRANSLOG
93      action = new StringBuilder(
94          "CREATE INDEX IDX_TRANSLOG ON " + DbTransferLog.table + '(');
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 = new StringBuilder("DROP SEQUENCE " + DbTransferLog.fieldseq);
115     SysErrLogger.FAKE_LOGGER.sysout(action);
116     try {
117       request.query(action.toString());
118     } catch (final WaarpDatabaseNoConnectionException e) {
119       SysErrLogger.FAKE_LOGGER.ignoreLog(e);
120     } catch (final WaarpDatabaseSqlException e) {
121       SysErrLogger.FAKE_LOGGER.ignoreLog(e);
122     } finally {
123       request.close();
124     }
125   }
126 
127   @Override
128   public final void resetSequence(final DbSession session, final long newvalue)
129       throws WaarpDatabaseNoConnectionException {
130     // Nothing
131   }
132 
133   @Override
134   public final long nextSequence(final DbSession dbSession) {
135     return LongUuid.getLongUuid();
136   }
137 
138   @Override
139   public final boolean upgradeDb(final DbSession session,
140                                  final String version) {
141     return true;
142   }
143 
144   @Override
145   public final boolean needUpgradeDb(final DbSession session,
146                                      final String version,
147                                      final boolean tryFix) {
148     return false;
149   }
150 }