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.SQLUnionQuery; 17 18 import hunt.sql.SQLUtils; 19 import hunt.sql.ast.SQLLimit; 20 import hunt.sql.ast.SQLObjectImpl; 21 import hunt.sql.ast.SQLOrderBy; 22 import hunt.sql.visitor.SQLASTOutputVisitor; 23 import hunt.sql.visitor.SQLASTVisitor; 24 import hunt.sql.ast.statement.SQLSelectQuery; 25 import hunt.sql.ast.statement.SQLSelectQueryBlock; 26 import hunt.sql.ast.statement.SQLUnionOperator; 27 import hunt.collection; 28 import hunt.util.StringBuilder; 29 30 public class SQLUnionQuery : SQLObjectImpl , SQLSelectQuery { 31 32 private bool bracket = false; 33 34 private SQLSelectQuery left; 35 private SQLSelectQuery right; 36 private SQLUnionOperator operator = SQLUnionOperator.UNION; 37 private SQLOrderBy orderBy; 38 39 private SQLLimit limit; 40 private string dbType; 41 42 public SQLUnionOperator getOperator() { 43 return operator; 44 } 45 46 public void setOperator(SQLUnionOperator operator) { 47 this.operator = operator; 48 } 49 50 public this(){ 51 52 } 53 54 public this(SQLSelectQuery left, SQLUnionOperator operator, SQLSelectQuery right){ 55 this.setLeft(left); 56 this.operator = operator; 57 this.setRight(right); 58 } 59 60 public SQLSelectQuery getLeft() { 61 return left; 62 } 63 64 public void setLeft(SQLSelectQuery left) { 65 if (left !is null) { 66 left.setParent(this); 67 } 68 this.left = left; 69 } 70 71 public SQLSelectQuery getRight() { 72 return right; 73 } 74 75 public void setRight(SQLSelectQuery right) { 76 if (right !is null) { 77 right.setParent(this); 78 } 79 this.right = right; 80 } 81 82 public SQLOrderBy getOrderBy() { 83 return orderBy; 84 } 85 86 public void setOrderBy(SQLOrderBy orderBy) { 87 if (orderBy !is null) { 88 orderBy.setParent(this); 89 } 90 this.orderBy = orderBy; 91 } 92 93 94 override protected void accept0(SQLASTVisitor visitor) { 95 if (visitor.visit(this)) { 96 acceptChild(visitor, left); 97 acceptChild(visitor, right); 98 acceptChild(visitor, orderBy); 99 acceptChild(visitor, limit); 100 } 101 visitor.endVisit(this); 102 } 103 104 105 public SQLLimit getLimit() { 106 return limit; 107 } 108 109 public void setLimit(SQLLimit limit) { 110 if (limit !is null) { 111 limit.setParent(this); 112 } 113 this.limit = limit; 114 } 115 116 public bool isBracket() { 117 return bracket; 118 } 119 120 public void setBracket(bool bracket) { 121 this.bracket = bracket; 122 } 123 124 override public SQLUnionQuery clone() { 125 SQLUnionQuery x = new SQLUnionQuery(); 126 127 x.bracket = bracket; 128 if (left !is null) { 129 x.setLeft(left.clone()); 130 } 131 if (right !is null) { 132 x.setRight(right.clone()); 133 } 134 x.operator = operator; 135 136 if (orderBy !is null) { 137 x.setOrderBy(orderBy.clone()); 138 } 139 140 if (limit !is null) { 141 x.setLimit(limit.clone()); 142 } 143 144 x.dbType = dbType; 145 146 return x; 147 } 148 149 public SQLSelectQueryBlock getFirstQueryBlock() { 150 if (cast(SQLSelectQueryBlock)(left) !is null ) { 151 return cast(SQLSelectQueryBlock) left; 152 } 153 154 if (cast(SQLUnionQuery)(left) !is null ) { 155 return (cast(SQLUnionQuery) left).getFirstQueryBlock(); 156 } 157 158 return null; 159 } 160 161 public string getDbType() { 162 return dbType; 163 } 164 165 public void setDbType(string dbType) { 166 this.dbType = dbType; 167 } 168 169 override public void output(StringBuilder buf) { 170 SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(buf, dbType); 171 this.accept(visitor); 172 } 173 }