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