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.keygen.KeyGenerators;
020import com.mybatisflex.core.util.SqlUtil;
021
022/**
023 * row 的主键策略
024 */
025public class RowKey {
026
027    /**
028     * 自增 ID
029     */
030    public static final RowKey AUTO = RowKey.of("id", KeyType.Auto, null, false);
031
032    /**
033     * UUID 的 ID
034     */
035    public static final RowKey UUID = RowKey.of("id", KeyType.Generator, KeyGenerators.uuid, true);
036
037    /**
038     * flexId
039     */
040    public static final RowKey FLEX_ID = RowKey.of("id", KeyType.Generator, KeyGenerators.flexId, true);
041
042    /**
043     * snowFlakeId
044     */
045    public static final RowKey SNOW_FLAKE_ID = RowKey.of("id", KeyType.Generator, KeyGenerators.snowFlakeId, true);
046
047
048    public static RowKey of(String keyColumn) {
049        SqlUtil.keepColumnSafely(keyColumn);
050        RowKey rowKey = new RowKey();
051        rowKey.keyColumn = keyColumn;
052        return rowKey;
053    }
054
055    public static RowKey of(String keyColumn, KeyType keyType) {
056        SqlUtil.keepColumnSafely(keyColumn);
057        RowKey rowKey = new RowKey();
058        rowKey.keyColumn = keyColumn;
059        rowKey.keyType = keyType;
060        return rowKey;
061    }
062
063    public static RowKey of(String keyColumn, KeyType keyType, String keyTypeValue) {
064        SqlUtil.keepColumnSafely(keyColumn);
065        RowKey rowKey = new RowKey();
066        rowKey.keyColumn = keyColumn;
067        rowKey.keyType = keyType;
068        rowKey.value = keyTypeValue;
069        return rowKey;
070    }
071
072    public static RowKey of(String keyColumn, KeyType keyType, String keyTypeValue, boolean before) {
073        SqlUtil.keepColumnSafely(keyColumn);
074        RowKey rowKey = new RowKey();
075        rowKey.keyColumn = keyColumn;
076        rowKey.keyType = keyType;
077        rowKey.value = keyTypeValue;
078        rowKey.before = before;
079        return rowKey;
080    }
081
082    /**
083     * 主键字段
084     */
085    protected String keyColumn;
086
087    /**
088     * 主键类型
089     */
090    protected KeyType keyType = KeyType.Auto;
091
092    /**
093     * 主键类型为 Sequence 和 Generator 时的对应的内容
094     */
095    protected String value;
096
097    /**
098     * 是否前执行
099     */
100    protected boolean before = true;
101
102    private RowKey() {
103    }
104
105    public String getKeyColumn() {
106        return keyColumn;
107    }
108
109
110    public KeyType getKeyType() {
111        return keyType;
112    }
113
114
115    public String getValue() {
116        return value;
117    }
118
119
120    public boolean isBefore() {
121        return before;
122    }
123
124    @Override
125    public boolean equals(Object o) {
126        if (this == o) {
127            return true;
128        }
129        if (o instanceof RowKey) {
130            return keyColumn.equals(((RowKey) o).keyColumn);
131        }
132        return false;
133    }
134
135    @Override
136    public int hashCode() {
137        return keyColumn.hashCode();
138    }
139
140}