001/*
002 *  Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.dialect;
017
018import com.mybatisflex.core.query.QueryTable;
019import com.mybatisflex.core.query.QueryWrapper;
020import com.mybatisflex.core.row.Row;
021import com.mybatisflex.core.table.TableInfo;
022import com.mybatisflex.core.table.TableManager;
023
024import java.util.Collection;
025import java.util.List;
026
027/**
028 * @author michael
029 */
030public interface IDialect {
031
032    String wrap(String keyword);
033
034    String wrapColumnAlias(String keyword);
035
036    default String getRealTable(String table, OperateType operateType) {
037        return TableManager.getRealTable(table, operateType);
038    }
039
040    default String getRealSchema(String schema, String table, OperateType operateType) {
041        return TableManager.getRealSchema(schema, table, operateType);
042    }
043
044    String forHint(String hintString);
045
046    String forInsertRow(String schema, String tableName, Row row);
047
048    String forInsertBatchWithFirstRowColumns(String schema, String tableName, List<Row> rows);
049
050    String forDeleteById(String schema, String tableName, String[] primaryKeys);
051
052    String forDeleteBatchByIds(String schema, String tableName, String[] primaryKeys, Object[] ids);
053
054    String forDeleteByQuery(QueryWrapper queryWrapper);
055
056    String forUpdateById(String schema, String tableName, Row row);
057
058    String forUpdateByQuery(QueryWrapper queryWrapper, Row data);
059
060    String forUpdateBatchById(String schema, String tableName, List<Row> rows);
061
062    String forSelectOneById(String schema, String tableName, String[] primaryKeys, Object[] primaryValues);
063
064    String forSelectByQuery(QueryWrapper queryWrapper);
065
066    String buildSelectSql(QueryWrapper queryWrapper);
067
068    default String buildSelectSql(QueryWrapper queryWrapper, List<QueryTable> contextTables) {
069        return buildSelectSql(queryWrapper);
070    }
071
072    String buildNoSelectSql(QueryWrapper queryWrapper);
073
074    String buildDeleteSql(QueryWrapper queryWrapper);
075
076    String buildWhereConditionSql(QueryWrapper queryWrapper);
077
078
079    //////for entity /////
080    String forInsertEntity(TableInfo tableInfo, Object entity, boolean ignoreNulls);
081
082    String forInsertEntityWithPk(TableInfo tableInfo, Object entity, boolean ignoreNulls);
083
084    String forInsertEntityBatch(TableInfo tableInfo, Collection<?> entities);
085
086    String forDeleteEntityById(TableInfo tableInfo);
087
088    String forDeleteEntityBatchByIds(TableInfo tableInfo, Object[] primaryValues);
089
090    String forDeleteEntityBatchByQuery(TableInfo tableInfo, QueryWrapper queryWrapper);
091
092    String forUpdateEntity(TableInfo tableInfo, Object entity, boolean ignoreNulls);
093
094    String forUpdateEntityByQuery(TableInfo tableInfo, Object entity, boolean ignoreNulls, QueryWrapper queryWrapper);
095
096    String forSelectOneEntityById(TableInfo tableInfo);
097
098    String forSelectEntityListByIds(TableInfo tableInfo, Object[] primaryValues);
099
100    /**
101     * 权限处理
102     *
103     * @param queryWrapper queryWrapper
104     * @param operateType  操作类型
105     */
106    default void prepareAuth(QueryWrapper queryWrapper, OperateType operateType) {
107    }
108
109    /**
110     * 权限处理
111     *
112     * @param schema      schema
113     * @param tableName   表名
114     * @param sql         sql
115     * @param operateType 操作类型
116     */
117    default void prepareAuth(String schema, String tableName, StringBuilder sql, OperateType operateType) {
118    }
119
120    /**
121     * 权限处理
122     *
123     * @param tableInfo   tableInfo
124     * @param sql         sql
125     * @param operateType 操作类型
126     */
127    default void prepareAuth(TableInfo tableInfo, StringBuilder sql, OperateType operateType) {
128    }
129}