DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
BooleanType.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
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 
17 using System;
18 using System.Globalization;
19 using System.IO;
20 
22 using Deveel.Data.Sql.Objects;
23 
24 namespace Deveel.Data.Types {
25  [Serializable]
26  public sealed class BooleanType : SqlType {
27  public BooleanType(SqlTypeCode typeCode)
28  : base("BOOLEAN", typeCode) {
29  AssertIsBoolean(typeCode);
30  }
31 
32  private BooleanType(ObjectData data)
33  : base(data) {
34  }
35 
36  private static void AssertIsBoolean(SqlTypeCode sqlType) {
37  if (!IsBooleanType(sqlType))
38  throw new ArgumentException(String.Format("The SQL type {0} is not BOOLEAN.", sqlType));
39  }
40 
41  internal static bool IsBooleanType(SqlTypeCode sqlType) {
42  return (sqlType == SqlTypeCode.Bit ||
43  sqlType == SqlTypeCode.Boolean);
44  }
45 
46  public override bool IsStorable {
47  get { return true; }
48  }
49 
50  public override bool IsCacheable(ISqlObject value) {
51  return value is SqlBoolean || value is SqlNull;
52  }
53 
54  public override Type GetObjectType() {
55  return typeof (SqlBoolean);
56  }
57 
58  public override Type GetRuntimeType() {
59  return typeof(bool);
60  }
61 
62  public override int Compare(ISqlObject x, ISqlObject y) {
63  if (!(x is SqlBoolean))
64  throw new ArgumentException();
65 
66  var a = (SqlBoolean) x;
67  SqlBoolean b;
68 
69  if (y is SqlNumber) {
70  b = ((SqlNumber) y) == SqlNumber.One ? SqlBoolean.True : ((SqlNumber) y) == SqlNumber.Zero ? SqlBoolean.False : SqlBoolean.Null;
71  } else if (y is SqlBoolean) {
72  b = (SqlBoolean) y;
73  } else {
74  throw new ArgumentException();
75  }
76 
77  return a.CompareTo(b);
78  }
79 
80  public override bool IsComparable(SqlType type) {
81  return type is BooleanType || type is NumericType;
82  }
83 
84  public override bool CanCastTo(SqlType destType) {
85  return destType is StringType ||
86  destType is NumericType ||
87  destType is BinaryType ||
88  destType is BooleanType;
89  }
90 
91  public override DataObject CastTo(DataObject value, SqlType destType) {
92  var bValue = ((SqlBoolean) value.Value);
93  if (destType is StringType) {
94  var s = Convert.ToString(bValue);
95  // TODO: Provide a method in StringType to build a string specific to the type
96  return new DataObject(destType, new SqlString(s));
97  }
98  if (destType is NumericType) {
99  SqlNumber num;
100  if (bValue == SqlBoolean.Null) {
101  num = SqlNumber.Null;
102  } else if (bValue) {
103  num = SqlNumber.One;
104  } else {
105  num = SqlNumber.Zero;
106  }
107 
108  return new DataObject(destType, num);
109  }
110  if (destType is BinaryType) {
111  var bytes = (byte[]) Convert.ChangeType(bValue, typeof (byte[]), CultureInfo.InvariantCulture);
112  return new DataObject(destType, new SqlBinary(bytes));
113  }
114  if (destType is BooleanType)
115  return value;
116 
117  return base.CastTo(value, destType);
118  }
119 
120  public override ISqlObject Negate(ISqlObject value) {
121  var b = (SqlBoolean) value;
122  return b.Not();
123  }
124 
125  public override void SerializeObject(Stream stream, ISqlObject obj) {
126  var writer = new BinaryWriter(stream);
127 
128  if (obj is SqlNull) {
129  writer.Write((byte)0);
130  } else if (obj is SqlBoolean) {
131  var b = (SqlBoolean)obj;
132 
133  if (b.IsNull) {
134  writer.Write((byte)0);
135  } else {
136  var value = (bool)b;
137  writer.Write((byte)1);
138  writer.Write((byte)(value ? 1 : 0));
139  }
140  } else {
141  throw new ArgumentException("Cannot serialize an object that is not a BOOLEAN using a boolean type.");
142  }
143  }
144 
145  public override ISqlObject DeserializeObject(Stream stream) {
146  var reader = new BinaryReader(stream);
147 
148  var type = reader.ReadByte();
149  if (type == 0)
150  return SqlBoolean.Null;
151 
152  var value = reader.ReadByte();
153  return new SqlBoolean(value);
154  }
155 
156  internal override int ColumnSizeOf(ISqlObject obj) {
157  return obj.IsNull ? 1 : 1 + 1;
158  }
159 
160  public override ISqlObject Reverse(ISqlObject value) {
161  return Negate(value);
162  }
163 
164  public override ISqlObject And(ISqlObject a, ISqlObject b) {
165  if (a.IsNull || b.IsNull)
166  return SqlBoolean.Null;
167 
168  var b1 = (SqlBoolean) a;
169  var b2 = (SqlBoolean) b;
170 
171  return b1.And(b2);
172  }
173 
174  public override ISqlObject Or(ISqlObject a, ISqlObject b) {
175  if (a.IsNull || b.IsNull)
176  return SqlBoolean.Null;
177 
178  var b1 = (SqlBoolean)a;
179  var b2 = (SqlBoolean)b;
180 
181  return b1.Or(b2);
182  }
183 
184  public override ISqlObject XOr(ISqlObject a, ISqlObject b) {
185  if (a.IsNull || b.IsNull)
186  return SqlBoolean.Null;
187 
188  var b1 = (SqlBoolean)a;
189  var b2 = (SqlBoolean)b;
190 
191  return b1.And(b2);
192  }
193  }
194 }
static readonly SqlBoolean True
Represents the materialization of a true boolean.
Definition: SqlBoolean.cs:41
override Type GetRuntimeType()
Definition: BooleanType.cs:58
static bool IsBooleanType(SqlTypeCode sqlType)
Definition: BooleanType.cs:41
A long string in the system.
BooleanType(SqlTypeCode typeCode)
Definition: BooleanType.cs:27
DataObject CastTo(SqlType destType)
Converts this object to the given SqlType.
Definition: DataObject.cs:488
override int Compare(ISqlObject x, ISqlObject y)
Definition: BooleanType.cs:62
Implements a BINARY object that handles a limited number of bytes, not exceding MaxLength.
Definition: SqlBinary.cs:27
BooleanType(ObjectData data)
Definition: BooleanType.cs:32
override ISqlObject XOr(ISqlObject a, ISqlObject b)
Definition: BooleanType.cs:184
override bool IsComparable(SqlType type)
Verifies if a given SqlType is comparable to this data-type.
Definition: BooleanType.cs:80
static readonly SqlBoolean False
Represents the materialization of a false boolean.
Definition: SqlBoolean.cs:46
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
override ISqlObject And(ISqlObject a, ISqlObject b)
Definition: BooleanType.cs:164
bool IsNull
Gets a boolean value indicating if the object is NULL.
Definition: ISqlObject.cs:28
static readonly SqlBoolean Null
Defines a null boolean.
Definition: SqlBoolean.cs:51
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
override ISqlObject Reverse(ISqlObject value)
Definition: BooleanType.cs:160
override void SerializeObject(Stream stream, ISqlObject obj)
Definition: BooleanType.cs:125
static readonly SqlNumber Zero
Definition: SqlNumber.cs:29
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
override Type GetObjectType()
Definition: BooleanType.cs:54
static void AssertIsBoolean(SqlTypeCode sqlType)
Definition: BooleanType.cs:36
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
override ISqlObject Negate(ISqlObject value)
Definition: BooleanType.cs:120
static readonly SqlNumber Null
Definition: SqlNumber.cs:31
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
static readonly SqlNumber One
Definition: SqlNumber.cs:30
override bool IsCacheable(ISqlObject value)
Definition: BooleanType.cs:50
Deveel.Data.Sql.Objects.SqlBoolean SqlBoolean
Definition: DataObject.cs:26
override int ColumnSizeOf(ISqlObject obj)
Definition: BooleanType.cs:156
override ISqlObject Or(ISqlObject a, ISqlObject b)
Definition: BooleanType.cs:174
override bool CanCastTo(SqlType destType)
Verifies if this type can cast any value to the given SqlType.
Definition: BooleanType.cs:84
override DataObject CastTo(DataObject value, SqlType destType)
Converts the given object value to a SqlType specified.
Definition: BooleanType.cs:91
Deveel.Data.Sql.Objects.SqlString SqlString
Definition: DataObject.cs:27
override ISqlObject DeserializeObject(Stream stream)
Definition: BooleanType.cs:145