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