Fix SqlParameter with xml schema construction (dotnet/corefx#41008)
authorWraith2 <wraith2@gmail.com>
Mon, 16 Sep 2019 19:48:10 +0000 (20:48 +0100)
committerCheena Malhotra <v-chmalh@microsoft.com>
Mon, 16 Sep 2019 19:48:10 +0000 (12:48 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/cd0576dad5fcfe73aeda81a0a81972843734e525

src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs
src/libraries/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs

index 6ffe502..862c683 100644 (file)
@@ -261,15 +261,8 @@ namespace System.Data.SqlClient
             }
             set
             {
-                bool collectionIsNull = _xmlSchemaCollection != null;
-                if (collectionIsNull)
-                {
-                    _xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
-                }
-                if (value != null || collectionIsNull)
-                {
-                    _xmlSchemaCollection.Database = value;
-                }
+                EnsureXmlSchemaCollectionExists();
+                _xmlSchemaCollection.Database = value;
             }
         }
 
@@ -281,15 +274,8 @@ namespace System.Data.SqlClient
             }
             set
             {
-                bool collectionIsNull = _xmlSchemaCollection != null;
-                if (collectionIsNull)
-                {
-                    _xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
-                }
-                if (value != null || collectionIsNull)
-                {
-                    _xmlSchemaCollection.OwningSchema = value;
-                }
+                EnsureXmlSchemaCollectionExists();
+                _xmlSchemaCollection.OwningSchema = value;
             }
         }
 
@@ -301,15 +287,8 @@ namespace System.Data.SqlClient
             }
             set
             {
-                bool collectionIsNull = _xmlSchemaCollection != null;
-                if (collectionIsNull)
-                {
-                    _xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
-                }
-                if (value != null || collectionIsNull)
-                {
-                    _xmlSchemaCollection.Name = value;
-                }
+                EnsureXmlSchemaCollectionExists();
+                _xmlSchemaCollection.Name = value;
             }
         }
 
@@ -1978,6 +1957,14 @@ namespace System.Data.SqlClient
             }
         }
 
+        private void EnsureXmlSchemaCollectionExists()
+        {
+            if (_xmlSchemaCollection is null)
+            {
+                _xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
+            }
+        }
+
         internal sealed class SqlParameterConverter : ExpandableObjectConverter
         {
 
index bd88851..40762d0 100644 (file)
@@ -38,5 +38,69 @@ namespace System.Data.SqlClient.Tests
             Assert.Equal(10, parameter.Precision);
             Assert.Equal(5, parameter.Scale);
         }
+
+        [Fact]
+        public void CreateParameterWithValidXmlSchema()
+        {
+            string xmlDatabase = "database";
+            string xmlSchema = "schema";
+            string xmlName = "name";
+
+            SqlParameter parameter = new SqlParameter("@name", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "name", DataRowVersion.Original, false, 1, xmlDatabase, xmlSchema, xmlName);
+
+            Assert.Equal(xmlDatabase, parameter.XmlSchemaCollectionDatabase);
+            Assert.Equal(xmlSchema, parameter.XmlSchemaCollectionOwningSchema);
+            Assert.Equal(xmlName, parameter.XmlSchemaCollectionName);
+        }
+
+        [Fact]
+        public void CreateParameterWithEmptyXmlSchema()
+        {
+            SqlParameter parameter = new SqlParameter("@name", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "name", DataRowVersion.Original, false, 1, string.Empty, string.Empty, string.Empty);
+
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionDatabase);
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionOwningSchema);
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName);
+        }
+
+        [Fact]
+        public void CreateParameterWithNullXmlSchema()
+        {
+            SqlParameter parameter = new SqlParameter("@name", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "name", DataRowVersion.Original, false, 1, null, null, null);
+
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionDatabase);
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionOwningSchema);
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName);
+        }
+
+        [Fact]
+        public void CreateParameterWithoutXmlSchema()
+        {
+            SqlParameter parameter = new SqlParameter();
+
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionDatabase);
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionOwningSchema);
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName);
+        }
+
+        [Fact]
+        public void SetParameterXmlSchema()
+        {
+            SqlParameter parameter = new SqlParameter();
+
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName);
+
+            // verify that if we set it to null we still get an empty string back
+            parameter.XmlSchemaCollectionName = null;
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName);
+
+            // verify that if we set a value we get it back
+            parameter.XmlSchemaCollectionName = "name";
+            Assert.Equal("name", parameter.XmlSchemaCollectionName);
+
+            // verify that if we set it explicitly to null it reverts to empty string
+            parameter.XmlSchemaCollectionName = null;
+            Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName);
+        }
     }
 }