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