Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Data / System / Data / SQLTypes / SQLBoolean.cs
blob21f2b3df4da42049d9bbcf67346ca7399bd83347
1 //------------------------------------------------------------------------------
2 // <copyright file="SQLBoolean.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
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
13 // Create by: JunFang
15 // Purpose: Implementation of SqlBoolean which is equivalent to
16 // data type "bit" in SQL Server
18 // Notes:
20 // History:
22 // 09/17/99 JunFang Created and implemented as first drop.
24 // @EndHeader@
25 //**************************************************************************
27 using System;
28 using System.Data.Common;
29 using System.Runtime.InteropServices;
30 using System.Globalization;
31 using System.Xml;
32 using System.Xml.Schema;
33 using System.Xml.Serialization;
35 namespace System.Data.SqlTypes {
36 /// <devdoc>
37 /// <para>
38 /// Represents an integer value that is either 1 or 0.
39 /// </para>
40 /// </devdoc>
41 [Serializable]
42 [StructLayout(LayoutKind.Sequential)]
43 [XmlSchemaProvider("GetXsdType")]
44 public struct SqlBoolean : INullable, IComparable, IXmlSerializable {
46 // m_value: 2 (true), 1 (false), 0 (unknown/Null)
47 private byte m_value;
49 private const byte x_Null = 0;
50 private const byte x_False = 1;
51 private const byte x_True = 2;
53 // constructor
55 /// <devdoc>
56 /// <para>
57 /// Initializes a new instance of the <see cref='System.Data.SqlTypes.SqlBoolean'/> class.
58 /// </para>
59 /// </devdoc>
60 public SqlBoolean(bool value) {
61 m_value = (byte)(value ? x_True : x_False);
64 /// <devdoc>
65 /// <para>[To be supplied.]</para>
66 /// </devdoc>
67 public SqlBoolean(int value) : this(value, false) {
70 private SqlBoolean(int value, bool fNull) {
71 if (fNull)
72 m_value = x_Null;
73 else
74 m_value = (value != 0) ? x_True : x_False;
78 // INullable
79 /// <devdoc>
80 /// <para>
81 /// Gets whether the current <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> is <see cref='System.Data.SqlTypes.SqlBoolean.Null'/>.
82 /// </para>
83 /// </devdoc>
84 public bool IsNull {
85 get { return m_value == x_Null;}
88 // property: Value
89 /// <devdoc>
90 /// <para>
91 /// Gets or sets the <see cref='System.Data.SqlTypes.SqlBoolean'/> to be <see langword='true'/> or
92 /// <see langword='false'/>.
93 /// </para>
94 /// </devdoc>
95 public bool Value {
96 get {
97 switch (m_value) {
98 case x_True:
99 return true;
101 case x_False:
102 return false;
104 default:
105 throw new SqlNullValueException();
110 // property: IsTrue
111 /// <devdoc>
112 /// <para>
113 /// Gets whether the current <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> is <see cref='System.Data.SqlTypes.SqlBoolean.True'/>.
114 /// </para>
115 /// </devdoc>
116 public bool IsTrue {
117 get { return m_value == x_True;}
120 // property: IsFalse
121 /// <devdoc>
122 /// <para>
123 /// Gets whether the current <see cref='System.Data.SqlTypes.SqlBoolean.Value'/> is <see cref='System.Data.SqlTypes.SqlBoolean.False'/>.
124 /// </para>
125 /// </devdoc>
126 public bool IsFalse {
127 get { return m_value == x_False;}
131 // Implicit conversion from bool to SqlBoolean
132 /// <devdoc>
133 /// <para>
134 /// Converts a boolean to a <see cref='System.Data.SqlTypes.SqlBoolean'/>.
135 /// </para>
136 /// </devdoc>
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.
142 /// <devdoc>
143 /// <para>
144 /// Converts a <see cref='System.Data.SqlTypes.SqlBoolean'/>
145 /// to a boolean.
146 /// </para>
147 /// </devdoc>
148 public static explicit operator bool(SqlBoolean x) {
149 return x.Value;
153 // Unary operators
155 /// <devdoc>
156 /// <para>
157 /// Performs a NOT operation on a <see cref='System.Data.SqlTypes.SqlBoolean'/>
158 /// .
159 /// </para>
160 /// </devdoc>
161 public static SqlBoolean operator !(SqlBoolean x) {
162 switch (x.m_value) {
163 case x_True:
164 return SqlBoolean.False;
166 case x_False:
167 return SqlBoolean.True;
169 default:
170 SQLDebug.Check(x.m_value == x_Null);
171 return SqlBoolean.Null;
175 /// <devdoc>
176 /// <para>[To be supplied.]</para>
177 /// </devdoc>
178 public static bool operator true(SqlBoolean x) {
179 return x.IsTrue;
182 /// <devdoc>
183 /// <para>[To be supplied.]</para>
184 /// </devdoc>
185 public static bool operator false(SqlBoolean x) {
186 return x.IsFalse;
189 // Binary operators
191 /// <devdoc>
192 /// <para>
193 /// Performs a bitwise AND operation on two instances of
194 /// <see cref='System.Data.SqlTypes.SqlBoolean'/>
195 /// .
196 /// </para>
197 /// </devdoc>
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;
203 else
204 return SqlBoolean.Null;
207 /// <devdoc>
208 /// <para>
209 /// Performs
210 /// a bitwise OR operation on two instances of a
211 /// <see cref='System.Data.SqlTypes.SqlBoolean'/>
212 /// .
213 /// </para>
214 /// </devdoc>
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;
220 else
221 return SqlBoolean.Null;
226 // property: ByteValue
227 /// <devdoc>
228 /// <para>[To be supplied.]</para>
229 /// </devdoc>
230 public byte ByteValue {
231 get {
232 if (!IsNull)
233 return (m_value == x_True) ? (byte)1 : (byte)0;
234 else
235 throw new SqlNullValueException();
239 /// <devdoc>
240 /// <para>[To be supplied.]</para>
241 /// </devdoc>
242 public override String ToString() {
243 return IsNull ? SQLResource.NullString : Value.ToString((IFormatProvider)null);
246 /// <devdoc>
247 /// <para>[To be supplied.]</para>
248 /// </devdoc>
249 public static SqlBoolean Parse(String s) {
250 if (null == s)
251 // Let Boolean.Parse throw exception
252 return new SqlBoolean(Boolean.Parse(s));
253 if (s == SQLResource.NullString)
254 return SqlBoolean.Null;
256 s = s.TrimStart();
257 char wchFirst = s[0];
258 if (Char.IsNumber(wchFirst) || ('-' == wchFirst) || ('+' == wchFirst)) {
259 return new SqlBoolean(Int32.Parse(s, (IFormatProvider)null));
261 else {
262 return new SqlBoolean(Boolean.Parse(s));
267 // Unary operators
268 /// <devdoc>
269 /// <para>[To be supplied.]</para>
270 /// </devdoc>
271 public static SqlBoolean operator ~(SqlBoolean x) {
272 return (!x);
276 // Binary operators
278 /// <devdoc>
279 /// <para>[To be supplied.]</para>
280 /// </devdoc>
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
293 /// <devdoc>
294 /// <para>[To be supplied.]</para>
295 /// </devdoc>
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
301 /// <devdoc>
302 /// <para>[To be supplied.]</para>
303 /// </devdoc>
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
309 /// <devdoc>
310 /// <para>[To be supplied.]</para>
311 /// </devdoc>
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
317 /// <devdoc>
318 /// <para>[To be supplied.]</para>
319 /// </devdoc>
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
325 /// <devdoc>
326 /// <para>[To be supplied.]</para>
327 /// </devdoc>
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
333 /// <devdoc>
334 /// <para>[To be supplied.]</para>
335 /// </devdoc>
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
341 /// <devdoc>
342 /// <para>[To be supplied.]</para>
343 /// </devdoc>
344 public static explicit operator SqlBoolean(SqlMoney x) {
345 return x.IsNull ? Null : (x != SqlMoney.Zero);
348 // Explicit conversion from SqlDecimal to SqlBoolean
349 /// <devdoc>
350 /// <para>[To be supplied.]</para>
351 /// </devdoc>
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.
359 /// <devdoc>
360 /// <para>[To be supplied.]</para>
361 /// </devdoc>
362 public static explicit operator SqlBoolean(SqlString x) {
363 return x.IsNull ? Null : SqlBoolean.Parse(x.Value);
366 // Overloading comparison operators
367 /// <devdoc>
368 /// <para>[To be supplied.]</para>
369 /// </devdoc>
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);
374 /// <devdoc>
375 /// <para>[To be supplied.]</para>
376 /// </devdoc>
377 public static SqlBoolean operator!=(SqlBoolean x, SqlBoolean y) {
378 return ! (x == y);
381 /// <devdoc>
382 /// <para>[To be supplied.]</para>
383 /// </devdoc>
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);
388 /// <devdoc>
389 /// <para>[To be supplied.]</para>
390 /// </devdoc>
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);
395 /// <devdoc>
396 /// <para>[To be supplied.]</para>
397 /// </devdoc>
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);
402 /// <devdoc>
403 /// <para>[To be supplied.]</para>
404 /// </devdoc>
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) {
415 return ~x;
418 // Alternative method for operator &
419 public static SqlBoolean And(SqlBoolean x, SqlBoolean y) {
420 return x & y;
423 // Alternative method for operator |
424 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y) {
425 return x | y;
428 // Alternative method for operator ^
429 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y) {
430 return x ^ y;
433 // Alternative method for operator ==
434 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y) {
435 return (x == y);
438 // Alternative method for operator !=
439 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y) {
440 return (x != y);
443 // Alternative method for operator >
444 public static SqlBoolean GreaterThan(SqlBoolean x, SqlBoolean y) {
445 return (x > y);
448 // Alternative method for operator <
449 public static SqlBoolean LessThan(SqlBoolean x, SqlBoolean y) {
450 return (x < y);
453 // Alternative method for operator <=
454 public static SqlBoolean GreaterThanOrEquals(SqlBoolean x, SqlBoolean y) {
455 return (x >= y);
458 // Alternative method for operator !=
459 public static SqlBoolean LessThanOrEquals(SqlBoolean x, SqlBoolean y) {
460 return (x <= 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;
503 // IComparable
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.
510 /// <devdoc>
511 /// <para>[To be supplied.]</para>
512 /// </devdoc>
513 public int CompareTo(Object value) {
514 if (value is SqlBoolean) {
515 SqlBoolean i = (SqlBoolean)value;
517 return CompareTo(i);
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.
525 if (IsNull)
526 return value.IsNull ? 0 : -1;
527 else if (value.IsNull)
528 return 1;
530 if (this.ByteValue < value.ByteValue) return -1;
531 if (this.ByteValue > value.ByteValue) return 1;
532 return 0;
535 // Compares this instance with a specified object
536 /// <devdoc>
537 /// <para>[To be supplied.]</para>
538 /// </devdoc>
539 public override bool Equals(Object value) {
540 if (!(value is SqlBoolean)) {
541 return false;
544 SqlBoolean i = (SqlBoolean)value;
546 if (i.IsNull || IsNull)
547 return (i.IsNull && IsNull);
548 else
549 return (this == i).Value;
552 // For hashing purpose
553 /// <devdoc>
554 /// <para>[To be supplied.]</para>
555 /// </devdoc>
556 public override int GetHashCode() {
557 return IsNull ? 0 : Value.GetHashCode();
560 /// <devdoc>
561 /// <para>[To be supplied.]</para>
562 /// </devdoc>
563 XmlSchema IXmlSerializable.GetSchema() { return null; }
565 /// <devdoc>
566 /// <para>[To be supplied.]</para>
567 /// </devdoc>
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();
573 m_value = x_Null;
575 else {
576 m_value = (byte)(XmlConvert.ToBoolean(reader.ReadElementString()) ? x_True : x_False);
580 /// <devdoc>
581 /// <para>[To be supplied.]</para>
582 /// </devdoc>
583 void IXmlSerializable.WriteXml(XmlWriter writer) {
584 if (IsNull) {
585 writer.WriteAttributeString("xsi", "nil", XmlSchema.InstanceNamespace, "true");
587 else {
588 writer.WriteString(m_value == x_True ? "true" : "false");
592 /// <devdoc>
593 /// <para>[To be supplied.]</para>
594 /// </devdoc>
595 public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) {
596 return new XmlQualifiedName("boolean", XmlSchema.Namespace);
599 /// <devdoc>
600 /// <para>
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.
604 /// </para>
605 /// </devdoc>
606 public static readonly SqlBoolean True = new SqlBoolean(true);
607 /// <devdoc>
608 /// <para>
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.
612 /// </para>
613 /// </devdoc>
614 public static readonly SqlBoolean False = new SqlBoolean(false);
615 /// <devdoc>
616 /// <para>
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.
619 /// </para>
620 /// </devdoc>
621 public static readonly SqlBoolean Null = new SqlBoolean(0, true);
623 /// <devdoc>
624 /// <para>[To be supplied.]</para>
625 /// </devdoc>
626 public static readonly SqlBoolean Zero = new SqlBoolean(0);
627 /// <devdoc>
628 /// <para>[To be supplied.]</para>
629 /// </devdoc>
630 public static readonly SqlBoolean One = new SqlBoolean(1);
633 } // SqlBoolean
635 } // namespace System.Data.SqlTypes