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