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.setProperty(columnInfo.getProperty());
049        this.setPropertyType(columnInfo.getPropertyType());
050
051        //当 id 的类型为数值时,默认设置为自增的方式
052        if (Number.class.isAssignableFrom(columnInfo.getPropertyType())) {
053            keyType = KeyType.Auto;
054        } else {
055            keyType = KeyType.None;
056        }
057    }
058
059    public IdInfo(String column, String property, Class<?> propertyType, Id id) {
060        this.column = column;
061        this.property = property;
062        this.propertyType = propertyType;
063        this.keyType = id.keyType();
064        this.value = id.value();
065        this.before = id.before();
066    }
067
068    public KeyType getKeyType() {
069        return keyType;
070    }
071
072    public void setKeyType(KeyType keyType) {
073        this.keyType = keyType;
074    }
075
076    public String getValue() {
077        return value;
078    }
079
080    public void setValue(String value) {
081        this.value = value;
082    }
083
084    public Boolean getBefore() {
085        return before;
086    }
087
088    public void setBefore(Boolean before) {
089        this.before = before;
090    }
091}