Skip to content

Commit

Permalink
moving on proper stored proc value creation
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilLord666 committed May 10, 2022
1 parent db44de9 commit d169ad3
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions DbTools/DbTools.Simple/Factories/DbParameterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ public static DbParameter Create(DbEngine dbEngine, string parameterName, int pa
}

if (dbEngine == DbEngine.MySql)
return new MySqlParameter(parameterName, (MySqlDbType)parameterType)
{
MySqlDbType type = (MySqlDbType)parameterType;
return new MySqlParameter(parameterName, type)
{
Direction = parameterDirection
Direction = parameterDirection,
Value = MapMySqlParameterValue(type, value)
};
}

if (dbEngine == DbEngine.PostgresSql)
return new NpgsqlParameter(parameterName, (NpgsqlDbType) parameterType)
{
NpgsqlDbType type = (NpgsqlDbType)parameterType;
return new NpgsqlParameter(parameterName, type)
{
Direction = parameterDirection
};
}

throw new NotImplementedException("Other db engine are not supported yet, please add a github issue https://github.com/EvilLord666/DbTools");
}

Expand All @@ -52,6 +61,20 @@ private static object MapSqlParameterValue(SqlDbType type, object value)
}
}

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

private static readonly IDictionary<SqlDbType, Type> SqlServerTypesMapping = new Dictionary<SqlDbType, Type>()
{
{ SqlDbType.BigInt, typeof(long)},
Expand All @@ -61,7 +84,27 @@ private static object MapSqlParameterValue(SqlDbType type, object value)
{ SqlDbType.DateTime, typeof(DateTime)},
{ SqlDbType.DateTimeOffset, typeof(DateTimeOffset)},
{ SqlDbType.Decimal, typeof(decimal)},
{ SqlDbType.Float, typeof(DateTime)}
{ SqlDbType.Float, typeof(float)},
{ SqlDbType.Image, typeof(byte[])},
{ SqlDbType.Int, typeof(int)},
{ SqlDbType.Money, typeof(decimal)},
{ SqlDbType.NChar, typeof(string)},
{ SqlDbType.NText, typeof(string)},
{ SqlDbType.NVarChar, typeof(string)},
{ SqlDbType.Real, typeof(Single)},
{ SqlDbType.SmallDateTime, typeof(DateTime)},
{ SqlDbType.SmallInt, typeof(int)},
{ SqlDbType.SmallMoney, typeof(decimal)},
{ SqlDbType.Text, typeof(string)},
{ SqlDbType.Timestamp, typeof(byte[])},
{ SqlDbType.UniqueIdentifier, typeof(Guid)},
{ SqlDbType.VarBinary, typeof(byte[])},
{ SqlDbType.VarChar, typeof(string)}
};

private static readonly IDictionary<MySqlDbType, Type> MySqlTypesMapping = new Dictionary<MySqlDbType, Type>()
{
{ MySqlDbType.Decimal, typeof(decimal) }
};
}
}

0 comments on commit d169ad3

Please sign in to comment.