fixed tests
[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.Xml;
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
39
40 namespace System.Data.SqlTypes
41 {
42 #if NET_2_0
43         [SerializableAttribute]
44         [XmlSchemaProvider ("GetSchema")]
45 #endif
46         public struct SqlInt64 : INullable, IComparable
47 #if NET_2_0
48                                 , IXmlSerializable
49 #endif
50         {
51                 #region Fields
52
53                 long value;
54
55                 private bool notNull;
56                 
57                 public static readonly SqlInt64 MaxValue = new SqlInt64 (9223372036854775807);
58                 public static readonly SqlInt64 MinValue = new SqlInt64 (-9223372036854775808);
59
60                 public static readonly SqlInt64 Null;
61                 public static readonly SqlInt64 Zero = new SqlInt64 (0);
62
63                 #endregion
64
65                 #region Constructors
66
67                 public SqlInt64 (long value) 
68                 {
69                         this.value = value;
70                         notNull = true;
71                 }
72
73                 #endregion
74
75                 #region Properties
76
77                 public bool IsNull { 
78                         get { return !notNull; }
79                 }
80
81                 public long Value { 
82                         get { 
83                                 if (this.IsNull) 
84                                         throw new SqlNullValueException ();
85                                 else 
86                                         return value; 
87                         }
88                 }
89
90                 #endregion
91
92                 #region Methods
93
94                 public static SqlInt64 Add (SqlInt64 x, SqlInt64 y)
95                 {
96                         return (x + y);
97                 }
98
99                 public static SqlInt64 BitwiseAnd (SqlInt64 x, SqlInt64 y)
100                 {
101                         return (x & y);
102                 }
103
104                 public static SqlInt64 BitwiseOr (SqlInt64 x, SqlInt64 y)
105                 {
106                         return (x | y);
107                 }
108
109                 public int CompareTo (object value)
110                 {
111                         if (value == null)
112                                 return 1;
113                         else if (!(value is SqlInt64))
114                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlInt64"));
115                         return CompareSqlInt64 ((SqlInt64) value);
116                 }
117
118                 #if NET_2_0
119                 public int CompareTo (SqlInt64 value)
120                 {
121                         return CompareSqlInt64 ((SqlInt64) value);
122                 }
123                 #endif  
124         
125                 private int CompareSqlInt64 (SqlInt64 value)
126                 {
127                         if (value.IsNull)
128                                 return 1;
129                         else
130                                 return this.value.CompareTo (value.Value);
131                 }
132
133                 public static SqlInt64 Divide (SqlInt64 x, SqlInt64 y)
134                 {
135                         return (x / y);
136                 }
137
138                 public override bool Equals (object value)
139                 {
140                         if (!(value is SqlInt64))
141                                 return false;
142                         else if (this.IsNull && ((SqlInt64)value).IsNull)
143                                 return true;
144                         else if (((SqlInt64)value).IsNull)
145                                 return false;
146                         else
147                                 return (bool) (this == (SqlInt64)value);
148                 }
149
150                 public static SqlBoolean Equals (SqlInt64 x, SqlInt64 y)
151                 {
152                         return (x == y);
153                 }
154
155                 public override int GetHashCode ()
156                 {
157                         return (int)(value & 0xffffffff) ^ (int)(value >> 32);
158                 }
159
160                 public static SqlBoolean GreaterThan (SqlInt64 x, SqlInt64 y)
161                 {
162                         return (x > y);
163                 }
164
165                 public static SqlBoolean GreaterThanOrEqual (SqlInt64 x, SqlInt64 y)
166                 {
167                         return (x >= y);
168                 }
169
170                 public static SqlBoolean LessThan (SqlInt64 x, SqlInt64 y)
171                 {
172                         return (x < y);
173                 }
174
175                 public static SqlBoolean LessThanOrEqual (SqlInt64 x, SqlInt64 y)
176                 {
177                         return (x <= y);
178                 }
179
180                 public static SqlInt64 Mod (SqlInt64 x, SqlInt64 y)
181                 {
182                         return (x % y);
183                 }
184
185                 #if NET_2_0
186                 public static SqlInt64 Modulus (SqlInt64 x, SqlInt64 y)
187                 {
188                         return (x % y);
189                 }
190                 #endif
191
192                 public static SqlInt64 Multiply (SqlInt64 x, SqlInt64 y)
193                 {
194                         return (x * y);
195                 }
196
197                 public static SqlBoolean NotEquals (SqlInt64 x, SqlInt64 y)
198                 {
199                         return (x != y);
200                 }
201
202                 public static SqlInt64 OnesComplement (SqlInt64 x)
203                 {
204                         if (x.IsNull)
205                                 return Null;
206
207                         return ~x;
208                 }
209
210
211                 public static SqlInt64 Parse (string s)
212                 {
213                         checked {
214                                 return new SqlInt64 (Int64.Parse (s));
215                         }
216                 }
217
218                 public static SqlInt64 Subtract (SqlInt64 x, SqlInt64 y)
219                 {
220                         return (x - y);
221                 }
222
223                 public SqlBoolean ToSqlBoolean ()
224                 {
225                         return ((SqlBoolean)this);
226                 }
227                 
228                 public SqlByte ToSqlByte ()
229                 {
230                         return ((SqlByte)this);
231                 }
232
233                 public SqlDecimal ToSqlDecimal ()
234                 {
235                         return ((SqlDecimal)this);
236                 }
237
238                 public SqlDouble ToSqlDouble ()
239                 {
240                         return ((SqlDouble)this);
241                 }
242
243                 public SqlInt16 ToSqlInt16 ()
244                 {
245                         return ((SqlInt16)this);
246                 }
247
248                 public SqlInt32 ToSqlInt32 ()
249                 {
250                         return ((SqlInt32)this);
251                 }
252
253                 public SqlMoney ToSqlMoney ()
254                 {
255                         return ((SqlMoney)this);
256                 }
257
258                 public SqlSingle ToSqlSingle ()
259                 {
260                         return ((SqlSingle)this);
261                 }
262
263                 public SqlString ToSqlString ()
264                 {
265                         return ((SqlString)this);
266                 }
267
268                 public override string ToString ()
269                 {
270                         if (this.IsNull)
271                                 return "Null";
272
273                         return value.ToString ();
274                 }
275
276                 public static SqlInt64 Xor (SqlInt64 x, SqlInt64 y)
277                 {
278                         return (x ^ y);
279                 }
280
281                 public static SqlInt64 operator + (SqlInt64 x, SqlInt64 y)
282                 {
283                         checked {
284                                 return new SqlInt64 (x.Value + y.Value);
285                         }
286                 }
287
288                 public static SqlInt64 operator & (SqlInt64 x, SqlInt64 y)
289                 {
290                         return new SqlInt64 (x.value & y.Value);
291                 }
292
293                 public static SqlInt64 operator | (SqlInt64 x, SqlInt64 y)
294                 {
295                         return new SqlInt64 (x.value | y.Value);
296                 }
297
298                 public static SqlInt64 operator / (SqlInt64 x, SqlInt64 y)
299                 {
300                         checked {
301                                 return new SqlInt64 (x.Value / y.Value);
302                         }
303                 }
304
305                 public static SqlBoolean operator == (SqlInt64 x, SqlInt64 y)
306                 {
307                         if (x.IsNull || y.IsNull) 
308                                 return SqlBoolean.Null;
309                         else
310                                 return new SqlBoolean (x.Value == y.Value);
311                 }
312
313                 public static SqlInt64 operator ^ (SqlInt64 x, SqlInt64 y)
314                 {
315                         return new SqlInt64 (x.Value ^ y.Value);
316                 }
317
318                 public static SqlBoolean operator > (SqlInt64 x, SqlInt64 y)
319                 {
320                         if (x.IsNull || y.IsNull) 
321                                 return SqlBoolean.Null;
322                         else
323                                 return new SqlBoolean (x.Value > y.Value);
324                 }
325
326                 public static SqlBoolean operator >= (SqlInt64 x, SqlInt64 y)
327                 {
328                         if (x.IsNull || y.IsNull) 
329                                 return SqlBoolean.Null;
330                         else
331                                 return new SqlBoolean (x.Value >= y.Value);
332                 }
333
334                 public static SqlBoolean operator != (SqlInt64 x, SqlInt64 y)
335                 {
336                         if (x.IsNull || y.IsNull) 
337                                 return SqlBoolean.Null;
338                         else
339                                 return new SqlBoolean (!(x.Value == y.Value));
340                 }
341
342                 public static SqlBoolean operator < (SqlInt64 x, SqlInt64 y)
343                 {
344                         if (x.IsNull || y.IsNull) 
345                                 return SqlBoolean.Null;
346                         else
347                                 return new SqlBoolean (x.Value < y.Value);
348                 }
349
350                 public static SqlBoolean operator <= (SqlInt64 x, SqlInt64 y)
351                 {
352                         if (x.IsNull || y.IsNull) 
353                                 return SqlBoolean.Null;
354                         else
355                                 return new SqlBoolean (x.Value <= y.Value);
356                 }
357
358                 public static SqlInt64 operator % (SqlInt64 x, SqlInt64 y)
359                 {
360                         return new SqlInt64(x.Value % y.Value);
361                 }
362
363                 public static SqlInt64 operator * (SqlInt64 x, SqlInt64 y)
364                 {
365                         checked {
366                                 return new SqlInt64 (x.Value * y.Value);
367                         }
368                 }
369
370                 public static SqlInt64 operator ~ (SqlInt64 x)
371                 {
372                         if (x.IsNull)
373                                 return SqlInt64.Null;
374
375                         return new SqlInt64 (~(x.Value));
376                 }
377
378                 public static SqlInt64 operator - (SqlInt64 x, SqlInt64 y)
379                 {
380                         checked {
381                                 return new SqlInt64 (x.Value - y.Value);
382                         }
383                 }
384
385                 public static SqlInt64 operator - (SqlInt64 n)
386                 {
387                         return new SqlInt64 (-(n.Value));
388                 }
389
390                 public static explicit operator SqlInt64 (SqlBoolean x)
391                 {
392                         if (x.IsNull) 
393                                 return SqlInt64.Null;
394                         else
395                                 return new SqlInt64 ((long)x.ByteValue);
396                 }
397
398                 public static explicit operator SqlInt64 (SqlDecimal x)
399                 {
400                         checked {
401                                 if (x.IsNull) 
402                                         return SqlInt64.Null;
403                                 else
404                                         return new SqlInt64 ((long)x.Value);
405                         }
406                 }
407
408                 public static explicit operator SqlInt64 (SqlDouble x)
409                 {
410                         if (x.IsNull) 
411                                 return SqlInt64.Null;
412                         else {
413                                 checked {
414                                         return new SqlInt64 ((long)x.Value);
415                                 }
416                         }
417                 }
418
419                 public static explicit operator long (SqlInt64 x)
420                 {
421                         return x.Value;
422                 }
423
424                 public static explicit operator SqlInt64 (SqlMoney x)
425                 {
426                         checked {
427                                 if (x.IsNull) 
428                                         return SqlInt64.Null;
429                                 else
430                                         return new SqlInt64 ((long) Math.Round (x.Value));
431                         }
432                 }
433
434                 public static explicit operator SqlInt64 (SqlSingle x)
435                 {
436                         if (x.IsNull) 
437                                 return SqlInt64.Null;
438                         else {
439                                 checked {
440                                         return new SqlInt64 ((long)x.Value);
441                                 }
442                         }
443                 }
444
445                 public static explicit operator SqlInt64 (SqlString x)
446                 {
447                         checked {
448                                 return SqlInt64.Parse (x.Value);
449                         }
450                 }
451
452                 public static implicit operator SqlInt64 (long x)
453                 {
454                         return new SqlInt64 (x);
455                 }
456
457                 public static implicit operator SqlInt64 (SqlByte x)
458                 {
459                         if (x.IsNull) 
460                                 return SqlInt64.Null;
461                         else
462                                 return new SqlInt64 ((long)x.Value);
463                 }
464
465                 public static implicit operator SqlInt64 (SqlInt16 x)
466                 {
467                         if (x.IsNull) 
468                                 return SqlInt64.Null;
469                         else
470                                 return new SqlInt64 ((long)x.Value);
471                 }
472
473                 public static implicit operator SqlInt64 (SqlInt32 x)
474                 {
475                         if (x.IsNull) 
476                                 return SqlInt64.Null;
477                         else
478                                 return new SqlInt64 ((long)x.Value);
479                 }
480
481 #if NET_2_0
482                 [MonoTODO]
483                 XmlSchema IXmlSerializable.GetSchema ()
484                 {
485                         throw new NotImplementedException ();
486                 }
487                 
488                 [MonoTODO]
489                 void IXmlSerializable.ReadXml (XmlReader reader)
490                 {
491                         throw new NotImplementedException ();
492                 }
493                 
494                 [MonoTODO]
495                 void IXmlSerializable.WriteXml (XmlWriter writer) 
496                 {
497                         throw new NotImplementedException ();
498                 }
499 #endif
500                 #endregion
501         }
502 }
503