# MariaDbSequenceHandler.java

    /*
    ** Module   : MariaDbSequenceHandler.java
    ** Abstract : Static class that handles sequence functions and statements from Progress / OpenEdge 10.x.
    **
    ** Copyright (c) 2022-2025, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ----------------------------------------Description---------------------------------------
    ** 001 OM  20220817 Created initial version with basic support only.
    **     OM  20221013 Fixed the query for sequences lookup.
    **     CA  20221014 In MariaDB, the current value is private per connection -there is no state being preserved
    **                  at the database server for current value, it 'select previous value' always return NULL if
    **                  'next value' has never been called for this sequence, in this connection. For this reason,
    **                  current value is determined by getting the next value, subtracting one, and adjusting the
    **                  sequence value again.
    **     CA  20221019 When populating 'sequences' registry, 'safeContains' must use physical database name, and
    **                  not the ldbname.
    **     OM  20221025 Improve sequence compatibility with 4GL.
    ** 002 GBB 20230512 Logging methods replaced by CentralLogger/ConversionStatus.
    ** 003 OM  20230623 Sequences use separate SQL session/connection to avoid deadlock caused by MDEV-13713.
    ** 004 OM  20241128 Multi-tenant runtime support: selected the proper persistence context, eventually
    **                  based on [sharedDb] parameter.
    ** 005 OM  20250110 Added sequence support for multi-tenant databases.
    */

    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.persist.sequence;

    import com.goldencode.p2j.persist.*;
    import com.goldencode.p2j.persist.orm.*;
    import com.goldencode.p2j.util.ErrorManager;
    import com.goldencode.p2j.util.*;
    import com.goldencode.p2j.util.logging.*;
    import java.sql.*;
    import java.util.*;
    import java.util.logging.*;

    /**
     * MariaDb implementation of sequences using a native SQL sequence primitives queries. Each primitive tries to
     * execute exactly one database query (except NEXT-VALUE, where the output is checked to match the P4GL
     * jump-to INITIAL value instead of MIN-VALUE).
     */
    public class MariaDbSequenceHandler
    extends SequenceHandler
    {
       /** Central Logger. */
       private static final CentralLogger LOG = CentralLogger.get(MariaDbSequenceHandler.class);

       /** The list of sequences grouped by databases. It uses lazy initialization. */
       private HashMap<String, Set<String>> sequences = null;

       /**
        * The only constructor takes the name of the database as argument. It is passed on to super class.
        *
        * @param   ldbName
        *          The database logical name on which this handler will work with.
        */
       public MariaDbSequenceHandler(String ldbName)
       {
          super(ldbName);
       }

       /**
        * Queries the current value of a sequence.
        * <p>
        * This method should be called only in a safe environment, where the database-level lock for
        * access to sequences has already been acquired.
        *
        * @param   seqName
        *          The name of the sequence.
        * @param   ldbName
        *          The database to which the sequence belongs to. It is assumed it is not null and valid.
        * @param   tenantId
        *          The tenant id. If present (not null) it is checked. If not it is computed as
        *          {@code tenant-id(ldbName}).
        *
        * @return  the current value of the sequence
        */
       @Override
       protected int64 safeGetCurrentValue(String seqName, String ldbName, Long tenantId)
       {
          Sequence seq = SequenceManager.getSequence(ldbName, seqName);
          if (seq == null)
          {
             ErrorManager.recordOrThrowError(2913, "Unknown sequence expression " + seqName.toUpperCase());
             return new int64();
          }

          // in MariaDB, sequence 'previous value' is kept per connection. It is also unreliable because, as
          // the name suggests it is the last value returned. If the sequence was reset the value is incorrect

          Persistence persistence = ConnectionManager.getPersistence(ldbName);
          if (tenantId != null)
          {
             int crtTenant = persistence.getContext(Persistence.PRIVATE_CTX).getTenantId();
             if (crtTenant != tenantId)
             {
                // NOTE: super-tenants are not implemented yet so we raise this unconditionally, for the moment:
                ErrorManager.recordOrThrowError(15956);
                // Only super-tenants may use a tenant-id other than their own in a sequence function. (15956)
                return new int64();
             }
          }
          String getNextValQuery = persistence.getDialect().getSequenceNextValString(seqName);

          // We need to get the NEXT VALUE and then set the current value to prevValue - increment
          int64 nextVal = getResultValue(executeSQL(getNextValQuery,
                                                    persistence.getDatabase(!seq.isMultiTenant()), true));

          if (nextVal.isUnknown())
          {
             // that failed too ?
             return new int64();
          }

          long currentValue = nextVal.longValue() - seq.getIncrement();
          safeSetValue(seqName, ldbName, currentValue, tenantId);

          return new int64(currentValue);
       }

       /**
        * Computes and returns the next value of a sequence.
        * <p>
        * This method should be called only in a safe environment, where the database-level lock for
        * access to sequences has already been acquired.
        *
        * @param   seqName
        *          The name of the sequence.
        * @param   ldbName
        *          The database to which the sequence belongs to. It is assumed it is not {@code null} and valid.
        * @param   tenantId
        *          The tenant id. If present (not null) it is checked. If not it is computed as
        *          {@code tenant-id(ldbName}).
        *
        * @return  the next value of the sequence or unknown value (?) if sequence/database not found
        *          or the sequence is not cycling and has passed the maximum/minimum value.
        */
       @Override
       protected int64 safeGetNextValue(String seqName, String ldbName, Long tenantId)
       {
          Sequence seq = SequenceManager.getSequence(ldbName, seqName);
          if (seq == null)
          {
             ErrorManager.recordOrThrowError(2913, "Unknown sequence expression " + seqName.toUpperCase());
             return new int64();
          }

          Persistence persistence = ConnectionManager.getPersistence(ldbName);
          if (tenantId != null)
          {
             int crtTenant = persistence.getContext(Persistence.PRIVATE_CTX).getTenantId();
             if (crtTenant != tenantId)
             {
                // NOTE: super-tenants are not implemented yet so we raise this unconditionally, for the moment:
                ErrorManager.recordOrThrowError(15956);
                // Only super-tenants may use a tenant-id other than their own in a sequence function. (15956)
                return new int64();
             }
          }
          String getNextValQuery = persistence.getDialect().getSequenceNextValString(seqName);
          int64 resultValue = getResultValue(executeSQL(getNextValQuery,
                                                        persistence.getDatabase(!seq.isMultiTenant()), true));

          // MariaDb is different from Progress when cycling:
          //    it will make the counter jump from MAX_VALUE to MIN_VALUE (and the reverse)
          //    instead, 4GL will jump back to INITIAL_VALUE
          if (seq.isCycle())
          {
             // in a cycling sequence resultValue is never null/unknown
             long crtVal = resultValue.longValue();

             // did the cycle jump occurred?
             if (seq.getIncrement() > 0 && crtVal == seq.getMin())
             {
                // if this is a nop, skip it
                long ret = seq.getInitial();
                if (crtVal != ret)
                {
                   safeSetValue(seqName, ldbName, ret, tenantId);
                   return new int64(ret);
                }
             }
          }

          return resultValue;
       }

       /**
        * Initialize (reset) the current value of a sequence.
        * <p>
        * This method should be called only in a safe environment, where the database-level lock for
        * access to sequences has already been acquired.
        *
        * @param   seqName
        *          The name of the sequence.
        * @param   ldbName
        *          The database to which the sequence belongs to.
        *          It is assumed it is not null and valid.
        * @param   newVal
        *          The value the sequence will be reset to.
        * @param   tenantId
        *          The tenant id. If present (not null) it is checked. If not it is computed as
        *          {@code tenant-id(ldbName}).
        */
       @Override
       protected void safeSetValue(String seqName, String ldbName, long newVal, Long tenantId)
       {
          Sequence seq = SequenceManager.getSequence(ldbName, seqName);
          if (seq == null)
          {
             ErrorManager.recordOrThrowError(3133,
                   "SYSTEM ERROR: Sequence " + seqName.toUpperCase() + " not found");
             return;
          }
          if (seq.getMin() > newVal || newVal > seq.getMax())
          {
             ErrorManager.recordOrThrowError(3132,
                   "Cannot set sequence " + seq.getLegacyName().toUpperCase() + " beyond its max/min values");
             return;
          }

          Persistence persistence = ConnectionManager.getPersistence(ldbName);
          if (tenantId != null)
          {
             int crtTenant = persistence.getContext(Persistence.PRIVATE_CTX).getTenantId();
             if (crtTenant != tenantId)
             {
                // NOTE: super-tenants are not implemented yet so we raise this unconditionally, for the moment:
                ErrorManager.recordOrThrowError(15956);
                // Only super-tenants may use a tenant-id other than their own in a sequence function. (15956)
                return;
             }
          }

          Database db = persistence.getDatabase(!seq.isMultiTenant());
          executeSQL("ALTER SEQUENCE " + seqName + " RESTART WITH ?", db, false, newVal); // expect [retVal] to be 0.
          executeSQL("SELECT SETVAL(" + seqName + ", ?, TRUE)", db, true, newVal);
       }

       /**
        * Test whether the database contains a sequence. This is useful to identify the correct
        * database/sequences when a 4GL static function / statement is called without the database
        * parameter.
        * <p>
        * This method should be called only in a safe environment, where the database-level lock for
        * access to sequences has already been acquired.
        *
        * @param   seqName
        *          The name of the sequence.
        * @param   ldbName
        *          The database to which the sequence belongs to. If is null then the first connected
        *          database is assumed.
        *
        * @return  <code>true</code> if the sequence is defined in the database
        */
       @Override
       protected boolean safeContains(String seqName, String ldbName)
       {
          // lazy initialization
          if (sequences == null)
          {
             sequences = new HashMap<>();
          }

          // make sure the structure is initialized for this database:
          Set<String> crtSeqList = sequences.get(ldbName);
          if (crtSeqList == null)
          {
             // create the list
             crtSeqList = new HashSet<>();
             sequences.put(ldbName, crtSeqList);

             Persistence persistence = ConnectionManager.getPersistence(ldbName);
             try
             {
                String catalog = persistence.getSession(Persistence.META_CTX).getConnection().getCatalog();

                ScrollableResults<String> rs = persistence.executeSQLQuery(
                      "SELECT table_name FROM information_schema.tables " +
                      "WHERE table_type = 'SEQUENCE' AND table_schema = ?",
                      Persistence.META_CTX,
                      new Object[] { catalog });
                while (rs.next())
                {
                   crtSeqList.add(rs.get(0, String.class));
                }
             }
             catch (PersistenceException | SQLException pe)
             {
                if (LOG.isLoggable(Level.WARNING))
                {
                   LOG.log(Level.WARNING,
                           "Failed to get sequence list from " + ldbName + ": " + pe.getMessage());
                }
                return false;
             }
          }

          // now check whether the sequence is in database
          return crtSeqList.contains(seqName);
       }

       /**
        * Execute a SQL statement and optionally return the result as a single value.
        *
        * @param   sqlStmt
        *          The SQL statement to be executed.
        * @param   db
        *          The {@code Database} on which the statement will be executed.
        * @param   select
        *          Flags a SELECT statement. The statement will be executed as a query and the first result will
        *          be returned as unique value.
        * @param   params
        *          An optional set of parameters for the statement.
        *
        * @return  In case of a SELECT statement the value is the unique (first) column in first row, or
        *          {@code null} is the query provided an empty result set.
        *          In case of not SELECT statements, the result is always {@code null}.
        */
       private static Object executeSQL(String sqlStmt, Database db, boolean select, Object... params)
       {
          try (Session s = new Session(db))
          {
             PreparedStatement preparedStatement = s.getConnection().prepareStatement(sqlStmt);
             for (int i = 0; i < params.length; i++)
             {
                preparedStatement.setObject(i + 1, params[i]);
             }

             if (select)
             {
                SQLExecutor.FunctionWithException<PreparedStatement, ResultSet> f = PreparedStatement::executeQuery;
                ResultSet rs = SQLExecutor.getInstance().execute(db, sqlStmt, f, preparedStatement);

                return rs.next() ? rs.getObject(1) : null;
             }
             else
             {
                SQLExecutor.FunctionWithException<PreparedStatement, Integer> f = PreparedStatement::executeUpdate;
                SQLExecutor.getInstance().execute(db, sqlStmt, f, preparedStatement);

                return null; // to be ignored
             }
          }
          catch (SQLException | PersistenceException e)
          {
             return null;
          }
       }
    }