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;
020
021public class IdInfo extends ColumnInfo {
022
023    /**
024     * id 生成策略
025     */
026    private KeyType keyType;
027
028    /**
029     * 1、若 keyType 类型是 sequence, value 则代表的是
030     * sequence 序列的 sql 内容
031     * 例如:select SEQ_USER_ID.nextval as id from dual
032     * <p>
033     * 2、若 keyType 是 Generator,value 则代表的是使用的那个 keyGenerator 的名称
034     */
035    private String value;
036
037
038    /**
039     * sequence 序列内容执行顺序
040     *
041     * @see org.apache.ibatis.executor.keygen.SelectKeyGenerator
042     */
043    private Boolean before;
044
045
046    public IdInfo(ColumnInfo columnInfo) {
047        this.setColumn(columnInfo.getColumn());
048        this.setAlias(columnInfo.getAlias());
049        this.setProperty(columnInfo.getProperty());
050        this.setPropertyType(columnInfo.getPropertyType());
051
052        //当 id 的类型为数值时,默认设置为自增的方式
053        if (Number.class.isAssignableFrom(columnInfo.getPropertyType())) {
054            keyType = KeyType.Auto;
055        } else {
056            keyType = KeyType.None;
057        }
058    }
059
060    public IdInfo(Id id) {
061        this.keyType = id.keyType();
062        this.value = id.value();
063        this.before = id.before();
064    }
065
066    public KeyType getKeyType() {
067        return keyType;
068    }
069
070    public void setKeyType(KeyType keyType) {
071        this.keyType = keyType;
072    }
073
074    public String getValue() {
075        return value;
076    }
077
078    public void setValue(String value) {
079        this.value = value;
080    }
081
082    public Boolean getBefore() {
083        return before;
084    }
085
086    public void setBefore(Boolean before) {
087        this.before = before;
088    }
089
090}