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.SQLLateralViewTableSource;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast.SQLName;
22 import hunt.sql.ast.expr.SQLMethodInvokeExpr;
23 import hunt.sql.visitor.SQLASTVisitor;
24 import hunt.sql.ast.statement.SQLTableSourceImpl;
25 import hunt.sql.ast.statement.SQLTableSource;
26 import hunt.sql.ast.SQLObject;
27 
28 public class SQLLateralViewTableSource : SQLTableSourceImpl {
29 
30     private SQLTableSource      tableSource;
31 
32     private SQLMethodInvokeExpr method;
33 
34     private List!SQLName       columns;
35 
36     this()
37     {
38         columns = new ArrayList!SQLName(2);
39     }
40 
41     
42     override  protected void accept0(SQLASTVisitor visitor) {
43         if (visitor.visit(this)) {
44             acceptChild(visitor, tableSource);
45             acceptChild(visitor, method);
46             acceptChild!SQLName(visitor, columns);
47         }
48         visitor.endVisit(this);
49     }
50 
51     public SQLTableSource getTableSource() {
52         return tableSource;
53     }
54 
55     public void setTableSource(SQLTableSource tableSource) {
56         if (tableSource !is null) {
57             tableSource.setParent(this);
58         }
59         this.tableSource = tableSource;
60     }
61 
62     public SQLMethodInvokeExpr getMethod() {
63         return method;
64     }
65 
66     public void setMethod(SQLMethodInvokeExpr method) {
67         if (method !is null) {
68             method.setParent(this);
69         }
70         this.method = method;
71     }
72 
73     public List!SQLName getColumns() {
74         return columns;
75     }
76 
77     public void setColumns(List!SQLName columns) {
78         this.columns = columns;
79     }
80 
81     override public SQLTableSource findTableSource(long alias_hash) {
82         long hash = this.aliasHashCode64();
83         if (hash != 0 && hash == alias_hash) {
84             return this;
85         }
86 
87         foreach (SQLName column ; columns) {
88             if (column.nameHashCode64() == alias_hash) {
89                 return this;
90             }
91         }
92 
93         if (tableSource !is null) {
94             return tableSource.findTableSource(alias_hash);
95         }
96 
97         return null;
98     }
99 
100     override public SQLTableSource findTableSourceWithColumn(long columnNameHash) {
101         foreach (SQLName column ; columns) {
102             if (column.nameHashCode64() == columnNameHash) {
103                 return this;
104             }
105         }
106 
107         if (tableSource !is null) {
108             return tableSource.findTableSourceWithColumn(columnNameHash);
109         }
110         return null;
111     }
112 }