Skip to content

Commit

Permalink
intermediate
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilLord666 committed May 10, 2022
1 parent 3120ebf commit db44de9
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions DbTools/DbTools.Simple/Factories/DbParameterFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using DbTools.Core;
Expand All @@ -11,14 +12,19 @@ namespace DbTools.Simple.Factories
{
public static class DbParameterFactory
{
public static DbParameter Create(DbEngine dbEngine, string parameterName, int parameterType,
public static DbParameter Create(DbEngine dbEngine, string parameterName, int parameterType, object value,
ParameterDirection parameterDirection = ParameterDirection.Input)
{
if (dbEngine == DbEngine.SqlServer)
return new SqlParameter(parameterName, (SqlDbType)parameterType)
{
SqlDbType type = (SqlDbType)parameterType;
return new SqlParameter(parameterName, type)
{
Direction = parameterDirection
Direction = parameterDirection,
Value = MapSqlParameterValue(type, value)
};
}

if (dbEngine == DbEngine.MySql)
return new MySqlParameter(parameterName, (MySqlDbType)parameterType)
{
Expand All @@ -31,5 +37,31 @@ public static DbParameter Create(DbEngine dbEngine, string parameterName, int pa
};
throw new NotImplementedException("Other db engine are not supported yet, please add a github issue https://github.com/EvilLord666/DbTools");
}

private static object MapSqlParameterValue(SqlDbType type, object value)
{
try
{
if (!SqlServerTypesMapping.ContainsKey(type))
return value;
return Convert.ChangeType(value, SqlServerTypesMapping[type]);
}
catch (Exception e)
{
return value;
}
}

private static readonly IDictionary<SqlDbType, Type> SqlServerTypesMapping = new Dictionary<SqlDbType, Type>()
{
{ SqlDbType.BigInt, typeof(long)},
{ SqlDbType.Binary, typeof(byte[])},
{ SqlDbType.Bit, typeof(bool)},
{ SqlDbType.Char, typeof(string)},
{ SqlDbType.DateTime, typeof(DateTime)},
{ SqlDbType.DateTimeOffset, typeof(DateTimeOffset)},
{ SqlDbType.Decimal, typeof(decimal)},
{ SqlDbType.Float, typeof(DateTime)}
};
}
}

0 comments on commit db44de9

Please sign in to comment.