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 * Schema entry inside a {@link CatalogModel}.
010 *
011 * <p>Plan §6. The empty-string {@code name} is allowed to model dialects
012 * without a schema layer (e.g., MySQL); see plan §9.4 dialect mapping.</p>
013 */
014public final class SchemaModel {
015
016    private final String name;
017    private final List<TableModel> tables;
018    private final List<ViewModel> views;
019    private final List<RoutineModel> routines;
020    private final List<SynonymModel> synonyms;
021    private final List<SequenceModel> sequences;
022
023    private SchemaModel(Builder b) {
024        if (b.name == null) {
025            throw new IllegalArgumentException(
026                "SchemaModel.name is required (use empty string for schema-less dialects)");
027        }
028        this.name = b.name;
029        this.tables = Collections.unmodifiableList(new ArrayList<TableModel>(b.tables));
030        this.views = Collections.unmodifiableList(new ArrayList<ViewModel>(b.views));
031        this.routines = Collections.unmodifiableList(new ArrayList<RoutineModel>(b.routines));
032        this.synonyms = Collections.unmodifiableList(new ArrayList<SynonymModel>(b.synonyms));
033        this.sequences = Collections.unmodifiableList(new ArrayList<SequenceModel>(b.sequences));
034    }
035
036    public static Builder builder() {
037        return new Builder();
038    }
039
040    public String name() {
041        return name;
042    }
043
044    public List<TableModel> tables() {
045        return tables;
046    }
047
048    public List<ViewModel> views() {
049        return views;
050    }
051
052    public List<RoutineModel> routines() {
053        return routines;
054    }
055
056    public List<SynonymModel> synonyms() {
057        return synonyms;
058    }
059
060    public List<SequenceModel> sequences() {
061        return sequences;
062    }
063
064    @Override
065    public boolean equals(Object o) {
066        if (this == o) return true;
067        if (!(o instanceof SchemaModel)) return false;
068        SchemaModel that = (SchemaModel) o;
069        return name.equals(that.name)
070            && tables.equals(that.tables)
071            && views.equals(that.views)
072            && routines.equals(that.routines)
073            && synonyms.equals(that.synonyms)
074            && sequences.equals(that.sequences);
075    }
076
077    @Override
078    public int hashCode() {
079        return Objects.hash(name, tables, views, routines, synonyms, sequences);
080    }
081
082    @Override
083    public String toString() {
084        return "SchemaModel{name=" + name
085            + ", tables=" + tables.size()
086            + ", views=" + views.size()
087            + ", routines=" + routines.size()
088            + ", synonyms=" + synonyms.size()
089            + ", sequences=" + sequences.size() + '}';
090    }
091
092    public static final class Builder {
093
094        private String name;
095        private final List<TableModel> tables = new ArrayList<TableModel>();
096        private final List<ViewModel> views = new ArrayList<ViewModel>();
097        private final List<RoutineModel> routines = new ArrayList<RoutineModel>();
098        private final List<SynonymModel> synonyms = new ArrayList<SynonymModel>();
099        private final List<SequenceModel> sequences = new ArrayList<SequenceModel>();
100
101        private Builder() {
102        }
103
104        public Builder name(String v) {
105            this.name = v;
106            return this;
107        }
108
109        public Builder addTable(TableModel v) {
110            if (v == null) {
111                throw new IllegalArgumentException("SchemaModel table may not be null");
112            }
113            this.tables.add(v);
114            return this;
115        }
116
117        public Builder addView(ViewModel v) {
118            if (v == null) {
119                throw new IllegalArgumentException("SchemaModel view may not be null");
120            }
121            this.views.add(v);
122            return this;
123        }
124
125        public Builder addRoutine(RoutineModel v) {
126            if (v == null) {
127                throw new IllegalArgumentException("SchemaModel routine may not be null");
128            }
129            this.routines.add(v);
130            return this;
131        }
132
133        public Builder addSynonym(SynonymModel v) {
134            if (v == null) {
135                throw new IllegalArgumentException("SchemaModel synonym may not be null");
136            }
137            this.synonyms.add(v);
138            return this;
139        }
140
141        public Builder addSequence(SequenceModel v) {
142            if (v == null) {
143                throw new IllegalArgumentException("SchemaModel sequence may not be null");
144            }
145            this.sequences.add(v);
146            return this;
147        }
148
149        public SchemaModel build() {
150            return new SchemaModel(this);
151        }
152    }
153}