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.SQLBinaryOperator;
17 
18 import std.uni;
19 /**
20  * 
21  * 二元操作符
22  * 
23  */
24 public struct SQLBinaryOperator {
25     enum SQLBinaryOperator Union = SQLBinaryOperator("UNION", 0);
26     enum SQLBinaryOperator COLLATE = SQLBinaryOperator("COLLATE", 20);
27     enum SQLBinaryOperator BitwiseXor = SQLBinaryOperator("^", 50);
28     enum SQLBinaryOperator BitwiseXorEQ = SQLBinaryOperator("^=", 110);
29 
30     enum SQLBinaryOperator Multiply = SQLBinaryOperator("*", 60);
31     enum SQLBinaryOperator Divide = SQLBinaryOperator("/", 60);
32     enum SQLBinaryOperator DIV = SQLBinaryOperator("DIV", 60); // mysql integer division
33     enum SQLBinaryOperator Modulus = SQLBinaryOperator("%", 60);
34     enum SQLBinaryOperator Mod = SQLBinaryOperator("MOD", 60);
35     
36     enum SQLBinaryOperator Add = SQLBinaryOperator("+", 70);
37     enum SQLBinaryOperator Subtract = SQLBinaryOperator("-", 70);
38     
39     enum SQLBinaryOperator SubGt = SQLBinaryOperator("->", 20);
40     enum SQLBinaryOperator SubGtGt = SQLBinaryOperator("->>", 20);
41     enum SQLBinaryOperator PoundGt = SQLBinaryOperator("#>", 20);
42     enum SQLBinaryOperator PoundGtGt = SQLBinaryOperator("#>>", 20);
43     enum SQLBinaryOperator QuesQues = SQLBinaryOperator("??", 20);
44     enum SQLBinaryOperator QuesBar = SQLBinaryOperator("?|", 20);
45     enum SQLBinaryOperator QuesAmp = SQLBinaryOperator("?&", 20);
46 
47     enum SQLBinaryOperator LeftShift = SQLBinaryOperator("<<", 80); 
48     enum SQLBinaryOperator RightShift = SQLBinaryOperator(">>", 80);
49 
50     enum SQLBinaryOperator BitwiseAnd = SQLBinaryOperator("&", 90);
51     enum SQLBinaryOperator BitwiseOr = SQLBinaryOperator("|", 100);
52     
53     enum SQLBinaryOperator GreaterThan = SQLBinaryOperator(">", 110);
54     enum SQLBinaryOperator GreaterThanOrEqual = SQLBinaryOperator(">=", 110);
55     enum SQLBinaryOperator Is = SQLBinaryOperator("IS", 110);
56     enum SQLBinaryOperator LessThan = SQLBinaryOperator("<", 110);
57     enum SQLBinaryOperator LessThanOrEqual = SQLBinaryOperator("<=", 110);
58     enum SQLBinaryOperator LessThanOrEqualOrGreaterThan = SQLBinaryOperator("<=>",110);
59     enum SQLBinaryOperator LessThanOrGreater = SQLBinaryOperator("<>", 110);
60     
61     enum SQLBinaryOperator Like = SQLBinaryOperator("LIKE", 110);
62     enum SQLBinaryOperator SoudsLike = SQLBinaryOperator("SOUNDS LIKE", 110);
63     enum SQLBinaryOperator NotLike = SQLBinaryOperator("NOT LIKE", 110);
64 
65     enum SQLBinaryOperator ILike = SQLBinaryOperator("ILIKE", 110);
66     enum SQLBinaryOperator NotILike = SQLBinaryOperator("NOT ILIKE", 110);
67     enum SQLBinaryOperator AT_AT = SQLBinaryOperator("@@", 110); // postgresql textsearch
68     enum SQLBinaryOperator SIMILAR_TO = SQLBinaryOperator("SIMILAR TO", 110);
69     enum SQLBinaryOperator POSIX_Regular_Match = SQLBinaryOperator("~", 110);
70     enum SQLBinaryOperator POSIX_Regular_Match_Insensitive = SQLBinaryOperator("~*", 110);
71     enum SQLBinaryOperator POSIX_Regular_Not_Match = SQLBinaryOperator("!~", 110);
72     enum SQLBinaryOperator POSIX_Regular_Not_Match_POSIX_Regular_Match_Insensitive = SQLBinaryOperator("!~*", 110);
73     enum SQLBinaryOperator Array_Contains = SQLBinaryOperator("@>", 110);
74     enum SQLBinaryOperator Array_ContainedBy = SQLBinaryOperator("<@", 110);
75     enum SQLBinaryOperator SAME_AS = SQLBinaryOperator("~=", 110);
76 
77     enum SQLBinaryOperator RLike = SQLBinaryOperator("RLIKE", 110);
78     enum SQLBinaryOperator NotRLike = SQLBinaryOperator("NOT RLIKE", 110);
79     
80     enum SQLBinaryOperator NotEqual = SQLBinaryOperator("!=", 110);
81     enum SQLBinaryOperator NotLessThan = SQLBinaryOperator("!<", 110);
82     enum SQLBinaryOperator NotGreaterThan = SQLBinaryOperator("!>", 110);
83     enum SQLBinaryOperator IsNot = SQLBinaryOperator("IS NOT", 110); 
84     enum SQLBinaryOperator Escape = SQLBinaryOperator("ESCAPE", 110); 
85     enum SQLBinaryOperator RegExp = SQLBinaryOperator("REGEXP", 110);
86     enum SQLBinaryOperator NotRegExp = SQLBinaryOperator("NOT REGEXP", 110);
87     enum SQLBinaryOperator Equality = SQLBinaryOperator("=", 110);
88     
89     enum SQLBinaryOperator BitwiseNot = SQLBinaryOperator("!", 130);
90     enum SQLBinaryOperator Concat = SQLBinaryOperator("||", 140);
91     
92     enum SQLBinaryOperator BooleanAnd = SQLBinaryOperator("AND", 140); 
93     enum SQLBinaryOperator BooleanXor = SQLBinaryOperator("XOR", 150); 
94     enum SQLBinaryOperator BooleanOr = SQLBinaryOperator("OR", 160); 
95     enum SQLBinaryOperator Assignment = SQLBinaryOperator(":=", 169);
96 
97     enum SQLBinaryOperator PG_And = SQLBinaryOperator("&&", 140);
98     enum SQLBinaryOperator PG_ST_DISTANCE = SQLBinaryOperator("<->", 20);
99     ;
100 
101     public static int getPriority(SQLBinaryOperator operator) {
102         return 0;
103     }
104 
105     public  string name;
106     public  string name_lcase;
107     public  int    priority;
108 
109     // this(){
110     //     this(sting.init, 0);
111     // }
112 
113     this(string name, int priority){
114         this.name = name;
115         this.name_lcase = toLower(name);
116         this.priority = priority;
117     }
118     
119     @property public string getName() {
120         return this.name;
121     }
122     
123     public int getPriority() {
124         return this.priority;
125     }
126     
127     public bool isRelational() {
128         switch (this.getName) {
129             case Equality.getName:
130             case Like.getName:
131             case NotEqual.getName:
132             case GreaterThan.getName:
133             case GreaterThanOrEqual.getName:
134             case LessThan.getName:
135             case LessThanOrEqual.getName:
136             case LessThanOrGreater.getName:
137             case NotLike.getName:
138             case NotLessThan.getName:
139             case NotGreaterThan.getName:
140             case RLike.getName:
141             case NotRLike.getName:
142             case RegExp.getName:
143             case NotRegExp.getName:
144             case Is.getName:
145             case IsNot.getName:
146                 return true;
147             default:
148                 return false;
149         }
150     }
151     
152     public bool isLogical() {
153         return this == BooleanAnd || this == BooleanOr || this == BooleanXor;
154     }
155 
156     public bool isArithmetic() {
157         switch (this.getName) {
158             case Add.getName:
159             case Subtract.getName:
160             case Multiply.getName:
161             case Divide.getName:
162             case DIV.getName:
163             case Modulus.getName:
164             case Mod.getName:
165                 return true;
166             default:
167                 return false;
168         }
169     }
170 
171     bool opEquals(const SQLBinaryOperator h) nothrow {
172         return name == h.name ;
173     } 
174 
175     bool opEquals(ref const SQLBinaryOperator h) nothrow {
176         return name == h.name ;
177     } 
178 }