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.expr.SQLExprUtils;
17 
18 import hunt.sql.ast.SQLExpr;
19 import hunt.sql.ast.expr.SQLIdentifierExpr;
20 import hunt.sql.ast.expr.SQLBinaryOpExpr;
21 import hunt.sql.ast.expr.SQLLiteralExpr;
22 
23 public class SQLExprUtils {
24 
25     public static bool opEquals(SQLExpr a, SQLExpr b) {
26         if (a == b) {
27             return true;
28         }
29 
30         if (a is null || b is null) {
31             return false;
32         }
33 
34         auto clazz_a = typeid(a);
35         auto clazz_b = typeid(b);
36         if (clazz_a != clazz_b) {
37             return false;
38         }
39 
40         if (clazz_a == typeid(SQLIdentifierExpr)) {
41             SQLIdentifierExpr x_a = cast(SQLIdentifierExpr) a;
42             SQLIdentifierExpr x_b = cast(SQLIdentifierExpr) b;
43             return (cast(Object)x_a).toHash() == (cast(Object)x_b).toHash();
44         }
45 
46         if (clazz_a == typeid(SQLBinaryOpExpr)) {
47             SQLBinaryOpExpr x_a = cast(SQLBinaryOpExpr) a;
48             SQLBinaryOpExpr x_b = cast(SQLBinaryOpExpr) b;
49 
50             return x_a.opEquals(cast(Object)(x_b));
51         }
52 
53         return (cast(Object)a).opEquals(cast(Object)(b));
54     }
55 
56     public static bool isLiteralExpr(SQLExpr expr) {
57         if (cast(SQLLiteralExpr)expr !is null) {
58             return true;
59         }
60         SQLBinaryOpExpr binary = cast(SQLBinaryOpExpr) expr;
61         if (binary !is null) {
62             return isLiteralExpr(binary.left) && isLiteralExpr(binary.right);
63         }
64 
65         return false;
66     }
67 }