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.OrderByResolve;
17 
18 import hunt.sql.ast.SQLExpr;
19 import hunt.sql.ast.SQLName;
20 import hunt.sql.ast.SQLOrderBy;
21 import hunt.sql.ast.statement.SQLSelect;
22 import hunt.sql.ast.statement.SQLSelectItem;
23 import hunt.sql.ast.statement.SQLSelectOrderByItem;
24 import hunt.sql.ast.statement.SQLSelectQueryBlock;
25 // import hunt.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
26 // import hunt.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
27 import hunt.sql.util.FnvHash;
28 
29 
30 import hunt.collection;
31 
32 /**
33  * Created by wenshao on 27/07/2017.
34  */
35 // public class OrderByResolve : OracleASTVisitorAdapter {
36 //      static long DBMS_RANDOM_VALUE = FnvHash.hashCode64("DBMS_RANDOM.value");
37 
38 //     public bool visit(SQLSelect x) {
39 //         SQLSelectQueryBlock queryBlock = x.getQueryBlock();
40 //         if (queryBlock is null) {
41 //             return super.visit(x);
42 //         }
43 
44 //         if (x.getOrderBy() !is null && queryBlock.isForUpdate() && queryBlock.getOrderBy() is null) {
45 //             queryBlock.setOrderBy(x.getOrderBy());
46 //             x.setOrderBy(null);
47 //         }
48 
49 //         SQLOrderBy orderBy = queryBlock.getOrderBy();
50 //         if (orderBy is null) {
51 //             return super.visit(x);
52 //         }
53 
54 
55 //         if (!queryBlock.selectItemHasAllColumn(false)) {
56 //             List!(SQLSelectOrderByItem) notContainsOrderBy = new ArrayList!(SQLSelectOrderByItem)();
57 
58 //             foreach(SQLSelectOrderByItem orderByItem ; orderBy.getItems()) {
59 //                 SQLExpr orderByExpr = orderByItem.getExpr();
60 
61 //                 if (cast(SQLName)(orderByExpr) !is null) {
62 //                     if ((cast(SQLName) orderByExpr).hashCode64() == DBMS_RANDOM_VALUE) {
63 //                         continue;
64 //                     }
65 
66 //                     long hashCode64 = (cast(SQLName) orderByExpr).nameHashCode64();
67 //                     SQLSelectItem selectItem = queryBlock.findSelectItem(hashCode64);
68 //                     if (selectItem is null) {
69 //                         queryBlock.addSelectItem(orderByExpr.clone());
70 //                     }
71 //                 }
72 //             }
73 
74 //             if (notContainsOrderBy.size() > 0) {
75 //                 foreach(SQLSelectOrderByItem orderByItem ; notContainsOrderBy) {
76 //                     queryBlock.addSelectItem(orderByItem.getExpr());
77 //                 }
78 
79 //                 OracleSelectQueryBlock queryBlock1 = new OracleSelectQueryBlock();
80 //                 queryBlock1.setFrom(queryBlock, "x");
81 //                 x.setQuery(queryBlock1);
82 //             }
83 //         }
84 
85 
86 
87 //         return super.visit(x);
88 //     }
89 // }