001package gudusoft.gsqlparser.catalog.input.model;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.List;
006import java.util.Objects;
007
008/**
009 * Table constraint entry. Plan §6.
010 *
011 * <p>{@link #type()} is a string (PK / FK / UNIQUE / CHECK / NOT_NULL) so the
012 * model stays forward-compatible with vendor-specific constraint flavors.</p>
013 */
014public final class ConstraintModel {
015
016    private final String name;
017    private final String type;
018    private final List<String> columns;
019
020    private ConstraintModel(Builder b) {
021        if (b.type == null || b.type.isEmpty()) {
022            throw new IllegalArgumentException("ConstraintModel.type is required");
023        }
024        this.name = b.name;
025        this.type = b.type;
026        this.columns = Collections.unmodifiableList(new ArrayList<String>(b.columns));
027    }
028
029    public static Builder builder() {
030        return new Builder();
031    }
032
033    public String name() {
034        return name;
035    }
036
037    /** PK / FK / UNIQUE / CHECK / NOT_NULL — string for forward compatibility. */
038    public String type() {
039        return type;
040    }
041
042    public List<String> columns() {
043        return columns;
044    }
045
046    @Override
047    public boolean equals(Object o) {
048        if (this == o) return true;
049        if (!(o instanceof ConstraintModel)) return false;
050        ConstraintModel that = (ConstraintModel) o;
051        return Objects.equals(name, that.name)
052            && type.equals(that.type)
053            && columns.equals(that.columns);
054    }
055
056    @Override
057    public int hashCode() {
058        return Objects.hash(name, type, columns);
059    }
060
061    @Override
062    public String toString() {
063        return "ConstraintModel{name=" + name + ", type=" + type + ", columns=" + columns + '}';
064    }
065
066    public static final class Builder {
067
068        private String name;
069        private String type;
070        private final List<String> columns = new ArrayList<String>();
071
072        private Builder() {
073        }
074
075        public Builder name(String v) {
076            this.name = v;
077            return this;
078        }
079
080        public Builder type(String v) {
081            this.type = v;
082            return this;
083        }
084
085        public Builder addColumn(String v) {
086            if (v == null) {
087                throw new IllegalArgumentException("ConstraintModel column may not be null");
088            }
089            this.columns.add(v);
090            return this;
091        }
092
093        public ConstraintModel build() {
094            return new ConstraintModel(this);
095        }
096    }
097}