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.repository.SchemaResolveVisitor;
17 
18 import hunt.sql.ast.SQLDeclareItem;
19 import hunt.sql.ast.SQLObject;
20 import hunt.sql.ast.statement.SQLTableSource;
21 import hunt.sql.visitor.SQLASTVisitor;
22 import hunt.sql.repository.SchemaRepository;
23 
24 import hunt.collection;
25 import hunt.Long;
26 /**
27  * Created by wenshao on 03/08/2017.
28  */
29 public interface SchemaResolveVisitor : SQLASTVisitor {
30 
31     bool isEnabled(Option option);
32 
33     public static struct Option {
34         enum Option ResolveAllColumn = Option(0);
35         enum Option ResolveIdentifierAlias = Option(1);
36         private this(int ord) {
37             mask = (1 << ord);
38         }
39 
40         public  int mask;
41 
42         public static int of(Option[] options...) {
43             if (options is null) {
44                 return 0;
45             }
46 
47             int value = 0;
48 
49             foreach(Option option ; options) {
50                 value |= option.mask;
51             }
52 
53             return value;
54         }
55 
56         bool opEquals(const Option h) nothrow {
57         return mask == h.mask ;
58         } 
59 
60         bool opEquals(ref const Option h) nothrow {
61             return mask == h.mask ;
62         } 
63     }
64 
65     SchemaRepository getRepository();
66 
67     Context getContext();
68     Context createContext(SQLObject object);
69     void popContext();
70 
71     static class Context {
72         public  Context parent;
73         public  SQLObject object;
74 
75         private SQLTableSource tableSource;
76 
77         private SQLTableSource from;
78 
79         private Map!(Long, SQLTableSource) tableSourceMap;
80 
81         protected Map!(Long, SQLDeclareItem) declares;
82 
83         public this(SQLObject object, Context parent) {
84             this.object = object;
85             this.parent = parent;
86         }
87 
88         public SQLTableSource getFrom() {
89             return from;
90         }
91 
92         public void setFrom(SQLTableSource from) {
93             this.from = from;
94         }
95 
96         public SQLTableSource getTableSource() {
97             return tableSource;
98         }
99 
100         public void setTableSource(SQLTableSource tableSource) {
101             this.tableSource = tableSource;
102         }
103 
104         public void addTableSource(long alias_hash, SQLTableSource tableSource) {
105             tableSourceMap.put(new Long(alias_hash), tableSource);
106         }
107 
108         public void declare(SQLDeclareItem x) {
109             if (declares is null) {
110                 declares = new HashMap!(Long, SQLDeclareItem)();
111             }
112             declares.put(new Long(x.getName().nameHashCode64()), x);
113         }
114 
115         public  SQLDeclareItem findDeclare(long nameHash) {
116             if (declares is null) {
117                 return null;
118             }
119             return declares.get(new Long(nameHash));
120         }
121     }
122 }