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