* OdbcParameterTest.cs: Fixed compilation on 1.0 profile.
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlString.cs
index a20f72292dfe16d621029f6ba460254f5ce0f0a3..02d903a48df2c6529155036b31f3080e1d5ce1ad 100644 (file)
 //
 
 using System;
+using System.Xml;
 using System.Text;
-using System.Globalization;
 using System.Threading;
+using System.Xml.Schema;
+using System.Globalization;
+using System.Xml.Serialization;
 
 namespace System.Data.SqlTypes
 {
@@ -45,7 +48,14 @@ namespace System.Data.SqlTypes
        /// A variable-length stream of characters 
        /// to be stored in or retrieved from the database
        /// </summary>
+#if NET_2_0
+       [SerializableAttribute]
+       [XmlSchemaProvider ("GetXsdType")]
+#endif
        public struct SqlString : INullable, IComparable 
+#if NET_2_0
+                               , IXmlSerializable
+#endif
        {
 
                #region Fields
@@ -82,7 +92,10 @@ namespace System.Data.SqlTypes
                {
                        this.value = data;
                        lcid = CultureInfo.CurrentCulture.LCID;
-                       notNull = true;
+                       if (value != null)
+                               notNull = true;
+                       else
+                               notNull = false;
                        this.compareOptions = SqlCompareOptions.IgnoreCase |
                                SqlCompareOptions.IgnoreKanaType |
                                SqlCompareOptions.IgnoreWidth;
@@ -93,7 +106,10 @@ namespace System.Data.SqlTypes
                {
                        this.value = data;
                        this.lcid = lcid;
-                       notNull = true;
+                       if (value != null)
+                               notNull = true;
+                       else
+                               notNull = false;
                        this.compareOptions = SqlCompareOptions.IgnoreCase |
                                SqlCompareOptions.IgnoreKanaType |
                                SqlCompareOptions.IgnoreWidth;
@@ -110,7 +126,10 @@ namespace System.Data.SqlTypes
                        this.value = data;
                        this.lcid = lcid;
                        this.compareOptions = compareOptions;
-                       notNull = true;
+                       if (value != null)
+                               notNull = true;
+                       else
+                               notNull = false;
                }
 
                // init with locale id, compare options, array of bytes data,
@@ -121,7 +140,10 @@ namespace System.Data.SqlTypes
                        this.value = encoding.GetString (data);
                        this.lcid = lcid;
                        this.compareOptions = compareOptions;
-                       notNull = true;
+                       if (value != null)
+                               notNull = true;
+                       else
+                               notNull = false;
                }
 
                // init with locale id, compare options, array of bytes data,
@@ -140,7 +162,10 @@ namespace System.Data.SqlTypes
                        this.value = encoding.GetString (data, index, count);
                        this.lcid = lcid;
                        this.compareOptions = compareOptions;
-                       notNull = true;
+                       if (value != null)
+                               notNull = true;
+                       else
+                               notNull = false;
                }
 
                #endregion // Constructors
@@ -264,8 +289,8 @@ namespace System.Data.SqlTypes
                {
                        if (!(value is SqlString))
                                return false;
-                       if (this.IsNull && ((SqlString)value).IsNull)
-                               return true;
+                       if (this.IsNull)
+                               return ((SqlString)value).IsNull;
                        else if (((SqlString)value).IsNull)
                                return false;
                        else
@@ -399,11 +424,8 @@ namespace System.Data.SqlTypes
                {
                         if (x.IsNull || y.IsNull)
                                 return SqlString.Null;
-
-                       if (( x == null) || (y == null))
-                                return SqlString.Null;
-                       
-                       return new SqlString (x.Value + y.Value);
+                       else
+                               return new SqlString (x.Value + y.Value);
                }
 
                // Equality
@@ -562,21 +584,79 @@ namespace System.Data.SqlTypes
                        return new SqlString (x);
                }
 
-               #if NET_2_0
+#if NET_2_0
                 public static SqlString Add (SqlString x, SqlString y)
                 {
-                       return ( x + y);
-                                                                                                    
+                       return (x + y);
                 }
 
                public int CompareTo (SqlString value)
                 {
                        return CompareSqlString (value);
                 }
-                #endif
+#endif
 
 
 
                #endregion // Public Methods
+#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 = "string";
+                               xs.Items.Add (ct);
+                               schemaSet.Add (xs);
+                       }
+                       return new XmlQualifiedName ("string", "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 <string> 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 = reader.Value;
+                               this.notNull = true;
+                               this.compareOptions = SqlCompareOptions.IgnoreCase |
+                                       SqlCompareOptions.IgnoreKanaType |
+                                       SqlCompareOptions.IgnoreWidth;
+                       }
+               }
+               
+               void IXmlSerializable.WriteXml (XmlWriter writer) 
+               {
+                       writer.WriteString (this.ToString ());
+               }
+#endif
        }
 }