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.MaskTypeHandler;
020import com.mybatisflex.core.util.StringUtil;
021import org.apache.ibatis.session.Configuration;
022import org.apache.ibatis.type.JdbcType;
023import org.apache.ibatis.type.TypeHandler;
024
025public class ColumnInfo {
026
027    /**
028     * 数据库列名。
029     */
030    protected String column;
031
032    /**
033     * 列的别名。
034     */
035    protected String[] alias;
036
037    /**
038     * java entity 定义的属性名称。
039     */
040    protected String property;
041
042    /**
043     * 属性类型。
044     */
045    protected Class<?> propertyType;
046
047    /**
048     * 该列对应的 jdbcType。
049     */
050    protected JdbcType jdbcType;
051
052    /**
053     * 自定义 TypeHandler。
054     */
055    protected TypeHandler typeHandler;
056
057    /**
058     * 数据脱敏类型。
059     */
060    protected String maskType;
061
062    /**
063     * 是否忽略
064     */
065    protected boolean ignore;
066
067
068    public String getColumn() {
069        return column;
070    }
071
072    public void setColumn(String column) {
073        this.column = column;
074    }
075
076    public String[] getAlias() {
077        return alias;
078    }
079
080    public void setAlias(String[] alias) {
081        this.alias = alias;
082    }
083
084    public String getProperty() {
085        return property;
086    }
087
088    public void setProperty(String property) {
089        this.property = property;
090    }
091
092    public Class<?> getPropertyType() {
093        return propertyType;
094    }
095
096    public void setPropertyType(Class<?> propertyType) {
097        this.propertyType = propertyType;
098    }
099
100    public JdbcType getJdbcType() {
101        return jdbcType;
102    }
103
104    public void setJdbcType(JdbcType jdbcType) {
105        this.jdbcType = jdbcType;
106    }
107
108    public TypeHandler buildTypeHandler(Configuration configuration) {
109
110        //优先使用自定义的 typeHandler
111        if (typeHandler != null) {
112            return typeHandler;
113        }
114        //枚举
115        else if (propertyType.isEnum()) {
116            if (configuration == null){
117                configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration();
118            }
119            this.typeHandler =  configuration.getTypeHandlerRegistry().getTypeHandler(propertyType);
120        }
121        //若用户未定义 typeHandler,而配置了数据脱敏,则使用脱敏的 handler 处理
122        else if (StringUtil.isNotBlank(maskType)) {
123            typeHandler = new MaskTypeHandler(maskType);
124        }
125
126        return typeHandler;
127    }
128
129    public void setTypeHandler(TypeHandler typeHandler) {
130        this.typeHandler = typeHandler;
131    }
132
133    public String getMaskType() {
134        return maskType;
135    }
136
137    public void setMaskType(String maskType) {
138        this.maskType = maskType;
139    }
140
141
142    public boolean isIgnore() {
143        return ignore;
144    }
145
146    public void setIgnore(boolean ignore) {
147        this.ignore = ignore;
148    }
149}