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.SQLBooleanExpr;
17 
18 import hunt.sql.ast.SQLDataType;
19 import hunt.sql.ast.SQLDataTypeImpl;
20 import hunt.sql.ast.SQLExpr;
21 import hunt.sql.ast.SQLExprImpl;
22 import hunt.sql.ast.statement.SQLCharacterDataType;
23 import hunt.sql.visitor.SQLASTVisitor;
24 import hunt.sql.ast.expr.SQLValuableExpr;
25 import hunt.sql.ast.expr.SQLLiteralExpr;
26 import hunt.collection;
27 import hunt.sql.ast.SQLObject;
28 import hunt.Boolean;
29 import hunt.util.StringBuilder;
30 
31 import std.concurrency : initOnce;
32 
33 public  class SQLBooleanExpr : SQLExprImpl , SQLExpr, SQLLiteralExpr, SQLValuableExpr {
34 
35     static SQLDataType DEFAULT_DATA_TYPE() {
36         __gshared SQLDataType inst;
37         return initOnce!inst(new SQLDataTypeImpl(SQLDataType.Constants.BOOLEAN));
38     }
39 
40     private bool value;
41 
42     public this(){
43 
44     }
45 
46     public this(bool value){
47         this.value = value;
48     }
49 
50     public Boolean getBooleanValue() {
51         return new Boolean(value);
52     }
53 
54     public Boolean getValue() {
55         return new Boolean(value);
56     }
57 
58     public void setValue(bool value) {
59         this.value = value;
60     }
61 
62     override  protected void accept0(SQLASTVisitor visitor) {
63         visitor.visit(this);
64 
65         visitor.endVisit(this);
66     }
67 
68     override public void output(StringBuilder buf) {
69         buf.append("x");
70         buf.append(value ? "TRUE" : "FALSE");
71     }
72 
73    override
74     public size_t toHash() @trusted nothrow {
75          int prime = 31;
76         size_t result = 1;
77         result = prime * result + (value ? 1231 : 1237);
78         return result;
79     }
80 
81    override
82     public bool opEquals(Object obj) {
83         if (this is obj) {
84             return true;
85         }
86         if (obj is null) {
87             return false;
88         }
89         if (typeid(this) != typeid(obj)) {
90             return false;
91         }
92         SQLBooleanExpr other = cast(SQLBooleanExpr) obj;
93         if (value != other.value) {
94             return false;
95         }
96         return true;
97     }
98 
99     override public SQLDataType computeDataType() {
100         return DEFAULT_DATA_TYPE;
101     }
102 
103     override public SQLBooleanExpr clone() {
104         return new SQLBooleanExpr(value);
105     }
106 
107    override
108     public List!SQLObject getChildren() {
109         return Collections.emptyList!(SQLObject)();
110     }
111 
112     public static enum Type {
113         ON_OFF
114     }
115 }