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