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.
52 [SerializableAttribute]
53 [XmlSchemaProvider ("GetXsdType")]
55 public struct SqlBoolean : INullable, IComparable
68 public static readonly SqlBoolean False = new SqlBoolean (false);
69 public static readonly SqlBoolean Null;
70 public static readonly SqlBoolean One = new SqlBoolean (1);
71 public static readonly SqlBoolean True = new SqlBoolean (true);
72 public static readonly SqlBoolean Zero = new SqlBoolean (0);
78 public SqlBoolean (bool value)
80 this.value = (byte) (value ? 1 : 0);
84 public SqlBoolean (int value)
86 this.value = (byte) (value != 0 ? 1 : 0);
90 #endregion // Constructors
94 public byte ByteValue {
97 throw new SqlNullValueException(Locale.GetText("The property is set to null."));
103 public bool IsFalse {
130 throw new SqlNullValueException(Locale.GetText("The property is set to null."));
136 #endregion // Properties
138 public static SqlBoolean And (SqlBoolean x, SqlBoolean y)
143 public int CompareTo (object value)
147 if (!(value is SqlBoolean))
148 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBoolean"));
150 return CompareTo ((SqlBoolean) value);
155 int CompareTo (SqlBoolean value)
160 return this.value.CompareTo (value.ByteValue);
163 public override bool Equals(object value)
165 if (!(value is SqlBoolean))
168 return ((SqlBoolean)value).IsNull;
169 else if (((SqlBoolean)value).IsNull)
172 return (bool) (this == (SqlBoolean)value);
175 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y)
181 public static SqlBoolean GreaterThan (SqlBoolean x, SqlBoolean y)
186 public static SqlBoolean GreaterThanOrEquals (SqlBoolean x, SqlBoolean y)
191 public static SqlBoolean LessThan (SqlBoolean x, SqlBoolean y)
196 public static SqlBoolean LessThanOrEquals (SqlBoolean x, SqlBoolean y)
201 public override int GetHashCode()
212 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y)
217 public static SqlBoolean OnesComplement(SqlBoolean x)
222 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y)
227 public static SqlBoolean Parse(string s)
231 return new SqlBoolean (false);
233 return new SqlBoolean (true);
235 return new SqlBoolean (Boolean.Parse (s));
238 public SqlByte ToSqlByte()
240 return new SqlByte (value);
243 // **************************************************
244 // Conversion from SqlBoolean to other SqlTypes
245 // **************************************************
247 public SqlDecimal ToSqlDecimal()
249 return ((SqlDecimal)this);
252 public SqlDouble ToSqlDouble()
254 return ((SqlDouble)this);
257 public SqlInt16 ToSqlInt16()
259 return ((SqlInt16)this);
262 public SqlInt32 ToSqlInt32()
264 return ((SqlInt32)this);
267 public SqlInt64 ToSqlInt64()
269 return ((SqlInt64)this);
272 public SqlMoney ToSqlMoney()
274 return ((SqlMoney)this);
277 public SqlSingle ToSqlSingle()
279 return ((SqlSingle)this);
282 public SqlString ToSqlString()
285 return new SqlString ("Null");
287 return new SqlString ("True");
289 return new SqlString ("False");
292 public override string ToString()
302 // Bitwise exclusive-OR (XOR)
303 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y)
308 // **************************************************
310 // **************************************************
313 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
315 return new SqlBoolean (x.Value & y.Value);
319 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
321 return new SqlBoolean (x.Value | y.Value);
325 // Compares two instances for equality
326 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
328 if (x.IsNull || y.IsNull)
329 return SqlBoolean.Null;
331 return new SqlBoolean (x.Value == y.Value);
334 // Bitwize exclusive-OR (XOR)
335 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y)
337 return new SqlBoolean (x.Value ^ y.Value);
340 // test Value of SqlBoolean to determine it is false.
341 public static bool operator false (SqlBoolean x)
347 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
349 if (x.IsNull || y.IsNull)
350 return SqlBoolean.Null;
352 return new SqlBoolean (x.Value != y.Value);
356 public static SqlBoolean operator ! (SqlBoolean x)
359 return SqlBoolean.Null;
361 return new SqlBoolean (!x.Value);
365 public static SqlBoolean operator ~ (SqlBoolean x)
369 b = new SqlBoolean(false);
371 b = new SqlBoolean(true);
377 public static SqlBoolean operator > (SqlBoolean x, SqlBoolean y)
379 if (x.IsNull || y.IsNull)
380 return SqlBoolean.Null;
382 return new SqlBoolean (Compare (x, y) > 0);
385 public static SqlBoolean operator >= (SqlBoolean x, SqlBoolean y)
387 if (x.IsNull || y.IsNull)
388 return SqlBoolean.Null;
390 return new SqlBoolean (Compare (x, y) >= 0);
393 public static SqlBoolean operator < (SqlBoolean x, SqlBoolean y)
395 if (x.IsNull || y.IsNull)
396 return SqlBoolean.Null;
398 return new SqlBoolean (Compare (x, y) < 0);
401 public static SqlBoolean operator <= (SqlBoolean x, SqlBoolean y)
403 if (x.IsNull || y.IsNull)
404 return SqlBoolean.Null;
406 return new SqlBoolean (Compare (x, y) <= 0);
409 // test to see if value is true
410 public static bool operator true (SqlBoolean x)
415 // ****************************************
417 // ****************************************
420 // SqlBoolean to Boolean
421 public static explicit operator bool (SqlBoolean x)
427 // SqlByte to SqlBoolean
428 public static explicit operator SqlBoolean (SqlByte x)
434 return new SqlBoolean ((int)x.Value);
438 // SqlDecimal to SqlBoolean
439 public static explicit operator SqlBoolean (SqlDecimal x)
445 return new SqlBoolean ((int)x.Value);
449 // SqlDouble to SqlBoolean
450 public static explicit operator SqlBoolean (SqlDouble x)
457 return new SqlBoolean ((int)x.Value);
461 // SqlInt16 to SqlBoolean
462 public static explicit operator SqlBoolean (SqlInt16 x)
468 return new SqlBoolean ((int)x.Value);
472 // SqlInt32 to SqlBoolean
473 public static explicit operator SqlBoolean (SqlInt32 x)
479 return new SqlBoolean (x.Value);
483 // SqlInt64 to SqlBoolean
484 public static explicit operator SqlBoolean (SqlInt64 x)
490 return new SqlBoolean ((int)x.Value);
494 // SqlMoney to SqlBoolean
495 public static explicit operator SqlBoolean (SqlMoney x)
501 return new SqlBoolean ((int)x.Value);
505 // SqlSingle to SqlBoolean
506 public static explicit operator SqlBoolean (SqlSingle x)
513 return new SqlBoolean ((int)x.Value);
517 // SqlString to SqlBoolean
518 public static explicit operator SqlBoolean (SqlString x)
523 return SqlBoolean.Parse (x.Value);
527 // Boolean to SqlBoolean
528 public static implicit operator SqlBoolean (bool x)
530 return new SqlBoolean (x);
533 // Helper method to Compare methods and operators.
534 // Returns 0 if x == y
537 private static int Compare (SqlBoolean x, SqlBoolean y)
541 if (x.IsTrue && y.IsFalse)
543 if (x.IsFalse && y.IsTrue)
548 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
550 XmlQualifiedName qualifiedName = new XmlQualifiedName ("boolean", "http://www.w3.org/2001/XMLSchema");
551 return qualifiedName;
555 XmlSchema IXmlSerializable.GetSchema ()
557 throw new NotImplementedException ();
561 void IXmlSerializable.ReadXml (XmlReader reader)
563 throw new NotImplementedException ();
567 void IXmlSerializable.WriteXml (XmlWriter writer)
569 throw new NotImplementedException ();