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.SQLAlterTableStatement;
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.SQLAlterTableItem;
29 import hunt.sql.ast.statement.SQLExprTableSource;
30 import hunt.sql.ast.statement.SQLDDLStatement;
31 import hunt.sql.ast.statement.SQLAlterStatement;
32 
33 public class SQLAlterTableStatement : SQLStatementImpl , SQLDDLStatement, SQLAlterStatement {
34 
35     private SQLExprTableSource      tableSource;
36     private List!SQLAlterTableItem items;
37 
38     // for mysql
39     private bool                 ignore                  = false;
40 
41     private bool                 updateGlobalIndexes     = false;
42     private bool                 invalidateGlobalIndexes = false;
43 
44     private bool                 removePatiting          = false;
45     private bool                 upgradePatiting         = false;
46     private Map!(string, SQLObject)  tableOptions;
47 
48     // odps
49     private bool                 mergeSmallFiles         = false;
50 
51     public this(){
52         items                   = new ArrayList!SQLAlterTableItem();
53         tableOptions            = new LinkedHashMap!(string, SQLObject)();
54     }
55 
56     public this(string dbType){
57         items                   = new ArrayList!SQLAlterTableItem();
58         tableOptions            = new LinkedHashMap!(string, SQLObject)();
59         super(dbType);
60     }
61 
62     public bool isIgnore() {
63         return ignore;
64     }
65 
66     public void setIgnore(bool ignore) {
67         this.ignore = ignore;
68     }
69 
70     public bool isRemovePatiting() {
71         return removePatiting;
72     }
73 
74     public void setRemovePatiting(bool removePatiting) {
75         this.removePatiting = removePatiting;
76     }
77 
78     public bool isUpgradePatiting() {
79         return upgradePatiting;
80     }
81 
82     public void setUpgradePatiting(bool upgradePatiting) {
83         this.upgradePatiting = upgradePatiting;
84     }
85 
86     public bool isUpdateGlobalIndexes() {
87         return updateGlobalIndexes;
88     }
89 
90     public void setUpdateGlobalIndexes(bool updateGlobalIndexes) {
91         this.updateGlobalIndexes = updateGlobalIndexes;
92     }
93 
94     public bool isInvalidateGlobalIndexes() {
95         return invalidateGlobalIndexes;
96     }
97 
98     public void setInvalidateGlobalIndexes(bool invalidateGlobalIndexes) {
99         this.invalidateGlobalIndexes = invalidateGlobalIndexes;
100     }
101 
102     public bool isMergeSmallFiles() {
103         return mergeSmallFiles;
104     }
105 
106     public void setMergeSmallFiles(bool mergeSmallFiles) {
107         this.mergeSmallFiles = mergeSmallFiles;
108     }
109 
110     public List!SQLAlterTableItem getItems() {
111         return items;
112     }
113 
114     public void addItem(SQLAlterTableItem item) {
115         if (item !is null) {
116             item.setParent(this);
117         }
118         this.items.add(item);
119     }
120 
121     public SQLExprTableSource getTableSource() {
122         return tableSource;
123     }
124 
125     public void setTableSource(SQLExprTableSource tableSource) {
126         this.tableSource = tableSource;
127     }
128 
129     public void setTableSource(SQLExpr table) {
130         this.setTableSource(new SQLExprTableSource(table));
131     }
132 
133     public SQLName getName() {
134         if (getTableSource() is null) {
135             return null;
136         }
137         return cast(SQLName) getTableSource().getExpr();
138     }
139 
140     public long nameHashCode64() {
141         if (getTableSource() is null) {
142             return 0L;
143         }
144         return (cast(SQLName) getTableSource().getExpr()).nameHashCode64();
145     }
146 
147     public void setName(SQLName name) {
148         this.setTableSource(new SQLExprTableSource(name));
149     }
150 
151     public Map!(string, SQLObject) getTableOptions() {
152         return tableOptions;
153     }
154 
155     override  protected void accept0(SQLASTVisitor visitor) {
156         if (visitor.visit(this)) {
157             acceptChild(visitor, getTableSource());
158             acceptChild!SQLAlterTableItem(visitor, getItems());
159         }
160         visitor.endVisit(this);
161     }
162 
163     override
164     public List!SQLObject getChildren() {
165         List!SQLObject children = new ArrayList!SQLObject();
166         if (tableSource !is null) {
167             children.add(tableSource);
168         }
169         children.addAll(cast(List!SQLObject)(this.items));
170         return children;
171     }
172 
173     public string getTableName() {
174         if (tableSource is null) {
175             return null;
176         }
177         SQLExpr expr = (cast(SQLExprTableSource) tableSource).getExpr();
178         if ( cast(SQLIdentifierExpr)expr !is null) {
179             return (cast(SQLIdentifierExpr) expr).getName();
180         } else if ( cast(SQLPropertyExpr)expr !is null) {
181             return (cast(SQLPropertyExpr) expr).getName();
182         }
183 
184         return null;
185     }
186 
187     public string getSchema() {
188         SQLName name = getName();
189         if (name is null) {
190             return null;
191         }
192 
193         if ( cast(SQLPropertyExpr)name !is null) {
194             return (cast(SQLPropertyExpr) name).getOwnernName();
195         }
196 
197         return null;
198     }
199 }