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.dialect.mysql.ast.statement.MySqlTableIndex; 17 18 19 import hunt.collection; 20 import hunt.sql.ast.SQLObject; 21 22 import hunt.sql.SQLUtils; 23 import hunt.sql.ast.SQLExpr; 24 import hunt.sql.ast.SQLName; 25 import hunt.sql.ast.expr.SQLMethodInvokeExpr; 26 import hunt.sql.ast.statement.SQLColumnDefinition; 27 import hunt.sql.ast.statement.SQLSelectOrderByItem; 28 import hunt.sql.ast.statement.SQLTableElement; 29 import hunt.sql.dialect.mysql.ast.MySqlObjectImpl; 30 import hunt.sql.dialect.mysql.visitor.MySqlASTVisitor; 31 32 public class MySqlTableIndex : MySqlObjectImpl , SQLTableElement { 33 alias accept0 = MySqlObjectImpl.accept0; 34 private SQLName name; 35 private string indexType; 36 private List!(SQLSelectOrderByItem) columns; 37 38 public this(){ 39 columns = new ArrayList!(SQLSelectOrderByItem)(); 40 } 41 42 public SQLName getName() { 43 return name; 44 } 45 46 public string getIndexType() { 47 return indexType; 48 } 49 50 public void setIndexType(string indexType) { 51 this.indexType = indexType; 52 } 53 54 public void setName(SQLName name) { 55 this.name = name; 56 } 57 58 public List!(SQLSelectOrderByItem) getColumns() { 59 return columns; 60 } 61 62 public void addColumn(SQLSelectOrderByItem column) { 63 if (column !is null) { 64 column.setParent(this); 65 } 66 this.columns.add(column); 67 } 68 69 override public void accept0(MySqlASTVisitor visitor) { 70 if (visitor.visit(this)) { 71 acceptChild(visitor, name); 72 acceptChild!SQLSelectOrderByItem(visitor, columns); 73 } 74 visitor.endVisit(this); 75 } 76 77 override public MySqlTableIndex clone() { 78 MySqlTableIndex x = new MySqlTableIndex(); 79 if (name !is null) { 80 x.setName(name.clone()); 81 } 82 x.indexType = indexType; 83 foreach(SQLSelectOrderByItem column ; columns) { 84 SQLSelectOrderByItem c2 = column.clone(); 85 c2.setParent(x); 86 x.columns.add(c2); 87 } 88 return x; 89 } 90 91 public bool applyColumnRename(SQLName columnName, SQLName to) { 92 foreach(SQLSelectOrderByItem orderByItem ; columns) { 93 SQLExpr expr = orderByItem.getExpr(); 94 if (cast(SQLName)(expr) !is null 95 && SQLUtils.nameEquals(cast(SQLName) expr, columnName)) { 96 orderByItem.setExpr(to.clone()); 97 return true; 98 } 99 } 100 return false; 101 } 102 103 public bool applyDropColumn(SQLName columnName) { 104 for (int i = columns.size() - 1; i >= 0; i--) { 105 SQLExpr expr = columns.get(i).getExpr(); 106 if (cast(SQLName)(expr) !is null 107 && SQLUtils.nameEquals(cast(SQLName) expr, columnName)) { 108 columns.removeAt(i); 109 return true; 110 } 111 if (cast(SQLMethodInvokeExpr)(expr) !is null 112 && SQLUtils.nameEquals((cast(SQLMethodInvokeExpr) expr).getMethodName(), columnName.getSimpleName())) { 113 columns.removeAt(i); 114 return true; 115 } 116 } 117 return false; 118 } 119 }