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.annotation.Id;
019import com.mybatisflex.annotation.KeyType;
020import com.mybatisflex.core.FlexGlobalConfig;
021import com.mybatisflex.core.util.StringUtil;
022
023public class IdInfo extends ColumnInfo {
024
025    /**
026     * id 生成策略
027     */
028    private KeyType keyType;
029
030    /**
031     * 1、若 keyType 类型是 sequence, value 则代表的是
032     * sequence 序列的 sql 内容
033     * 例如:select SEQ_USER_ID.nextval as id from dual
034     * <p>
035     * 2、若 keyType 是 Generator,value 则代表的是使用的那个 keyGenerator 的名称
036     */
037    private String value;
038
039
040    /**
041     * sequence 序列内容执行顺序
042     *
043     * @see org.apache.ibatis.executor.keygen.SelectKeyGenerator
044     */
045    private Boolean before;
046
047
048    public IdInfo(Id id) {
049        this.keyType = id.keyType();
050        this.value = id.value();
051        this.before = id.before();
052        this.comment = id.comment();
053
054        initDefaultKeyType();
055    }
056
057    /**
058     * 用户未配置 keyType 是,配置默认的 key Type
059     */
060    private void initDefaultKeyType() {
061        if (this.keyType == null || this.keyType == KeyType.None) {
062            FlexGlobalConfig.KeyConfig defaultKeyConfig = FlexGlobalConfig.getDefaultConfig().getKeyConfig();
063            if (defaultKeyConfig != null) {
064                if (defaultKeyConfig.getKeyType() != null) {
065                    this.keyType = defaultKeyConfig.getKeyType();
066                    this.before = defaultKeyConfig.isBefore();
067                }
068                if (StringUtil.isBlank(this.value) && StringUtil.isNotBlank(defaultKeyConfig.getValue())) {
069                    this.value = defaultKeyConfig.getValue();
070                }
071            }
072        }
073    }
074
075
076    public KeyType getKeyType() {
077        return keyType;
078    }
079
080    public void setKeyType(KeyType keyType) {
081        this.keyType = keyType;
082    }
083
084    public String getValue() {
085        return value;
086    }
087
088    public void setValue(String value) {
089        this.value = value;
090    }
091
092    public Boolean getBefore() {
093        return before;
094    }
095
096    public void setBefore(Boolean before) {
097        this.before = before;
098    }
099
100}