// If its a SQL Type or Nullable UDT
object rawValue = GetSqlValueFromSqlBufferInternal(data, metaData);
- // Special case: User wants SqlString, but we have a SqlXml
- // SqlXml can not be typecast into a SqlString, but we need to support SqlString on XML Types - so do a manual conversion
if (typeofT == s_typeofSqlString)
{
+ // Special case: User wants SqlString, but we have a SqlXml
+ // SqlXml can not be typecast into a SqlString, but we need to support SqlString on XML Types - so do a manual conversion
SqlXml xmlValue = rawValue as SqlXml;
if (xmlValue != null)
{
}
else
{
- // Otherwise Its a CLR or non-Nullable UDT
- try
+ if (typeof(XmlReader) == typeofT)
{
- return (T)GetValueFromSqlBufferInternal(data, metaData);
+ if (metaData.metaType.SqlDbType != SqlDbType.Xml)
+ {
+ throw SQL.XmlReaderNotSupportOnColumnType(metaData.column);
+ }
+ else
+ {
+ object clrValue = null;
+ if (!data.IsNull)
+ {
+ clrValue = GetValueFromSqlBufferInternal(data, metaData);
+ }
+ if (clrValue is null) // covers IsNull and when there is data which is present but is a clr null somehow
+ {
+ return (T)(object)SqlTypeWorkarounds.SqlXmlCreateSqlXmlReader(
+ new MemoryStream(Array.Empty<byte>(), writable: false),
+ closeInput: true
+ );
+ }
+ else if (clrValue.GetType() == typeof(string))
+ {
+ return (T)(object)SqlTypeWorkarounds.SqlXmlCreateSqlXmlReader(
+ new StringReader(clrValue as string),
+ closeInput: true
+ );
+ }
+ else
+ {
+ // try the type cast to throw the invalid cast exception and inform the user what types they're trying to use and that why it is wrong
+ return (T)clrValue;
+ }
+ }
}
- catch (InvalidCastException)
+ else
{
- if (data.IsNull)
+ try
{
- // If the value was actually null, then we should throw a SqlNullValue instead
- throw SQL.SqlNullValue();
+ return (T)GetValueFromSqlBufferInternal(data, metaData);
}
- else
+ catch (InvalidCastException)
{
- // Legitimate InvalidCast, rethrow
- throw;
+ if (data.IsNull)
+ {
+ // If the value was actually null, then we should throw a SqlNullValue instead
+ throw SQL.SqlNullValue();
+ }
+ else
+ {
+ // Legitimate InvalidCast, rethrow
+ throw;
+ }
}
}
}
return XmlReader.Create(stream, settingsToUse);
}
+
+ internal static XmlReader SqlXmlCreateSqlXmlReader(TextReader textReader, bool closeInput = false, bool async = false)
+ {
+ Debug.Assert(closeInput || !async, "Currently we do not have pre-created settings for !closeInput+async");
+
+ XmlReaderSettings settingsToUse = closeInput ?
+ (async ? s_defaultXmlReaderSettingsAsyncCloseInput : s_defaultXmlReaderSettingsCloseInput) :
+ s_defaultXmlReaderSettings;
+
+ return XmlReader.Create(textReader, settingsToUse);
+ }
#endregion
#region Work around inability to access SqlDateTime.ToDateTime