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.FromSubqueryResolver;
17 
18 import hunt.sql.SQLUtils;
19 import hunt.sql.ast.SQLExpr;
20 import hunt.sql.ast.SQLObject;
21 import hunt.sql.ast.SQLStatement;
22 import hunt.sql.ast.expr.SQLIdentifierExpr;
23 import hunt.sql.ast.statement;
24 // import hunt.sql.dialect.oracle.ast.stmt.OracleSelectSubqueryTableSource;
25 // import hunt.sql.dialect.oracle.ast.stmt.OracleSelectTableReference;
26 // import hunt.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
27 
28 
29 import hunt.collection;
30 
31 
32 /**
33  */
34 // public class FromSubqueryResolver : OracleASTVisitorAdapter {
35 //     private  List!(SQLStatement) targetList;
36 //     private  string viewName;
37 //     private  Map!(string, string) mappings;
38 
39 //     private int viewNameSeed = 1;
40 
41 //     this()
42 //     {
43 //         mappings = new LinkedHashMap!(string, string)();
44 //     }
45 
46 //     public this(List!(SQLStatement) targetList, string viewName) {
47 //         this.targetList = targetList;
48 //         this.viewName = viewName;
49 //     }
50 
51 //     // public bool visit(OracleSelectSubqueryTableSource x) {
52 //     //     return visit(cast(SQLSubqueryTableSource) x);
53 //     // }
54 
55 //     public bool visit(SQLSubqueryTableSource x) {
56 //         string subViewName = generateSubViewName();
57 
58 //         SQLObject parent = x.getParent();
59 //         if(cast(SQLSelectQueryBlock)(parent) !is null) {
60 //             SQLSelectQueryBlock queryBlock = cast(SQLSelectQueryBlock) parent;
61 //             queryBlock.setFrom(subViewName, x.getAlias());
62 //         } else if(cast(SQLJoinTableSource)(parent) !is null) {
63 //             SQLJoinTableSource join = cast(SQLJoinTableSource) parent;
64 //             if (join.getLeft() == x) {
65 //                 join.setLeft(subViewName, x.getAlias());
66 //             } else if (join.getRight() == x) {
67 //                 join.setRight(subViewName, x.getAlias());
68 //             }
69 //         }
70 
71 //         SQLCreateViewStatement stmt = new SQLCreateViewStatement();
72 
73 //         stmt.setName(generateSubViewName());
74 
75 //         SQLSelect select = x.getSelect();
76 //         stmt.setSubQuery(select);
77 
78 //         targetList.add(0, stmt);
79 
80 //         stmt.accept(new FromSubqueryResolver(targetList, viewName));
81 
82 //         return false;
83 //     }
84 
85 //     public bool visit(SQLExprTableSource x) {
86 //         SQLExpr expr = x.getExpr();
87 //         if (cast(SQLIdentifierExpr)(expr) !is null) {
88 //             SQLIdentifierExpr identifierExpr = cast(SQLIdentifierExpr) expr;
89 //             string ident = identifierExpr.getName();
90 //             string mappingIdent = mappings.get(ident);
91 //             if (mappingIdent !is null) {
92 //                 x.setExpr(new SQLIdentifierExpr(mappingIdent));
93 //             }
94 //         }
95 //         return false;
96 //     }
97 
98 //     public bool visit(OracleSelectTableReference x) {
99 //         return visit(cast(SQLExprTableSource) x);
100 //     }
101 
102 //     private string generateSubViewName() {
103 //         return this.viewName ~ "_" ~ targetList.size();
104 //     }
105 
106 //     public static List!(SQLStatement) resolve(SQLCreateViewStatement stmt) {
107 //         List!(SQLStatement) targetList = new ArrayList!(SQLStatement)();
108 //         targetList.add(stmt);
109 
110 //         string viewName = SQLUtils.normalize(stmt.getName().getSimpleName());
111 
112 //         FromSubqueryResolver visitor = new FromSubqueryResolver(targetList, viewName);
113 
114 //         SQLWithSubqueryClause withSubqueryClause = stmt.getSubQuery().getWithSubQuery();
115 //         if (withSubqueryClause !is null) {
116 //             stmt.getSubQuery().setWithSubQuery(null);
117 
118 //             foreach(SQLWithSubqueryClause.Entry entry ; withSubqueryClause.getEntries()) {
119 //                 string entryName = entry.getAlias();
120 
121 //                 SQLCreateViewStatement entryStmt = new SQLCreateViewStatement();
122 //                 entryStmt.setOrReplace(true);
123 //                 entryStmt.setDbType(stmt.getDbType());
124 
125 //                 string entryViewName = visitor.generateSubViewName();
126 //                 entryStmt.setName(entryViewName);
127 //                 entryStmt.setSubQuery(entry.getSubQuery());
128 
129 //                 visitor.targetList.add(0, entryStmt);
130 //                 visitor.mappings.put(entryName, entryViewName);
131 
132 //                 entryStmt.accept(visitor);
133 //             }
134 //         }
135 
136 //         stmt.accept(visitor);
137 
138 //         string dbType = stmt.getDbType();
139 //         for (int i = 0; i < targetList.size() - 1; ++i) {
140 //             SQLCreateViewStatement targetStmt = cast(SQLCreateViewStatement) targetList.get(i);
141 //             targetStmt.setOrReplace(true);
142 //             targetStmt.setDbType(dbType);
143 //             targetStmt.setAfterSemi(true);
144 //         }
145 
146 //         return targetList;
147 //     }
148 // }