# MariaDbDialect.java

    /*
    ** Module   : MariaDbDialect.java
    ** Abstract : Concrete implementation of Dialect for a strict(er) MariaDb backend.
    **
    ** Copyright (c) 2022-2025, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------------Description---------------------------------------
    ** 001 OM  20220715 Created initial version with minimal support.
    **     IAS 20220816 Added getScriptRunner() method.
    **     OM  20220911 Fixed datetime(-tz) datatype to include the second fractions.
    **     IAS 20220913 Re-work processing of UDFs errors/warnings.
    **     OM  20220914 Use MAX-WIDTH to generate custom sized varchar columns.
    **     OM  20220922 Validate the parameters for sequence definitions.
    **     OM  20221012 Fixed regression (invalid sort criteria) in previous commit.
    **     OM  20221018 Extracted common code to MariaDbLenientDialect super class.
    **     OM  20230131 Code cleanup.
    ** 002 GBB 20230512 Logging methods replaced by CentralLogger/ConversionStatus.
    ** 003 DDF 20240205 Non null fields should not augment __null when creating an index.
    **     DDF 20240209 Mandatory column should not generate a computed column.
    **     DDF 20240625 Fix number of expressions in orderByNulls().
    */

    /*
    ** 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.dialect;

    import com.goldencode.p2j.util.logging.*;
    import java.util.*;

    /**
     * Extends abstract {@link Dialect} for an MariaDb/MySql backend.
     */
    public class MariaDbDialect
    extends MariaDbLenientDialect
    {
       /** The id of this dialect variant as a string. */
       public static final String DIALECT_ID = "mariadb";

       /** Logger */
       private static final CentralLogger LOG = CentralLogger.get(MariaDbDialect.class.getName());

       /**
        * Get dialect id.
        *
        * @return  the id of the dialect.
        */
       @Override
       public String id()
       {
          return MariaDbDialect.DIALECT_ID;
       }

       /**
        * Get a collection of reserved keywords specific to this dialect.
        * <p>
        * The {@link com.goldencode.p2j.convert.NameConverter} uses thes list to create an exclusion
        * list of names so there will not be schema collisions with identifiers (tables, fields) from
        * the generated schema.
        *
        * @return  A list with MariaDb reserved keywords.
        *
        * @see     <a href="https://msdn.microsoft.com/en-us/library/ms189822%28v=sql.110%29.aspx">
        *          https://msdn.microsoft.com/en-us/library/ms189822%28v=sql.110%29.aspx</a>
        */
       public static List<String> getReservedKeywords()
       {
          return MariaDbLenientDialect.getReservedKeywords();
       }

       /**
        * MariaDb requires injecting computed columns for indexed fields due to lack of "nulls last" feature.
        * There is a combinations of factors: the indexes cannot be defined on expressions, but we can create
        * 'generated' columns which can be used as index components.
        *
        * @return  {@code true}.
        */
       @Override
       public boolean injectComputedColumns()
       {
          return true;
       }

       /**
        * Writes the declaration of a computed column to a {@code StringBuilder} based on its name, type, scale,
        * and case-sensitiveness.
        * <p>
        * This dialect uses the computed columns for all columns which are components of an index in order to
        * implement 'nulls last' idiom. It writes the computed column as:
        *       {@code "<name>__null boolean generated always as (<name> is null) virtual"}
        * <p>
        * <b>Note:</b><br>
        * Improvement: if the field/column is mandatory, this augmentation is NOT necessary.
        *
        * @param   name
        *          The original column name.
        * @param   type
        *          The original column type.
        * @param   scale
        *          The optional scale, if needed.
        * @param   cs
        *          The case-sensitiveness.
        * @param   mandatory
        *          If the field is not-null.
        *
        * @return  The declaration of the computed column(s).
        */
       @Override
       public String getComputedColumnString(String name, String type, int scale, boolean cs, boolean mandatory)
       {
          if (mandatory)
          {
             return null;
          }

          return name + "__null boolean generated always as (" + name + " is null) virtual";
       }

       /**
        * Determine if the sort component or index needs to be augmented with a NULLS FIRST/LAST option and
        * add the required expression(s) as needed by this dialect.
        *
        * @param   sb
        *          The {@code StringBuilder} which contains the SQL statement to be built.
        * @param   expression
        *          The field name (or expression) which is the current criterion to be augmented.
        * @param   field
        *          The field name which is the base of the previous parameter (possible {@code null}).
        * @param   asc
        *          The sort direction.
        * @param   forceAsc
        *          If {@code true}, force the insertion of the default {@code asc} direction.
        * @param   mandatory
        *          {@code true} if the field is already non-null, in which case it is not
        *          augmented with __null, {@code false} otherwise.
        *
        * @return  the number of expressions which were added to the string builder.
        */
       @Override
       public int orderByNulls(StringBuilder sb,
                               String expression,
                               String field,
                               boolean asc,
                               boolean forceAsc,
                               boolean mandatory)
       {
          if (!mandatory)
          {
             sb.append(field != null ? field : expression).append("__null");
             if (!asc)
             {
                sb.append(" desc");
             }
             else if (forceAsc)
             {
                sb.append(" asc");
             }
             sb.append(",");

             return 1 + super.orderByNulls(sb, expression, null, asc, forceAsc, mandatory);
          }

          return super.orderByNulls(sb, expression, null, asc, forceAsc, mandatory);
       }

       /**
        * Get the logger specific to this MariaDb dialect variant.
        *
        * @return  the logger specific to this MariaDb dialect variant.
        */
       @Override
       protected CentralLogger getLog()
       {
          return LOG;
       }
    }