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.table;
017
018import com.mybatisflex.core.FlexGlobalConfig;
019import com.mybatisflex.core.mask.CompositeMaskTypeHandler;
020import com.mybatisflex.core.mask.MaskTypeHandler;
021import com.mybatisflex.core.util.ArrayUtil;
022import com.mybatisflex.core.util.StringUtil;
023import org.apache.ibatis.session.Configuration;
024import org.apache.ibatis.type.*;
025
026import java.sql.Time;
027import java.sql.Timestamp;
028import java.time.*;
029import java.time.chrono.JapaneseDate;
030import java.util.Date;
031
032public class ColumnInfo {
033
034    private static final Class<?>[] needGetTypeHandlerTypes = {
035        Date.class, java.sql.Date.class, Time.class, Timestamp.class,
036        Instant.class, LocalDate.class, LocalDateTime.class, LocalTime.class, OffsetDateTime.class, OffsetTime.class, ZonedDateTime.class,
037        Year.class, Month.class, YearMonth.class, JapaneseDate.class,
038        byte[].class, Byte[].class, Byte.class,
039    };
040
041    /**
042     * 数据库列名。
043     */
044    protected String column;
045
046    /**
047     * 列的别名。
048     */
049    protected String[] alias;
050
051    /**
052     * java entity 定义的属性名称(field name)。
053     */
054    protected String property;
055
056    /**
057     * 数据库字段注释,在 AI 时代,注释的内容往往可用于 AI 辅助对话
058     */
059    protected String comment;
060
061    /**
062     * 属性类型。
063     */
064    protected Class<?> propertyType;
065
066    /**
067     * 该列对应的 jdbcType。
068     */
069    protected JdbcType jdbcType;
070
071    /**
072     * 自定义 TypeHandler。
073     */
074    protected TypeHandler<?> typeHandler;
075
076    /**
077     * 最终使用和构建出来的 typeHandler
078     */
079    protected TypeHandler<?> buildTypeHandler;
080
081    /**
082     * 数据脱敏类型。
083     */
084    protected String maskType;
085
086    /**
087     * 是否忽略
088     */
089    protected boolean ignore;
090
091
092    public String getColumn() {
093        return column;
094    }
095
096    public void setColumn(String column) {
097        this.column = column;
098    }
099
100    public String[] getAlias() {
101        return alias;
102    }
103
104    public void setAlias(String[] alias) {
105        this.alias = alias;
106    }
107
108    public String getProperty() {
109        return property;
110    }
111
112    public void setProperty(String property) {
113        this.property = property;
114    }
115
116    public String getComment() {
117        return comment;
118    }
119
120    public void setComment(String comment) {
121        this.comment = comment;
122    }
123
124    public Class<?> getPropertyType() {
125        return propertyType;
126    }
127
128    public void setPropertyType(Class<?> propertyType) {
129        this.propertyType = propertyType;
130    }
131
132    public JdbcType getJdbcType() {
133        return jdbcType;
134    }
135
136    public void setJdbcType(JdbcType jdbcType) {
137        this.jdbcType = jdbcType;
138    }
139
140    public TypeHandler<?> buildTypeHandler(Configuration configuration) {
141
142        if (buildTypeHandler != null) {
143            return buildTypeHandler;
144        }
145
146        //脱敏规则配置
147        else if (StringUtil.isNotBlank(maskType)) {
148            if (typeHandler != null) {
149                //noinspection unchecked
150                buildTypeHandler = new CompositeMaskTypeHandler(maskType, (TypeHandler<Object>) typeHandler);
151            } else {
152                buildTypeHandler = new MaskTypeHandler(maskType);
153            }
154        }
155
156        //用户自定义的 typeHandler
157        else if (typeHandler != null) {
158            buildTypeHandler = typeHandler;
159        }
160
161        //枚举
162        else if (propertyType.isEnum() || ArrayUtil.contains(needGetTypeHandlerTypes, propertyType)) {
163            if (configuration == null) {
164                configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration();
165            }
166            if (configuration != null) {
167                buildTypeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType);
168            }
169        }
170
171        return buildTypeHandler;
172    }
173
174    public void setTypeHandler(TypeHandler<?> typeHandler) {
175        this.typeHandler = typeHandler;
176    }
177
178    public String getMaskType() {
179        return maskType;
180    }
181
182    public void setMaskType(String maskType) {
183        this.maskType = maskType;
184    }
185
186
187    public boolean isIgnore() {
188        return ignore;
189    }
190
191    public void setIgnore(boolean ignore) {
192        this.ignore = ignore;
193    }
194}