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.SQLCaseStatement; 17 18 import hunt.sql.ast; 19 import hunt.sql.visitor.SQLASTVisitor; 20 import hunt.collection; 21 22 public class SQLCaseStatement : SQLStatementImpl //, Serializable 23 { 24 private List!Item items; 25 private SQLExpr valueExpr; 26 private List!SQLStatement elseStatements; 27 28 public this(){ 29 items = new ArrayList!Item(); 30 elseStatements = new ArrayList!SQLStatement(); 31 } 32 33 public SQLExpr getValueExpr() { 34 return this.valueExpr; 35 } 36 37 public void setValueExpr(SQLExpr valueExpr) { 38 if (valueExpr !is null) { 39 valueExpr.setParent(this); 40 } 41 this.valueExpr = valueExpr; 42 } 43 44 public List!SQLStatement getElseStatements() { 45 return elseStatements; 46 } 47 48 public List!Item getItems() { 49 return this.items; 50 } 51 52 public void addItem(Item item) { 53 if (item !is null) { 54 item.setParent(this); 55 this.items.add(item); 56 } 57 } 58 59 override protected void accept0(SQLASTVisitor visitor) { 60 if (visitor.visit(this)) { 61 acceptChild(visitor, this.valueExpr); 62 acceptChild!(SQLCaseStatement.Item)(visitor, this.items); 63 acceptChild!SQLStatement(visitor, this.elseStatements); 64 } 65 visitor.endVisit(this); 66 } 67 68 override 69 public List!SQLObject getChildren() { 70 List!SQLObject children = new ArrayList!SQLObject(); 71 if (valueExpr !is null) { 72 children.add(valueExpr); 73 } 74 children.addAll(cast(List!SQLObject)(this.items)); 75 children.addAll(cast(List!SQLObject)(this.elseStatements)); 76 return children; 77 } 78 79 public static class Item : SQLObjectImpl //, Serializable 80 { 81 82 private SQLExpr conditionExpr; 83 private SQLStatement statement; 84 85 public this(){ 86 87 } 88 89 public this(SQLExpr conditionExpr, SQLStatement statement){ 90 91 setConditionExpr(conditionExpr); 92 setStatement(statement); 93 } 94 95 public SQLExpr getConditionExpr() { 96 return this.conditionExpr; 97 } 98 99 public void setConditionExpr(SQLExpr conditionExpr) { 100 if (conditionExpr !is null) { 101 conditionExpr.setParent(this); 102 } 103 this.conditionExpr = conditionExpr; 104 } 105 106 public SQLStatement getStatement() { 107 return this.statement; 108 } 109 110 public void setStatement(SQLStatement statement) { 111 if (statement !is null) { 112 statement.setParent(this); 113 } 114 this.statement = statement; 115 } 116 117 override protected void accept0(SQLASTVisitor visitor) { 118 if (visitor.visit(this)) { 119 acceptChild(visitor, this.conditionExpr); 120 acceptChild(visitor, this.statement); 121 } 122 visitor.endVisit(this); 123 } 124 125 override 126 public size_t toHash() @trusted nothrow { 127 int prime = 31; 128 size_t result = 1; 129 result = prime * result + ((conditionExpr is null) ? 0 : (cast(Object)conditionExpr).toHash()); 130 result = prime * result + ((statement is null) ? 0 : (cast(Object)statement).toHash()); 131 return result; 132 } 133 134 override 135 public bool opEquals(Object obj) { 136 if (this is obj) return true; 137 if (obj is null) return false; 138 if (typeid(this) != typeid(obj)) return false; 139 Item other = cast(Item) obj; 140 if (conditionExpr is null) { 141 if (other.conditionExpr !is null) return false; 142 } else if (!(cast(Object)(conditionExpr)).opEquals(cast(Object)(other.conditionExpr))) return false; 143 if (statement is null) { 144 if (other.statement !is null) return false; 145 } else if (!(cast(Object)(statement)).opEquals(cast(Object)(other.statement))) return false; 146 return true; 147 } 148 149 } 150 151 override 152 public bool opEquals(Object o) { 153 if (this == o) return true; 154 if (o is null || typeid(this) != typeid(o)) return false; 155 156 SQLCaseStatement that = cast(SQLCaseStatement) o; 157 158 if (items !is null ? !(cast(Object)(items)).opEquals(cast(Object)(that.items)) : that.items !is null) return false; 159 if (valueExpr !is null ? !(cast(Object)(valueExpr)).opEquals(cast(Object)(that.valueExpr)) : that.valueExpr !is null) return false; 160 return elseStatements !is null ? (cast(Object)(elseStatements)).opEquals(cast(Object)(that.elseStatements)) : that.elseStatements is null; 161 } 162 163 override 164 public size_t toHash() @trusted nothrow { 165 size_t result = items !is null ? (cast(Object)items).toHash() : 0; 166 result = 31 * result + (valueExpr !is null ? (cast(Object)valueExpr).toHash() : 0); 167 result = 31 * result + (elseStatements !is null ? (cast(Object)elseStatements).toHash() : 0); 168 return result; 169 } 170 }