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.SQLTableSourceImpl;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.SQLUtils;
22 import hunt.sql.ast.SQLExpr;
23 import hunt.sql.ast.SQLHint;
24 import hunt.sql.ast.SQLObjectImpl;
25 import hunt.sql.util.FnvHash;
26 import hunt.sql.ast.statement.SQLColumnDefinition;
27 import hunt.sql.ast.statement.SQLTableSource;
28 
29 public abstract class SQLTableSourceImpl : SQLObjectImpl , SQLTableSource {
30     protected string        _alias;
31     protected List!SQLHint hints;
32     protected SQLExpr       flashback;
33     protected long          aliasHashCod64;
34 
35     public this(){
36 
37     }
38 
39     public this(string alias_p){
40         this._alias = alias_p;
41     }
42 
43     public string getAlias() {
44         return this._alias;
45     }
46 
47     public void setAlias(string alias_p) {
48         this._alias = alias_p;
49         this.aliasHashCod64 = 0L;
50     }
51 
52     public int getHintsSize() {
53         if (hints is null) {
54             return 0;
55         }
56 
57         return hints.size();
58     }
59 
60     public List!SQLHint getHints() {
61         if (hints is null) {
62             hints = new ArrayList!SQLHint(2);
63         }
64         return hints;
65     }
66 
67     public void setHints(List!SQLHint hints) {
68         this.hints = hints;
69     }
70 
71     override public SQLTableSource clone() {
72         throw new Exception(typeof(this).stringof);
73     }
74 
75     public string computeAlias() {
76         return _alias;
77     }
78 
79     public SQLExpr getFlashback() {
80         return flashback;
81     }
82 
83     public void setFlashback(SQLExpr flashback) {
84         if (flashback !is null) {
85             flashback.setParent(this);
86         }
87         this.flashback = flashback;
88     }
89 
90     public bool containsAlias(string alias_p) {
91         if (SQLUtils.nameEquals(this._alias, alias_p)) {
92             return true;
93         }
94 
95         return false;
96     }
97 
98     public long aliasHashCode64() {
99         if (aliasHashCod64 == 0
100                 && _alias !is null) {
101             aliasHashCod64 = FnvHash.hashCode64(_alias);
102         }
103         return aliasHashCod64;
104     }
105 
106     public SQLColumnDefinition findColumn(string columnName) {
107         if (columnName is null) {
108             return null;
109         }
110 
111         long hash = FnvHash.hashCode64(_alias);
112         return findColumn(hash);
113     }
114 
115     public SQLColumnDefinition findColumn(long columnNameHash) {
116         return null;
117     }
118 
119     public SQLTableSource findTableSourceWithColumn(string columnName) {
120         if (columnName is null) {
121             return null;
122         }
123 
124         long hash = FnvHash.hashCode64(_alias);
125         return findTableSourceWithColumn(hash);
126     }
127 
128     public SQLTableSource findTableSourceWithColumn(long columnNameHash) {
129         return null;
130     }
131 
132     public SQLTableSource findTableSource(string alias_p) {
133         long hash = FnvHash.hashCode64(alias_p);
134         return findTableSource(hash);
135     }
136 
137     public SQLTableSource findTableSource(long alias_hash) {
138         long hash = this.aliasHashCode64();
139         if (hash != 0 && hash == alias_hash) {
140             return this;
141         }
142         return null;
143     }
144 }