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