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    private TSourceToken ALL= null;
043
044    private boolean rollupModifier = false;
045    private boolean cubeModifier = false;
046    private boolean allModifier = false;
047
048    public void setCubeModifier(boolean cubeModifier) {
049        this.cubeModifier = cubeModifier;
050    }
051
052    public boolean isCubeModifier() {
053        return cubeModifier;
054    }
055
056    public void setRollupModifier(boolean rollupModifier) {
057        this.rollupModifier = rollupModifier;
058    }
059
060    public boolean isRollupModifier() {
061
062        return rollupModifier;
063    }
064
065    /**
066     * Sets whether this GROUP BY clause uses the ALL modifier.
067     * <p>GROUP BY ALL is a shorthand notation to add all SELECT-list expressions
068     * not containing aggregate functions as group expressions.
069     * <p>Applies to Databricks SQL Databricks Runtime 12.2 LTS and above.
070     *
071     * @param allModifier true if GROUP BY ALL is used, false otherwise
072     */
073    public void setAllModifier(boolean allModifier) {
074        this.allModifier = allModifier;
075    }
076
077    /**
078     * Returns whether this GROUP BY clause uses the ALL modifier.
079     * <p>GROUP BY ALL automatically groups by all non-aggregate expressions
080     * in the SELECT clause.
081     *
082     * @return true if GROUP BY ALL is used, false otherwise
083     */
084    public boolean isAllModifier() {
085        return allModifier;
086    }
087
088    public void setBY(TSourceToken BY) {
089        this.BY = BY;
090    }
091
092    public void setGROUP(TSourceToken GROUP) {
093        this.GROUP = GROUP;
094    }
095
096    public void setHAVING(TSourceToken HAVING) {
097        this.HAVING = HAVING;
098    }
099
100    /**
101     * Sets the ALL token for GROUP BY ALL syntax.
102     *
103     * @param ALL the ALL token
104     */
105    public void setALL(TSourceToken ALL) {
106        this.ALL = ALL;
107    }
108
109    public TSourceToken getBY() {
110
111        return BY;
112    }
113
114    public TSourceToken getGROUP() {
115        return GROUP;
116    }
117
118    public TSourceToken getHAVING() {
119        return HAVING;
120    }
121
122    /**
123     * Gets the ALL token for GROUP BY ALL syntax.
124     *
125     * @return the ALL token, or null if GROUP BY ALL is not used
126     */
127    public TSourceToken getALL() {
128        return ALL;
129    }
130
131    private TSourceToken HAVING= null;
132
133    /**
134     * Restrict the groups of returned rows to those groups for which the specified condition is TRUE.
135     * <p>If you omit this clause, then the database returns summary rows for all groups.
136     * @return
137     */
138    public TExpression getHavingClause() {
139        return havingClause;
140    }
141
142    /**
143     *
144     * @return Items in group by clause, can be expr, rollup_cube_clause and grouping_sets_clause.
145     * @see TExpression
146     * @see TRollupCube
147     * @see TGroupingSet
148     */
149    public TGroupByItemList getItems() {
150        if (items == null){
151            items = new TGroupByItemList();
152        }
153        return items;
154    }
155
156    public void setHavingClause(TExpression havingClause) {
157        this.havingClause = havingClause;
158        if ((this.getHAVING() != null)&&(this.havingClause == null)){
159            this.getHAVING().removeFromChain();
160        }
161    }
162
163    public void init(Object arg1){
164        bindings = (TPTNodeList<TBinding>)arg1;
165    }
166
167    public void init(Object arg1,Object arg2)
168    {
169        if (arg1 != null){
170            if (arg1 instanceof TGroupByItemList){
171                items = (TGroupByItemList)arg1;
172            }else if (arg1 instanceof TExpressionList){
173                items = new TGroupByItemList();
174                TExpressionList expressionList  = (TExpressionList)arg1;
175                for(int i=0;i<expressionList.size();i++ ){
176                    TGroupByItem item = new TGroupByItem();
177                    item.setExpr(expressionList.getExpression(i));
178                    item.setStartToken(expressionList.getExpression(i));
179                    item.setEndToken(expressionList.getExpression(i));
180                    items.addGroupByItem(item);
181                }
182            }
183        }
184        if (arg2 != null){
185            havingClause = (TExpression)arg2;
186        }
187    }
188
189    public void doParse(TCustomSqlStatement psql, ESqlClause plocation){
190        if (items != null){
191            items.doParse(psql,plocation);
192        }
193        if (havingClause != null){
194            havingClause.doParse(psql,ESqlClause.having);
195        }
196    }
197
198    public void setBindings(TPTNodeList<TBinding> bindings) {
199        this.bindings = bindings;
200    }
201
202    public void accept(TParseTreeVisitor v){
203        v.preVisit(this);
204        v.postVisit(this);
205    }
206
207    public void acceptChildren(TParseTreeVisitor v){
208        v.preVisit(this);
209        if (this.getItems() != null){
210            this.getItems().acceptChildren(v);
211        }
212
213        if (this.getHavingClause() != null){
214            this.getHavingClause().acceptChildren(v);
215        }
216        v.postVisit(this);
217    }
218
219    public void setItems(TGroupByItemList items) {
220        this.items = items;
221    }
222}