Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink SqlParameter XmlSchema #1033

Merged
merged 5 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ private InstanceDescriptor ConvertToInstanceDescriptor(SqlParameter p)

private MetaType _metaType;
private SqlCollation _collation;
private string _xmlSchemaCollectionDatabase;
private string _xmlSchemaCollectionOwningSchema;
private string _xmlSchemaCollectionName;
private SqlMetaDataXmlSchemaCollection _xmlSchemaCollection;
private string _udtTypeName;
private string _typeName;
private Exception _udtLoadError;
Expand Down Expand Up @@ -244,6 +242,7 @@ public SqlParameter() : base()
{
_isNull = true;
_actualSize = -1;
_direction = ParameterDirection.Input;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/ctorParameterNameDbType/*' />
Expand Down Expand Up @@ -331,9 +330,13 @@ string xmlSchemaCollectionName
SourceVersion = sourceVersion;
SourceColumnNullMapping = sourceColumnNullMapping;
Value = value;
XmlSchemaCollectionDatabase = xmlSchemaCollectionDatabase;
XmlSchemaCollectionOwningSchema = xmlSchemaCollectionOwningSchema;
XmlSchemaCollectionName = xmlSchemaCollectionName;
if (!string.IsNullOrEmpty(xmlSchemaCollectionDatabase) || !string.IsNullOrEmpty(xmlSchemaCollectionOwningSchema) || !string.IsNullOrEmpty(xmlSchemaCollectionName))
{
EnsureXmlSchemaCollection();
_xmlSchemaCollection.Database = xmlSchemaCollectionDatabase;
_xmlSchemaCollection.OwningSchema = xmlSchemaCollectionOwningSchema;
_xmlSchemaCollection.Name = xmlSchemaCollectionName;
}
}

private SqlParameter(SqlParameter source) : this()
Expand Down Expand Up @@ -404,24 +407,24 @@ public SqlCompareOptions CompareInfo
[ResCategory("XML")]
public string XmlSchemaCollectionDatabase
{
get => _xmlSchemaCollectionDatabase ?? ADP.StrEmpty;
set => _xmlSchemaCollectionDatabase = value;
get => _xmlSchemaCollection?.Database ?? string.Empty;
set => EnsureXmlSchemaCollection().Database = value;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/XmlSchemaCollectionOwningSchema/*' />
[ResCategory("XML")]
public string XmlSchemaCollectionOwningSchema
{
get => _xmlSchemaCollectionOwningSchema ?? ADP.StrEmpty;
set => _xmlSchemaCollectionOwningSchema = value;
get => _xmlSchemaCollection?.OwningSchema ?? string.Empty;
set => EnsureXmlSchemaCollection().OwningSchema = value;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/XmlSchemaCollectionName/*' />
[ResCategory("XML")]
public string XmlSchemaCollectionName
{
get => _xmlSchemaCollectionName ?? ADP.StrEmpty;
set => _xmlSchemaCollectionName = value;
get => _xmlSchemaCollection?.Name ?? string.Empty;
set => EnsureXmlSchemaCollection().Name = value;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/ForceColumnEncryption/*' />
Expand Down Expand Up @@ -452,7 +455,7 @@ public override DbType DbType
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/ParameterName/*' />
public override string ParameterName
{
get => _parameterName ?? ADP.StrEmpty;
get => _parameterName ?? string.Empty;
set
{
if (
Expand Down Expand Up @@ -657,7 +660,7 @@ public object SqlValue
]
public string UdtTypeName
{
get => _udtTypeName ?? ADP.StrEmpty;
get => _udtTypeName ?? string.Empty;
set => _udtTypeName = value;
}

Expand All @@ -668,7 +671,7 @@ public string UdtTypeName
]
public string TypeName
{
get => _typeName ?? ADP.StrEmpty;
get => _typeName ?? string.Empty;
set
{
_typeName = value;
Expand Down Expand Up @@ -725,11 +728,7 @@ public override object Value
]
public override ParameterDirection Direction
{
get
{
ParameterDirection direction = _direction;
return (direction != 0) ? direction : ParameterDirection.Input;
}
get => _direction;
set
{
if (_direction != value)
Expand Down Expand Up @@ -813,7 +812,7 @@ private void ResetSize()
[ResCategory("Update")]
public override string SourceColumn
{
get => _sourceColumn ?? ADP.StrEmpty;
get => _sourceColumn ?? string.Empty;
set => _sourceColumn = value;
}

Expand Down Expand Up @@ -984,9 +983,10 @@ private void CloneHelper(SqlParameter destination)

destination._metaType = _metaType;
destination._collation = _collation;
destination._xmlSchemaCollectionDatabase = _xmlSchemaCollectionDatabase;
destination._xmlSchemaCollectionOwningSchema = _xmlSchemaCollectionOwningSchema;
destination._xmlSchemaCollectionName = _xmlSchemaCollectionName;
if (_xmlSchemaCollection != null)
Wraith2 marked this conversation as resolved.
Show resolved Hide resolved
{
destination.EnsureXmlSchemaCollection().CopyFrom(_xmlSchemaCollection);
}
destination._udtTypeName = _udtTypeName;
destination._typeName = _typeName;
destination._udtLoadError = _udtLoadError;
Expand Down Expand Up @@ -1022,6 +1022,15 @@ internal object CompareExchangeParent(object value, object comparand)
return parent;
}

private SqlMetaDataXmlSchemaCollection EnsureXmlSchemaCollection()
{
if (_xmlSchemaCollection is null)
{
_xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
}
return _xmlSchemaCollection;
}

internal void FixStreamDataForNonPLP()
{
object value = GetCoercedValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ private InstanceDescriptor ConvertToInstanceDescriptor(SqlParameter p)

private MetaType _metaType;
private SqlCollation _collation;
private string _xmlSchemaCollectionDatabase;
private string _xmlSchemaCollectionOwningSchema;
private string _xmlSchemaCollectionName;
private SqlMetaDataXmlSchemaCollection _xmlSchemaCollection;
private string _udtTypeName;
private string _typeName;
private Exception _udtLoadError;
Expand Down Expand Up @@ -227,6 +225,7 @@ public SqlParameter() : base()
{
_isNull = true;
_actualSize = -1;
_direction = ParameterDirection.Input;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/ctorParameterNameDbType/*' />
Expand Down Expand Up @@ -276,16 +275,12 @@ public SqlParameter(
DataRowVersion sourceVersion,
object value
)
: this()
: this(parameterName, dbType, size, sourceColumn)
{
ParameterName = parameterName;
SqlDbType = dbType;
Size = size;
Direction = direction;
IsNullable = isNullable;
PrecisionInternal = precision;
ScaleInternal = scale;
SourceColumn = sourceColumn;
SourceVersion = sourceVersion;
Value = value;
}
Expand Down Expand Up @@ -318,9 +313,13 @@ string xmlSchemaCollectionName
SourceVersion = sourceVersion;
SourceColumnNullMapping = sourceColumnNullMapping;
Value = value;
_xmlSchemaCollectionDatabase = xmlSchemaCollectionDatabase;
_xmlSchemaCollectionOwningSchema = xmlSchemaCollectionOwningSchema;
_xmlSchemaCollectionName = xmlSchemaCollectionName;
if (!string.IsNullOrEmpty(xmlSchemaCollectionDatabase) || !string.IsNullOrEmpty(xmlSchemaCollectionOwningSchema) || !string.IsNullOrEmpty(xmlSchemaCollectionName))
{
EnsureXmlSchemaCollection();
_xmlSchemaCollection.Database = xmlSchemaCollectionDatabase;
_xmlSchemaCollection.OwningSchema = xmlSchemaCollectionOwningSchema;
_xmlSchemaCollection.Name = xmlSchemaCollectionName;
}
}

private SqlParameter(SqlParameter source) : this()
Expand Down Expand Up @@ -391,24 +390,24 @@ public SqlCompareOptions CompareInfo
[ResCategory("XML")]
public string XmlSchemaCollectionDatabase
{
get => _xmlSchemaCollectionDatabase ?? ADP.StrEmpty;
set => _xmlSchemaCollectionDatabase = value;
get => _xmlSchemaCollection?.Database ?? string.Empty;
set => EnsureXmlSchemaCollection().Database = value;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/XmlSchemaCollectionOwningSchema/*' />
[ResCategory("XML")]
public string XmlSchemaCollectionOwningSchema
{
get => _xmlSchemaCollectionOwningSchema ?? ADP.StrEmpty;
set => _xmlSchemaCollectionOwningSchema = value;
get => _xmlSchemaCollection?.OwningSchema ?? string.Empty;
set => EnsureXmlSchemaCollection().OwningSchema = value;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/XmlSchemaCollectionName/*' />
[ResCategory("XML")]
public string XmlSchemaCollectionName
{
get => _xmlSchemaCollectionName ?? ADP.StrEmpty;
set => _xmlSchemaCollectionName = value;
get => _xmlSchemaCollection?.Name ?? string.Empty;
set => EnsureXmlSchemaCollection().Name = value;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/ForceColumnEncryption/*' />
Expand Down Expand Up @@ -439,7 +438,7 @@ public override DbType DbType
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlParameter.xml' path='docs/members[@name="SqlParameter"]/ParameterName/*' />
public override string ParameterName
{
get => _parameterName ?? ADP.StrEmpty;
get => _parameterName ?? string.Empty;
Wraith2 marked this conversation as resolved.
Show resolved Hide resolved
set
{
if (
Expand Down Expand Up @@ -644,7 +643,7 @@ public object SqlValue
]
public string UdtTypeName
{
get => _udtTypeName ?? ADP.StrEmpty;
get => _udtTypeName ?? string.Empty;
set => _udtTypeName = value;
}

Expand All @@ -655,7 +654,7 @@ public string UdtTypeName
]
public string TypeName
{
get => _typeName ?? ADP.StrEmpty;
get => _typeName ?? string.Empty;
set
{
_typeName = value;
Expand Down Expand Up @@ -712,11 +711,7 @@ public override object Value
]
public override ParameterDirection Direction
{
get
{
ParameterDirection direction = _direction;
return (direction != 0) ? direction : ParameterDirection.Input;
}
get => _direction;
set
{
if (_direction != value)
Expand Down Expand Up @@ -800,7 +795,7 @@ private void ResetSize()
[ResCategory("Update")]
public override string SourceColumn
{
get => _sourceColumn ?? ADP.StrEmpty;
get => _sourceColumn ?? string.Empty;
set => _sourceColumn = value;
}

Expand Down Expand Up @@ -975,9 +970,10 @@ private void CloneHelper(SqlParameter destination)

destination._metaType = _metaType;
destination._collation = _collation;
destination._xmlSchemaCollectionDatabase = _xmlSchemaCollectionDatabase;
destination._xmlSchemaCollectionOwningSchema = _xmlSchemaCollectionOwningSchema;
destination._xmlSchemaCollectionName = _xmlSchemaCollectionName;
if (_xmlSchemaCollection != null)
{
destination.EnsureXmlSchemaCollection().CopyFrom(_xmlSchemaCollection);
}
destination._udtTypeName = _udtTypeName;
destination._typeName = _typeName;
destination._udtLoadError = _udtLoadError;
Expand Down Expand Up @@ -1017,6 +1013,15 @@ internal object CompareExchangeParent(object value, object comparand)
return parent;
}

private SqlMetaDataXmlSchemaCollection EnsureXmlSchemaCollection()
{
if (_xmlSchemaCollection is null)
{
_xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection();
}
return _xmlSchemaCollection;
}

internal void FixStreamDataForNonPLP()
{
object value = GetCoercedValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,70 @@ public void XmlSchemaTest()
Assert.Equal(" a ", p1.XmlSchemaCollectionOwningSchema);
}

[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);
}

private enum ByteEnum : byte
{
A = 0x0a,
Expand Down