001/*
002 *  Copyright (c) 2022-2023, 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     * 属性类型。
058     */
059    protected Class<?> propertyType;
060
061    /**
062     * 该列对应的 jdbcType。
063     */
064    protected JdbcType jdbcType;
065
066    /**
067     * 自定义 TypeHandler。
068     */
069    protected TypeHandler typeHandler;
070
071    /**
072     * 最终使用和构建出来的 typeHandler
073     */
074    protected TypeHandler buildTypeHandler;
075
076    /**
077     * 数据脱敏类型。
078     */
079    protected String maskType;
080
081    /**
082     * 是否忽略
083     */
084    protected boolean ignore;
085
086
087    public String getColumn() {
088        return column;
089    }
090
091    public void setColumn(String column) {
092        this.column = column;
093    }
094
095    public String[] getAlias() {
096        return alias;
097    }
098
099    public void setAlias(String[] alias) {
100        this.alias = alias;
101    }
102
103    public String getProperty() {
104        return property;
105    }
106
107    public void setProperty(String property) {
108        this.property = property;
109    }
110
111    public Class<?> getPropertyType() {
112        return propertyType;
113    }
114
115    public void setPropertyType(Class<?> propertyType) {
116        this.propertyType = propertyType;
117    }
118
119    public JdbcType getJdbcType() {
120        return jdbcType;
121    }
122
123    public void setJdbcType(JdbcType jdbcType) {
124        this.jdbcType = jdbcType;
125    }
126
127    public TypeHandler buildTypeHandler(Configuration configuration) {
128
129        if (buildTypeHandler != null) {
130            return buildTypeHandler;
131        }
132
133        //脱敏规则配置
134        else if (StringUtil.isNotBlank(maskType)) {
135            if (typeHandler != null) {
136                buildTypeHandler = new CompositeMaskTypeHandler(maskType, typeHandler);
137            } else {
138                buildTypeHandler = new MaskTypeHandler(maskType);
139            }
140        }
141
142        //用户自定义的 typeHandler
143        else if (typeHandler != null) {
144            buildTypeHandler = typeHandler;
145        }
146
147        //枚举
148        else if (propertyType.isEnum() || ArrayUtil.contains(needGetTypeHandlerTypes, propertyType)) {
149            if (configuration == null) {
150                configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration();
151            }
152            buildTypeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType);
153        }
154
155        return buildTypeHandler;
156    }
157
158    public void setTypeHandler(TypeHandler typeHandler) {
159        this.typeHandler = typeHandler;
160    }
161
162    public String getMaskType() {
163        return maskType;
164    }
165
166    public void setMaskType(String maskType) {
167        this.maskType = maskType;
168    }
169
170
171    public boolean isIgnore() {
172        return ignore;
173    }
174
175    public void setIgnore(boolean ignore) {
176        this.ignore = ignore;
177    }
178}