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.SQLSubqueryTableSource; 17 18 import hunt.sql.visitor.SQLASTVisitor; 19 import hunt.sql.ast.statement.SQLTableSourceImpl; 20 import hunt.sql.ast.statement.SQLSelect; 21 import hunt.sql.ast.statement.SQLSelectQuery; 22 import hunt.sql.ast.statement.SQLSelectQueryBlock; 23 import hunt.sql.ast.statement.SQLTableSource; 24 import hunt.collection; 25 import hunt.util.StringBuilder; 26 27 public class SQLSubqueryTableSource : SQLTableSourceImpl { 28 29 public SQLSelect select; 30 31 public this(){ 32 33 } 34 35 public this(string alias_p){ 36 super(alias_p); 37 } 38 39 public this(SQLSelect select, string alias_p){ 40 super(alias_p); 41 this.setSelect(select); 42 } 43 44 public this(SQLSelect select){ 45 this.setSelect(select); 46 } 47 48 public this(SQLSelectQuery query){ 49 this(new SQLSelect(query)); 50 } 51 52 public SQLSelect getSelect() { 53 return this.select; 54 } 55 56 public void setSelect(SQLSelect select) { 57 if (select !is null) { 58 select.setParent(this); 59 } 60 this.select = select; 61 } 62 63 64 override protected void accept0(SQLASTVisitor visitor) { 65 if (visitor.visit(this)) { 66 acceptChild(visitor, select); 67 } 68 visitor.endVisit(this); 69 } 70 71 override public void output(StringBuilder buf) { 72 buf.append("("); 73 this.select.output(buf); 74 buf.append(")"); 75 } 76 77 public void cloneTo(SQLSubqueryTableSource x) { 78 x._alias = _alias; 79 80 if (select !is null) { 81 x.select = select.clone(); 82 } 83 } 84 85 override public SQLSubqueryTableSource clone() { 86 SQLSubqueryTableSource x = new SQLSubqueryTableSource(); 87 cloneTo(x); 88 return x; 89 } 90 91 override public SQLTableSource findTableSourceWithColumn(string columnName) { 92 if (select is null) { 93 return null; 94 } 95 96 SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock(); 97 if (queryBlock is null) { 98 return null; 99 } 100 101 if (queryBlock.findSelectItem(columnName) !is null) { 102 return this; 103 } 104 105 return null; 106 } 107 108 override public SQLTableSource findTableSourceWithColumn(long columnNameHash) { 109 if (select is null) { 110 return null; 111 } 112 113 SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock(); 114 if (queryBlock is null) { 115 return null; 116 } 117 118 if (queryBlock.findSelectItem(columnNameHash) !is null) { 119 return this; 120 } 121 122 return null; 123 } 124 }