1 module hunt.sql.ast.expr.SQLVariantRefExpr;
2 
3 import hunt.sql.ast.SQLExprImpl;
4 import hunt.collection;
5 import hunt.sql.visitor.SQLASTVisitor;
6 import hunt.sql.ast.SQLObject;
7 import hunt.util.StringBuilder;
8 
9 public class SQLVariantRefExpr : SQLExprImpl {
10 
11     private string  name;
12 
13     private bool global = false;
14 
15     private bool session = false;
16 
17     private int     index  = -1;
18 
19     public this(string name){
20         this.name = name;
21     }
22 
23     public this(string name, bool global){
24         this.name = name;
25         this.global = global;
26     }
27 
28     public this(string name, bool global,bool session){
29         this.name = name;
30         this.global = global;
31         this.session = session;
32     }
33 
34     public this(){
35 
36     }
37 
38     public int getIndex() {
39         return index;
40     }
41 
42     public void setIndex(int index) {
43         this.index = index;
44     }
45 
46     public string getName() {
47         return this.name;
48     }
49 
50     public void setName(string name) {
51         this.name = name;
52     }
53 
54     override public void output(StringBuilder buf) {
55         buf.append(this.name);
56     }
57 
58 
59     public bool isSession() {
60         return session;
61     }
62 
63     public void setSession(bool session) {
64         this.session = session;
65     }
66 
67     protected override void accept0(SQLASTVisitor visitor) {
68         visitor.visit(this);
69 
70         visitor.endVisit(this);
71     }
72 
73     public override size_t toHash() @trusted nothrow {
74          int prime = 31;
75         size_t result = 1;
76         result = prime * result + hashOf(name);
77         return result;
78     }
79 
80     public override bool opEquals(Object obj) {
81         if (this is obj) {
82             return true;
83         }
84         if (obj is null) {
85             return false;
86         }
87         // if (!(cast(SQLVariantRefExpr)(obj) !is null)) {
88         //     return false;
89         // }
90         SQLVariantRefExpr other = cast(SQLVariantRefExpr) obj;
91         if(other is null)
92             return false;
93         if (name is null) {
94             if (other.name !is null) {
95                 return false;
96             }
97         } else if (!(name == other.name)) {
98             return false;
99         }
100         return true;
101     }
102 
103     public bool isGlobal() {
104         return global;
105     }
106 
107     public void setGlobal(bool global) {
108         this.global = global;
109     }
110 
111     override public SQLVariantRefExpr clone() {
112         SQLVariantRefExpr var =  new SQLVariantRefExpr(name, global);
113         var.index = index;
114 
115         if (attributes !is null) {
116             var.attributes = new HashMap!(string, Object)(attributes.size());
117             foreach (string k,Object v ; attributes) {
118                 // string k = entry.getKey();
119                 // Object v = entry.getValue();
120 
121                 if (cast(SQLObject)v !is null) {
122                     var.attributes.put(k, cast(Object)((cast(SQLObject) v).clone()));
123                 } else {
124                     var.attributes.put(k, v);
125                 }
126             }
127         }
128 
129         return var;
130     }
131 
132     public override List!SQLObject getChildren() {
133         return Collections.emptyList!(SQLObject)();
134     }
135 }