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.ast.expr.SQLAnyExpr; 17 18 import hunt.sql.ast.SQLDataType; 19 import hunt.sql.ast.SQLExprImpl; 20 import hunt.sql.ast.SQLObject; 21 import hunt.sql.ast.statement.SQLSelect; 22 import hunt.sql.ast.statement.SQLSelectItem; 23 import hunt.sql.ast.statement.SQLSelectQueryBlock; 24 import hunt.sql.visitor.SQLASTVisitor; 25 import hunt.collection; 26 import hunt.util.StringBuilder; 27 28 public class SQLAnyExpr : SQLExprImpl { 29 30 public SQLSelect subQuery; 31 32 public this(){ 33 34 } 35 36 public this(SQLSelect select){ 37 setSubQuery(select); 38 } 39 40 override public SQLAnyExpr clone() { 41 SQLAnyExpr x = new SQLAnyExpr(); 42 if (subQuery !is null) { 43 x.setSubQuery(subQuery.clone()); 44 } 45 return x; 46 } 47 48 public SQLSelect getSubQuery() { 49 return this.subQuery; 50 } 51 52 public void setSubQuery(SQLSelect x) { 53 if (x !is null) { 54 x.setParent(this); 55 } 56 this.subQuery = x; 57 } 58 59 override public void output(StringBuilder buf) { 60 this.subQuery.output(buf); 61 } 62 63 override protected void accept0(SQLASTVisitor visitor) { 64 if (visitor.visit(this)) { 65 acceptChild(visitor, this.subQuery); 66 } 67 68 visitor.endVisit(this); 69 } 70 71 override public List!SQLObject getChildren() { 72 return Collections.singletonList!SQLObject(this.subQuery); 73 } 74 75 override 76 public size_t toHash() @trusted nothrow { 77 int prime = 31; 78 size_t result = 1; 79 result = prime * result + ((subQuery is null) ? 0 : (cast(Object)subQuery).toHash()); 80 return result; 81 } 82 83 override 84 public bool opEquals(Object obj) { 85 if (this is obj) { 86 return true; 87 } 88 if (obj is null) { 89 return false; 90 } 91 if (typeid(this) != typeid(obj)) { 92 return false; 93 } 94 SQLAnyExpr other = cast(SQLAnyExpr) obj; 95 if (subQuery is null) { 96 if (other.subQuery !is null) { 97 return false; 98 } 99 } else if (!(cast(Object)(subQuery)).opEquals(cast(Object)(other.subQuery))) { 100 return false; 101 } 102 return true; 103 } 104 105 override public SQLDataType computeDataType() { 106 if (subQuery is null) { 107 return null; 108 } 109 110 SQLSelectQueryBlock queryBlock = subQuery.getFirstQueryBlock(); 111 if (queryBlock is null) { 112 return null; 113 } 114 115 List!SQLSelectItem selectList = queryBlock.getSelectList(); 116 if (selectList.size() == 1) { 117 return selectList.get(0).computeDataType(); 118 } 119 120 return null; 121 } 122 }