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.SQLUnique; 17 18 19 import hunt.collection; 20 21 import hunt.sql.SQLUtils; 22 import hunt.sql.ast.SQLExpr; 23 import hunt.sql.ast.SQLName; 24 import hunt.sql.ast.expr.SQLIdentifierExpr; 25 import hunt.sql.ast.expr.SQLMethodInvokeExpr; 26 import hunt.sql.visitor.SQLASTVisitor; 27 import hunt.sql.ast.statement.SQLConstraintImpl; 28 import hunt.sql.ast.statement.SQLUniqueConstraint; 29 import hunt.sql.ast.statement.SQLSelectOrderByItem; 30 import hunt.sql.ast.statement.SQLTableElement; 31 import hunt.sql.ast.SQLObject; 32 33 public class SQLUnique : SQLConstraintImpl , SQLUniqueConstraint, SQLTableElement { 34 35 alias cloneTo = SQLConstraintImpl.cloneTo; 36 37 public List!SQLSelectOrderByItem columns; 38 39 public this(){ 40 columns = new ArrayList!SQLSelectOrderByItem(); 41 } 42 43 public List!SQLSelectOrderByItem getColumns() { 44 return columns; 45 } 46 47 public void addColumn(SQLExpr column) { 48 if (column is null) { 49 return; 50 } 51 52 addColumn(new SQLSelectOrderByItem(column)); 53 } 54 55 public void addColumn(SQLSelectOrderByItem column) { 56 if (column !is null) { 57 column.setParent(this); 58 } 59 this.columns.add(column); 60 } 61 62 63 override protected void accept0(SQLASTVisitor visitor) { 64 if (visitor.visit(this)) { 65 acceptChild(visitor, this.getName()); 66 acceptChild!SQLSelectOrderByItem(visitor, this.getColumns()); 67 } 68 visitor.endVisit(this); 69 } 70 71 public bool containsColumn(string column) { 72 foreach (SQLSelectOrderByItem item ; columns) { 73 SQLExpr expr = item.getExpr(); 74 if (cast(SQLIdentifierExpr)(expr) !is null ) { 75 if (SQLUtils.nameEquals((cast(SQLIdentifierExpr) expr).getName(), column)) { 76 return true; 77 } 78 } 79 } 80 return false; 81 } 82 83 public bool containsColumn(long columnNameHash) { 84 foreach (SQLSelectOrderByItem item ; columns) { 85 SQLExpr expr = item.getExpr(); 86 if (cast(SQLIdentifierExpr)(expr) !is null ) { 87 if ((cast(SQLIdentifierExpr) expr).nameHashCode64() == columnNameHash) { 88 return true; 89 } 90 } 91 } 92 return false; 93 } 94 95 public void cloneTo(SQLUnique x) { 96 super.cloneTo(x); 97 98 foreach (SQLSelectOrderByItem column ; columns) { 99 SQLSelectOrderByItem column2 = column.clone(); 100 column2.setParent(x); 101 x.columns.add(column2); 102 } 103 } 104 105 override public SQLUnique clone() { 106 SQLUnique x = new SQLUnique(); 107 cloneTo(x); 108 return x; 109 } 110 111 override public void simplify() { 112 super.simplify(); 113 114 foreach (SQLSelectOrderByItem item ; columns) { 115 SQLExpr column = item.getExpr(); 116 if (cast(SQLIdentifierExpr)(column) !is null ) { 117 SQLIdentifierExpr identExpr = cast(SQLIdentifierExpr) column; 118 string columnName = identExpr.getName(); 119 string normalized = SQLUtils.normalize(columnName, dbType); 120 if (normalized != columnName) { 121 item.setExpr(new SQLIdentifierExpr(columnName)); 122 } 123 } 124 } 125 } 126 127 public bool applyColumnRename(SQLName columnName, SQLName to) { 128 foreach (SQLSelectOrderByItem orderByItem ; columns) { 129 SQLExpr expr = orderByItem.getExpr(); 130 if ( cast(SQLName)expr !is null 131 && SQLUtils.nameEquals(cast(SQLName) expr, columnName)) { 132 orderByItem.setExpr(to.clone()); 133 return true; 134 } 135 } 136 return false; 137 } 138 139 public bool applyDropColumn(SQLName columnName) { 140 for (int i = columns.size() - 1; i >= 0; i--) { 141 SQLExpr expr = columns.get(i).getExpr(); 142 if ( cast(SQLName)expr !is null 143 && SQLUtils.nameEquals(cast(SQLName) expr, columnName)) { 144 columns.removeAt(i); 145 return true; 146 } 147 148 if ( cast(SQLMethodInvokeExpr)expr !is null 149 && SQLUtils.nameEquals((cast(SQLMethodInvokeExpr) expr).getMethodName(), columnName.getSimpleName())) { 150 columns.removeAt(i); 151 return true; 152 } 153 } 154 return false; 155 } 156 }