227 lines
6.8 KiB
Plaintext
227 lines
6.8 KiB
Plaintext
using System;
|
|
using System.Collections;
|
|
using System.Data;
|
|
|
|
namespace DTS.Common.Storage
|
|
{
|
|
/// <summary>
|
|
/// Convert a base data type to another base data type
|
|
/// </summary>
|
|
public sealed class TypeConvertor
|
|
{
|
|
|
|
private struct DbTypeMapEntry
|
|
{
|
|
public Type Type;
|
|
public DbType DbType;
|
|
public SqlDbType SqlDbType;
|
|
|
|
public DbTypeMapEntry(Type type, DbType dbType, SqlDbType sqlDbType)
|
|
{
|
|
Type = type;
|
|
DbType = dbType;
|
|
SqlDbType = sqlDbType;
|
|
}
|
|
|
|
};
|
|
|
|
private static ArrayList _DbTypeList = new ArrayList();
|
|
|
|
#region Constructors
|
|
|
|
static TypeConvertor()
|
|
{
|
|
var dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(bool), DbType.Boolean, SqlDbType.Bit);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(byte), DbType.Byte, SqlDbType.TinyInt);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(byte[]), DbType.Binary, SqlDbType.Image);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(DateTime), DbType.DateTime, SqlDbType.DateTime);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(decimal), DbType.Decimal, SqlDbType.Decimal);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(double), DbType.Double, SqlDbType.Float);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(Guid), DbType.Guid, SqlDbType.UniqueIdentifier);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(short), DbType.Int16, SqlDbType.SmallInt);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(int), DbType.Int32, SqlDbType.Int);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(long), DbType.Int64, SqlDbType.BigInt);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(object), DbType.Object, SqlDbType.Variant);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry
|
|
= new DbTypeMapEntry(typeof(string), DbType.String, SqlDbType.NVarChar);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
|
|
dbTypeMapEntry = new DbTypeMapEntry(typeof(TimeSpan), DbType.Time, SqlDbType.Time); _DbTypeList.Add(dbTypeMapEntry);
|
|
_DbTypeList.Add(dbTypeMapEntry);
|
|
}
|
|
|
|
private TypeConvertor()
|
|
{
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Convert db type to .Net data type
|
|
/// </summary>
|
|
/// <param name="dbType"></param>
|
|
/// <returns></returns>
|
|
public static Type ToNetType(DbType dbType)
|
|
{
|
|
var entry = Find(dbType);
|
|
return entry.Type;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert TSQL type to .Net data type
|
|
/// </summary>
|
|
/// <param name="sqlDbType"></param>
|
|
/// <returns></returns>
|
|
public static Type ToNetType(SqlDbType sqlDbType)
|
|
{
|
|
var entry = Find(sqlDbType);
|
|
return entry.Type;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert .Net type to Db type
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
public static DbType ToDbType(Type type)
|
|
{
|
|
var entry = Find(type);
|
|
return entry.DbType;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert TSQL data type to DbType
|
|
/// </summary>
|
|
/// <param name="sqlDbType"></param>
|
|
/// <returns></returns>
|
|
public static DbType ToDbType(SqlDbType sqlDbType)
|
|
{
|
|
var entry = Find(sqlDbType);
|
|
return entry.DbType;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert .Net type to TSQL data type
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
public static SqlDbType ToSqlDbType(Type type)
|
|
{
|
|
var entry = Find(type);
|
|
return entry.SqlDbType;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert DbType type to TSQL data type
|
|
/// </summary>
|
|
/// <param name="dbType"></param>
|
|
/// <returns></returns>
|
|
public static SqlDbType ToSqlDbType(DbType dbType)
|
|
{
|
|
var entry = Find(dbType);
|
|
return entry.SqlDbType;
|
|
}
|
|
|
|
private static DbTypeMapEntry Find(Type type)
|
|
{
|
|
object retObj = null;
|
|
for (var i = 0; i < _DbTypeList.Count; i++)
|
|
{
|
|
var entry = (DbTypeMapEntry)_DbTypeList[i];
|
|
if (entry.Type == (Nullable.GetUnderlyingType(type) ?? type))
|
|
{
|
|
retObj = entry;
|
|
break;
|
|
}
|
|
}
|
|
if (retObj == null)
|
|
{
|
|
throw
|
|
new ApplicationException("Referenced an unsupported Type");
|
|
}
|
|
|
|
return (DbTypeMapEntry)retObj;
|
|
}
|
|
|
|
private static DbTypeMapEntry Find(DbType dbType)
|
|
{
|
|
object retObj = null;
|
|
for (var i = 0; i < _DbTypeList.Count; i++)
|
|
{
|
|
var entry = (DbTypeMapEntry)_DbTypeList[i];
|
|
if (entry.DbType == dbType)
|
|
{
|
|
retObj = entry;
|
|
break;
|
|
}
|
|
}
|
|
if (retObj == null)
|
|
{
|
|
throw
|
|
new ApplicationException("Referenced an unsupported DbType");
|
|
}
|
|
|
|
return (DbTypeMapEntry)retObj;
|
|
}
|
|
|
|
private static DbTypeMapEntry Find(SqlDbType sqlDbType)
|
|
{
|
|
object retObj = null;
|
|
for (var i = 0; i < _DbTypeList.Count; i++)
|
|
{
|
|
var entry = (DbTypeMapEntry)_DbTypeList[i];
|
|
if (entry.SqlDbType == sqlDbType)
|
|
{
|
|
retObj = entry;
|
|
break;
|
|
}
|
|
}
|
|
if (retObj == null)
|
|
{
|
|
throw
|
|
new ApplicationException("Referenced an unsupported SqlDbType");
|
|
}
|
|
|
|
return (DbTypeMapEntry)retObj;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|