001package gudusoft.gsqlparser.common.structured;
002
003import gudusoft.gsqlparser.nodes.TObjectName;
004import gudusoft.gsqlparser.nodes.TTypeName;
005
006import java.util.Collections;
007import java.util.List;
008
009/**
010 * One node of the type a column's DDL declares, read by {@link DeclaredTypeReader} from whichever members of
011 * {@link TTypeName} the dialect's grammar fills (plan section 4.2): a STRUCT with its fields in declaration order, an
012 * ARRAY with its element, a MAP with its key and value, or a scalar.
013 */
014public final class DeclaredTypeNode {
015
016    public enum Kind {
017        STRUCT,
018        ARRAY,
019        MAP,
020        SCALAR
021    }
022
023    private final Kind kind;
024    private final TObjectName fieldName;
025    private final List<DeclaredTypeNode> fields;
026    private final DeclaredTypeNode element;
027    private final DeclaredTypeNode key;
028    private final TTypeName source;
029
030    DeclaredTypeNode(Kind kind, TObjectName fieldName, List<DeclaredTypeNode> fields, DeclaredTypeNode element,
031                     DeclaredTypeNode key, TTypeName source) {
032        this.kind = kind;
033        this.fieldName = fieldName;
034        this.fields = fields == null ? null : Collections.unmodifiableList(fields);
035        this.element = element;
036        this.key = key;
037        this.source = source;
038    }
039
040    public Kind getKind() {
041        return kind;
042    }
043
044    /** For a STRUCT member, the name as the DDL writes it (its token is the field's identity); null otherwise. */
045    public TObjectName getFieldName() {
046        return fieldName;
047    }
048
049    /**
050     * For a STRUCT, its fields in declaration order; null when the grammar kept no member list
051     * ({@code STRUCT<T>}, Databricks {@code ROW(...)}), so the fields are not known.
052     */
053    public List<DeclaredTypeNode> getFields() {
054        return fields;
055    }
056
057    /** For an ARRAY its element, for a MAP its value; null when the grammar dropped it. */
058    public DeclaredTypeNode getElement() {
059        return element;
060    }
061
062    /** For a MAP its key; null otherwise or when the grammar dropped it. */
063    public DeclaredTypeNode getKey() {
064        return key;
065    }
066
067    /** The type as parsed, for its position. */
068    public TTypeName getSource() {
069        return source;
070    }
071}