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;
40 using System.Xml.Schema;
41 using System.Xml.Serialization;
42 using System.Runtime.Serialization;
45 namespace System.Data.SqlTypes
48 /// Represents an integer value that is either 1 or 0
49 /// to be stored in or retrieved from a database.
51 public struct SqlBoolean : INullable, IComparable
64 public static readonly SqlBoolean False = new SqlBoolean (false);
65 public static readonly SqlBoolean Null;
66 public static readonly SqlBoolean One = new SqlBoolean (1);
67 public static readonly SqlBoolean True = new SqlBoolean (true);
68 public static readonly SqlBoolean Zero = new SqlBoolean (0);
74 public SqlBoolean (bool value)
76 this.value = (byte) (value ? 1 : 0);
80 public SqlBoolean (int value)
82 this.value = (byte) (value != 0 ? 1 : 0);
86 #endregion // Constructors
90 public byte ByteValue {
93 throw new SqlNullValueException(Locale.GetText("The property is set to null."));
126 throw new SqlNullValueException(Locale.GetText("The property is set to null."));
132 #endregion // Properties
134 public static SqlBoolean And (SqlBoolean x, SqlBoolean y)
139 public int CompareTo (object value)
143 if (!(value is SqlBoolean))
144 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBoolean"));
146 return CompareTo ((SqlBoolean) value);
151 int CompareTo (SqlBoolean value)
156 return this.value.CompareTo (value.ByteValue);
159 public override bool Equals(object value)
161 if (!(value is SqlBoolean))
163 if (this.IsNull && ((SqlBoolean)value).IsNull)
165 else if (((SqlBoolean)value).IsNull)
168 return (bool) (this == (SqlBoolean)value);
171 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y)
177 public static SqlBoolean GreaterThan (SqlBoolean x, SqlBoolean y)
182 public static SqlBoolean GreaterThanOrEqual (SqlBoolean x, SqlBoolean y)
187 public static SqlBoolean LessThan (SqlBoolean x, SqlBoolean y)
192 public static SqlBoolean LessThanOrEqual (SqlBoolean x, SqlBoolean y)
197 public override int GetHashCode()
208 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y)
213 public static SqlBoolean OnesComplement(SqlBoolean x)
218 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y)
223 public static SqlBoolean Parse(string s)
227 return new SqlBoolean (false);
229 return new SqlBoolean (true);
231 return new SqlBoolean (Boolean.Parse (s));
234 public SqlByte ToSqlByte()
236 return new SqlByte (value);
239 // **************************************************
240 // Conversion from SqlBoolean to other SqlTypes
241 // **************************************************
243 public SqlDecimal ToSqlDecimal()
245 return ((SqlDecimal)this);
248 public SqlDouble ToSqlDouble()
250 return ((SqlDouble)this);
253 public SqlInt16 ToSqlInt16()
255 return ((SqlInt16)this);
258 public SqlInt32 ToSqlInt32()
260 return ((SqlInt32)this);
263 public SqlInt64 ToSqlInt64()
265 return ((SqlInt64)this);
268 public SqlMoney ToSqlMoney()
270 return ((SqlMoney)this);
273 public SqlSingle ToSqlSingle()
275 return ((SqlSingle)this);
278 public SqlString ToSqlString()
281 return new SqlString ("Null");
283 return new SqlString ("True");
285 return new SqlString ("False");
288 public override string ToString()
298 // Bitwise exclusive-OR (XOR)
299 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y)
304 // **************************************************
306 // **************************************************
309 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
311 return new SqlBoolean (x.Value & y.Value);
315 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
317 return new SqlBoolean (x.Value | y.Value);
321 // Compares two instances for equality
322 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
324 if (x.IsNull || y.IsNull)
325 return SqlBoolean.Null;
327 return new SqlBoolean (x.Value == y.Value);
330 // Bitwize exclusive-OR (XOR)
331 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y)
333 return new SqlBoolean (x.Value ^ y.Value);
336 // test Value of SqlBoolean to determine it is false.
337 public static bool operator false (SqlBoolean x)
343 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
345 if (x.IsNull || y.IsNull)
346 return SqlBoolean.Null;
348 return new SqlBoolean (x.Value != y.Value);
352 public static SqlBoolean operator ! (SqlBoolean x)
355 return SqlBoolean.Null;
357 return new SqlBoolean (!x.Value);
361 public static SqlBoolean operator ~ (SqlBoolean x)
365 b = new SqlBoolean(false);
367 b = new SqlBoolean(true);
373 public static SqlBoolean operator > (SqlBoolean x, SqlBoolean y)
375 if (x.IsNull || y.IsNull)
376 return SqlBoolean.Null;
378 return new SqlBoolean (Compare (x, y) > 0);
381 public static SqlBoolean operator >= (SqlBoolean x, SqlBoolean y)
383 if (x.IsNull || y.IsNull)
384 return SqlBoolean.Null;
386 return new SqlBoolean (Compare (x, y) >= 0);
389 public static SqlBoolean operator < (SqlBoolean x, SqlBoolean y)
391 if (x.IsNull || y.IsNull)
392 return SqlBoolean.Null;
394 return new SqlBoolean (Compare (x, y) < 0);
397 public static SqlBoolean operator <= (SqlBoolean x, SqlBoolean y)
399 if (x.IsNull || y.IsNull)
400 return SqlBoolean.Null;
402 return new SqlBoolean (Compare (x, y) <= 0);
405 // test to see if value is true
406 public static bool operator true (SqlBoolean x)
411 // ****************************************
413 // ****************************************
416 // SqlBoolean to Boolean
417 public static explicit operator bool (SqlBoolean x)
423 // SqlByte to SqlBoolean
424 public static explicit operator SqlBoolean (SqlByte x)
430 return new SqlBoolean ((int)x.Value);
434 // SqlDecimal to SqlBoolean
435 public static explicit operator SqlBoolean (SqlDecimal x)
441 return new SqlBoolean ((int)x.Value);
445 // SqlDouble to SqlBoolean
446 public static explicit operator SqlBoolean (SqlDouble x)
453 return new SqlBoolean ((int)x.Value);
457 // SqlInt16 to SqlBoolean
458 public static explicit operator SqlBoolean (SqlInt16 x)
464 return new SqlBoolean ((int)x.Value);
468 // SqlInt32 to SqlBoolean
469 public static explicit operator SqlBoolean (SqlInt32 x)
475 return new SqlBoolean (x.Value);
479 // SqlInt64 to SqlBoolean
480 public static explicit operator SqlBoolean (SqlInt64 x)
486 return new SqlBoolean ((int)x.Value);
490 // SqlMoney to SqlBoolean
491 public static explicit operator SqlBoolean (SqlMoney x)
497 return new SqlBoolean ((int)x.Value);
501 // SqlSingle to SqlBoolean
502 public static explicit operator SqlBoolean (SqlSingle x)
509 return new SqlBoolean ((int)x.Value);
513 // SqlString to SqlBoolean
514 public static explicit operator SqlBoolean (SqlString x)
519 return SqlBoolean.Parse (x.Value);
523 // Boolean to SqlBoolean
524 public static implicit operator SqlBoolean (bool x)
526 return new SqlBoolean (x);
529 // Helper method to Compare methods and operators.
530 // Returns 0 if x == y
533 private static int Compare (SqlBoolean x, SqlBoolean y)
537 if (x.IsTrue && y.IsFalse)
539 if (x.IsFalse && y.IsTrue)
544 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
546 XmlQualifiedName qualifiedName = new XmlQualifiedName ("boolean", "http://www.w3.org/2001/XMLSchema");
547 return qualifiedName;
551 XmlSchema IXmlSerializable.GetSchema ()
553 throw new NotImplementedException ();
557 void IXmlSerializable.ReadXml (XmlReader reader)
559 throw new NotImplementedException ();
563 void IXmlSerializable.WriteXml (XmlWriter writer)
565 throw new NotImplementedException ();