001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.ESqlClause;
004import gudusoft.gsqlparser.TCustomSqlStatement;
005import gudusoft.gsqlparser.TSourceToken;
006import gudusoft.gsqlparser.nodes.couchbase.TBinding;
007
008/**
009 * This class represents group by clause and having clause.
010 * <p>
011 * <p>Specify the GROUP BY clause if you want the database to group the selected rows based on
012 * the value of expr(s) for each row and return a single row of summary information for each group.
013 * <p>If this clause contains CUBE or ROLLUP extensions, then the database
014 * produces superaggregate groupings in addition to the regular groupings.
015 * <p>
016 * <p>Use the HAVING clause to restrict the groups of returned rows to those groups for
017 * which the specified condition is TRUE. If you omit this clause, then the database returns summary rows for all groups.
018*/
019public class TGroupBy extends TParseTreeNode {
020
021    private TQualifyClause qualifyClause;
022
023    public void setQualifyClause(TQualifyClause qualifyClause) {
024        this.qualifyClause = qualifyClause;
025    }
026
027    public TQualifyClause getQualifyClause() {
028        return qualifyClause;
029    }
030
031    private TPTNodeList<TBinding> bindings;
032
033    public TPTNodeList<TBinding> getBindings() {
034        return bindings;
035    }
036
037    private TGroupByItemList items = null;
038    private TExpression havingClause = null;
039
040    private TSourceToken GROUP= null;
041    private TSourceToken BY= null;
042
043    private boolean rollupModifier = false;
044    private boolean cubeModifier = false;
045
046    public void setCubeModifier(boolean cubeModifier) {
047        this.cubeModifier = cubeModifier;
048    }
049
050    public boolean isCubeModifier() {
051        return cubeModifier;
052    }
053
054    public void setRollupModifier(boolean rollupModifier) {
055        this.rollupModifier = rollupModifier;
056    }
057
058    public boolean isRollupModifier() {
059
060        return rollupModifier;
061    }
062
063    public void setBY(TSourceToken BY) {
064        this.BY = BY;
065    }
066
067    public void setGROUP(TSourceToken GROUP) {
068        this.GROUP = GROUP;
069    }
070
071    public void setHAVING(TSourceToken HAVING) {
072        this.HAVING = HAVING;
073    }
074
075    public TSourceToken getBY() {
076
077        return BY;
078    }
079
080    public TSourceToken getGROUP() {
081        return GROUP;
082    }
083
084    public TSourceToken getHAVING() {
085        return HAVING;
086    }
087
088    private TSourceToken HAVING= null;
089
090    /**
091     * Restrict the groups of returned rows to those groups for which the specified condition is TRUE.
092     * <p>If you omit this clause, then the database returns summary rows for all groups.
093     * @return
094     */
095    public TExpression getHavingClause() {
096        return havingClause;
097    }
098
099    /**
100     *
101     * @return Items in group by clause, can be expr, rollup_cube_clause and grouping_sets_clause.
102     * @see TExpression
103     * @see TRollupCube
104     * @see TGroupingSet
105     */
106    public TGroupByItemList getItems() {
107        if (items == null){
108            items = new TGroupByItemList();
109        }
110        return items;
111    }
112
113    public void setHavingClause(TExpression havingClause) {
114        this.havingClause = havingClause;
115        if ((this.getHAVING() != null)&&(this.havingClause == null)){
116            this.getHAVING().removeFromChain();
117        }
118    }
119
120    public void init(Object arg1){
121        bindings = (TPTNodeList<TBinding>)arg1;
122    }
123
124    public void init(Object arg1,Object arg2)
125    {
126       if (arg1 != null){
127           if (arg1 instanceof TGroupByItemList){
128            items = (TGroupByItemList)arg1;
129           }else if (arg1 instanceof TExpressionList){
130               items = new TGroupByItemList();
131               TExpressionList expressionList  = (TExpressionList)arg1;
132               for(int i=0;i<expressionList.size();i++ ){
133                   TGroupByItem item = new TGroupByItem();
134                   item.setExpr(expressionList.getExpression(i));
135                   item.setStartToken(expressionList.getExpression(i));
136                   item.setEndToken(expressionList.getExpression(i));
137                   items.addGroupByItem(item);
138               }
139           }
140       }
141       if (arg2 != null){
142           havingClause = (TExpression)arg2;
143       }
144    }
145
146    public void doParse(TCustomSqlStatement psql, ESqlClause plocation){
147        if (items != null){
148            items.doParse(psql,plocation);
149        }
150        if (havingClause != null){
151            havingClause.doParse(psql,ESqlClause.having);
152        }
153    }
154
155    public void setBindings(TPTNodeList<TBinding> bindings) {
156        this.bindings = bindings;
157    }
158
159    public void accept(TParseTreeVisitor v){
160        v.preVisit(this);
161        v.postVisit(this);
162    }
163
164    public void acceptChildren(TParseTreeVisitor v){
165        v.preVisit(this);
166        if (this.getItems() != null){
167            this.getItems().acceptChildren(v);
168        }
169
170        if (this.getHavingClause() != null){
171            this.getHavingClause().acceptChildren(v);
172        }
173        v.postVisit(this);
174    }
175
176    public void setItems(TGroupByItemList items) {
177        this.items = items;
178    }
179}