|
| 1 | +package com.bencodez.advancedcore.core.user.storage.sql; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.LinkedHashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Locale; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKey; |
| 13 | +import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKeyBoolean; |
| 14 | +import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKeyInt; |
| 15 | +import com.bencodez.simpleapi.sql.DataType; |
| 16 | + |
| 17 | +public final class SqlUserSchema { |
| 18 | + public static final String UUID_COLUMN = "uuid"; |
| 19 | + |
| 20 | + public record ColumnDefinition(String name, String sqlType, DataType dataType) { |
| 21 | + public ColumnDefinition { |
| 22 | + Objects.requireNonNull(name, "name"); |
| 23 | + Objects.requireNonNull(sqlType, "sqlType"); |
| 24 | + Objects.requireNonNull(dataType, "dataType"); |
| 25 | + if (name.isBlank()) throw new IllegalArgumentException("Column name cannot be blank"); |
| 26 | + if (sqlType.isBlank()) throw new IllegalArgumentException("SQL type cannot be blank"); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private final Map<String, ColumnDefinition> columnsByLowerName; |
| 31 | + |
| 32 | + private SqlUserSchema(Map<String, ColumnDefinition> columnsByLowerName) { |
| 33 | + this.columnsByLowerName = Collections.unmodifiableMap(new LinkedHashMap<>(columnsByLowerName)); |
| 34 | + } |
| 35 | + |
| 36 | + public static Builder builder() { return new Builder(); } |
| 37 | + |
| 38 | + public static SqlUserSchema fromKeys(Collection<? extends UserDataKey> keys) { |
| 39 | + Builder builder = builder(); |
| 40 | + for (UserDataKey key : Objects.requireNonNull(keys, "keys")) { |
| 41 | + DataType type = DataType.STRING; |
| 42 | + if (key instanceof UserDataKeyInt) type = DataType.INTEGER; |
| 43 | + else if (key instanceof UserDataKeyBoolean) type = DataType.BOOLEAN; |
| 44 | + builder.column(key.getKey(), key.getColumnType(), type); |
| 45 | + } |
| 46 | + return builder.build(); |
| 47 | + } |
| 48 | + |
| 49 | + public List<ColumnDefinition> columns() { return new ArrayList<>(columnsByLowerName.values()); } |
| 50 | + |
| 51 | + public ColumnDefinition column(String name) { |
| 52 | + if (name == null) return null; |
| 53 | + return columnsByLowerName.get(name.toLowerCase(Locale.ROOT)); |
| 54 | + } |
| 55 | + |
| 56 | + public boolean contains(String name) { return column(name) != null; } |
| 57 | + |
| 58 | + public static final class Builder { |
| 59 | + private final Map<String, ColumnDefinition> columns = new LinkedHashMap<>(); |
| 60 | + |
| 61 | + private Builder() { |
| 62 | + columns.put(UUID_COLUMN, new ColumnDefinition(UUID_COLUMN, "VARCHAR(37)", DataType.STRING)); |
| 63 | + } |
| 64 | + |
| 65 | + public Builder column(String name, String sqlType, DataType dataType) { |
| 66 | + Objects.requireNonNull(name, "name"); |
| 67 | + if (UUID_COLUMN.equalsIgnoreCase(name)) { |
| 68 | + throw new IllegalArgumentException("Column name 'uuid' is reserved for user identity"); |
| 69 | + } |
| 70 | + String canonical = name.toLowerCase(Locale.ROOT); |
| 71 | + ColumnDefinition existing = columns.get(canonical); |
| 72 | + if (existing != null) { |
| 73 | + throw new IllegalArgumentException("Duplicate SQL column name: " + name + " conflicts with " + existing.name()); |
| 74 | + } |
| 75 | + columns.put(canonical, new ColumnDefinition(name, sqlType, dataType)); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + public SqlUserSchema build() { return new SqlUserSchema(columns); } |
| 80 | + } |
| 81 | +} |
0 commit comments