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