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.row;
017
018import com.mybatisflex.annotation.KeyType;
019import com.mybatisflex.core.util.SqlUtil;
020
021/**
022 * row 的主键策略
023 */
024public class RowKey {
025
026    /**
027     * 自增 ID
028     */
029    public static final RowKey ID_AUTO = RowKey.of("id", KeyType.Auto, null, false);
030
031    /**
032     * UUID 的 ID
033     */
034    public static final RowKey ID_UUID = RowKey.of("id", KeyType.Generator, "uuid", true);
035
036
037    public static RowKey of(String keyColumn) {
038        SqlUtil.keepColumnSafely(keyColumn);
039        RowKey rowKey = new RowKey();
040        rowKey.keyColumn = keyColumn;
041        return rowKey;
042    }
043
044    public static RowKey of(String keyColumn, KeyType keyType) {
045        SqlUtil.keepColumnSafely(keyColumn);
046        RowKey rowKey = new RowKey();
047        rowKey.keyColumn = keyColumn;
048        rowKey.keyType = keyType;
049        return rowKey;
050    }
051
052    public static RowKey of(String keyColumn, KeyType keyType, String keyTypeValue) {
053        SqlUtil.keepColumnSafely(keyColumn);
054        RowKey rowKey = new RowKey();
055        rowKey.keyColumn = keyColumn;
056        rowKey.keyType = keyType;
057        rowKey.value = keyTypeValue;
058        return rowKey;
059    }
060
061    public static RowKey of(String keyColumn, KeyType keyType, String keyTypeValue, boolean before) {
062        SqlUtil.keepColumnSafely(keyColumn);
063        RowKey rowKey = new RowKey();
064        rowKey.keyColumn = keyColumn;
065        rowKey.keyType = keyType;
066        rowKey.value = keyTypeValue;
067        rowKey.before = before;
068        return rowKey;
069    }
070
071    /**
072     * 主键字段
073     */
074    protected String keyColumn;
075
076    /**
077     * 主键类型
078     */
079    protected KeyType keyType = KeyType.Auto;
080
081    /**
082     * 主键类型为 Sequence 和 Generator 时的对应的内容
083     */
084    protected String value;
085
086    /**
087     * 是否前执行
088     */
089    protected boolean before = true;
090
091
092    public String getKeyColumn() {
093        return keyColumn;
094    }
095
096
097    public KeyType getKeyType() {
098        return keyType;
099    }
100
101
102    public String getValue() {
103        return value;
104    }
105
106
107    public boolean isBefore() {
108        return before;
109    }
110
111
112}