1 //------------------------------------------------------------------------------
2 // <copyright file="SQLBoolean.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">junfang</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="true" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
10 //**************************************************************************
11 // @File: SqlBoolean.cs
15 // Purpose: Implementation of SqlBoolean which is equivalent to
16 // data type "bit" in SQL Server
22 // 09/17/99 JunFang Created and implemented as first drop.
25 //**************************************************************************
28 using System
.Data
.Common
;
29 using System
.Runtime
.InteropServices
;
30 using System
.Globalization
;
32 using System
.Xml
.Schema
;
33 using System
.Xml
.Serialization
;
35 namespace System
.Data
.SqlTypes
{
38 /// Represents an integer value that is either 1 or 0.
42 [StructLayout(LayoutKind
.Sequential
)]
43 [XmlSchemaProvider("GetXsdType")]
44 public struct SqlBoolean
: INullable
, IComparable
, IXmlSerializable
{
46 // m_value: 2 (true), 1 (false), 0 (unknown/Null)
49 private const byte x_Null
= 0;
50 private const byte x_False
= 1;
51 private const byte x_True
= 2;
57 /// Initializes a new instance of the <see cref='System.Data.SqlTypes.SqlBoolean'/> class.
60 public SqlBoolean(bool value) {
61 m_value
= (byte)(value ? x_True
: x_False
);
65 /// <para>[To be supplied.]</para>
67 public SqlBoolean(int value) : this(value, false) {
70 private SqlBoolean(int value, bool fNull
) {
74 m_value
= (value != 0) ? x_True
: x_False
;
81 /// Gets whether the current <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> is <see cref='System.Data.SqlTypes.SqlBoolean.Null'/>.
85 get { return m_value == x_Null;}
91 /// Gets or sets the <see cref='System.Data.SqlTypes.SqlBoolean'/> to be <see langword='true'/> or
92 /// <see langword='false'/>.
105 throw new SqlNullValueException();
113 /// Gets whether the current <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> is <see cref='System.Data.SqlTypes.SqlBoolean.True'/>.
117 get { return m_value == x_True;}
123 /// Gets whether the current <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> is <see cref='System.Data.SqlTypes.SqlBoolean.False'/>.
126 public bool IsFalse
{
127 get { return m_value == x_False;}
131 // Implicit conversion from bool to SqlBoolean
134 /// Converts a boolean to a <see cref='System.Data.SqlTypes.SqlBoolean'/>.
137 public static implicit operator SqlBoolean(bool x
) {
138 return new SqlBoolean(x
);
141 // Explicit conversion from SqlBoolean to bool. Throw exception if x is Null.
144 /// Converts a <see cref='System.Data.SqlTypes.SqlBoolean'/>
148 public static explicit operator bool(SqlBoolean x
) {
157 /// Performs a NOT operation on a <see cref='System.Data.SqlTypes.SqlBoolean'/>
161 public static SqlBoolean
operator !(SqlBoolean x
) {
164 return SqlBoolean
.False
;
167 return SqlBoolean
.True
;
170 SQLDebug
.Check(x
.m_value
== x_Null
);
171 return SqlBoolean
.Null
;
176 /// <para>[To be supplied.]</para>
178 public static bool operator true(SqlBoolean x
) {
183 /// <para>[To be supplied.]</para>
185 public static bool operator false(SqlBoolean x
) {
193 /// Performs a bitwise AND operation on two instances of
194 /// <see cref='System.Data.SqlTypes.SqlBoolean'/>
198 public static SqlBoolean
operator &(SqlBoolean x
, SqlBoolean y
) {
199 if (x
.m_value
== x_False
|| y
.m_value
== x_False
)
200 return SqlBoolean
.False
;
201 else if (x
.m_value
== x_True
&& y
.m_value
== x_True
)
202 return SqlBoolean
.True
;
204 return SqlBoolean
.Null
;
210 /// a bitwise OR operation on two instances of a
211 /// <see cref='System.Data.SqlTypes.SqlBoolean'/>
215 public static SqlBoolean
operator |(SqlBoolean x
, SqlBoolean y
) {
216 if (x
.m_value
== x_True
|| y
.m_value
== x_True
)
217 return SqlBoolean
.True
;
218 else if (x
.m_value
== x_False
&& y
.m_value
== x_False
)
219 return SqlBoolean
.False
;
221 return SqlBoolean
.Null
;
226 // property: ByteValue
228 /// <para>[To be supplied.]</para>
230 public byte ByteValue
{
233 return (m_value
== x_True
) ? (byte)1 : (byte)0;
235 throw new SqlNullValueException();
240 /// <para>[To be supplied.]</para>
242 public override String
ToString() {
243 return IsNull
? SQLResource
.NullString
: Value
.ToString((IFormatProvider
)null);
247 /// <para>[To be supplied.]</para>
249 public static SqlBoolean
Parse(String s
) {
251 // Let Boolean.Parse throw exception
252 return new SqlBoolean(Boolean
.Parse(s
));
253 if (s
== SQLResource
.NullString
)
254 return SqlBoolean
.Null
;
257 char wchFirst
= s
[0];
258 if (Char
.IsNumber(wchFirst
) || ('-' == wchFirst
) || ('+' == wchFirst
)) {
259 return new SqlBoolean(Int32
.Parse(s
, (IFormatProvider
)null));
262 return new SqlBoolean(Boolean
.Parse(s
));
269 /// <para>[To be supplied.]</para>
271 public static SqlBoolean
operator ~
(SqlBoolean x
) {
279 /// <para>[To be supplied.]</para>
281 public static SqlBoolean
operator ^
(SqlBoolean x
, SqlBoolean y
) {
282 return(x
.IsNull
|| y
.IsNull
) ? Null
: new SqlBoolean(x
.m_value
!= y
.m_value
);
287 // Implicit conversions
290 // Explicit conversions
292 // Explicit conversion from SqlByte to SqlBoolean
294 /// <para>[To be supplied.]</para>
296 public static explicit operator SqlBoolean(SqlByte x
) {
297 return x
.IsNull
? Null
: new SqlBoolean(x
.Value
!= 0);
300 // Explicit conversion from SqlInt16 to SqlBoolean
302 /// <para>[To be supplied.]</para>
304 public static explicit operator SqlBoolean(SqlInt16 x
) {
305 return x
.IsNull
? Null
: new SqlBoolean(x
.Value
!= 0);
308 // Explicit conversion from SqlInt32 to SqlBoolean
310 /// <para>[To be supplied.]</para>
312 public static explicit operator SqlBoolean(SqlInt32 x
) {
313 return x
.IsNull
? Null
: new SqlBoolean(x
.Value
!= 0);
316 // Explicit conversion from SqlInt64 to SqlBoolean
318 /// <para>[To be supplied.]</para>
320 public static explicit operator SqlBoolean(SqlInt64 x
) {
321 return x
.IsNull
? Null
: new SqlBoolean(x
.Value
!= 0);
324 // Explicit conversion from SqlDouble to SqlBoolean
326 /// <para>[To be supplied.]</para>
328 public static explicit operator SqlBoolean(SqlDouble x
) {
329 return x
.IsNull
? Null
: new SqlBoolean(x
.Value
!= 0.0);
332 // Explicit conversion from SqlSingle to SqlBoolean
334 /// <para>[To be supplied.]</para>
336 public static explicit operator SqlBoolean(SqlSingle x
) {
337 return x
.IsNull
? Null
: new SqlBoolean(x
.Value
!= 0.0);
340 // Explicit conversion from SqlMoney to SqlBoolean
342 /// <para>[To be supplied.]</para>
344 public static explicit operator SqlBoolean(SqlMoney x
) {
345 return x
.IsNull
? Null
: (x
!= SqlMoney
.Zero
);
348 // Explicit conversion from SqlDecimal to SqlBoolean
350 /// <para>[To be supplied.]</para>
352 public static explicit operator SqlBoolean(SqlDecimal x
) {
353 return x
.IsNull
? SqlBoolean
.Null
: new SqlBoolean(x
.m_data1
!= 0 || x
.m_data2
!= 0 ||
354 x
.m_data3
!= 0 || x
.m_data4
!= 0);
357 // Explicit conversion from SqlString to SqlBoolean
358 // Throws FormatException or OverflowException if necessary.
360 /// <para>[To be supplied.]</para>
362 public static explicit operator SqlBoolean(SqlString x
) {
363 return x
.IsNull
? Null
: SqlBoolean
.Parse(x
.Value
);
366 // Overloading comparison operators
368 /// <para>[To be supplied.]</para>
370 public static SqlBoolean
operator==(SqlBoolean x
, SqlBoolean y
) {
371 return(x
.IsNull
|| y
.IsNull
) ? SqlBoolean
.Null
: new SqlBoolean(x
.m_value
== y
.m_value
);
375 /// <para>[To be supplied.]</para>
377 public static SqlBoolean
operator!=(SqlBoolean x
, SqlBoolean y
) {
382 /// <para>[To be supplied.]</para>
384 public static SqlBoolean
operator<(SqlBoolean x
, SqlBoolean y
) {
385 return(x
.IsNull
|| y
.IsNull
) ? SqlBoolean
.Null
: new SqlBoolean(x
.m_value
< y
.m_value
);
389 /// <para>[To be supplied.]</para>
391 public static SqlBoolean
operator>(SqlBoolean x
, SqlBoolean y
) {
392 return(x
.IsNull
|| y
.IsNull
) ? SqlBoolean
.Null
: new SqlBoolean(x
.m_value
> y
.m_value
);
396 /// <para>[To be supplied.]</para>
398 public static SqlBoolean
operator<=(SqlBoolean x
, SqlBoolean y
) {
399 return(x
.IsNull
|| y
.IsNull
) ? SqlBoolean
.Null
: new SqlBoolean(x
.m_value
<= y
.m_value
);
403 /// <para>[To be supplied.]</para>
405 public static SqlBoolean
operator>=(SqlBoolean x
, SqlBoolean y
) {
406 return(x
.IsNull
|| y
.IsNull
) ? SqlBoolean
.Null
: new SqlBoolean(x
.m_value
>= y
.m_value
);
409 //--------------------------------------------------
410 // Alternative methods for overloaded operators
411 //--------------------------------------------------
413 // Alternative method for operator ~
414 public static SqlBoolean
OnesComplement(SqlBoolean x
) {
418 // Alternative method for operator &
419 public static SqlBoolean
And(SqlBoolean x
, SqlBoolean y
) {
423 // Alternative method for operator |
424 public static SqlBoolean
Or(SqlBoolean x
, SqlBoolean y
) {
428 // Alternative method for operator ^
429 public static SqlBoolean
Xor(SqlBoolean x
, SqlBoolean y
) {
433 // Alternative method for operator ==
434 public static SqlBoolean
Equals(SqlBoolean x
, SqlBoolean y
) {
438 // Alternative method for operator !=
439 public static SqlBoolean
NotEquals(SqlBoolean x
, SqlBoolean y
) {
443 // Alternative method for operator >
444 public static SqlBoolean
GreaterThan(SqlBoolean x
, SqlBoolean y
) {
448 // Alternative method for operator <
449 public static SqlBoolean
LessThan(SqlBoolean x
, SqlBoolean y
) {
453 // Alternative method for operator <=
454 public static SqlBoolean
GreaterThanOrEquals(SqlBoolean x
, SqlBoolean y
) {
458 // Alternative method for operator !=
459 public static SqlBoolean
LessThanOrEquals(SqlBoolean x
, SqlBoolean y
) {
463 // Alternative method for conversions.
465 public SqlByte
ToSqlByte() {
466 return (SqlByte
)this;
469 public SqlDouble
ToSqlDouble() {
470 return (SqlDouble
)this;
473 public SqlInt16
ToSqlInt16() {
474 return (SqlInt16
)this;
477 public SqlInt32
ToSqlInt32() {
478 return (SqlInt32
)this;
481 public SqlInt64
ToSqlInt64() {
482 return (SqlInt64
)this;
485 public SqlMoney
ToSqlMoney() {
486 return (SqlMoney
)this;
489 public SqlDecimal
ToSqlDecimal() {
490 return (SqlDecimal
)this;
493 public SqlSingle
ToSqlSingle() {
494 return (SqlSingle
)this;
497 public SqlString
ToSqlString() {
498 return (SqlString
)this;
504 // Compares this object to another object, returning an integer that
505 // indicates the relationship.
506 // Returns a value less than zero if this < object, zero if this = object,
507 // or a value greater than zero if this > object.
508 // null is considered to be less than any instance.
509 // If object is not of same type, this method throws an ArgumentException.
511 /// <para>[To be supplied.]</para>
513 public int CompareTo(Object
value) {
514 if (value is SqlBoolean
) {
515 SqlBoolean i
= (SqlBoolean
)value;
519 throw ADP
.WrongType(value.GetType(), typeof(SqlBoolean
));
522 public int CompareTo(SqlBoolean
value) {
523 // If both Null, consider them equal.
524 // Otherwise, Null is less than anything.
526 return value.IsNull
? 0 : -1;
527 else if (value.IsNull
)
530 if (this.ByteValue
< value.ByteValue
) return -1;
531 if (this.ByteValue
> value.ByteValue
) return 1;
535 // Compares this instance with a specified object
537 /// <para>[To be supplied.]</para>
539 public override bool Equals(Object
value) {
540 if (!(value is SqlBoolean
)) {
544 SqlBoolean i
= (SqlBoolean
)value;
546 if (i
.IsNull
|| IsNull
)
547 return (i
.IsNull
&& IsNull
);
549 return (this == i
).Value
;
552 // For hashing purpose
554 /// <para>[To be supplied.]</para>
556 public override int GetHashCode() {
557 return IsNull
? 0 : Value
.GetHashCode();
561 /// <para>[To be supplied.]</para>
563 XmlSchema IXmlSerializable
.GetSchema() { return null; }
566 /// <para>[To be supplied.]</para>
568 void IXmlSerializable
.ReadXml(XmlReader reader
) {
569 string isNull
= reader
.GetAttribute("nil", XmlSchema
.InstanceNamespace
);
570 if (isNull
!= null && XmlConvert
.ToBoolean(isNull
)) {
571 // VSTFDevDiv# 479603 - SqlTypes read null value infinitely and never read the next value. Fix - Read the next value.
572 reader
.ReadElementString();
576 m_value
= (byte)(XmlConvert
.ToBoolean(reader
.ReadElementString()) ? x_True
: x_False
);
581 /// <para>[To be supplied.]</para>
583 void IXmlSerializable
.WriteXml(XmlWriter writer
) {
585 writer
.WriteAttributeString("xsi", "nil", XmlSchema
.InstanceNamespace
, "true");
588 writer
.WriteString(m_value
== x_True
? "true" : "false");
593 /// <para>[To be supplied.]</para>
595 public static XmlQualifiedName
GetXsdType(XmlSchemaSet schemaSet
) {
596 return new XmlQualifiedName("boolean", XmlSchema
.Namespace
);
601 /// Represents a true value that can be assigned to the
602 /// <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> property of an instance of
603 /// the <see cref='System.Data.SqlTypes.SqlBoolean'/> class.
606 public static readonly SqlBoolean True
= new SqlBoolean(true);
609 /// Represents a false value that can be assigned to the
610 /// <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> property of an instance of
611 /// the <see cref='System.Data.SqlTypes.SqlBoolean'/> class.
614 public static readonly SqlBoolean False
= new SqlBoolean(false);
617 /// Represents a null value that can be assigned to the <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> property of an instance of
618 /// the <see cref='System.Data.SqlTypes.SqlBoolean'/> class.
621 public static readonly SqlBoolean Null
= new SqlBoolean(0, true);
624 /// <para>[To be supplied.]</para>
626 public static readonly SqlBoolean Zero
= new SqlBoolean(0);
628 /// <para>[To be supplied.]</para>
630 public static readonly SqlBoolean One
= new SqlBoolean(1);
635 } // namespace System.Data.SqlTypes