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.SQLCreateIndexStatement;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast.SQLExpr;
22 import hunt.sql.ast.SQLName;
23 import hunt.sql.ast.SQLObject;
24 import hunt.sql.ast.SQLStatementImpl;
25 import hunt.sql.ast.expr.SQLIdentifierExpr;
26 import hunt.sql.ast.expr.SQLPropertyExpr;
27 import hunt.sql.visitor.SQLASTVisitor;
28 import hunt.sql.ast.statement.SQLSelectOrderByItem;
29 import hunt.sql.ast.statement.SQLExprTableSource;
30 import hunt.sql.ast.statement.SQLTableSource;
31 import hunt.sql.ast.statement.SQLCreateStatement;
32 
33 public class SQLCreateIndexStatement : SQLStatementImpl , SQLCreateStatement {
34 
35     private SQLName                    name;
36 
37     private SQLTableSource             table;
38 
39     private List!SQLSelectOrderByItem items;
40 
41     private string                     type;
42     
43     // for mysql
44     private string                     using;
45 
46     private SQLExpr                    comment;
47 
48     public this(){
49         items = new ArrayList!SQLSelectOrderByItem();
50     }
51     
52     public this(string dbType){
53         items = new ArrayList!SQLSelectOrderByItem();
54         super (dbType);
55     }
56 
57     public SQLTableSource getTable() {
58         return table;
59     }
60 
61     public void setTable(SQLName table) {
62         this.setTable(new SQLExprTableSource(table));
63     }
64 
65     public void setTable(SQLTableSource table) {
66         this.table = table;
67     }
68 
69     public string getTableName() {
70         if (cast(SQLExprTableSource)(table) !is null ) {
71             SQLExpr expr = (cast(SQLExprTableSource) table).getExpr();
72             if (cast(SQLIdentifierExpr)(expr) !is null ) {
73                 return (cast(SQLIdentifierExpr) expr).getName();
74             } else if (cast(SQLPropertyExpr)(expr) !is null ) {
75                 return (cast(SQLPropertyExpr) expr).getName();
76             }
77         }
78 
79         return null;
80     }
81 
82     public List!SQLSelectOrderByItem getItems() {
83         return items;
84     }
85 
86     public void addItem(SQLSelectOrderByItem item) {
87         if (item !is null) {
88             item.setParent(this);
89         }
90         this.items.add(item);
91     }
92 
93     public SQLName getName() {
94         return name;
95     }
96 
97     public void setName(SQLName name) {
98         this.name = name;
99     }
100 
101     public string getType() {
102         return type;
103     }
104 
105     public void setType(string type) {
106         this.type = type;
107     }
108     
109     public string getUsing() {
110         return using;
111     }
112 
113     public void setUsing(string using) {
114         this.using = using;
115     }
116 
117     
118     override  protected void accept0(SQLASTVisitor visitor) {
119         if (visitor.visit(this)) {
120             acceptChild(visitor, name);
121             acceptChild(visitor, table);
122             acceptChild!SQLSelectOrderByItem(visitor, items);
123         }
124         visitor.endVisit(this);
125     }
126 
127     override
128     public List!SQLObject getChildren() {
129         List!SQLObject children = new ArrayList!SQLObject();
130         if (name !is null) {
131             children.add(name);
132         }
133 
134         if (table !is null) {
135             children.add(table);
136         }
137 
138         children.addAll(cast(List!SQLObject)(this.items));
139         return children;
140     }
141 
142     public string getSchema() {
143         SQLName name = null;
144         if (cast(SQLExprTableSource)(table) !is null ) {
145             SQLExpr expr = (cast(SQLExprTableSource) table).getExpr();
146             if (cast(SQLName)(expr) !is null ) {
147                 name = cast(SQLName) expr;
148             }
149         }
150 
151         if (name is null) {
152             return null;
153         }
154 
155         if (cast(SQLPropertyExpr)(name) !is null ) {
156             return (cast(SQLPropertyExpr) name).getOwnernName();
157         }
158 
159         return null;
160     }
161 
162 
163     override public SQLCreateIndexStatement clone() {
164         SQLCreateIndexStatement x = new SQLCreateIndexStatement();
165         if (name !is null) {
166             x.setName(name.clone());
167         }
168         if (table !is null) {
169             x.setTable(table.clone());
170         }
171         foreach (SQLSelectOrderByItem item ; items) {
172             SQLSelectOrderByItem item2 = item.clone();
173             item2.setParent(x);
174             x.items.add(item2);
175         }
176         x.type = type;
177         x.using = using;
178         if (comment !is null) {
179             x.setComment(comment.clone());
180         }
181         return x;
182     }
183 
184     public SQLExpr getComment() {
185         return comment;
186     }
187 
188     public void setComment(SQLExpr x) {
189         if (x !is null) {
190             x.setParent(this);
191         }
192         this.comment = x;
193     }
194 }