001package gudusoft.gsqlparser.nodes;
002/*
003 * Date: 11-7-27
004 */
005
006import gudusoft.gsqlparser.ESqlClause;
007import gudusoft.gsqlparser.TCustomSqlStatement;
008
009/**
010 * when [not] matched clause in merge statement.
011 * <pre>
012 *     merge into ....
013 *     [when matched [and search_conditions ] then update set {col_name = expression} | delete]
014 *     [when not matched [and search_conditions ] then insert [(column_list)] values (value_list)]
015 * </pre>
016 * <p> call method {@link #getCondition()} to return search_condition if any.
017 * <p> call method {@link #getUpdateClause()}, {@link #getDeleteClause()}
018 * and {@link #getInsertClause()} separately to get related update clause,
019 * delete clause and insert clause.
020 *
021 * @see TMergeUpdateClause
022 * @see TMergeDeleteClause
023 * @see TMergeInsertClause
024 *
025 * */
026public class TMergeWhenClause extends TParseTreeNode {
027    public void setCondition(TExpression condition) {
028        this.condition = condition;
029    }
030
031    public static final int  matched  = 1;
032    public static final int  not_matched  = 2;
033    public static final int  matched_with_condition  = 3;
034    public static final int  not_matched_with_condition  = 4;
035    public static final int  not_matched_by_target  = 5;
036    public static final int  not_matched_by_target_with_condition  = 6;
037    public static final int  not_matched_by_source  = 7;
038    public static final int  not_matched_by_source_with_condition  = 8;
039
040    private int type = matched;
041
042    private TExpression condition;
043
044    private void setMatchCondition(TDummy dummy){
045        this.type = dummy.int1;
046        if ((this.type == TMergeWhenClause.matched_with_condition)
047            ||(this.type == TMergeWhenClause.not_matched_with_condition)){
048            this.condition = (TExpression)dummy.node1;
049        }
050    }
051
052    public int getType() {
053        return type;
054    }
055
056    public TExpression getCondition() {
057
058        return condition;
059    }
060
061    private TMergeUpdateClause updateClause;
062    private TMergeInsertClause insertClause;
063    private TMergeDeleteClause deleteClause;
064    private TMergeDoNothingClause doNothingClause;
065
066    public TMergeDoNothingClause getDoNothingClause() {
067        return doNothingClause;
068    }
069
070    public TMergeUpdateClause getUpdateClause() {
071        return updateClause;
072    }
073
074    public TMergeDeleteClause getDeleteClause() {
075
076        return deleteClause;
077    }
078
079    public TMergeInsertClause getInsertClause() {
080        return insertClause;
081    }
082
083    public void setType(int type) {
084        this.type = type;
085    }
086
087    public void init(Object arg1){
088        if (arg1 instanceof  TMergeUpdateClause){
089            this.updateClause = (TMergeUpdateClause)arg1;
090        }else if (arg1 instanceof  TMergeInsertClause){
091            this.insertClause = (TMergeInsertClause)arg1;
092        }else if (arg1 instanceof  TMergeDeleteClause){
093            this.deleteClause = (TMergeDeleteClause)arg1;
094        }else if (arg1 instanceof TMergeDoNothingClause){
095            this.doNothingClause = (TMergeDoNothingClause) arg1;
096        }
097    }
098    public void init(Object arg1,Object arg2){
099        if (arg1 != null){
100            this.setMatchCondition((TDummy)arg1);
101        }
102
103        if (arg2 != null){
104            init(arg2);
105        }
106    }
107
108    public void doParse(TCustomSqlStatement psql, ESqlClause plocation){
109       if (this.condition != null){
110           this.condition.doParse(psql, plocation);
111       }
112
113       if (this.updateClause != null){
114           this.updateClause.doParse(psql,plocation);
115       }
116
117        if (this.insertClause != null){
118            this.insertClause.doParse(psql,plocation);
119        }
120
121        if (this.deleteClause != null){
122            this.deleteClause.doParse(psql,plocation);
123        }
124
125    }
126
127    public void accept(TParseTreeVisitor v){
128        v.preVisit(this);
129        v.postVisit(this);
130    }
131
132    public void acceptChildren(TParseTreeVisitor v){
133        v.preVisit(this);
134        if (this.condition != null){
135            this.condition.acceptChildren(v);
136        }
137
138        if (this.updateClause != null){
139            this.updateClause.acceptChildren(v);
140        }
141
142        if (this.insertClause != null){
143            this.insertClause.acceptChildren(v);
144        }
145
146        if (this.deleteClause != null){
147            this.deleteClause.acceptChildren(v);
148        }
149        v.postVisit(this);
150    }
151
152    public void setUpdateClause(TMergeUpdateClause updateClause) {
153        this.updateClause = updateClause;
154    }
155
156    public void setInsertClause(TMergeInsertClause insertClause) {
157        this.insertClause = insertClause;
158    }
159
160    public void setDeleteClause(TMergeDeleteClause deleteClause) {
161        this.deleteClause = deleteClause;
162    }
163}