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.dialect.mysql.visitor.transform.NameResolveVisitor;
17 
18 import hunt.sql.SQLUtils;
19 import hunt.sql.ast.SQLName;
20 import hunt.sql.ast.SQLObject;
21 import hunt.sql.ast.SQLStatement;
22 import hunt.sql.ast.expr;
23 import hunt.sql.ast.statement;
24 // import hunt.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
25 import hunt.sql.util.FnvHash;
26 
27 /**
28  * Created by wenshao on 26/07/2017.
29  */
30 // public class NameResolveVisitor : OracleASTVisitorAdapter {
31 //     public bool visit(SQLIdentifierExpr x) {
32 //         SQLObject parent = x.getParent();
33 
34 //         if (cast(SQLBinaryOpExpr)(parent) !is null
35 //                 && x.getResolvedColumn() is null) {
36 //             SQLBinaryOpExpr binaryOpExpr = cast(SQLBinaryOpExpr) parent;
37 //             bool isJoinCondition = cast(SQLName)binaryOpExpr.getLeft() !is null
38 //                     && cast(SQLName)binaryOpExpr.getRight() !is null;
39 //             if (isJoinCondition) {
40 //                 return false;
41 //             }
42 //         }
43 
44 //         string name = x.getName();
45 
46 //         if ("ROWNUM".equalsIgnoreCase(name)) {
47 //             return false;
48 //         }
49 
50 //         long hash = x.nameHashCode64();
51 //         SQLTableSource tableSource = null;
52 
53 //         if (hash == FnvHash.Constants.LEVEL
54 //                 || hash == FnvHash.Constants.CONNECT_BY_ISCYCLE
55 //                 || hash == FnvHash.Constants.SYSTIMESTAMP) {
56 //             return false;
57 //         }
58 
59 //         if (cast(SQLPropertyExpr)(parent) !is null) {
60 //             return false;
61 //         }
62 
63 //         for (; parent !is null; parent = parent.getParent()) {
64 //             if (cast(SQLTableSource)(parent) !is null) {
65 //                 return false;
66 //             }
67 
68 //             if (cast(SQLSelectQueryBlock)(parent) !is null) {
69 //                 SQLSelectQueryBlock queryBlock = cast(SQLSelectQueryBlock) parent;
70 
71 //                 if (queryBlock.getInto() !is null) {
72 //                     return false;
73 //                 }
74 
75 //                 if (cast(SQLSelect)queryBlock.getParent() !is null) {
76 //                     SQLObject pp = queryBlock.getParent().getParent();
77 //                     if ( cast(SQLInSubQueryExpr)pp !is null || cast(SQLExistsExpr)(pp) !is null) {
78 //                         return false;
79 //                     }
80 //                 }
81 
82 //                 SQLTableSource from = queryBlock.getFrom();
83 //                 if (cast(SQLExprTableSource)(from) !is null || cast(SQLSubqueryTableSource)(from) !is null) {
84 //                     string alias_p = from.getAlias();
85 //                     if (alias_p !is null) {
86 //                         SQLUtils.replaceInParent(x, new SQLPropertyExpr(alias_p, name));
87 //                     }
88 //                 }
89 //                 return false;
90 //             }
91 //         }
92 //         return true;
93 //     }
94 
95 //     public bool visit(SQLPropertyExpr x) {
96 //         string ownerName = x.getOwnernName();
97 //         if (ownerName is null) {
98 //             return super.visit(x);
99 //         }
100 
101 //         for (SQLObject parent = x.getParent(); parent !is null; parent = parent.getParent()) {
102 //             if (cast(SQLSelectQueryBlock)(parent) !is null) {
103 //                 SQLSelectQueryBlock queryBlock = cast(SQLSelectQueryBlock) parent;
104 //                 SQLTableSource tableSource = queryBlock.findTableSource(ownerName);
105 //                 if (tableSource is null) {
106 //                     continue;
107 //                 }
108 
109 //                 string alias_p = tableSource.computeAlias();
110 //                 if (tableSource !is null
111 //                         && equalsIgnoreCase(ownerName, alias_p)
112 //                         && !ownerName.opEquals(alias_p)) {
113 //                     x.setOwner(alias_p);
114 //                 }
115 
116 //                 break;
117 //             }
118 //         }
119 
120 //         return super.visit(x);
121 //     }
122 // }