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.statement.SQLSelectOrderByItem; 17 18 import hunt.sql.ast.SQLExpr; 19 import hunt.sql.ast.SQLObjectImpl; 20 import hunt.sql.ast.SQLOrderingSpecification; 21 import hunt.sql.ast.SQLReplaceable; 22 import hunt.sql.visitor.SQLASTVisitor; 23 import hunt.sql.ast.statement.SQLSelectItem; 24 25 public class SQLSelectOrderByItem : SQLObjectImpl , SQLReplaceable { 26 27 protected SQLExpr expr; 28 protected string collate; 29 protected SQLOrderingSpecification type; 30 protected NullsOrderType nullsOrderType; 31 32 protected SQLSelectItem resolvedSelectItem; 33 34 public this(){ 35 36 } 37 38 public this(SQLExpr expr){ 39 this.setExpr(expr); 40 } 41 42 public SQLExpr getExpr() { 43 return this.expr; 44 } 45 46 public void setExpr(SQLExpr expr) { 47 if (expr !is null) { 48 expr.setParent(this); 49 } 50 this.expr = expr; 51 } 52 53 public string getCollate() { 54 return collate; 55 } 56 57 public void setCollate(string collate) { 58 this.collate = collate; 59 } 60 61 public SQLOrderingSpecification getType() { 62 return this.type; 63 } 64 65 public void setType(SQLOrderingSpecification type) { 66 this.type = type; 67 } 68 69 public NullsOrderType getNullsOrderType() { 70 return this.nullsOrderType; 71 } 72 73 public void setNullsOrderType(NullsOrderType nullsOrderType) { 74 this.nullsOrderType = nullsOrderType; 75 } 76 77 override protected void accept0(SQLASTVisitor visitor) { 78 if (visitor.visit(this)) { 79 acceptChild(visitor, this.expr); 80 } 81 82 visitor.endVisit(this); 83 } 84 85 override 86 public size_t toHash() @trusted nothrow { 87 int prime = 31; 88 size_t result = 1; 89 result = prime * result + hashOf(collate); 90 result = prime * result + ((expr is null) ? 0 : (cast(Object)expr).toHash()); 91 result = prime * result + (type is null) ? 0 : type.toHash(); 92 return result; 93 } 94 95 override 96 public bool opEquals(Object obj) { 97 if (this == obj) return true; 98 if (obj is null) return false; 99 if (typeid(this) != typeid(obj)) return false; 100 SQLSelectOrderByItem other = cast(SQLSelectOrderByItem) obj; 101 if (collate is null) { 102 if (other.collate !is null) return false; 103 } else if (!(collate == other.collate)) return false; 104 if (expr is null) { 105 if (other.expr !is null) return false; 106 } else if (!(cast(Object)(expr)).opEquals(cast(Object)(other.expr))) return false; 107 if (type != other.type) return false; 108 return true; 109 } 110 111 override 112 public bool replace(SQLExpr expr, SQLExpr target) { 113 if (this.expr == expr) { 114 this.setExpr(target); 115 return true; 116 } 117 return false; 118 } 119 120 public static struct NullsOrderType { 121 enum NullsOrderType NullsFirst = NullsOrderType("NULLS FIRST"); 122 enum NullsOrderType NullsLast = NullsOrderType("NULLS LAST"); 123 124 private string _name; 125 this(string name) 126 { 127 _name = name; 128 } 129 130 @property string name() 131 { 132 return _name; 133 } 134 135 public string toFormalString() { 136 return _name; 137 } 138 } 139 140 override public SQLSelectOrderByItem clone() { 141 SQLSelectOrderByItem x = new SQLSelectOrderByItem(); 142 if (expr !is null) { 143 x.setExpr(expr.clone()); 144 } 145 x.collate = collate; 146 x.type = type; 147 x.nullsOrderType = nullsOrderType; 148 return x; 149 } 150 151 public SQLSelectItem getResolvedSelectItem() { 152 return resolvedSelectItem; 153 } 154 155 public void setResolvedSelectItem(SQLSelectItem resolvedSelectItem) { 156 this.resolvedSelectItem = resolvedSelectItem; 157 } 158 }