2 // System.Data.SqlTypes.SqlInt64
5 // Tim Coleman <tim@timcoleman.com>
6 // Ville Palo <vi64pa@koti.soon.fi>
8 // (C) Copyright 2002 Tim Coleman
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
40 namespace System.Data.SqlTypes
43 [SerializableAttribute]
44 [XmlSchemaProvider ("GetXsdType")]
46 public struct SqlInt64 : INullable, IComparable
56 public static readonly SqlInt64 MaxValue = new SqlInt64 (9223372036854775807);
57 public static readonly SqlInt64 MinValue = new SqlInt64 (-9223372036854775808);
59 public static readonly SqlInt64 Null;
60 public static readonly SqlInt64 Zero = new SqlInt64 (0);
66 public SqlInt64 (long value)
77 get { return !notNull; }
83 throw new SqlNullValueException ();
93 public static SqlInt64 Add (SqlInt64 x, SqlInt64 y)
98 public static SqlInt64 BitwiseAnd (SqlInt64 x, SqlInt64 y)
103 public static SqlInt64 BitwiseOr (SqlInt64 x, SqlInt64 y)
108 public int CompareTo (object value)
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);
118 public int CompareTo (SqlInt64 value)
120 return CompareSqlInt64 ((SqlInt64) value);
124 private int CompareSqlInt64 (SqlInt64 value)
129 return this.value.CompareTo (value.Value);
132 public static SqlInt64 Divide (SqlInt64 x, SqlInt64 y)
137 public override bool Equals (object value)
139 if (!(value is SqlInt64))
141 else if (this.IsNull)
142 return ((SqlInt64)value).IsNull;
143 else if (((SqlInt64)value).IsNull)
146 return (bool) (this == (SqlInt64)value);
149 public static SqlBoolean Equals (SqlInt64 x, SqlInt64 y)
154 public override int GetHashCode ()
156 return (int)(value & 0xffffffff) ^ (int)(value >> 32);
159 public static SqlBoolean GreaterThan (SqlInt64 x, SqlInt64 y)
164 public static SqlBoolean GreaterThanOrEqual (SqlInt64 x, SqlInt64 y)
169 public static SqlBoolean LessThan (SqlInt64 x, SqlInt64 y)
174 public static SqlBoolean LessThanOrEqual (SqlInt64 x, SqlInt64 y)
179 public static SqlInt64 Mod (SqlInt64 x, SqlInt64 y)
185 public static SqlInt64 Modulus (SqlInt64 x, SqlInt64 y)
191 public static SqlInt64 Multiply (SqlInt64 x, SqlInt64 y)
196 public static SqlBoolean NotEquals (SqlInt64 x, SqlInt64 y)
201 public static SqlInt64 OnesComplement (SqlInt64 x)
210 public static SqlInt64 Parse (string s)
213 return new SqlInt64 (Int64.Parse (s));
217 public static SqlInt64 Subtract (SqlInt64 x, SqlInt64 y)
222 public SqlBoolean ToSqlBoolean ()
224 return ((SqlBoolean) this);
227 public SqlByte ToSqlByte ()
229 return ((SqlByte) this);
232 public SqlDecimal ToSqlDecimal ()
234 return ((SqlDecimal) this);
237 public SqlDouble ToSqlDouble ()
239 return ((SqlDouble) this);
242 public SqlInt16 ToSqlInt16 ()
244 return ((SqlInt16) this);
247 public SqlInt32 ToSqlInt32 ()
249 return ((SqlInt32) this);
252 public SqlMoney ToSqlMoney ()
254 return ((SqlMoney) this);
257 public SqlSingle ToSqlSingle ()
259 return ((SqlSingle) this);
262 public SqlString ToSqlString ()
264 return ((SqlString) this);
267 public override string ToString ()
272 return value.ToString ();
275 public static SqlInt64 Xor (SqlInt64 x, SqlInt64 y)
280 public static SqlInt64 operator + (SqlInt64 x, SqlInt64 y)
283 return new SqlInt64 (x.Value + y.Value);
287 public static SqlInt64 operator & (SqlInt64 x, SqlInt64 y)
289 return new SqlInt64 (x.value & y.Value);
292 public static SqlInt64 operator | (SqlInt64 x, SqlInt64 y)
294 return new SqlInt64 (x.value | y.Value);
297 public static SqlInt64 operator / (SqlInt64 x, SqlInt64 y)
300 return new SqlInt64 (x.Value / y.Value);
304 public static SqlBoolean operator == (SqlInt64 x, SqlInt64 y)
306 if (x.IsNull || y.IsNull)
307 return SqlBoolean.Null;
309 return new SqlBoolean (x.Value == y.Value);
312 public static SqlInt64 operator ^ (SqlInt64 x, SqlInt64 y)
314 return new SqlInt64 (x.Value ^ y.Value);
317 public static SqlBoolean operator > (SqlInt64 x, SqlInt64 y)
319 if (x.IsNull || y.IsNull)
320 return SqlBoolean.Null;
322 return new SqlBoolean (x.Value > y.Value);
325 public static SqlBoolean operator >= (SqlInt64 x, SqlInt64 y)
327 if (x.IsNull || y.IsNull)
328 return SqlBoolean.Null;
330 return new SqlBoolean (x.Value >= y.Value);
333 public static SqlBoolean operator != (SqlInt64 x, SqlInt64 y)
335 if (x.IsNull || y.IsNull)
336 return SqlBoolean.Null;
338 return new SqlBoolean (!(x.Value == y.Value));
341 public static SqlBoolean operator < (SqlInt64 x, SqlInt64 y)
343 if (x.IsNull || y.IsNull)
344 return SqlBoolean.Null;
346 return new SqlBoolean (x.Value < y.Value);
349 public static SqlBoolean operator <= (SqlInt64 x, SqlInt64 y)
351 if (x.IsNull || y.IsNull)
352 return SqlBoolean.Null;
354 return new SqlBoolean (x.Value <= y.Value);
357 public static SqlInt64 operator % (SqlInt64 x, SqlInt64 y)
359 return new SqlInt64 (x.Value % y.Value);
362 public static SqlInt64 operator * (SqlInt64 x, SqlInt64 y)
365 return new SqlInt64 (x.Value * y.Value);
369 public static SqlInt64 operator ~ (SqlInt64 x)
372 return SqlInt64.Null;
374 return new SqlInt64 (~(x.Value));
377 public static SqlInt64 operator - (SqlInt64 x, SqlInt64 y)
380 return new SqlInt64 (x.Value - y.Value);
384 public static SqlInt64 operator - (SqlInt64 x)
386 return new SqlInt64 (-(x.Value));
389 public static explicit operator SqlInt64 (SqlBoolean x)
392 return SqlInt64.Null;
394 return new SqlInt64 ((long) x.ByteValue);
397 public static explicit operator SqlInt64 (SqlDecimal x)
401 return SqlInt64.Null;
403 return new SqlInt64 ((long) x.Value);
407 public static explicit operator SqlInt64 (SqlDouble x)
410 return SqlInt64.Null;
413 return new SqlInt64 ((long) x.Value);
418 public static explicit operator long (SqlInt64 x)
423 public static explicit operator SqlInt64 (SqlMoney x)
427 return SqlInt64.Null;
429 return new SqlInt64 ((long) Math.Round (x.Value));
433 public static explicit operator SqlInt64 (SqlSingle x)
436 return SqlInt64.Null;
439 return new SqlInt64 ((long) x.Value);
444 public static explicit operator SqlInt64 (SqlString x)
447 return SqlInt64.Parse (x.Value);
451 public static implicit operator SqlInt64 (long x)
453 return new SqlInt64 (x);
456 public static implicit operator SqlInt64 (SqlByte x)
459 return SqlInt64.Null;
461 return new SqlInt64 ((long) x.Value);
464 public static implicit operator SqlInt64 (SqlInt16 x)
467 return SqlInt64.Null;
469 return new SqlInt64 ((long) x.Value);
472 public static implicit operator SqlInt64 (SqlInt32 x)
475 return SqlInt64.Null;
477 return new SqlInt64 ((long) x.Value);
481 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
483 if (schemaSet != null && schemaSet.Count == 0) {
484 XmlSchema xs = new XmlSchema ();
485 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
490 return new XmlQualifiedName ("long", "http://www.w3.org/2001/XMLSchema");
493 XmlSchema IXmlSerializable.GetSchema ()
498 void IXmlSerializable.ReadXml (XmlReader reader)
503 switch (reader.ReadState) {
504 case ReadState.EndOfFile:
505 case ReadState.Error:
506 case ReadState.Closed:
510 // Skip XML declaration and prolog
511 // or do I need to validate for the <SqlInt64> tag?
512 reader.MoveToContent ();
517 if (reader.NodeType == XmlNodeType.EndElement)
520 if (reader.Value.Length > 0) {
521 if (String.Compare ("Null", reader.Value) == 0) {
522 // means a null reference/invalid value
526 // FIXME: Validate the value for expected format
527 this.value = Int64.Parse (reader.Value);
532 void IXmlSerializable.WriteXml (XmlWriter writer)
534 writer.WriteString (this.ToString ());