001package gudusoft.gsqlparser.dlineage.dataflow.model;
002
003import java.util.Collections;
004import java.util.IdentityHashMap;
005import java.util.Set;
006
007/**
008 * The nested fields one CREATE TABLE declares for a table (plan rule R10): which columns and fields list their
009 * fields, and which field nodes the DDL made. A table keeps one per CREATE; a read is validated against the one active
010 * at its own statement ({@link Table#getActiveFieldDeclaration()}), so after {@code DROP TABLE} the next
011 * {@code CREATE TABLE} of the name replaces the fields a read may use, while a read before the DROP keeps the first.
012 */
013public final class FieldDeclaration {
014
015    /** No statement of the script drops the table after this declaration. */
016    public static final long NEVER_DROPPED = Long.MAX_VALUE;
017
018    private final long created;
019    private final long dropped;
020    private final Set<TableColumn> fieldsDeclared =
021            Collections.newSetFromMap(new IdentityHashMap<TableColumn, Boolean>());
022    private final Set<TableColumn> declaredNodes =
023            Collections.newSetFromMap(new IdentityHashMap<TableColumn, Boolean>());
024
025    FieldDeclaration(long created, long dropped) {
026        this.created = created;
027        this.dropped = dropped;
028    }
029
030    /** The ordinal of the CREATE TABLE statement; negative when unknown. */
031    public long getCreated() {
032        return created;
033    }
034
035    /** The ordinal of the DROP TABLE that ends this declaration, {@link #NEVER_DROPPED} when none does. */
036    public long getDropped() {
037        return dropped;
038    }
039
040    /** Whether the declared type of {@code node} (a column or a field) lists its fields. */
041    public boolean declaresFieldsOf(TableColumn node) {
042        return fieldsDeclared.contains(node);
043    }
044
045    /** Whether this declaration made the field node {@code node}. */
046    public boolean declares(TableColumn node) {
047        return declaredNodes.contains(node);
048    }
049
050    void addFieldsDeclared(TableColumn node) {
051        fieldsDeclared.add(node);
052    }
053
054    void addDeclaredNode(TableColumn node) {
055        declaredNodes.add(node);
056    }
057}