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.SQLConstraintImpl;
17 
18 import hunt.sql.SQLUtils;
19 import hunt.sql.ast.SQLCommentHint;
20 import hunt.sql.ast.SQLExpr;
21 import hunt.sql.ast.SQLName;
22 import hunt.sql.ast.SQLObjectImpl;
23 import hunt.sql.ast.expr.SQLIdentifierExpr;
24 import hunt.sql.ast.statement.SQLConstraint;
25 import hunt.Boolean;
26 import hunt.collection;
27 
28 public abstract class SQLConstraintImpl : SQLObjectImpl , SQLConstraint {
29     protected string  dbType;
30     protected SQLName name;
31     protected Boolean enable;
32     protected Boolean validate;
33     protected Boolean rely;
34     protected SQLExpr comment;
35 
36     public List!SQLCommentHint hints;
37 
38     public this(){
39 
40     }
41 
42     public void cloneTo(SQLConstraintImpl x) {
43         if (name !is null) {
44             x.setName(name.clone());
45         }
46 
47         x.enable = enable;
48         x.validate = validate;
49         x.rely = rely;
50     }
51 
52     public List!SQLCommentHint getHints() {
53         return hints;
54     }
55 
56     public void setHints(List!SQLCommentHint hints) {
57         this.hints = hints;
58     }
59 
60 
61     public SQLName getName() {
62         return name;
63     }
64 
65     public void setName(SQLName name) {
66         this.name = name;
67     }
68 
69     public void setName(string name) {
70         this.setName(new SQLIdentifierExpr(name));
71     }
72 
73     public Boolean getEnable() {
74         return enable;
75     }
76 
77     public void setEnable(Boolean enable) {
78         this.enable = enable;
79     }
80 
81     public void cloneTo(SQLConstraint x) {
82         if (name !is null) {
83             x.setName(name.clone());
84         }
85     }
86 
87     public Boolean getValidate() {
88         return validate;
89     }
90 
91     public void setValidate(Boolean validate) {
92         this.validate = validate;
93     }
94 
95     public Boolean getRely() {
96         return rely;
97     }
98 
99     public void setRely(Boolean rely) {
100         this.rely = rely;
101     }
102 
103     public string getDbType() {
104         return dbType;
105     }
106 
107     public void setDbType(string dbType) {
108         this.dbType = dbType;
109     }
110 
111     public SQLExpr getComment() {
112         return comment;
113     }
114 
115     public void setComment(SQLExpr x) {
116         if (x !is null) {
117             x.setParent(this);
118         }
119         this.comment = x;
120     }
121 
122     public void simplify() {
123         if (cast(SQLIdentifierExpr)(this.name) !is null ) {
124             SQLIdentifierExpr identExpr = cast(SQLIdentifierExpr) this.name;
125             string columnName = identExpr.getName();
126 
127             string normalized = SQLUtils.normalize(columnName, dbType);
128             if (columnName != normalized) {
129                 this.setName(normalized);
130             }
131         }
132     }
133 }