From: Wraith2 Date: Mon, 16 Sep 2019 19:48:10 +0000 (+0100) Subject: Fix SqlParameter with xml schema construction (dotnet/corefx#41008) X-Git-Tag: submit/tizen/20210909.063632~11031^2~457 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dcc4c5255abb5df9ffa0ad1d38d0263d8640e586;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Fix SqlParameter with xml schema construction (dotnet/corefx#41008) Commit migrated from https://github.com/dotnet/corefx/commit/cd0576dad5fcfe73aeda81a0a81972843734e525 --- diff --git a/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs b/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs index 6ffe502..862c683 100644 --- a/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs +++ b/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs @@ -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 { diff --git a/src/libraries/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs b/src/libraries/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs index bd88851..40762d0 100644 --- a/src/libraries/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs +++ b/src/libraries/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs @@ -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); + } } }