2010-03-26 Veerapuram Varadhan <vvaradhan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlByte.cs
1 //
2 // System.Data.SqlTypes.SqlByte
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //   Ville Palo <vi64pa@kolumbus.fi>
7 //
8 // (C) Copyright 2002 Tim Coleman
9 // (C) Copyright 2003 Ville Palo
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Globalization;
37 #if NET_2_0
38 using System.Xml;
39 using System.Xml.Schema;
40 using System.Xml.Serialization;
41 using System.Runtime.Serialization;
42 #endif
43
44 namespace System.Data.SqlTypes
45 {
46 #if NET_2_0
47         [SerializableAttribute]
48         [XmlSchemaProvider ("GetXsdType")]
49 #endif
50         public struct SqlByte : INullable, IComparable
51 #if NET_2_0
52                                    , IXmlSerializable
53 #endif
54         {
55                 #region Fields
56
57                 byte value;
58                 private bool notNull;
59
60                 public static readonly SqlByte MaxValue = new SqlByte (0xff);
61                 public static readonly SqlByte MinValue = new SqlByte (0);
62                 public static readonly SqlByte Null;
63                 public static readonly SqlByte Zero = new SqlByte (0);
64
65                 #endregion
66
67                 #region Constructors
68
69                 public SqlByte (byte value) 
70                 {
71                         this.value = value;
72                         notNull = true;
73                 }
74
75                 #endregion
76
77                 #region Properties
78
79                 public bool IsNull {
80                         get { return !notNull; }
81                 }
82
83                 public byte Value { 
84                         get { 
85                                 if (this.IsNull) 
86                                         throw new SqlNullValueException ();
87                                 else 
88                                         return value; 
89                         }
90                 }
91
92                 #endregion
93
94                 #region Methods
95
96                 public static SqlByte Add (SqlByte x, SqlByte y)
97                 {
98                         return (x + y);
99                 }
100
101                 public static SqlByte BitwiseAnd (SqlByte x, SqlByte y)
102                 {
103                         return (x & y);
104                 }
105
106                 public static SqlByte BitwiseOr (SqlByte x, SqlByte y)
107                 {
108                         return (x | y);
109                 }
110
111                 public int CompareTo (object value)
112                 {
113                         if (value == null)
114                                 return 1;
115                         if (!(value is SqlByte))
116                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlByte"));
117
118                         return CompareTo ((SqlByte) value);
119                 }
120 #if NET_2_0
121                 public
122 #endif
123                 int CompareTo (SqlByte value)
124                 {
125                         if (value.IsNull)
126                                 return 1;
127                         else
128                                 return this.value.CompareTo (value.Value);
129                 }
130
131                 public static SqlByte Divide (SqlByte x, SqlByte y)
132                 {
133                         return (x / y);
134                 }
135
136                 public override bool Equals (object value)
137                 {
138                         if (!(value is SqlByte))
139                                 return false;
140                         else if (this.IsNull)
141                                 return ((SqlByte)value).IsNull;
142                         else if (((SqlByte)value).IsNull)
143                                 return false;
144                         else
145                                 return (bool) (this == (SqlByte)value);
146                 }
147
148                 public static SqlBoolean Equals (SqlByte x, SqlByte y)
149                 {
150                         return (x == y);
151                 }
152
153                 public override int GetHashCode ()
154                 {
155                         return (int)value;
156                 }
157
158                 public static SqlBoolean GreaterThan (SqlByte x, SqlByte y)
159                 {
160                         return (x > y);
161                 }
162
163                 public static SqlBoolean GreaterThanOrEqual (SqlByte x, SqlByte y)
164                 {
165                         return (x >= y);
166                 }
167
168                 public static SqlBoolean LessThan (SqlByte x, SqlByte y)
169                 {
170                         return (x < y);
171                 }
172
173                 public static SqlBoolean LessThanOrEqual (SqlByte x, SqlByte y)
174                 {
175                         return (x <= y);
176                 }
177
178                 public static SqlByte Mod (SqlByte x, SqlByte y)
179                 {
180                         return (x % y);
181                 }
182
183 #if NET_2_0
184                 // Why did Microsoft add this method in 2.0???  What's 
185                 // the difference????
186                 public static SqlByte Modulus (SqlByte x, SqlByte y) 
187                 {
188                         return (x % y);
189                 }
190 #endif
191
192                 public static SqlByte Multiply (SqlByte x, SqlByte y)
193                 {
194                         return (x * y);
195                 }
196
197                 public static SqlBoolean NotEquals (SqlByte x, SqlByte y)
198                 {
199                         return (x != y);
200                 }
201
202                 public static SqlByte OnesComplement (SqlByte x)
203                 {
204                         return ~x;
205                 }
206
207                 public static SqlByte Parse (string s)
208                 {
209                         checked {
210                                 return new SqlByte (Byte.Parse (s));
211                         }
212                 }
213
214                 public static SqlByte Subtract (SqlByte x, SqlByte y)
215                 {
216                         return (x - y);
217                 }
218
219                 public SqlBoolean ToSqlBoolean ()
220                 {
221                         return ((SqlBoolean)this);
222                 }
223                 
224                 public SqlDecimal ToSqlDecimal ()
225                 {
226                         return ((SqlDecimal)this);
227                 }
228
229                 public SqlDouble ToSqlDouble ()
230                 {
231                         return ((SqlDouble)this);
232                 }
233
234                 public SqlInt16 ToSqlInt16 ()
235                 {
236                         return ((SqlInt16)this);
237                 }
238
239                 public SqlInt32 ToSqlInt32 ()
240                 {
241                         return ((SqlInt32)this);
242                 }
243
244                 public SqlInt64 ToSqlInt64 ()
245                 {
246                         return ((SqlInt64)this);
247                 }
248
249                 public SqlMoney ToSqlMoney ()
250                 {
251                         return ((SqlMoney)this);
252                 }
253
254                 public SqlSingle ToSqlSingle ()
255                 {
256                         return ((SqlSingle)this);
257                 }
258
259                 public SqlString ToSqlString ()
260                 {
261                         return ((SqlString)this);
262                 }
263
264                 public override string ToString ()
265                 {
266                         if (this.IsNull)
267                                 return "Null";
268                         else
269                                 return value.ToString ();
270                 }
271
272                 public static SqlByte Xor (SqlByte x, SqlByte y)
273                 {
274                         return (x ^ y);
275                 }
276
277                 public static SqlByte operator + (SqlByte x, SqlByte y)
278                 {
279                         checked {
280                                 return new SqlByte ((byte) (x.Value + y.Value));
281                         }
282                 }
283
284                 public static SqlByte operator & (SqlByte x, SqlByte y)
285                 {
286                         return new SqlByte ((byte) (x.Value & y.Value));
287                 }
288
289                 public static SqlByte operator | (SqlByte x, SqlByte y)
290                 {
291                         return new SqlByte ((byte) (x.Value | y.Value));
292                 }
293
294                 public static SqlByte operator / (SqlByte x, SqlByte y)
295                 {
296                         checked {
297                                 return new SqlByte ((byte) (x.Value / y.Value));
298                         }
299                 }
300
301                 public static SqlBoolean operator == (SqlByte x, SqlByte y)
302                 {
303                         if (x.IsNull || y.IsNull) 
304                                 return SqlBoolean.Null;
305                         else
306                                 return new SqlBoolean (x.Value == y.Value);
307                 }
308
309                 public static SqlByte operator ^ (SqlByte x, SqlByte y)
310                 {
311                         return new SqlByte ((byte) (x.Value ^ y.Value));
312                 }
313
314                 public static SqlBoolean operator > (SqlByte x, SqlByte y)
315                 {
316                         if (x.IsNull || y.IsNull) 
317                                 return SqlBoolean.Null;
318                         else
319                                 return new SqlBoolean (x.Value > y.Value);
320                 }
321
322                 public static SqlBoolean operator >= (SqlByte x, SqlByte 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                 public static SqlBoolean operator != (SqlByte x, SqlByte y)
331                 {
332                         if (x.IsNull || y.IsNull) 
333                                 return SqlBoolean.Null;
334                         else
335                                 return new SqlBoolean (!(x.Value == y.Value));
336                 }
337
338                 public static SqlBoolean operator < (SqlByte x, SqlByte y)
339                 {
340                         if (x.IsNull || y.IsNull) 
341                                 return SqlBoolean.Null;
342                         else
343                                 return new SqlBoolean (x.Value < y.Value);
344                 }
345
346                 public static SqlBoolean operator <= (SqlByte x, SqlByte y)
347                 {
348                         if (x.IsNull || y.IsNull) 
349                                 return SqlBoolean.Null;
350                         else
351                                 return new SqlBoolean (x.Value <= y.Value);
352                 }
353
354                 public static SqlByte operator % (SqlByte x, SqlByte y)
355                 {
356                         return new SqlByte ((byte) (x.Value % y.Value));
357                 }
358
359                 public static SqlByte operator * (SqlByte x, SqlByte y)
360                 {
361                         checked {
362                                 return new SqlByte ((byte) (x.Value * y.Value));
363                         }
364                 }
365
366                 public static SqlByte operator ~ (SqlByte x)
367                 {
368                         return new SqlByte ((byte) ~x.Value);
369                 }
370
371                 public static SqlByte operator - (SqlByte x, SqlByte y)
372                 {
373                         checked {
374                                 return new SqlByte ((byte) (x.Value - y.Value));
375                         }
376                 }
377
378                 public static explicit operator SqlByte (SqlBoolean x)
379                 {
380                         if (x.IsNull)
381                                 return Null;
382                         else
383                                 return new SqlByte (x.ByteValue);
384                 }
385
386                 public static explicit operator byte (SqlByte x)
387                 {
388                         return x.Value;
389                 }
390
391                 public static explicit operator SqlByte (SqlDecimal x)
392                 {
393                         checked {
394                                 if (x.IsNull)
395                                         return Null;
396                                 else 
397                                         return new SqlByte ((byte)x.Value);
398                         }
399                 }
400
401                 public static explicit operator SqlByte (SqlDouble x)
402                 {
403                         if (x.IsNull)
404                                 return Null;
405                         else {
406                                 checked {
407                                         return new SqlByte ((byte)x.Value);
408                                 }
409                         }
410                 }
411
412                 public static explicit operator SqlByte (SqlInt16 x)
413                 {
414                         checked {
415                                 if (x.IsNull)
416                                         return Null;
417                                 else                           
418                                         return new SqlByte ((byte)x.Value);
419                         }
420                 }
421
422                 public static explicit operator SqlByte (SqlInt32 x)
423                 {
424                         checked {
425                                 if (x.IsNull)
426                                         return Null;
427                                 else                           
428                                         return new SqlByte ((byte)x.Value);
429                         }
430                 }
431
432                 public static explicit operator SqlByte (SqlInt64 x)
433                 {
434                         if (x.IsNull)
435                                 return Null;
436                         else {
437                                 checked {
438                                         return new SqlByte ((byte)x.Value);
439                                 }
440                         }
441                 }
442
443                 public static explicit operator SqlByte (SqlMoney x)
444                 {
445                         checked {
446                                 if (x.IsNull)
447                                         return Null;
448                                 else                                    
449                                         return new SqlByte ((byte)x.Value);
450                         }
451                 }
452
453                 public static explicit operator SqlByte (SqlSingle x)
454                 {
455                         if (x.IsNull)
456                                 return Null;
457                         else {
458                                 checked {
459                                         return new SqlByte ((byte)x.Value);
460                                 }
461                         }
462                 }
463
464
465                 public static explicit operator SqlByte (SqlString x)
466                 {                       
467                         checked {
468                                 return SqlByte.Parse (x.Value);
469                         }
470                 }
471
472                 public static implicit operator SqlByte (byte x)
473                 {
474                         return new SqlByte (x);
475                 }
476                 
477 #if NET_2_0
478                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
479                 {
480                         XmlQualifiedName qualifiedName = new XmlQualifiedName ("unsignedByte", "http://www.w3.org/2001/XMLSchema");
481                         return qualifiedName;
482                 }
483                 
484                 [MonoTODO]
485                 XmlSchema IXmlSerializable.GetSchema ()
486                 {
487                         throw new NotImplementedException ();
488                 }
489                                                                                 
490                 [MonoTODO]
491                 void IXmlSerializable.ReadXml (XmlReader reader)
492                 {
493                         throw new NotImplementedException ();
494                 }
495                                                                                 
496                 [MonoTODO]
497                 void IXmlSerializable.WriteXml (XmlWriter writer)
498                 {
499                         throw new NotImplementedException ();
500                 }
501 #endif
502                 #endregion
503         }
504 }
505