2 // System.Data.SqlTypes.SqlBoolean
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
9 // (C) Ximian, Inc. 2002
10 // (C) Copyright 2002 Tim Coleman
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Globalization;
39 namespace System.Data.SqlTypes
42 /// Represents an integer value that is either 1 or 0
43 /// to be stored in or retrieved from a database.
45 public struct SqlBoolean : INullable, IComparable
55 public static readonly SqlBoolean False = new SqlBoolean (false);
56 public static readonly SqlBoolean Null;
57 public static readonly SqlBoolean One = new SqlBoolean (1);
58 public static readonly SqlBoolean True = new SqlBoolean (true);
59 public static readonly SqlBoolean Zero = new SqlBoolean (0);
65 public SqlBoolean (bool value)
67 this.value = (byte) (value ? 1 : 0);
71 public SqlBoolean (int value)
73 this.value = (byte) (value != 0 ? 1 : 0);
77 #endregion // Constructors
81 public byte ByteValue {
84 throw new SqlNullValueException(Locale.GetText("The property is set to null."));
117 throw new SqlNullValueException(Locale.GetText("The property is set to null."));
123 #endregion // Properties
125 public static SqlBoolean And (SqlBoolean x, SqlBoolean y)
130 public int CompareTo (object value)
134 else if (!(value is SqlBoolean))
135 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBoolean"));
136 else if (((SqlBoolean)value).IsNull)
139 return this.value.CompareTo (((SqlBoolean)value).ByteValue);
142 public override bool Equals(object value)
144 if (!(value is SqlBoolean))
146 if (this.IsNull && ((SqlBoolean)value).IsNull)
148 else if (((SqlBoolean)value).IsNull)
151 return (bool) (this == (SqlBoolean)value);
154 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y)
159 public override int GetHashCode()
170 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y)
175 public static SqlBoolean OnesComplement(SqlBoolean x)
180 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y)
185 public static SqlBoolean Parse(string s)
189 return new SqlBoolean (false);
191 return new SqlBoolean (true);
193 return new SqlBoolean (Boolean.Parse (s));
196 public SqlByte ToSqlByte()
198 return new SqlByte (value);
201 // **************************************************
202 // Conversion from SqlBoolean to other SqlTypes
203 // **************************************************
205 public SqlDecimal ToSqlDecimal()
207 return ((SqlDecimal)this);
210 public SqlDouble ToSqlDouble()
212 return ((SqlDouble)this);
215 public SqlInt16 ToSqlInt16()
217 return ((SqlInt16)this);
220 public SqlInt32 ToSqlInt32()
222 return ((SqlInt32)this);
225 public SqlInt64 ToSqlInt64()
227 return ((SqlInt64)this);
230 public SqlMoney ToSqlMoney()
232 return ((SqlMoney)this);
235 public SqlSingle ToSqlSingle()
237 return ((SqlSingle)this);
240 public SqlString ToSqlString()
243 return new SqlString ("Null");
245 return new SqlString ("True");
247 return new SqlString ("False");
250 public override string ToString()
260 // Bitwise exclusive-OR (XOR)
261 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y)
266 // **************************************************
268 // **************************************************
271 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
273 return new SqlBoolean (x.Value & y.Value);
277 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
279 return new SqlBoolean (x.Value | y.Value);
283 // Compares two instances for equality
284 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
286 if (x.IsNull || y.IsNull)
287 return SqlBoolean.Null;
289 return new SqlBoolean (x.Value == y.Value);
292 // Bitwize exclusive-OR (XOR)
293 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y)
295 return new SqlBoolean (x.Value ^ y.Value);
298 // test Value of SqlBoolean to determine it is false.
299 public static bool operator false (SqlBoolean x)
305 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
307 if (x.IsNull || y.IsNull)
308 return SqlBoolean.Null;
310 return new SqlBoolean (x.Value != y.Value);
314 public static SqlBoolean operator ! (SqlBoolean x)
317 return SqlBoolean.Null;
319 return new SqlBoolean (!x.Value);
323 public static SqlBoolean operator ~ (SqlBoolean x)
327 b = new SqlBoolean(false);
329 b = new SqlBoolean(true);
334 // test to see if value is true
335 public static bool operator true (SqlBoolean x)
340 // ****************************************
342 // ****************************************
345 // SqlBoolean to Boolean
346 public static explicit operator bool (SqlBoolean x)
352 // SqlByte to SqlBoolean
353 public static explicit operator SqlBoolean (SqlByte x)
359 return new SqlBoolean ((int)x.Value);
363 // SqlDecimal to SqlBoolean
364 public static explicit operator SqlBoolean (SqlDecimal x)
370 return new SqlBoolean ((int)x.Value);
374 // SqlDouble to SqlBoolean
375 public static explicit operator SqlBoolean (SqlDouble x)
382 return new SqlBoolean ((int)x.Value);
386 // SqlInt16 to SqlBoolean
387 public static explicit operator SqlBoolean (SqlInt16 x)
393 return new SqlBoolean ((int)x.Value);
397 // SqlInt32 to SqlBoolean
398 public static explicit operator SqlBoolean (SqlInt32 x)
404 return new SqlBoolean (x.Value);
408 // SqlInt64 to SqlBoolean
409 public static explicit operator SqlBoolean (SqlInt64 x)
415 return new SqlBoolean ((int)x.Value);
419 // SqlMoney to SqlBoolean
420 public static explicit operator SqlBoolean (SqlMoney x)
426 return new SqlBoolean ((int)x.Value);
430 // SqlSingle to SqlBoolean
431 public static explicit operator SqlBoolean (SqlSingle x)
438 return new SqlBoolean ((int)x.Value);
442 // SqlString to SqlBoolean
443 public static explicit operator SqlBoolean (SqlString x)
448 return SqlBoolean.Parse (x.Value);
452 // Boolean to SqlBoolean
453 public static implicit operator SqlBoolean (bool x)
455 return new SqlBoolean (x);