Remove confusing trailing dot
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlBoolean.cs
1 //
2 // System.Data.SqlTypes.SqlBoolean
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //   Tim Coleman (tim@timcoleman.com)
8 //
9 // (C) Ximian, Inc. 2002
10 // (C) Copyright 2002 Tim Coleman
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
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:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
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.
34 //
35
36 using System;
37 using System.Globalization;
38 #if NET_2_0
39 using System.Xml;
40 using System.Xml.Schema;
41 using System.Xml.Serialization;
42 using System.Runtime.Serialization;
43 #endif
44
45 namespace System.Data.SqlTypes
46 {
47         /// <summary>
48         /// Represents an integer value that is either 1 or 0 
49         /// to be stored in or retrieved from a database.
50         /// </summary>
51 #if NET_2_0
52         [SerializableAttribute]
53         [XmlSchemaProvider ("GetXsdType")]
54 #endif
55         public struct SqlBoolean : INullable, IComparable
56 #if NET_2_0
57                                    , IXmlSerializable
58 #endif
59         {
60
61                 #region Fields
62
63                 byte value;
64                 
65                 // default is false
66                 private bool notNull;
67
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);
73
74                 #endregion // Fields
75
76                 #region Constructors
77
78                 public SqlBoolean (bool value) 
79                 {
80                         this.value = (byte) (value ? 1 : 0);
81                         notNull = true;
82                 }
83
84                 public SqlBoolean (int value) 
85                 {
86                         this.value = (byte) (value != 0 ? 1 : 0);
87                         notNull = true;
88                 }
89
90                 #endregion // Constructors
91
92                 #region Properties
93
94                 public byte ByteValue {
95                         get {
96                                 if (this.IsNull)
97                                         throw new SqlNullValueException(Locale.GetText("The property is set to null."));
98                                 else
99                                         return value;
100                         }
101                 }
102
103                 public bool IsFalse {
104                         get { 
105                                 if (this.IsNull) 
106                                         return false;
107                                 else 
108                                         return (value == 0);
109                         }
110                 }
111
112                 public bool IsNull {
113                         get { 
114                                 return !notNull;
115                         }
116                 }
117
118                 public bool IsTrue {
119                         get { 
120                                 if (this.IsNull) 
121                                         return false;
122                                 else    
123                                         return (value != 0);
124                         }
125                 }
126
127                 public bool Value {
128                         get { 
129                                 if (this.IsNull)
130                                         throw new SqlNullValueException(Locale.GetText("The property is set to null."));
131                                 else
132                                         return this.IsTrue;
133                         }
134                 }
135
136                 #endregion // Properties
137
138                 public static SqlBoolean And (SqlBoolean x, SqlBoolean y) 
139                 {
140                         return (x & y);
141                 }
142
143                 public int CompareTo (object value)
144                 {
145                         if (value == null)
146                                 return 1;
147                         if (!(value is SqlBoolean))
148                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBoolean"));
149
150                         return CompareTo ((SqlBoolean) value);
151                 }
152 #if NET_2_0
153                 public
154 #endif
155                 int CompareTo (SqlBoolean value) 
156                 {
157                         if (value.IsNull)
158                                 return 1;
159                         else
160                                 return this.value.CompareTo (value.ByteValue);
161                 }
162
163                 public override bool Equals(object value) 
164                 {
165                         if (!(value is SqlBoolean))
166                                 return false;
167                         if (this.IsNull)
168                                 return ((SqlBoolean)value).IsNull;
169                         else if (((SqlBoolean)value).IsNull)
170                                 return false;
171                         else
172                                 return (bool) (this == (SqlBoolean)value);
173                 }
174
175                 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y) 
176                 {
177                         return (x == y);
178                 }
179
180 #if NET_2_0
181                 public static SqlBoolean GreaterThan (SqlBoolean x, SqlBoolean y) 
182                 {
183                         return (x > y);
184                 }
185
186                 public static SqlBoolean GreaterThanOrEquals (SqlBoolean x, SqlBoolean y) 
187                 {
188                         return (x >= y);
189                 }
190
191                 public static SqlBoolean LessThan (SqlBoolean x, SqlBoolean y) 
192                 {
193                         return (x < y);
194                 }
195
196                 public static SqlBoolean LessThanOrEquals (SqlBoolean x, SqlBoolean y) 
197                 {
198                         return (x <= y);
199                 }
200 #endif
201                 public override int GetHashCode() 
202                 {
203                         int hash;
204                         if (this.IsTrue)
205                                 hash = 1;
206                         else 
207                                 hash = 0;
208
209                         return hash;
210                 }
211
212                 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y) 
213                 {
214                         return (x != y);
215                 }
216
217                 public static SqlBoolean OnesComplement(SqlBoolean x) 
218                 {
219                         return ~x;
220                 }
221
222                 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y) 
223                 {
224                         return (x | y);
225                 }
226
227                 public static SqlBoolean Parse(string s) 
228                 {
229                         switch (s) {
230                         case "0":
231                                 return new SqlBoolean (false);
232                         case "1":
233                                 return new SqlBoolean (true);
234                         }
235                         return new SqlBoolean (Boolean.Parse (s));
236                 }
237
238                 public SqlByte ToSqlByte() 
239                 {
240                         return new SqlByte (value);
241                 }
242
243                 // **************************************************
244                 // Conversion from SqlBoolean to other SqlTypes
245                 // **************************************************
246
247                 public SqlDecimal ToSqlDecimal() 
248                 {
249                         return ((SqlDecimal)this);
250                 }
251
252                 public SqlDouble ToSqlDouble() 
253                 {
254                         return ((SqlDouble)this);
255                 }
256
257                 public SqlInt16 ToSqlInt16() 
258                 {
259                         return ((SqlInt16)this);
260                 }
261
262                 public SqlInt32 ToSqlInt32() 
263                 {
264                         return ((SqlInt32)this);
265                 }
266
267                 public SqlInt64 ToSqlInt64() 
268                 {
269                         return ((SqlInt64)this);
270                 }
271
272                 public SqlMoney ToSqlMoney() 
273                 {
274                         return ((SqlMoney)this);
275                 }
276
277                 public SqlSingle ToSqlSingle() 
278                 {
279                         return ((SqlSingle)this);
280                 }
281
282                 public SqlString ToSqlString() 
283                 {
284                         if (this.IsNull)
285                                 return new SqlString ("Null");
286                         if (this.IsTrue)
287                                 return new SqlString ("True");
288                         else
289                                 return new SqlString ("False");
290                 }
291
292                 public override string ToString() 
293                 {
294                         if (this.IsNull)
295                                 return "Null";
296                         if (this.IsTrue)
297                                 return "True";
298                         else
299                                 return "False";
300                 }
301
302                 // Bitwise exclusive-OR (XOR)
303                 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y) 
304                 {
305                         return (x ^ y);
306                 }
307
308                 // **************************************************
309                 // Public Operators
310                 // **************************************************
311
312                 // Bitwise AND
313                 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
314                 {
315                         return new SqlBoolean (x.Value & y.Value);
316                 }
317
318                 // Bitwise OR
319                 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
320                 {
321                         return new SqlBoolean (x.Value | y.Value);
322
323                 }
324
325                 // Compares two instances for equality
326                 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
327                 {
328                         if (x.IsNull || y.IsNull) 
329                                 return SqlBoolean.Null;
330                         else
331                                 return new SqlBoolean (x.Value == y.Value);
332                 }
333                 
334                 // Bitwize exclusive-OR (XOR)
335                 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y) 
336                 {
337                         return new SqlBoolean (x.Value ^ y.Value);
338                 }
339
340                 // test Value of SqlBoolean to determine it is false.
341                 public static bool operator false (SqlBoolean x) 
342                 {
343                         return x.IsFalse;
344                 }
345
346                 // in-equality
347                 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
348                 {
349                         if (x.IsNull || y.IsNull) 
350                                 return SqlBoolean.Null;
351                         else
352                                 return new SqlBoolean (x.Value != y.Value);
353                 }
354
355                 // Logical NOT
356                 public static SqlBoolean operator ! (SqlBoolean x) 
357                 {
358                         if (x.IsNull)
359                                 return SqlBoolean.Null;
360                         else
361                                 return new SqlBoolean (!x.Value);
362                 }
363
364                 // One's Complement
365                 public static SqlBoolean operator ~ (SqlBoolean x) 
366                 {
367                         SqlBoolean b;
368                         if (x.IsTrue)
369                                 b = new SqlBoolean(false);
370                         else
371                                 b = new SqlBoolean(true);
372
373                         return b;
374                 }
375
376 #if NET_2_0
377                 public static SqlBoolean operator > (SqlBoolean x, SqlBoolean y) 
378                 {
379                         if (x.IsNull || y.IsNull) 
380                                 return SqlBoolean.Null;
381
382                         return new SqlBoolean (Compare (x, y) > 0);
383                 }
384
385                 public static SqlBoolean operator >= (SqlBoolean x, SqlBoolean y) 
386                 {
387                         if (x.IsNull || y.IsNull) 
388                                 return SqlBoolean.Null;
389
390                         return new SqlBoolean (Compare (x, y) >= 0);
391                 }
392
393                 public static SqlBoolean operator < (SqlBoolean x, SqlBoolean y) 
394                 {
395                         if (x.IsNull || y.IsNull) 
396                                 return SqlBoolean.Null;
397
398                         return new SqlBoolean (Compare (x, y) < 0);
399                 }
400
401                 public static SqlBoolean operator <= (SqlBoolean x, SqlBoolean y) 
402                 {
403                         if (x.IsNull || y.IsNull) 
404                                 return SqlBoolean.Null;
405
406                         return new SqlBoolean (Compare (x, y) <= 0);
407                 }
408 #endif
409                 // test to see if value is true
410                 public static bool operator true (SqlBoolean x) 
411                 {
412                         return x.IsTrue;
413                 }
414
415                 // ****************************************
416                 // Type Conversion 
417                 // ****************************************
418
419                 
420                 // SqlBoolean to Boolean
421                 public static explicit operator bool (SqlBoolean x) 
422                 {
423                         return x.Value;
424                 }
425
426                 
427                 // SqlByte to SqlBoolean
428                 public static explicit operator SqlBoolean (SqlByte x) 
429                 {
430                         checked {
431                                 if (x.IsNull)
432                                         return Null;
433                                 else
434                                         return new SqlBoolean ((int)x.Value);
435                         }
436                 }
437
438                 // SqlDecimal to SqlBoolean
439                 public static explicit operator SqlBoolean (SqlDecimal x) 
440                 {
441                         checked {
442                                 if (x.IsNull)
443                                         return Null;
444                                 else
445                                         return new SqlBoolean ((int)x.Value);
446                         }
447                 }
448                 
449                 // SqlDouble to SqlBoolean
450                 public static explicit operator SqlBoolean (SqlDouble x) 
451                 {
452                         // FIXME
453                         //checked {
454                                 if (x.IsNull)
455                                         return Null;
456                                 else
457                                         return new SqlBoolean ((int)x.Value);
458                                 //}
459                 }
460
461                 // SqlInt16 to SqlBoolean
462                 public static explicit operator SqlBoolean (SqlInt16 x) 
463                 {
464                         checked {
465                                 if (x.IsNull)
466                                         return Null;
467                                 else
468                                         return new SqlBoolean ((int)x.Value);
469                         }
470                 }
471
472                 // SqlInt32 to SqlBoolean
473                 public static explicit operator SqlBoolean (SqlInt32 x) 
474                 {
475                         checked {
476                                 if (x.IsNull)
477                                         return Null;
478                                 else
479                                         return new SqlBoolean (x.Value);
480                         }
481                 }
482
483                 // SqlInt64 to SqlBoolean
484                 public static explicit operator SqlBoolean (SqlInt64 x) 
485                 {
486                         checked {
487                                 if (x.IsNull)
488                                         return Null;
489                                 else
490                                         return new SqlBoolean ((int)x.Value);
491                         }
492                 }
493
494                 // SqlMoney to SqlBoolean
495                 public static explicit operator SqlBoolean (SqlMoney x) 
496                 {
497                         checked {
498                                 if (x.IsNull)
499                                         return Null;
500                                 else
501                                         return new SqlBoolean ((int)x.Value);
502                         }
503                 }
504
505                 // SqlSingle to SqlBoolean
506                 public static explicit operator SqlBoolean (SqlSingle x) 
507                 {
508                         // FIXME
509                         //checked {
510                                 if (x.IsNull)
511                                         return Null;
512                                 else
513                                         return new SqlBoolean ((int)x.Value);
514                                 //}
515                 }
516
517                 // SqlString to SqlBoolean
518                 public static explicit operator SqlBoolean (SqlString x) 
519                 {
520                         checked {
521                                 if (x.IsNull)
522                                         return Null;
523                                 return SqlBoolean.Parse (x.Value);
524                         }
525                 }
526
527                 // Boolean to SqlBoolean
528                 public static implicit operator SqlBoolean (bool x) 
529                 {
530                         return new SqlBoolean (x);
531                 }
532 #if NET_2_0
533                 // Helper method to Compare methods and operators.
534                 // Returns 0 if x == y
535                 //         1 if x > y
536                 //        -1 if x < y
537                 private static int Compare (SqlBoolean x, SqlBoolean y)
538                 {
539                         if (x == y)
540                                 return 0;
541                         if (x.IsTrue && y.IsFalse)
542                                 return 1;
543                         if (x.IsFalse && y.IsTrue)
544                                 return -1;
545                         return 0;
546                 }
547
548                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
549                 {
550                         XmlQualifiedName qualifiedName = new XmlQualifiedName ("boolean", "http://www.w3.org/2001/XMLSchema");
551                         return qualifiedName;
552                 }
553                 
554                 [MonoTODO]
555                 XmlSchema IXmlSerializable.GetSchema ()
556                 {
557                         throw new NotImplementedException ();
558                 }
559                                                                                 
560                 [MonoTODO]
561                 void IXmlSerializable.ReadXml (XmlReader reader)
562                 {
563                         throw new NotImplementedException ();
564                 }
565                                                                                 
566                 [MonoTODO]
567                 void IXmlSerializable.WriteXml (XmlWriter writer)
568                 {
569                         throw new NotImplementedException ();
570                 }
571 #endif
572         }
573 }