Ups, there was a litle bug
[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 //
7 // (C) Copyright 2002 Tim Coleman
8 //
9
10 using System;
11 using System.Globalization;
12
13 namespace System.Data.SqlTypes
14 {
15         public struct SqlByte : INullable, IComparable
16         {
17                 #region Fields
18
19                 byte value;
20                 private bool notNull;
21
22                 public static readonly SqlByte MaxValue = new SqlByte (0xff);
23                 public static readonly SqlByte MinValue = new SqlByte (0);
24                 public static readonly SqlByte Null;
25                 public static readonly SqlByte Zero = new SqlByte (0);
26
27                 #endregion
28
29                 #region Constructors
30
31                 public SqlByte (byte value) 
32                 {
33                         this.value = value;
34                         notNull = true;
35                 }
36
37                 #endregion
38
39                 #region Properties
40
41                 public bool IsNull {
42                         get { return !notNull; }
43                 }
44
45                 public byte Value { 
46                         get { 
47                                 if (this.IsNull) 
48                                         throw new SqlNullValueException ();
49                                 else 
50                                         return value; 
51                         }
52                 }
53
54                 #endregion
55
56                 #region Methods
57
58                 public static SqlByte Add (SqlByte x, SqlByte y)
59                 {
60                         return (x + y);
61                 }
62
63                 public static SqlByte BitwiseAnd (SqlByte x, SqlByte y)
64                 {
65                         return (x & y);
66                 }
67
68                 public static SqlByte BitwiseOr (SqlByte x, SqlByte y)
69                 {
70                         return (x | y);
71                 }
72
73                 public int CompareTo (object value)
74                 {
75                         if (value == null)
76                                 return 1;
77                         else if (!(value is SqlByte))
78                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlByte"));
79                         else if (((SqlByte)value).IsNull)
80                                 return 1;
81                         else
82                                 return this.value.CompareTo (((SqlByte)value).Value);
83                 }
84
85                 public static SqlByte Divide (SqlByte x, SqlByte y)
86                 {
87                         return (x / y);
88                 }
89
90                 public override bool Equals (object value)
91                 {
92                         if (!(value is SqlByte))
93                                 return false;
94                         else if (this.IsNull && ((SqlByte)value).IsNull)
95                                  return true;
96                         else if (((SqlByte)value).IsNull)
97                                 return false;
98                         else
99                                 return (bool) (this == (SqlByte)value);
100                 }
101
102                 public static SqlBoolean Equals (SqlByte x, SqlByte y)
103                 {
104                         return (x == y);
105                 }
106
107                 public override int GetHashCode ()
108                 {
109                         return (int)value;
110                 }
111
112                 public static SqlBoolean GreaterThan (SqlByte x, SqlByte y)
113                 {
114                         return (x > y);
115                 }
116
117                 public static SqlBoolean GreaterThanOrEqual (SqlByte x, SqlByte y)
118                 {
119                         return (x >= y);
120                 }
121
122                 public static SqlBoolean LessThan (SqlByte x, SqlByte y)
123                 {
124                         return (x < y);
125                 }
126
127                 public static SqlBoolean LessThanOrEqual (SqlByte x, SqlByte y)
128                 {
129                         return (x <= y);
130                 }
131
132                 public static SqlByte Mod (SqlByte x, SqlByte y)
133                 {
134                         return (x % y);
135                 }
136
137                 public static SqlByte Multiply (SqlByte x, SqlByte y)
138                 {
139                         return (x * y);
140                 }
141
142                 public static SqlBoolean NotEquals (SqlByte x, SqlByte y)
143                 {
144                         return (x != y);
145                 }
146
147                 public static SqlByte OnesComplement (SqlByte x)
148                 {
149                         return ~x;
150                 }
151
152                 public static SqlByte Parse (string s)
153                 {
154                         checked {
155                                 return new SqlByte (Byte.Parse (s));
156                         }
157                 }
158
159                 public static SqlByte Subtract (SqlByte x, SqlByte y)
160                 {
161                         return (x - y);
162                 }
163
164                 public SqlBoolean ToSqlBoolean ()
165                 {
166                         return ((SqlBoolean)this);
167                 }
168                 
169                 public SqlDecimal ToSqlDecimal ()
170                 {
171                         return ((SqlDecimal)this);
172                 }
173
174                 public SqlDouble ToSqlDouble ()
175                 {
176                         return ((SqlDouble)this);
177                 }
178
179                 public SqlInt16 ToSqlInt16 ()
180                 {
181                         return ((SqlInt16)this);
182                 }
183
184                 public SqlInt32 ToSqlInt32 ()
185                 {
186                         return ((SqlInt32)this);
187                 }
188
189                 public SqlInt64 ToSqlInt64 ()
190                 {
191                         return ((SqlInt64)this);
192                 }
193
194                 public SqlMoney ToSqlMoney ()
195                 {
196                         return ((SqlMoney)this);
197                 }
198
199                 public SqlSingle ToSqlSingle ()
200                 {
201                         return ((SqlSingle)this);
202                 }
203
204                 public SqlString ToSqlString ()
205                 {
206                         return ((SqlString)this);
207                 }
208
209                 public override string ToString ()
210                 {
211                         if (this.IsNull)
212                                 return "Null";
213                         else
214                                 return value.ToString ();
215                 }
216
217                 public static SqlByte Xor (SqlByte x, SqlByte y)
218                 {
219                         return (x ^ y);
220                 }
221
222                 public static SqlByte operator + (SqlByte x, SqlByte y)
223                 {
224                         checked {
225                                 return new SqlByte ((byte) (x.Value + y.Value));
226                         }
227                 }
228
229                 public static SqlByte operator & (SqlByte x, SqlByte y)
230                 {
231                         return new SqlByte ((byte) (x.Value & y.Value));
232                 }
233
234                 public static SqlByte operator | (SqlByte x, SqlByte y)
235                 {
236                         return new SqlByte ((byte) (x.Value | y.Value));
237                 }
238
239                 public static SqlByte operator / (SqlByte x, SqlByte y)
240                 {
241                         checked {
242                                 return new SqlByte ((byte) (x.Value / y.Value));
243                         }
244                 }
245
246                 public static SqlBoolean operator == (SqlByte x, SqlByte y)
247                 {
248                         if (x.IsNull || y.IsNull) 
249                                 return SqlBoolean.Null;
250                         else
251                                 return new SqlBoolean (x.Value == y.Value);
252                 }
253
254                 public static SqlByte operator ^ (SqlByte x, SqlByte y)
255                 {
256                         return new SqlByte ((byte) (x.Value ^ y.Value));
257                 }
258
259                 public static SqlBoolean operator > (SqlByte x, SqlByte y)
260                 {
261                         if (x.IsNull || y.IsNull) 
262                                 return SqlBoolean.Null;
263                         else
264                                 return new SqlBoolean (x.Value > y.Value);
265                 }
266
267                 public static SqlBoolean operator >= (SqlByte x, SqlByte y)
268                 {
269                         if (x.IsNull || y.IsNull) 
270                                 return SqlBoolean.Null;
271                         else
272                                 return new SqlBoolean (x.Value >= y.Value);
273                 }
274
275                 public static SqlBoolean operator != (SqlByte x, SqlByte y)
276                 {
277                         if (x.IsNull || y.IsNull) 
278                                 return SqlBoolean.Null;
279                         else
280                                 return new SqlBoolean (!(x.Value == y.Value));
281                 }
282
283                 public static SqlBoolean operator < (SqlByte x, SqlByte y)
284                 {
285                         if (x.IsNull || y.IsNull) 
286                                 return SqlBoolean.Null;
287                         else
288                                 return new SqlBoolean (x.Value < y.Value);
289                 }
290
291                 public static SqlBoolean operator <= (SqlByte x, SqlByte y)
292                 {
293                         if (x.IsNull || y.IsNull) 
294                                 return SqlBoolean.Null;
295                         else
296                                 return new SqlBoolean (x.Value <= y.Value);
297                 }
298
299                 public static SqlByte operator % (SqlByte x, SqlByte y)
300                 {
301                         return new SqlByte ((byte) (x.Value % y.Value));
302                 }
303
304                 public static SqlByte operator * (SqlByte x, SqlByte y)
305                 {
306                         checked {
307                                 return new SqlByte ((byte) (x.Value * y.Value));
308                         }
309                 }
310
311                 public static SqlByte operator ~ (SqlByte x)
312                 {
313                         return new SqlByte ((byte) ~x.Value);
314                 }
315
316                 public static SqlByte operator - (SqlByte x, SqlByte y)
317                 {
318                         checked {
319                                 return new SqlByte ((byte) (x.Value - y.Value));
320                         }
321                 }
322
323                 public static explicit operator SqlByte (SqlBoolean x)
324                 {
325                         if (x.IsNull)
326                                 return Null;
327                         else
328                                 return new SqlByte (x.ByteValue);
329                 }
330
331                 public static explicit operator byte (SqlByte x)
332                 {
333                         return x.Value;
334                 }
335
336                 public static explicit operator SqlByte (SqlDecimal x)
337                 {
338                         checked {
339                                 if (x.IsNull)
340                                         return Null;
341                                 else 
342                                         return new SqlByte ((byte)x.Value);
343                         }
344                 }
345
346                 public static explicit operator SqlByte (SqlDouble x)
347                 {
348                         // FIXME
349                         //                      checked {
350                                 if (x.IsNull)
351                                         return Null;
352                                 else                                    
353                                         return new SqlByte ((byte)x.Value);
354                                 //}
355                 }
356
357                 public static explicit operator SqlByte (SqlInt16 x)
358                 {
359                         checked {
360                                 if (x.IsNull)
361                                         return Null;
362                                 else                           
363                                         return new SqlByte ((byte)x.Value);
364                         }
365                 }
366
367                 public static explicit operator SqlByte (SqlInt32 x)
368                 {
369                         checked {
370                                 if (x.IsNull)
371                                         return Null;
372                                 else                           
373                                         return new SqlByte ((byte)x.Value);
374                         }
375                 }
376
377                 public static explicit operator SqlByte (SqlInt64 x)
378                 {
379                         // FIXME
380                         //checked {
381                                 if (x.IsNull)
382                                         return Null;
383                                 else
384                                         return new SqlByte ((byte)x.Value);
385                                 //}
386                 }
387
388                 public static explicit operator SqlByte (SqlMoney x)
389                 {
390                         checked {
391                                 if (x.IsNull)
392                                         return Null;
393                                 else                                    
394                                         return new SqlByte ((byte)x.Value);
395                         }
396                 }
397
398                 public static explicit operator SqlByte (SqlSingle x)
399                 {
400                         // FIXME:
401                         //checked {
402                                 if (x.IsNull)
403                                         return Null;
404                                 else
405                                         return new SqlByte ((byte)x.Value);
406                                 //}
407                 }
408
409
410                 public static explicit operator SqlByte (SqlString x)
411                 {                       
412                         checked {
413                                 return SqlByte.Parse (x.Value);
414                         }
415                 }
416
417                 public static implicit operator SqlByte (byte x)
418                 {
419                         return new SqlByte (x);
420                 }
421                 
422                 #endregion
423         }
424 }
425