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.SQLForeignKeyImpl;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast.SQLName;
22 import hunt.sql.visitor.SQLASTVisitor;
23 import hunt.sql.ast.statement.SQLConstraintImpl;
24 import hunt.sql.ast.statement.SQLForeignKeyConstraint;
25 import hunt.sql.ast.statement.SQLExprTableSource;
26 import std.uni;
27 import hunt.sql.ast.SQLObject;
28 
29 public class SQLForeignKeyImpl : SQLConstraintImpl , SQLForeignKeyConstraint {
30 
31     alias cloneTo = SQLConstraintImpl.cloneTo;
32 
33     private SQLExprTableSource referencedTable;
34     private List!SQLName      referencingColumns ;
35     private List!SQLName      referencedColumns ;
36     private bool            onDeleteCascade    = false;
37     private bool            onDeleteSetNull    = false;
38 
39     public this(){
40         referencingColumns = new ArrayList!SQLName();
41         referencedColumns  = new ArrayList!SQLName();
42     }
43 
44     override
45     public List!SQLName getReferencingColumns() {
46         return referencingColumns;
47     }
48 
49     override
50     public SQLExprTableSource getReferencedTable() {
51         return referencedTable;
52     }
53 
54     override
55     public SQLName getReferencedTableName() {
56         if (referencedTable is null) {
57             return null;
58         }
59         return referencedTable.getName();
60     }
61 
62     override
63     public void setReferencedTableName(SQLName value) {
64         if (value is null) {
65             this.referencedTable = null;
66             return;
67         }
68         this.setReferencedTable(new SQLExprTableSource(value));
69     }
70 
71     public void setReferencedTable(SQLExprTableSource x) {
72         if (x !is null) {
73             x.setParent(this);
74         }
75         this.referencedTable = x;
76     }
77 
78     override
79     public List!SQLName getReferencedColumns() {
80         return referencedColumns;
81     }
82 
83     public bool isOnDeleteCascade() {
84         return onDeleteCascade;
85     }
86 
87     public void setOnDeleteCascade(bool onDeleteCascade) {
88         this.onDeleteCascade = onDeleteCascade;
89     }
90 
91     public bool isOnDeleteSetNull() {
92         return onDeleteSetNull;
93     }
94 
95     public void setOnDeleteSetNull(bool onDeleteSetNull) {
96         this.onDeleteSetNull = onDeleteSetNull;
97     }
98 
99     
100     override  protected void accept0(SQLASTVisitor visitor) {
101         if (visitor.visit(this)) {
102             acceptChild(visitor, this.getName());
103             acceptChild(visitor, this.getReferencedTableName());
104             acceptChild!SQLName(visitor, this.getReferencingColumns());
105             acceptChild!SQLName(visitor, this.getReferencedColumns());
106         }
107         visitor.endVisit(this);        
108     }
109 
110     public void cloneTo(SQLForeignKeyImpl x) {
111         super.cloneTo(x);
112 
113         if (referencedTable !is null) {
114             x.setReferencedTable(referencedTable.clone());
115         }
116 
117         foreach (SQLName column ; referencingColumns) {
118             SQLName columnClone = column.clone();
119             columnClone.setParent(x);
120             x.getReferencingColumns().add(columnClone);
121         }
122 
123         foreach (SQLName column ; referencedColumns) {
124             SQLName columnClone = column.clone();
125             columnClone.setParent(x);
126             x.getReferencedColumns().add(columnClone);
127         }
128     }
129 
130     override public SQLForeignKeyImpl clone() {
131         SQLForeignKeyImpl x = new SQLForeignKeyImpl();
132         cloneTo(x);
133         return x;
134     }
135 
136     public static struct Match {
137         enum Match FULL = Match("FULL");
138         enum Match PARTIAL = Match("PARTIAL");
139         enum Match SIMPLE = Match("SIMPLE");
140 
141         public  string name;
142         public  string name_lcase;
143 
144         this(string name){
145             this.name = name;
146             this.name_lcase = toLower(name);
147         }
148 
149         bool opEquals(const Match h) nothrow {
150             return name == h.name ;
151         } 
152 
153         bool opEquals(ref const Match h) nothrow {
154             return name == h.name ;
155         }
156     }
157 
158     public static struct On {
159         enum On DELETE = On("DELETE"); //
160         enum On UPDATE = On("UPDATE");
161 
162         public  string name;
163         public  string name_lcase;
164 
165         this(string name){
166             this.name = name;
167             this.name_lcase = toLower(name);
168         }
169 
170         bool opEquals(const On h) nothrow {
171             return name == h.name ;
172         } 
173 
174         bool opEquals(ref const On h) nothrow {
175             return name == h.name ;
176         }
177     }
178 
179     public static struct Option {
180 
181         enum Option RESTRICT = Option("RESTRICT");
182         enum Option CASCADE = Option("CASCADE");
183         enum Option SET_NULL = Option("SET NULL");
184         enum Option NO_ACTION = Option("NO ACTION");
185 
186         public  string name;
187         public  string name_lcase;
188 
189         this(string name){
190             this.name = name;
191             this.name_lcase = toLower(name);
192         }
193 
194         bool opEquals(const Option h) nothrow {
195             return name == h.name ;
196         } 
197 
198         bool opEquals(ref const Option h) nothrow {
199             return name == h.name ;
200         }
201 
202         public string getText() {
203             return name;
204         }
205 
206     }
207 }