MyBatis3FormattingUtilities.java

/*
 *    Copyright 2006-2026 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.runtime.mybatis3;

import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;

import org.jspecify.annotations.Nullable;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;

public class MyBatis3FormattingUtilities {

    private MyBatis3FormattingUtilities() {
        super();
    }

    public static final FullyQualifiedJavaType BUILDER_IMPORT =
            new FullyQualifiedJavaType("org.apache.ibatis.jdbc.SQL"); //$NON-NLS-1$

    public static String getParameterClause(IntrospectedColumn introspectedColumn) {
        return getParameterClause(introspectedColumn, null);
    }

    public static String getParameterClause(IntrospectedColumn introspectedColumn, @Nullable String prefix) {
        StringBuilder sb = new StringBuilder();

        sb.append("#{"); //$NON-NLS-1$
        sb.append(introspectedColumn.getJavaProperty(prefix));
        sb.append(",jdbcType="); //$NON-NLS-1$
        sb.append(introspectedColumn.getJdbcTypeName());

        introspectedColumn.getTypeHandler().ifPresent(th -> sb.append(",typeHandler=").append(th)); //$NON-NLS-1$

        sb.append('}');

        return sb.toString();
    }

    /**
     * The phrase to use in a select list. If there is a table alias, the value will be "alias.columnName as
     * alias_columnName"
     *
     * @param introspectedColumn
     *            the introspected column
     *
     * @return the proper phrase
     */
    public static String getSelectListPhrase(IntrospectedColumn introspectedColumn) {
        return introspectedColumn.getTableAlias().map(a -> {
            StringBuilder sb = new StringBuilder();

            sb.append(getAliasedEscapedColumnName(introspectedColumn));
            sb.append(" as "); //$NON-NLS-1$
            if (introspectedColumn.isColumnNameDelimited()) {
                sb.append(introspectedColumn.getContext().getBeginningDelimiter());
            }
            sb.append(a);
            sb.append('_');
            sb.append(introspectedColumn.getActualColumnName());
            if (introspectedColumn.isColumnNameDelimited()) {
                sb.append(introspectedColumn.getContext().getEndingDelimiter());
            }
            return sb.toString();
        }).orElseGet(() -> getEscapedColumnName(introspectedColumn));
    }

    public static String getEscapedColumnName(IntrospectedColumn introspectedColumn) {
        StringBuilder sb = new StringBuilder();
        sb.append(introspectedColumn.getActualColumnName());

        if (introspectedColumn.isColumnNameDelimited()) {
            sb.insert(0, introspectedColumn.getContext().getBeginningDelimiter());
            sb.append(introspectedColumn.getContext().getEndingDelimiter());
        }

        return sb.toString();
    }

    public static String getAliasedEscapedColumnName(IntrospectedColumn introspectedColumn) {
        return introspectedColumn.getTableAlias().map(a -> a + '.' + getEscapedColumnName(introspectedColumn))
                .orElseGet(() -> getEscapedColumnName(introspectedColumn));
    }

    /**
     * The aliased column name for a select statement generated by the example clauses. This is not appropriate for
     * selects in SqlMaps because the column is not escaped for MyBatis. If there is a table alias, the value will be
     * alias.columnName.
     *
     * <p>This method is used in the Example classes and the returned value will be in a Java string. So we need to
     * escape double quotes if they are the delimiters.
     *
     * @param introspectedColumn
     *            the introspected column
     *
     * @return the aliased column name
     */
    public static String getAliasedActualColumnName(IntrospectedColumn introspectedColumn) {
        StringBuilder sb = new StringBuilder();
        introspectedColumn.getTableAlias().ifPresent(a -> sb.append(a).append('.'));

        if (introspectedColumn.isColumnNameDelimited()) {
            sb.append(escapeStringForJava(introspectedColumn.getContext().getBeginningDelimiter()));
        }

        sb.append(introspectedColumn.getActualColumnName());

        if (introspectedColumn.isColumnNameDelimited()) {
            sb.append(escapeStringForJava(introspectedColumn.getContext().getEndingDelimiter()));
        }

        return sb.toString();
    }

    /**
     * The renamed column name for a select statement. If there is a table alias, the value will be alias_columnName.
     * This is appropriate for use in a result map.
     *
     * @param introspectedColumn
     *            the introspected column
     *
     * @return the renamed column name
     */
    public static String getRenamedColumnNameForResultMap(IntrospectedColumn introspectedColumn) {
        return introspectedColumn.getTableAlias().map(a -> a + '_' + introspectedColumn.getActualColumnName())
                .orElseGet(introspectedColumn::getActualColumnName);
    }
}