001package gudusoft.gsqlparser.nodes.teradata;
002
003import gudusoft.gsqlparser.nodes.TObjectName;
004import gudusoft.gsqlparser.nodes.TParseTreeNode;
005import gudusoft.gsqlparser.nodes.TParseTreeVisitor;
006
007/**
008 * Represents a Teradata PERIOD FOR clause that defines a derived temporal column.
009 *
010 * <p>Syntax:
011 * <pre>
012 * PERIOD FOR period_name (start_column, end_column) [AS VALIDTIME | AS TRANSACTIONTIME]
013 * </pre>
014 *
015 * <p>The PERIOD FOR clause creates a derived column that represents a time period
016 * spanning from start_column to end_column. This derived column can be used with
017 * temporal constraints like SEQUENCED VALIDTIME PRIMARY KEY.
018 *
019 * <p>Example:
020 * <pre>
021 * PERIOD FOR effective_period (eff_begin_dt, eff_end_dt) AS VALIDTIME
022 * </pre>
023 *
024 * @see <a href="https://docs.teradata.com/r/Teradata-Database-SQL-Data-Definition-Language-Syntax-and-Examples/June-2017/Table-Statements/CREATE-TABLE/Syntax-for-Period-Definition">Teradata PERIOD FOR Documentation</a>
025 */
026public class TTeradataPeriodForClause extends TParseTreeNode {
027
028    /**
029     * Temporal type for the PERIOD FOR clause.
030     */
031    public enum ETemporalType {
032        /** No temporal type specified (basic PERIOD FOR) */
033        none,
034        /** AS VALIDTIME - business effective dating */
035        validtime,
036        /** AS TRANSACTIONTIME - system versioning */
037        transactiontime
038    }
039
040    private TObjectName periodName;
041    private TObjectName startColumnName;
042    private TObjectName endColumnName;
043    private ETemporalType temporalType = ETemporalType.none;
044
045    /**
046     * Initialize with period name and column references.
047     *
048     * @param periodName the name of the derived period column
049     * @param startColumnName the start column of the period
050     * @param endColumnName the end column of the period
051     */
052    public void init(Object periodName, Object startColumnName, Object endColumnName) {
053        this.periodName = (TObjectName) periodName;
054        this.startColumnName = (TObjectName) startColumnName;
055        this.endColumnName = (TObjectName) endColumnName;
056    }
057
058    /**
059     * Gets the name of the derived period column.
060     *
061     * @return the period column name
062     */
063    public TObjectName getPeriodName() {
064        return periodName;
065    }
066
067    /**
068     * Sets the name of the derived period column.
069     *
070     * @param periodName the period column name
071     */
072    public void setPeriodName(TObjectName periodName) {
073        this.periodName = periodName;
074    }
075
076    /**
077     * Gets the start column name for the period.
078     *
079     * @return the start column reference
080     */
081    public TObjectName getStartColumnName() {
082        return startColumnName;
083    }
084
085    /**
086     * Sets the start column name for the period.
087     *
088     * @param startColumnName the start column reference
089     */
090    public void setStartColumnName(TObjectName startColumnName) {
091        this.startColumnName = startColumnName;
092    }
093
094    /**
095     * Gets the end column name for the period.
096     *
097     * @return the end column reference
098     */
099    public TObjectName getEndColumnName() {
100        return endColumnName;
101    }
102
103    /**
104     * Sets the end column name for the period.
105     *
106     * @param endColumnName the end column reference
107     */
108    public void setEndColumnName(TObjectName endColumnName) {
109        this.endColumnName = endColumnName;
110    }
111
112    /**
113     * Gets the temporal type (VALIDTIME, TRANSACTIONTIME, or none).
114     *
115     * @return the temporal type
116     */
117    public ETemporalType getTemporalType() {
118        return temporalType;
119    }
120
121    /**
122     * Sets the temporal type.
123     *
124     * @param temporalType the temporal type
125     */
126    public void setTemporalType(ETemporalType temporalType) {
127        this.temporalType = temporalType;
128    }
129
130    @Override
131    public void accept(TParseTreeVisitor v) {
132        v.preVisit(this);
133        v.postVisit(this);
134    }
135
136    @Override
137    public void acceptChildren(TParseTreeVisitor v) {
138        v.preVisit(this);
139        if (periodName != null) {
140            periodName.acceptChildren(v);
141        }
142        if (startColumnName != null) {
143            startColumnName.acceptChildren(v);
144        }
145        if (endColumnName != null) {
146            endColumnName.acceptChildren(v);
147        }
148        v.postVisit(this);
149    }
150
151    @Override
152    public String toString() {
153        StringBuilder sb = new StringBuilder();
154        sb.append("PERIOD FOR ");
155        if (periodName != null) {
156            sb.append(periodName.toString());
157        }
158        sb.append(" (");
159        if (startColumnName != null) {
160            sb.append(startColumnName.toString());
161        }
162        sb.append(", ");
163        if (endColumnName != null) {
164            sb.append(endColumnName.toString());
165        }
166        sb.append(")");
167        if (temporalType == ETemporalType.validtime) {
168            sb.append(" AS VALIDTIME");
169        } else if (temporalType == ETemporalType.transactiontime) {
170            sb.append(" AS TRANSACTIONTIME");
171        }
172        return sb.toString();
173    }
174}