static internal MetaType GetMetaTypeFromValue(object value, bool inferLen = true, bool streamAllowed = true)
{
if (value == null)
{
throw ADP.InvalidDataType("null");
}
if (value is DBNull)
{
throw ADP.InvalidDataType(nameof(DBNull));
}
Type dataType = value.GetType();
switch (Convert.GetTypeCode(value))
{
case TypeCode.Empty:
throw ADP.InvalidDataType(nameof(TypeCode.Empty));
case TypeCode.Object:
if (dataType == typeof (System.Byte[]))
{
if (!inferLen || ((byte[]) value).Length <= TdsEnums.TYPE_SIZE_LIMIT)
{
return MetaVarBinary;
}
else
{
return MetaImage;
}
}
if (dataType == typeof (System.Guid))
{
return s_metaUniqueId;
}
if (dataType == typeof (System.Object))
{
return s_metaVariant;
}
// check sql types now
if (dataType == typeof (SqlBinary))
return MetaVarBinary;
if (dataType == typeof (SqlBoolean))
return s_metaBit;
if (dataType == typeof (SqlByte))
return s_metaTinyInt;
if (dataType == typeof (SqlBytes))
return MetaVarBinary;
if (dataType == typeof (SqlChars))
return MetaNVarChar;
if (dataType == typeof (SqlDateTime))
return s_metaDateTime;
if (dataType == typeof (SqlDouble))
return s_metaFloat;
if (dataType == typeof (SqlGuid))
return s_metaUniqueId;
if (dataType == typeof (SqlInt16))
return s_metaSmallInt;
if (dataType == typeof (SqlInt32))
return s_metaInt;
if (dataType == typeof (SqlInt64))
return s_metaBigInt;
if (dataType == typeof (SqlMoney))
return s_metaMoney;
if (dataType == typeof (SqlDecimal))
return MetaDecimal;
if (dataType == typeof (SqlSingle))
return s_metaReal;
if (dataType == typeof (SqlXml))
return MetaXml;
if (dataType == typeof (SqlString))
{
return ((inferLen && !((SqlString) value).IsNull)
? PromoteStringType(((SqlString) value).Value)
: MetaNVarChar);
}
if (dataType == typeof (IEnumerable<DbDataRecord>) || dataType == typeof (DataTable))
{
return s_metaTable;
}
if (dataType == typeof (TimeSpan))
{
return MetaTime;
}
if (dataType == typeof (DateTimeOffset))
{
return MetaDateTimeOffset;
}
if (streamAllowed)
{
// Derived from Stream ?
if (value is Stream)
{
return MetaVarBinary;
}
// Derived from TextReader ?
if (value is TextReader)
{
return MetaNVarChar;
}
// Derived from XmlReader ?
if (value is XmlReader)
{
return MetaXml;
}
}
throw ADP.UnknownDataType(dataType);
case TypeCode.Boolean:
return s_metaBit;
case TypeCode.Char:
throw ADP.InvalidDataType(nameof(TypeCode.Char));
case TypeCode.SByte:
throw ADP.InvalidDataType(nameof(TypeCode.SByte));
case TypeCode.Byte:
return s_metaTinyInt;
case TypeCode.Int16:
return s_metaSmallInt;
case TypeCode.UInt16:
throw ADP.InvalidDataType(nameof(TypeCode.UInt16));
case TypeCode.Int32:
return s_metaInt;
case TypeCode.UInt32:
throw ADP.InvalidDataType(nameof(TypeCode.UInt32));
case TypeCode.Int64:
return s_metaBigInt;
case TypeCode.UInt64:
throw ADP.InvalidDataType(nameof(TypeCode.UInt64));
case TypeCode.Single:
return s_metaReal;
case TypeCode.Double:
return s_metaFloat;
case TypeCode.Decimal:
return MetaDecimal;
case TypeCode.DateTime:
return s_metaDateTime;
case TypeCode.String:
return (inferLen ? PromoteStringType((string) value) : MetaNVarChar);
default:
throw ADP.UnknownDataType(dataType);
}
}