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.SQLCharacterDataType;
17 
18 import hunt.sql.SQLUtils;
19 import hunt.sql.ast.SQLCommentHint;
20 import hunt.sql.ast.SQLDataTypeImpl;
21 import hunt.sql.ast.SQLExpr;
22 import hunt.sql.ast.expr.SQLIntegerExpr;
23 import hunt.sql.visitor.SQLASTVisitor;
24 import hunt.sql.ast.SQLObject;
25 
26 import hunt.collection;
27 
28 public class SQLCharacterDataType : SQLDataTypeImpl {
29 
30     private string             charSetName;
31     private string             collate;
32 
33     private string             charType;
34     private bool            hasBinary;
35 
36     public List!SQLCommentHint hints;
37 
38     public  static string CHAR_TYPE_BYTE = "BYTE";
39     public  static string CHAR_TYPE_CHAR = "CHAR";
40 
41     public this(string name){
42         super(name);
43     }
44 
45     public this(string name, int precision){
46         super(name, precision);
47     }
48 
49     public string getCharSetName() {
50         return charSetName;
51     }
52 
53     public void setCharSetName(string charSetName) {
54         this.charSetName = charSetName;
55     }
56     
57     public bool isHasBinary() {
58         return hasBinary;
59     }
60 
61     public void setHasBinary(bool hasBinary) {
62         this.hasBinary = hasBinary;
63     }
64 
65     public string getCollate() {
66         return collate;
67     }
68 
69     public void setCollate(string collate) {
70         this.collate = collate;
71     }
72 
73     public string getCharType() {
74         return charType;
75     }
76 
77     public void setCharType(string charType) {
78         this.charType = charType;
79     }
80 
81     public List!SQLCommentHint getHints() {
82         return hints;
83     }
84 
85     public void setHints(List!SQLCommentHint hints) {
86         this.hints = hints;
87     }
88 
89     public int getLength() {
90         if (this.arguments.size() == 1) {
91             SQLExpr arg = this.arguments.get(0);
92             if (cast(SQLIntegerExpr)(arg) !is null ) {
93                 return (cast(SQLIntegerExpr) arg).getNumber().intValue();
94             }
95         }
96 
97         return -1;
98     }
99 
100     
101     override  protected void accept0(SQLASTVisitor visitor) {
102         if (visitor.visit(this)) {
103             acceptChild!SQLExpr(visitor, this.arguments);
104         }
105 
106         visitor.endVisit(this);
107     }
108 
109 
110     override public SQLCharacterDataType clone() {
111         SQLCharacterDataType x = new SQLCharacterDataType(getName());
112 
113         super.cloneTo(x);
114 
115         x.charSetName = charSetName;
116         x.collate = collate;
117         x.charType = charType;
118         x.hasBinary = hasBinary;
119 
120         return x;
121     }
122 
123     override
124     public string toString() {
125         return SQLUtils.toSQLString(this);
126     }
127 }