1 module hunt.sql.ast.SQLDataTypeImpl;
2 
3 import hunt.sql.ast.SQLObjectImpl;
4 import hunt.sql.ast.SQLDataType;
5 import hunt.sql.ast.SQLExpr;
6 import hunt.sql.visitor.SQLASTVisitor;
7 import hunt.sql.ast.SQLObject;
8 import hunt.sql.util.FnvHash;
9 import hunt.sql.SQLUtils;
10 
11 import hunt.Boolean;
12 import hunt.collection;
13 
14 
15 
16 public class SQLDataTypeImpl : SQLObjectImpl , SQLDataType {
17 
18     private         string        name;
19     private         long          _nameHashCode64;
20     protected  List!SQLExpr arguments;
21     private         Boolean       withTimeZone;
22     private         bool       withLocalTimeZone = false;
23     private         string        dbType;
24 
25     private         bool       unsigned;
26     private         bool       zerofill;
27 
28     public this(){
29         arguments = new ArrayList!SQLExpr();
30     }
31 
32     public this(string name){
33         this.name = name;
34         arguments = new ArrayList!SQLExpr();
35     }
36 
37     public this(string name, int precision) {
38         this(name);
39         // addArgument(new SQLIntegerExpr(precision));@gxc
40     }
41 
42     public this(string name, SQLExpr arg) {
43         this(name);
44         addArgument(arg);
45     }
46 
47     public this(string name, int precision, int scale) {
48         this(name);
49         // addArgument(new SQLIntegerExpr(precision)); @gxc
50         // addArgument(new SQLIntegerExpr(scale)); @gxc
51     }
52 
53     protected override void accept0(SQLASTVisitor visitor) {
54         if (visitor.visit(this)) {
55             acceptChild!SQLExpr(visitor, this.arguments);
56         }
57 
58         visitor.endVisit(this);
59     }
60 
61     public string getName() {
62         return this.name;
63     }
64 
65     public long nameHashCode64() @trusted nothrow {
66         if (_nameHashCode64 == 0) {
67             _nameHashCode64 = FnvHash.hashCode64(name);
68         }
69         return _nameHashCode64;
70     }
71 
72     public void setName(string name) {
73         this.name = name;
74         _nameHashCode64 = 0L;
75     }
76 
77     public List!SQLExpr getArguments() {
78         return this.arguments;
79     }
80     
81     public void addArgument(SQLExpr argument) {
82         if (argument !is null) {
83             argument.setParent(this);
84         }
85         this.arguments.add(argument);
86     }
87 
88     override public  bool opEquals(Object o) {
89         if (this == o) return true;
90         // if (o is null || typeid(this) != typeid(o)) return false;
91 
92         SQLDataTypeImpl dataType = cast(SQLDataTypeImpl) o;
93 
94         if (name !is null ? !(name == dataType.name) : dataType.name !is null) return false;
95         if (arguments !is null ? !(arguments == dataType.arguments) : dataType.arguments !is null) return false;
96         return withTimeZone !is null ? 
97             (withTimeZone.value() == dataType.withTimeZone.value) : (dataType.withTimeZone is null);
98     }
99 
100     override public  size_t toHash() @trusted nothrow {
101         long value = nameHashCode64();
102         return cast(size_t)(value ^ (value >>> 32));
103     }
104 
105     public override Boolean getWithTimeZone() {
106         return withTimeZone;
107     }
108 
109     public void setWithTimeZone(Boolean withTimeZone) {
110         this.withTimeZone = withTimeZone;
111     }
112 
113     public bool isWithLocalTimeZone() {
114         return withLocalTimeZone;
115     }
116 
117     public void setWithLocalTimeZone(bool withLocalTimeZone) {
118         this.withLocalTimeZone = withLocalTimeZone;
119     }
120 
121     public string getDbType() {
122         return dbType;
123     }
124 
125     public void setDbType(string dbType) {
126         this.dbType = dbType;
127     }
128 
129     public  override SQLDataTypeImpl clone() {
130         SQLDataTypeImpl x = new SQLDataTypeImpl();
131 
132         cloneTo(x);
133 
134         return x;
135     }
136 
137     public void cloneTo(SQLDataTypeImpl x) {
138         x.dbType = dbType;
139         x.name = name;
140         x._nameHashCode64 = _nameHashCode64;
141 
142         foreach(SQLExpr arg ; arguments) {
143             x.addArgument(arg.clone());
144         }
145 
146         x.withTimeZone = withTimeZone;
147         x.withLocalTimeZone = withLocalTimeZone;
148         x.zerofill = zerofill;
149         x.unsigned = unsigned;
150     }
151 
152     public override string toString() {
153         return SQLUtils.toSQLString(this, dbType);
154     }
155 
156     public bool isUnsigned() {
157         return unsigned;
158     }
159 
160     public void setUnsigned(bool unsigned) {
161         this.unsigned = unsigned;
162     }
163 
164     public bool isZerofill() {
165         return zerofill;
166     }
167 
168     public void setZerofill(bool zerofill) {
169         this.zerofill = zerofill;
170     }
171 }