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