2010-03-26 Veerapuram Varadhan <vvaradhan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlInt64.cs
index aea159d17d4716f0c77d2f8013dcb53534e304cc..a7cb6055117d344e9c6050297d4cfc8d303d8666 100644 (file)
@@ -3,26 +3,58 @@
 //
 // Author:
 //   Tim Coleman <tim@timcoleman.com>
+//   Ville Palo <vi64pa@koti.soon.fi>
 //
 // (C) Copyright 2002 Tim Coleman
 //
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
+using System.Xml;
+using System.Xml.Schema;
 using System.Globalization;
+using System.Xml.Serialization;
 
 namespace System.Data.SqlTypes
 {
+#if NET_2_0
+       [SerializableAttribute]
+       [XmlSchemaProvider ("GetXsdType")]
+#endif
        public struct SqlInt64 : INullable, IComparable
+#if NET_2_0
+                               , IXmlSerializable
+#endif
        {
                #region Fields
 
-               long value;
+               private long value;
+               private bool notNull;
                
-               [MonoTODO]
-               public static readonly SqlInt64 MaxValue; // 2^63 - 1
-
-               [MonoTODO]
-               public static readonly SqlInt64 MinValue; // -2^63
+               public static readonly SqlInt64 MaxValue = new SqlInt64 (9223372036854775807);
+               public static readonly SqlInt64 MinValue = new SqlInt64 (-9223372036854775808);
 
                public static readonly SqlInt64 Null;
                public static readonly SqlInt64 Zero = new SqlInt64 (0);
@@ -31,25 +63,26 @@ namespace System.Data.SqlTypes
 
                #region Constructors
 
-               public SqlInt64 (long value) 
+               public SqlInt64 (long value)
                {
                        this.value = value;
+                       notNull = true;
                }
 
                #endregion
 
                #region Properties
 
-               public bool IsNull { 
-                       get { return (bool) (this == Null); }
+               public bool IsNull {
+                       get { return !notNull; }
                }
 
-               public long Value { 
-                       get { 
+               public long Value {
+                       get {
                                if (this.IsNull) 
                                        throw new SqlNullValueException ();
                                else 
-                                       return value; 
+                                       return value;
                        }
                }
 
@@ -78,10 +111,22 @@ namespace System.Data.SqlTypes
                                return 1;
                        else if (!(value is SqlInt64))
                                throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlInt64"));
-                       else if (((SqlInt64)value).IsNull)
+                       return CompareSqlInt64 ((SqlInt64) value);
+               }
+
+#if NET_2_0
+               public int CompareTo (SqlInt64 value)
+               {
+                       return CompareSqlInt64 ((SqlInt64) value);
+               }
+#endif
+       
+               private int CompareSqlInt64 (SqlInt64 value)
+               {
+                       if (value.IsNull)
                                return 1;
                        else
-                               return this.value.CompareTo (((SqlInt64)value).Value);
+                               return this.value.CompareTo (value.Value);
                }
 
                public static SqlInt64 Divide (SqlInt64 x, SqlInt64 y)
@@ -93,6 +138,10 @@ namespace System.Data.SqlTypes
                {
                        if (!(value is SqlInt64))
                                return false;
+                       else if (this.IsNull)
+                               return ((SqlInt64)value).IsNull;
+                       else if (((SqlInt64)value).IsNull)
+                               return false;
                        else
                                return (bool) (this == (SqlInt64)value);
                }
@@ -102,10 +151,9 @@ namespace System.Data.SqlTypes
                        return (x == y);
                }
 
-               [MonoTODO]
                public override int GetHashCode ()
                {
-                       return (int)value;
+                       return (int)(value & 0xffffffff) ^ (int)(value >> 32);
                }
 
                public static SqlBoolean GreaterThan (SqlInt64 x, SqlInt64 y)
@@ -133,6 +181,13 @@ namespace System.Data.SqlTypes
                        return (x % y);
                }
 
+#if NET_2_0
+               public static SqlInt64 Modulus (SqlInt64 x, SqlInt64 y)
+               {
+                       return (x % y);
+               }
+#endif
+
                public static SqlInt64 Multiply (SqlInt64 x, SqlInt64 y)
                {
                        return (x * y);
@@ -145,13 +200,18 @@ namespace System.Data.SqlTypes
 
                public static SqlInt64 OnesComplement (SqlInt64 x)
                {
+                       if (x.IsNull)
+                               return Null;
+
                        return ~x;
                }
 
-               [MonoTODO]
+
                public static SqlInt64 Parse (string s)
                {
-                       throw new NotImplementedException ();
+                       checked {
+                               return new SqlInt64 (Int64.Parse (s));
+                       }
                }
 
                public static SqlInt64 Subtract (SqlInt64 x, SqlInt64 y)
@@ -161,51 +221,54 @@ namespace System.Data.SqlTypes
 
                public SqlBoolean ToSqlBoolean ()
                {
-                       return ((SqlBoolean)this);
+                       return ((SqlBoolean) this);
                }
                
                public SqlByte ToSqlByte ()
                {
-                       return ((SqlByte)this);
+                       return ((SqlByte) this);
                }
 
                public SqlDecimal ToSqlDecimal ()
                {
-                       return ((SqlDecimal)this);
+                       return ((SqlDecimal) this);
                }
 
                public SqlDouble ToSqlDouble ()
                {
-                       return ((SqlDouble)this);
+                       return ((SqlDouble) this);
                }
 
                public SqlInt16 ToSqlInt16 ()
                {
-                       return ((SqlInt16)this);
+                       return ((SqlInt16) this);
                }
 
                public SqlInt32 ToSqlInt32 ()
                {
-                       return ((SqlInt32)this);
+                       return ((SqlInt32) this);
                }
 
                public SqlMoney ToSqlMoney ()
                {
-                       return ((SqlMoney)this);
+                       return ((SqlMoney) this);
                }
 
                public SqlSingle ToSqlSingle ()
                {
-                       return ((SqlSingle)this);
+                       return ((SqlSingle) this);
                }
 
                public SqlString ToSqlString ()
                {
-                       return ((SqlString)this);
+                       return ((SqlString) this);
                }
 
                public override string ToString ()
                {
+                       if (this.IsNull)
+                               return "Null";
+
                        return value.ToString ();
                }
 
@@ -216,7 +279,9 @@ namespace System.Data.SqlTypes
 
                public static SqlInt64 operator + (SqlInt64 x, SqlInt64 y)
                {
-                       return new SqlInt64 (x.Value + y.Value);
+                       checked {
+                               return new SqlInt64 (x.Value + y.Value);
+                       }
                }
 
                public static SqlInt64 operator & (SqlInt64 x, SqlInt64 y)
@@ -231,12 +296,14 @@ namespace System.Data.SqlTypes
 
                public static SqlInt64 operator / (SqlInt64 x, SqlInt64 y)
                {
-                       return new SqlInt64 (x.Value / y.Value);
+                       checked {
+                               return new SqlInt64 (x.Value / y.Value);
+                       }
                }
 
                public static SqlBoolean operator == (SqlInt64 x, SqlInt64 y)
                {
-                       if (x.IsNull || y.IsNull) 
+                       if (x.IsNull || y.IsNull)
                                return SqlBoolean.Null;
                        else
                                return new SqlBoolean (x.Value == y.Value);
@@ -249,7 +316,7 @@ namespace System.Data.SqlTypes
 
                public static SqlBoolean operator > (SqlInt64 x, SqlInt64 y)
                {
-                       if (x.IsNull || y.IsNull) 
+                       if (x.IsNull || y.IsNull)
                                return SqlBoolean.Null;
                        else
                                return new SqlBoolean (x.Value > y.Value);
@@ -257,7 +324,7 @@ namespace System.Data.SqlTypes
 
                public static SqlBoolean operator >= (SqlInt64 x, SqlInt64 y)
                {
-                       if (x.IsNull || y.IsNull) 
+                       if (x.IsNull || y.IsNull)
                                return SqlBoolean.Null;
                        else
                                return new SqlBoolean (x.Value >= y.Value);
@@ -265,7 +332,7 @@ namespace System.Data.SqlTypes
 
                public static SqlBoolean operator != (SqlInt64 x, SqlInt64 y)
                {
-                       if (x.IsNull || y.IsNull) 
+                       if (x.IsNull || y.IsNull)
                                return SqlBoolean.Null;
                        else
                                return new SqlBoolean (!(x.Value == y.Value));
@@ -273,7 +340,7 @@ namespace System.Data.SqlTypes
 
                public static SqlBoolean operator < (SqlInt64 x, SqlInt64 y)
                {
-                       if (x.IsNull || y.IsNull) 
+                       if (x.IsNull || y.IsNull)
                                return SqlBoolean.Null;
                        else
                                return new SqlBoolean (x.Value < y.Value);
@@ -281,7 +348,7 @@ namespace System.Data.SqlTypes
 
                public static SqlBoolean operator <= (SqlInt64 x, SqlInt64 y)
                {
-                       if (x.IsNull || y.IsNull) 
+                       if (x.IsNull || y.IsNull)
                                return SqlBoolean.Null;
                        else
                                return new SqlBoolean (x.Value <= y.Value);
@@ -289,51 +356,63 @@ namespace System.Data.SqlTypes
 
                public static SqlInt64 operator % (SqlInt64 x, SqlInt64 y)
                {
-                       return new SqlInt64(x.Value % y.Value);
+                       return new SqlInt64 (x.Value % y.Value);
                }
 
                public static SqlInt64 operator * (SqlInt64 x, SqlInt64 y)
                {
-                       return new SqlInt64 (x.Value * y.Value);
+                       checked {
+                               return new SqlInt64 (x.Value * y.Value);
+                       }
                }
 
                public static SqlInt64 operator ~ (SqlInt64 x)
                {
+                       if (x.IsNull)
+                               return SqlInt64.Null;
+
                        return new SqlInt64 (~(x.Value));
                }
 
                public static SqlInt64 operator - (SqlInt64 x, SqlInt64 y)
                {
-                       return new SqlInt64 (x.Value - y.Value);
+                       checked {
+                               return new SqlInt64 (x.Value - y.Value);
+                       }
                }
 
-               public static SqlInt64 operator - (SqlInt64 n)
+               public static SqlInt64 operator - (SqlInt64 x)
                {
-                       return new SqlInt64 (-(n.Value));
+                       return new SqlInt64 (-(x.Value));
                }
 
                public static explicit operator SqlInt64 (SqlBoolean x)
                {
-                       if (x.IsNull) 
+                       if (x.IsNull)
                                return SqlInt64.Null;
                        else
-                               return new SqlInt64 ((long)x.ByteValue);
+                               return new SqlInt64 ((long) x.ByteValue);
                }
 
                public static explicit operator SqlInt64 (SqlDecimal x)
                {
-                       if (x.IsNull) 
-                               return SqlInt64.Null;
-                       else
-                               return new SqlInt64 ((long)x.Value);
+                       checked {
+                               if (x.IsNull)
+                                       return SqlInt64.Null;
+                               else
+                                       return new SqlInt64 ((long) x.Value);
+                       }
                }
 
                public static explicit operator SqlInt64 (SqlDouble x)
                {
-                       if (x.IsNull) 
+                       if (x.IsNull)
                                return SqlInt64.Null;
-                       else
-                               return new SqlInt64 ((long)x.Value);
+                       else {
+                               checked {
+                                       return new SqlInt64 ((long) x.Value);
+                               }
+                       }
                }
 
                public static explicit operator long (SqlInt64 x)
@@ -343,24 +422,30 @@ namespace System.Data.SqlTypes
 
                public static explicit operator SqlInt64 (SqlMoney x)
                {
-                       if (x.IsNull) 
-                               return SqlInt64.Null;
-                       else
-                               return new SqlInt64 ((long)x.Value);
+                       checked {
+                               if (x.IsNull)
+                                       return SqlInt64.Null;
+                               else
+                                       return new SqlInt64 ((long) Math.Round (x.Value));
+                       }
                }
 
                public static explicit operator SqlInt64 (SqlSingle x)
                {
-                       if (x.IsNull) 
+                       if (x.IsNull)
                                return SqlInt64.Null;
-                       else
-                               return new SqlInt64 ((long)x.Value);
+                       else {
+                               checked {
+                                       return new SqlInt64 ((long) x.Value);
+                               }
+                       }
                }
 
-               [MonoTODO]
                public static explicit operator SqlInt64 (SqlString x)
                {
-                       throw new NotImplementedException ();
+                       checked {
+                               return SqlInt64.Parse (x.Value);
+                       }
                }
 
                public static implicit operator SqlInt64 (long x)
@@ -370,29 +455,86 @@ namespace System.Data.SqlTypes
 
                public static implicit operator SqlInt64 (SqlByte x)
                {
-                       if (x.IsNull) 
+                       if (x.IsNull)
                                return SqlInt64.Null;
                        else
-                               return new SqlInt64 ((long)x.Value);
+                               return new SqlInt64 ((long) x.Value);
                }
 
                public static implicit operator SqlInt64 (SqlInt16 x)
                {
-                       if (x.IsNull) 
+                       if (x.IsNull)
                                return SqlInt64.Null;
                        else
-                               return new SqlInt64 ((long)x.Value);
+                               return new SqlInt64 ((long) x.Value);
                }
 
                public static implicit operator SqlInt64 (SqlInt32 x)
                {
-                       if (x.IsNull) 
+                       if (x.IsNull)
                                return SqlInt64.Null;
                        else
-                               return new SqlInt64 ((long)x.Value);
+                               return new SqlInt64 ((long) x.Value);
+               }
+
+#if NET_2_0
+               public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
+               {
+                       if (schemaSet != null && schemaSet.Count == 0) {
+                               XmlSchema xs = new XmlSchema ();
+                               XmlSchemaComplexType ct = new XmlSchemaComplexType ();
+                               ct.Name = "long";
+                               xs.Items.Add (ct);
+                               schemaSet.Add (xs);
+                       }
+                       return new XmlQualifiedName ("long", "http://www.w3.org/2001/XMLSchema");
+               }
+
+               XmlSchema IXmlSerializable.GetSchema ()
+               {
+                       return null;
+               }
+               
+               void IXmlSerializable.ReadXml (XmlReader reader)
+               {
+                       if (reader == null)
+                               return;
+
+                       switch (reader.ReadState) {
+                       case ReadState.EndOfFile:
+                       case ReadState.Error:
+                       case ReadState.Closed:
+                               return;
+                       }
+
+                       // Skip XML declaration and prolog
+                       // or do I need to validate for the <SqlInt64> tag?
+                       reader.MoveToContent ();
+                       if (reader.EOF)
+                               return;
+
+                       reader.Read ();
+                       if (reader.NodeType == XmlNodeType.EndElement)
+                               return;
+
+                       if (reader.Value.Length > 0) {
+                               if (String.Compare ("Null", reader.Value) == 0) {
+                                       // means a null reference/invalid value
+                                       notNull = false;
+                                       return; 
+                               }
+                               // FIXME: Validate the value for expected format
+                               this.value = Int64.Parse (reader.Value);
+                               this.notNull = true;
+                       }
+               }
+               
+               void IXmlSerializable.WriteXml (XmlWriter writer) 
+               {
+                       writer.WriteString (this.ToString ());
                }
+#endif
 
                #endregion
        }
 }
-