DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlBoolean.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 
20 
21 namespace Deveel.Data.Sql.Objects {
34  [Serializable]
35  public struct SqlBoolean : ISqlObject, IEquatable<SqlBoolean>, IComparable<SqlBoolean>, IConvertible, ISerializable {
36  private readonly byte? value;
37 
41  public static readonly SqlBoolean True = new SqlBoolean(1);
42 
46  public static readonly SqlBoolean False = new SqlBoolean(0);
47 
51  public static readonly SqlBoolean Null = new SqlBoolean((byte?)null);
52 
61  public SqlBoolean(byte value)
62  : this() {
63  if (value != 0 &&
64  value != 1)
65  throw new ArgumentOutOfRangeException("value");
66 
67  this.value = value;
68  }
69 
74  public SqlBoolean(bool value)
75  : this((byte)(value ? 1 : 0)) {
76  }
77 
78  private SqlBoolean(byte? value)
79  : this() {
80  this.value = value;
81  }
82 
83  private SqlBoolean(ObjectData data)
84  : this() {
85  if (data.HasValue("Value"))
86  value = data.GetByte("Value");
87  }
88 
89  int IComparable.CompareTo(object obj) {
90  if (!(obj is ISqlObject))
91  throw new ArgumentException();
92 
93  return CompareTo((ISqlObject) obj);
94  }
95 
97  if (value != null)
98  data.SetValue("Value", value.Value);
99  }
100 
102  public int CompareTo(ISqlObject other) {
103  if (other is SqlNull) {
104  if (IsNull)
105  return 0;
106  return 1;
107  }
108 
109  SqlBoolean otherBoolean;
110  if (other is SqlNumber) {
111  var num = (SqlNumber) other;
112  if (num.IsNull) {
113  otherBoolean = Null;
114  } else if (num == SqlNumber.One) {
115  otherBoolean = True;
116  } else if (num == SqlNumber.Zero) {
117  otherBoolean = False;
118  } else {
119  throw new ArgumentOutOfRangeException("other", "The given numeric value is out of range for a comparison with SQL BOOLEAN.");
120  }
121  } else if (other is SqlBoolean) {
122  otherBoolean = (SqlBoolean) other;
123  } else {
124  throw new ArgumentException(String.Format("Object of type {0} cannot be compared to SQL BOOLEAN", other.GetType().FullName));
125  }
126 
127  return CompareTo(otherBoolean);
128  }
129 
131  public bool IsNull {
132  get { return value == null; }
133  }
134 
149  public bool IsComparableTo(ISqlObject other) {
150  if (other is SqlBoolean || other is SqlNull)
151  return true;
152 
153  if (other is SqlNumber) {
154  var num = (SqlNumber) other;
155  return num == SqlNumber.Zero || num == SqlNumber.One;
156  }
157 
158  return false;
159  }
160 
162  public override bool Equals(object obj) {
163  if (obj is SqlNull && IsNull)
164  return true;
165 
166  if (!(obj is SqlBoolean))
167  return false;
168 
169  return Equals((SqlBoolean) obj);
170  }
171 
173  public bool Equals(SqlBoolean other) {
174  if (IsNull && other.IsNull)
175  return true;
176  if (IsNull && !other.IsNull)
177  return false;
178  if (!IsNull && other.IsNull)
179  return false;
180 
181  return value.Equals(other.value);
182  }
183 
184  public SqlBoolean Not() {
185  if (value == null)
186  return Null;
187 
188  if (value == 1)
189  return False;
190  if (value == 0)
191  return True;
192 
193  throw new InvalidOperationException();
194  }
195 
196  public SqlBoolean Or(SqlBoolean other) {
197  if (value == null ||
198  other.IsNull)
199  return Null;
200 
201  if (value == 1 || other.value == 1)
202  return True;
203 
204  return False;
205  }
206 
207  public SqlBoolean And(SqlBoolean other) {
208  if (value == null ||
209  other.IsNull)
210  return Null;
211 
212  if (value == 1 && other.value == 1)
213  return True;
214 
215  return False;
216  }
217 
218  public SqlBoolean XOr(SqlBoolean other) {
219  if (value == null ||
220  other.IsNull)
221  return Null;
222 
223  if (value == 1 && other.value == 0)
224  return True;
225  if (value == 0 && other.value == 1)
226  return True;
227 
228  return False;
229  }
230 
232  public override int GetHashCode() {
233  return value == null ? 0 : value.Value.GetHashCode();
234  }
235 
236  TypeCode IConvertible.GetTypeCode() {
237  return TypeCode.Boolean;
238  }
239 
240  bool IConvertible.ToBoolean(IFormatProvider provider) {
241  return this;
242  }
243 
244  char IConvertible.ToChar(IFormatProvider provider) {
245  throw new InvalidCastException();
246  }
247 
248  sbyte IConvertible.ToSByte(IFormatProvider provider) {
249  return (sbyte) (this as IConvertible).ToInt32(provider);
250  }
251 
252  byte IConvertible.ToByte(IFormatProvider provider) {
253  if (value == null)
254  throw new NullReferenceException();
255 
256  return value.Value;
257  }
258 
259  short IConvertible.ToInt16(IFormatProvider provider) {
260  return (short) (this as IConvertible).ToInt32(provider);
261  }
262 
263  ushort IConvertible.ToUInt16(IFormatProvider provider) {
264  return (ushort) (this as IConvertible).ToInt32(provider);
265  }
266 
267  int IConvertible.ToInt32(IFormatProvider provider) {
268  if (value == null)
269  throw new NullReferenceException();
270 
271  return value.Value;
272  }
273 
274  uint IConvertible.ToUInt32(IFormatProvider provider) {
275  return (uint) (this as IConvertible).ToInt32(provider);
276  }
277 
278  long IConvertible.ToInt64(IFormatProvider provider) {
279  return (this as IConvertible).ToInt64(provider);
280  }
281 
282  ulong IConvertible.ToUInt64(IFormatProvider provider) {
283  return (this as IConvertible).ToUInt64(provider);
284  }
285 
286  float IConvertible.ToSingle(IFormatProvider provider) {
287  return (this as IConvertible).ToInt32(provider);
288  }
289 
290  double IConvertible.ToDouble(IFormatProvider provider) {
291  return (this as IConvertible).ToInt32(provider);
292  }
293 
294  decimal IConvertible.ToDecimal(IFormatProvider provider) {
295  return (this as IConvertible).ToInt32(provider);
296  }
297 
298  DateTime IConvertible.ToDateTime(IFormatProvider provider) {
299  throw new InvalidCastException();
300  }
301 
302  string IConvertible.ToString(IFormatProvider provider) {
303  return ToString();
304  }
305 
306  object IConvertible.ToType(Type conversionType, IFormatProvider provider) {
307  if (conversionType == typeof (bool))
308  return (bool) this;
309 
310  throw new InvalidCastException(String.Format("Cannot convert a SQL BOOLEAN to {0}", conversionType.FullName));
311  }
312 
314  public int CompareTo(SqlBoolean other) {
315  if (other.IsNull && IsNull)
316  return 0;
317 
318  if (IsNull && !other.IsNull)
319  return -1;
320  if (!IsNull && other.IsNull)
321  return 1;
322 
323  return value.Value.CompareTo(other.value.Value);
324  }
325 
334  public static SqlBoolean operator ==(SqlBoolean a, SqlBoolean b) {
335  return a.Equals(b);
336  }
337 
344  public static SqlBoolean operator !=(SqlBoolean a, SqlBoolean b) {
345  return !(a == b);
346  }
347 
348  public static SqlBoolean operator ==(SqlBoolean a, ISqlObject b) {
349  return a.Equals(b);
350  }
351 
352  public static SqlBoolean operator !=(SqlBoolean a, ISqlObject b) {
353  return !(a == b);
354  }
355 
356  public static SqlBoolean operator &(SqlBoolean a, SqlBoolean b) {
357  return a.And(b);
358  }
359 
360  public static SqlBoolean operator |(SqlBoolean a, SqlBoolean b) {
361  return a.Or(b);
362  }
363 
364  public static SqlBoolean operator ^(SqlBoolean a, SqlBoolean b) {
365  return a.XOr(b);
366  }
367 
368  public static SqlBoolean operator !(SqlBoolean a) {
369  return a.Not();
370  }
371 
380  public static implicit operator bool(SqlBoolean value) {
381  if (value.IsNull)
382  throw new InvalidCastException();
383 
384  return value.value == 1;
385  }
386 
395  public static implicit operator SqlBoolean(bool value) {
396  return new SqlBoolean(value);
397  }
398 
414  public static SqlBoolean Parse(string s) {
415  if (String.IsNullOrEmpty(s))
416  throw new ArgumentNullException("s");
417 
418  SqlBoolean value;
419  if (!TryParse(s, out value))
420  throw new FormatException();
421 
422  return value;
423  }
424 
442  public static bool TryParse(string s, out SqlBoolean value) {
443  value = new SqlBoolean();
444 
445  if (String.IsNullOrEmpty(s))
446  return false;
447 
448  if (String.Equals(s, "true", StringComparison.OrdinalIgnoreCase) ||
449  String.Equals(s, "1")) {
450  value = True;
451  return true;
452  }
453  if (String.Equals(s, "false", StringComparison.OrdinalIgnoreCase) ||
454  String.Equals(s, "0")) {
455  value = False;
456  return true;
457  }
458 
459  return false;
460  }
461 
463  public override string ToString() {
464  if (value == null)
465  return "NULL";
466  if (value == 1)
467  return "true";
468  if (value == 0)
469  return "false";
470 
471  throw new InvalidOperationException("Should never happen!");
472  }
473  }
474 }
static bool TryParse(string s, out SqlBoolean value)
Attempts to parse a given string to an instance of SqlBoolean.
Definition: SqlBoolean.cs:442
void GetData(SerializeData data)
SqlBoolean(byte value)
Constructs a given boolean object with a defined byte value.
Definition: SqlBoolean.cs:61
SqlBoolean XOr(SqlBoolean other)
Definition: SqlBoolean.cs:218
void SetValue(string key, Type type, object value)
override bool Equals(object obj)
Definition: SqlBoolean.cs:162
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
bool Equals(SqlBoolean other)
Definition: SqlBoolean.cs:173
SqlBoolean(bool value)
Constructs an object from a runtime boolean object.
Definition: SqlBoolean.cs:74
static SqlBoolean Parse(string s)
Parses the given string to extract a boolean value equivalent.
Definition: SqlBoolean.cs:414
bool IsComparableTo(ISqlObject other)
Indicates if the given ISqlObject can be compared to this SqlBoolean.
Definition: SqlBoolean.cs:149
Deveel.Data.Sql.Objects.SqlBoolean SqlBoolean
Definition: DataObject.cs:26
SqlBoolean Or(SqlBoolean other)
Definition: SqlBoolean.cs:196
SqlBoolean And(SqlBoolean other)
Definition: SqlBoolean.cs:207
int CompareTo(ISqlObject other)
Definition: SqlBoolean.cs:102
int CompareTo(SqlBoolean other)
Definition: SqlBoolean.cs:314