Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix SqlParameter with xml schema construction (#41178)
Browse files Browse the repository at this point in the history
Port PR #41008 for Release 3.1
  • Loading branch information
cheenamalhotra authored and Anipik committed Sep 20, 2019
1 parent eaa4640 commit a7ce132
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 27 deletions.
41 changes: 14 additions & 27 deletions src/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,8 @@ public string XmlSchemaCollectionDatabase
}
set
{
bool collectionIsNull = _xmlSchemaCollection != null;
if (collectionIsNull)
{
_xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
}
if (value != null || collectionIsNull)
{
_xmlSchemaCollection.Database = value;
}
EnsureXmlSchemaCollectionExists();
_xmlSchemaCollection.Database = value;
}
}

Expand All @@ -281,15 +274,8 @@ public string XmlSchemaCollectionOwningSchema
}
set
{
bool collectionIsNull = _xmlSchemaCollection != null;
if (collectionIsNull)
{
_xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
}
if (value != null || collectionIsNull)
{
_xmlSchemaCollection.OwningSchema = value;
}
EnsureXmlSchemaCollectionExists();
_xmlSchemaCollection.OwningSchema = value;
}
}

Expand All @@ -301,15 +287,8 @@ public string XmlSchemaCollectionName
}
set
{
bool collectionIsNull = _xmlSchemaCollection != null;
if (collectionIsNull)
{
_xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
}
if (value != null || collectionIsNull)
{
_xmlSchemaCollection.Name = value;
}
EnsureXmlSchemaCollectionExists();
_xmlSchemaCollection.Name = value;
}
}

Expand Down Expand Up @@ -1977,6 +1956,14 @@ internal static string[] ParseTypeName(string typeName, bool isUdtTypeName)
}
}
}

private void EnsureXmlSchemaCollectionExists()
{
if (_xmlSchemaCollection is null)
{
_xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
}
}

internal sealed class SqlParameterConverter : ExpandableObjectConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,69 @@ public void ParameterPrecisionOnBaseType()
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);
}
}
}

0 comments on commit a7ce132

Please sign in to comment.