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 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 public static RowKey of(String keyColumn) { 051 SqlUtil.keepColumnSafely(keyColumn); 052 RowKey rowKey = new RowKey(); 053 rowKey.keyColumn = keyColumn; 054 return rowKey; 055 } 056 057 public static RowKey of(String keyColumn, KeyType keyType) { 058 SqlUtil.keepColumnSafely(keyColumn); 059 RowKey rowKey = new RowKey(); 060 rowKey.keyColumn = keyColumn; 061 rowKey.keyType = keyType; 062 return rowKey; 063 } 064 065 public static RowKey of(String keyColumn, KeyType keyType, String keyTypeValue) { 066 SqlUtil.keepColumnSafely(keyColumn); 067 RowKey rowKey = new RowKey(); 068 rowKey.keyColumn = keyColumn; 069 rowKey.keyType = keyType; 070 rowKey.value = keyTypeValue; 071 return rowKey; 072 } 073 074 public static RowKey of(String keyColumn, KeyType keyType, String keyTypeValue, boolean before) { 075 SqlUtil.keepColumnSafely(keyColumn); 076 RowKey rowKey = new RowKey(); 077 rowKey.keyColumn = keyColumn; 078 rowKey.keyType = keyType; 079 rowKey.value = keyTypeValue; 080 rowKey.before = before; 081 return rowKey; 082 } 083 084 /** 085 * 主键字段 086 */ 087 protected String keyColumn; 088 089 /** 090 * 主键类型 091 */ 092 protected KeyType keyType = KeyType.Auto; 093 094 /** 095 * 主键类型为 Sequence 和 Generator 时的对应的内容 096 */ 097 protected String value; 098 099 /** 100 * 是否前执行 101 */ 102 protected boolean before = true; 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}