1 /*
2  * Copyright 2015-2018 HuntLabs.cn
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 module hunt.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
17 
18 import hunt.Boolean;
19 import hunt.collection;
20 import hunt.sql.ast.SQLObject;
21 
22 import hunt.sql.SQLUtils;
23 import hunt.sql.ast;
24 import hunt.sql.ast.statement.SQLSelectItem;
25 import hunt.sql.ast.statement.SQLSelectQueryBlock;
26 import hunt.sql.dialect.mysql.ast.MySqlObject;
27 import hunt.sql.dialect.mysql.visitor.MySqlASTVisitor;
28 // import hunt.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
29 import hunt.sql.visitor.SQLASTVisitor;
30 import hunt.sql.util.DBType;
31 
32 public class MySqlSelectQueryBlock : SQLSelectQueryBlock , MySqlObject {
33     private bool              hignPriority;
34     private bool              straightJoin;
35     private bool              smallResult;
36     private bool              bigResult;
37     private bool              bufferResult;
38     private Boolean              cache;
39     private bool              calcFoundRows;
40     private SQLName              procedureName;
41     private List!(SQLExpr)        procedureArgumentList;
42     private bool              lockInShareMode;
43     private SQLName              forcePartition; // for petadata
44 
45     public this(){
46         dbType = DBType.MYSQL.name;
47     }
48 
49     override public MySqlSelectQueryBlock clone() {
50         MySqlSelectQueryBlock x = new MySqlSelectQueryBlock();
51         cloneTo(x);
52 
53         x.hignPriority = hignPriority;
54         x.straightJoin = straightJoin;
55 
56         x.smallResult = smallResult;
57         x.bigResult = bigResult;
58         x.bufferResult = bufferResult;
59         x.cache = cache;
60         x.calcFoundRows = calcFoundRows;
61 
62         if (procedureName !is null) {
63             x.setProcedureName(procedureName.clone());
64         }
65         if (procedureArgumentList !is null) {
66             foreach(SQLExpr arg ; procedureArgumentList) {
67                 SQLExpr arg_cloned = arg.clone();
68                 arg_cloned.setParent(this);
69                 x.procedureArgumentList.add(arg_cloned);
70             }
71         }
72         x.lockInShareMode = lockInShareMode;
73 
74         return x;
75     }
76 
77     public bool isLockInShareMode() {
78         return lockInShareMode;
79     }
80 
81     public void setLockInShareMode(bool lockInShareMode) {
82         this.lockInShareMode = lockInShareMode;
83     }
84 
85     public SQLName getProcedureName() {
86         return procedureName;
87     }
88 
89     public void setProcedureName(SQLName procedureName) {
90         this.procedureName = procedureName;
91     }
92 
93     public List!(SQLExpr) getProcedureArgumentList() {
94         if (procedureArgumentList is null) {
95             procedureArgumentList = new ArrayList!(SQLExpr)(2);
96         }
97         return procedureArgumentList;
98     }
99 
100     public bool isHignPriority() {
101         return hignPriority;
102     }
103 
104     public void setHignPriority(bool hignPriority) {
105         this.hignPriority = hignPriority;
106     }
107 
108     public bool isStraightJoin() {
109         return straightJoin;
110     }
111 
112     public void setStraightJoin(bool straightJoin) {
113         this.straightJoin = straightJoin;
114     }
115 
116     public bool isSmallResult() {
117         return smallResult;
118     }
119 
120     public void setSmallResult(bool smallResult) {
121         this.smallResult = smallResult;
122     }
123 
124     public bool isBigResult() {
125         return bigResult;
126     }
127 
128     public void setBigResult(bool bigResult) {
129         this.bigResult = bigResult;
130     }
131 
132     public bool isBufferResult() {
133         return bufferResult;
134     }
135 
136     public void setBufferResult(bool bufferResult) {
137         this.bufferResult = bufferResult;
138     }
139 
140     public Boolean getCache() {
141         return cache;
142     }
143 
144     public void setCache(Boolean cache) {
145         this.cache = cache;
146     }
147 
148     public bool isCalcFoundRows() {
149         return calcFoundRows;
150     }
151 
152     public void setCalcFoundRows(bool calcFoundRows) {
153         this.calcFoundRows = calcFoundRows;
154     }
155 
156     override
157     public size_t toHash() @trusted nothrow {
158          int prime = 31;
159         size_t result = 1;
160         result = prime * result + (bigResult ? 1231 : 1237);
161         result = prime * result + (bufferResult ? 1231 : 1237);
162         result = prime * result + hashOf(cache);
163         result = prime * result + (calcFoundRows ? 1231 : 1237);
164         result = prime * result + (forUpdate ? 1231 : 1237);
165         result = prime * result + (hignPriority ? 1231 : 1237);
166         result = prime * result + ((hints is null) ? 0 : (cast(Object)hints).toHash());
167         result = prime * result + ((_limit is null) ? 0 : (cast(Object)_limit).toHash());
168         result = prime * result + (lockInShareMode ? 1231 : 1237);
169         result = prime * result + ((orderBy is null) ? 0 : (cast(Object)orderBy).toHash());
170         result = prime * result + ((procedureArgumentList is null) ? 0 : (cast(Object)procedureArgumentList).toHash());
171         result = prime * result + ((procedureName is null) ? 0 : (cast(Object)procedureName).toHash());
172         result = prime * result + (smallResult ? 1231 : 1237);
173         result = prime * result + (straightJoin ? 1231 : 1237);
174         return result;
175     }
176 
177     override
178     public bool opEquals(Object obj) {
179         if (this == obj) return true;
180         if (obj is null) return false;
181         if ( typeid(this) != typeid(obj)) return false;
182         MySqlSelectQueryBlock other = cast(MySqlSelectQueryBlock) obj;
183         if (bigResult != other.bigResult) return false;
184         if (bufferResult != other.bufferResult) return false;
185         if (cache is null) {
186             if (other.cache !is null) return false;
187         } else if (!(cache == other.cache)) return false;
188         if (calcFoundRows != other.calcFoundRows) return false;
189         if (forUpdate != other.forUpdate) return false;
190         if (hignPriority != other.hignPriority) return false;
191         if (hints is null) {
192             if (other.hints !is null) return false;
193         } else if (!(cast(Object)(hints)).opEquals(cast(Object)(other.hints))) return false;
194         if (_limit is null) {
195             if (other._limit !is null) return false;
196         } else if (!(_limit == other._limit)) return false;
197         if (lockInShareMode != other.lockInShareMode) return false;
198         if (orderBy is null) {
199             if (other.orderBy !is null) return false;
200         } else if (!(cast(Object)(orderBy)).opEquals(cast(Object)(other.orderBy))) return false;
201         if (procedureArgumentList is null) {
202             if (other.procedureArgumentList !is null) return false;
203         } else if (!(cast(Object)(procedureArgumentList)).opEquals(cast(Object)(other.procedureArgumentList))) return false;
204         if (procedureName is null) {
205             if (other.procedureName !is null) return false;
206         } else if (!(cast(Object)(procedureName)).opEquals(cast(Object)(other.procedureName))) return false;
207         if (smallResult != other.smallResult) return false;
208         if (straightJoin != other.straightJoin) return false;
209         return true;
210     }
211 
212     
213     override  protected void accept0(SQLASTVisitor visitor) {
214         if (cast(MySqlASTVisitor)(visitor) !is null) {
215             accept0(cast(MySqlASTVisitor) visitor);
216             return;
217         }
218 
219         super.accept0(visitor);
220     }
221 
222     override
223     public void accept0(MySqlASTVisitor visitor) {
224         if (visitor.visit(this)) {
225             acceptChild!SQLSelectItem(visitor, this.selectList);
226             acceptChild(visitor, this.forcePartition);
227             acceptChild(visitor, this.from);
228             acceptChild(visitor, this.where);
229             acceptChild(visitor, this.groupBy);
230             acceptChild(visitor, this.orderBy);
231             acceptChild(visitor, this._limit);
232             acceptChild(visitor, this.procedureName);
233             acceptChild!SQLExpr(visitor, this.procedureArgumentList);
234             acceptChild(visitor, this.into);
235         }
236 
237         visitor.endVisit(this);
238     }
239 
240     public SQLName getForcePartition() {
241         return forcePartition;
242     }
243 
244     public void setForcePartition(SQLName x) {
245         if (x !is null) {
246             x.setParent(this);
247         }
248         this.forcePartition = x;
249     }
250 
251     override public string toString() {
252         return SQLUtils.toMySqlString(this);
253     }
254 }