Skip to content

Commit

Permalink
parameter factory type processing
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilLord666 committed May 11, 2022
1 parent 89ff7da commit 816ae81
Showing 1 changed file with 81 additions and 27 deletions.
108 changes: 81 additions & 27 deletions DbTools/DbTools.Simple/Factories/DbParameterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace DbTools.Simple.Factories
public static class DbParameterFactory
{
public static DbParameter Create(DbEngine dbEngine, string parameterName, int parameterType, object value,
ParameterDirection parameterDirection = ParameterDirection.Input)
ParameterDirection parameterDirection = ParameterDirection.Input)
{
if (dbEngine == DbEngine.SqlServer)
{
Expand All @@ -40,7 +40,8 @@ public static DbParameter Create(DbEngine dbEngine, string parameterName, int pa
NpgsqlDbType type = (NpgsqlDbType)parameterType;
return new NpgsqlParameter(parameterName, type)
{
Direction = parameterDirection
Direction = parameterDirection,
Value = MapPostgresParameterValue(type, value)
};
}

Expand Down Expand Up @@ -75,31 +76,45 @@ private static object MapMySqlParameterValue(MySqlDbType type, object value)
}
}

private static object MapPostgresParameterValue(NpgsqlDbType type, object value)
{
try
{
if (!PostgresTypesMapping.ContainsKey(type))
return value;
return Convert.ChangeType(value, PostgresTypesMapping[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(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(Int16)},
{ SqlDbType.SmallMoney, typeof(decimal)},
{ SqlDbType.Text, typeof(string)},
{ SqlDbType.Timestamp, typeof(byte[])},
{ SqlDbType.UniqueIdentifier, typeof(Guid)},
{ SqlDbType.VarBinary, typeof(byte[])},
{ SqlDbType.VarChar, typeof(string)}
{ 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(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(Int16) },
{ 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>()
Expand All @@ -116,8 +131,47 @@ private static object MapMySqlParameterValue(MySqlDbType type, object value)
{ MySqlDbType.Date, typeof(DateTime) },
{ MySqlDbType.Time, typeof(string) },
{ MySqlDbType.DateTime, typeof(DateTime) },
{ MySqlDbType.Datetime, typeof(DateTime) }
{ MySqlDbType.Datetime, typeof(DateTime) },
{ MySqlDbType.Year, typeof(string) },
{ MySqlDbType.Newdate, typeof(DateTime) },
{ MySqlDbType.Year, typeof(string) },
{ MySqlDbType.VarString, typeof(string) },
{ MySqlDbType.NewDecimal, typeof(decimal) },
{ MySqlDbType.TinyBlob, typeof(byte[]) },
{ MySqlDbType.MediumBlob, typeof(byte[]) },
{ MySqlDbType.LongBlob, typeof(byte[]) },
{ MySqlDbType.Blob, typeof(byte[]) },
{ MySqlDbType.VarChar, typeof(string) },
{ MySqlDbType.String, typeof(string) },
{ MySqlDbType.UByte, typeof(byte) },
{ MySqlDbType.UInt16, typeof(UInt16) },
{ MySqlDbType.UInt24, typeof(UInt32) },
{ MySqlDbType.UInt32, typeof(UInt32) },
{ MySqlDbType.UInt64, typeof(UInt64) },
{ MySqlDbType.Binary, typeof(byte[]) },
{ MySqlDbType.VarBinary, typeof(byte[]) },
{ MySqlDbType.TinyText, typeof(string) },
{ MySqlDbType.MediumText, typeof(string) },
{ MySqlDbType.LongText, typeof(string) },
{ MySqlDbType.Text, typeof(string) }
};

private static readonly IDictionary<NpgsqlDbType, Type> PostgresTypesMapping = new Dictionary<NpgsqlDbType, Type>()
{
{ NpgsqlDbType.Bigint, typeof(long) },
{ NpgsqlDbType.Bit, typeof(string) },
{ NpgsqlDbType.Boolean, typeof(bool) },
{ NpgsqlDbType.Bytea, typeof(byte[]) },
{ NpgsqlDbType.Char, typeof(string) },
{ NpgsqlDbType.Date, typeof(DateTime) },
{ NpgsqlDbType.Timestamp, typeof(DateTime) },
{ NpgsqlDbType.Double, typeof(double) },
{ NpgsqlDbType.Integer, typeof(int) },
{ NpgsqlDbType.Text, typeof(string) },
{ NpgsqlDbType.Integer, typeof(int) },
{ NpgsqlDbType.Varchar, typeof(string) },
{ NpgsqlDbType.Real, typeof(double) },
{ NpgsqlDbType.Uuid, typeof(string) }
};
};
}
}

0 comments on commit 816ae81

Please sign in to comment.