Skip to content

Commit

Permalink
#873 Added a base class for the DbParameter property value setter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikependon committed Sep 10, 2021
1 parent 41762f9 commit b0940a9
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Data;
using System.Reflection;

namespace RepoDb.Attributes
{
/// <summary>
/// An attribute that is being used to set a value to any property of the <see cref="IDbDataParameter"/> object.
/// </summary>
public class ParameterPropertyValueSetterAttribute : Attribute
{
/// <summary>
/// Creates a new instance of <see cref="ParameterPropertyValueSetterAttribute"/> class.
/// </summary>
/// <param name="parameterType">The type of the parameter.</param>
/// <param name="propertyName">The name of the property to be set.</param>
/// <param name="value">The value to be set.</param>
public ParameterPropertyValueSetterAttribute(Type parameterType,
string propertyName,
object value)
{
ParameterType = parameterType;
PropertyName = propertyName;
Value = value;
}

// Properties

/// <summary>
/// Gets the represented <see cref="Type"/> of the <see cref="IDbDataParameter"/> object.
/// </summary>
public Type ParameterType { get; }

/// <summary>
/// Gets the name of the target property to be set.
/// </summary>
public string PropertyName { get; }

/// <summary>
/// Gets the value that is used to set property.
/// </summary>
public object Value { get; }

// Methods

/// <summary>
/// Gets the instance of the <see cref="PropertyInfo"/> based on the target property name.
/// </summary>
/// <returns></returns>
internal PropertyInfo GetPropertyInfo() =>
ParameterType.GetProperty(PropertyName);

/// <summary>
/// Sets the value of the <see cref="IDbDataParameter"/> object.
/// </summary>
/// <param name="parameter">The instance of the <see cref="IDbDataParameter"/> object.</param>
/// <param name="throwError">
/// Throw an error if the parameter instance is null or the type is different from the
/// <see cref="ParameterType"/> property.
/// </param>
internal void SetValue(IDbDataParameter parameter,
bool throwError = true)
{
if (parameter == null)
{
if (throwError)
{
throw new NullReferenceException("Parameter");
}
return;
}

if (ParameterType != parameter?.GetType())
{
if (throwError)
{
throw new InvalidOperationException($"The instance of the given parameter must be of type '{ParameterType.FullName}'.");
}
return;
}

GetPropertyInfo().SetValue(parameter, Value);
}
}
}
7 changes: 6 additions & 1 deletion RepoDb.Core/RepoDb/StaticType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ internal static class StaticType
public static Type DbParameterCollection => typeof(DbParameterCollection);

/// <summary>
/// Gets a type of the <see cref="System.Data.DbType"/> (array) .NET CLR type.
/// Gets a type of the <see cref="System.Data.DbType"/> .NET CLR type.
/// </summary>
public static Type DbType => typeof(DbType);

Expand Down Expand Up @@ -228,6 +228,11 @@ internal static class StaticType
/// </summary>
public static Type Operation => typeof(Operation);

/// <summary>
/// Gets a type of the <see cref="Attributes.ParameterPropertyValueSetterAttribute"/> .NET CLR type.
/// </summary>
public static Type ParameterPropertyValueSetterAttribute => typeof(ParameterPropertyValueSetterAttribute);

/// <summary>
/// Gets a type of the <see cref="Attributes.PropertyHandlerAttribute"/> .NET CLR type.
/// </summary>
Expand Down
15 changes: 4 additions & 11 deletions RepoDb.MySql/RepoDb.MySql/Attributes/MySqlTypeMapAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ namespace RepoDb.Attributes
/// <summary>
/// An attribute used to define a mapping of .NET CLR <see cref="Type"/> into its equivalent <see cref="MySqlDbType"/> value.
/// </summary>
public class MySqlTypeMapAttribute : Attribute
public class MySqlTypeMapAttribute : ParameterPropertyValueSetterAttribute
{
/// <summary>
/// Creates a new instance of <see cref="MySqlTypeMapAttribute"/> class.
/// </summary>
/// <param name="dbType">A target <see cref="MySqlDbType"/> value.</param>
public MySqlTypeMapAttribute(MySqlDbType dbType)
{
DbType = dbType;
ParameterType = typeof(MySqlParameter);
}
: base(typeof(MySqlParameter), nameof(MySqlParameter.MySqlDbType), dbType)
{ }

/// <summary>
/// Gets a <see cref="MySqlDbType"/> that is currently mapped.
/// </summary>
public MySqlDbType DbType { get; }

/// <summary>
/// Gets the represented <see cref="Type"/> of the <see cref="MySqlDbType"/>.
/// </summary>
public Type ParameterType { get; }
public MySqlDbType DbType => (MySqlDbType)Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ namespace RepoDb.Attributes
/// <summary>
/// An attribute used to define a mapping of .NET CLR <see cref="Type"/> into its equivalent <see cref="MySqlDbType"/> value.
/// </summary>
public class MySqlConnectorTypeMapAttribute : Attribute
public class MySqlConnectorTypeMapAttribute : ParameterPropertyValueSetterAttribute
{
/// <summary>
/// Creates a new instance of <see cref="MySqlConnectorTypeMapAttribute"/> class.
/// </summary>
/// <param name="dbType">A target <see cref="MySqlDbType"/> value.</param>
public MySqlConnectorTypeMapAttribute(MySqlDbType dbType)
{
DbType = dbType;
ParameterType = typeof(MySqlParameter);
}
: base(typeof(MySqlParameter), nameof(MySqlParameter.MySqlDbType), dbType)
{ }

/// <summary>
/// Gets a <see cref="MySqlDbType"/> that is currently mapped.
/// </summary>
public MySqlDbType DbType { get; }

/// <summary>
/// Gets the represented <see cref="Type"/> of the <see cref="MySqlDbType"/>.
/// </summary>
public Type ParameterType { get; }
public MySqlDbType DbType => (MySqlDbType)Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@ namespace RepoDb.Attributes
/// <summary>
/// An attribute used to define a mapping of .NET CLR <see cref="Type"/> into its equivalent <see cref="NpgsqlDbType"/> value.
/// </summary>
public class NpgsqlTypeMapAttribute : Attribute
public class NpgsqlTypeMapAttribute : ParameterPropertyValueSetterAttribute
{
/// <summary>
/// Creates a new instance of <see cref="NpgsqlTypeMapAttribute"/> class.
/// </summary>
/// <param name="dbType">A target <see cref="NpgsqlDbType"/> value.</param>
public NpgsqlTypeMapAttribute(NpgsqlDbType dbType)
{
DbType = dbType;
ParameterType = typeof(NpgsqlParameter);
}
: base(typeof(NpgsqlParameter), nameof(NpgsqlParameter.NpgsqlDbType), dbType)
{ }

/// <summary>
/// Gets a <see cref="NpgsqlDbType"/> that is currently mapped.
/// Gets the actual database type value.
/// </summary>
public NpgsqlDbType DbType { get; }

/// <summary>
/// Gets the represented <see cref="Type"/> of the <see cref="NpgsqlParameter"/>.
/// </summary>
public Type ParameterType { get; }
public NpgsqlDbType DbType => (NpgsqlDbType)Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@ namespace RepoDb.Attributes
/// <summary>
/// An attribute used to define a mapping of .NET CLR <see cref="Type"/> into its equivalent <see cref="SqlDbType"/> value.
/// </summary>
public class MicrosoftSqlServerTypeMapAttribute : Attribute
public class MicrosoftSqlServerTypeMapAttribute : ParameterPropertyValueSetterAttribute
{
/// <summary>
/// Creates a new instance of <see cref="MicrosoftSqlServerTypeMapAttribute"/> class.
/// </summary>
/// <param name="dbType">A target <see cref="SqlDbType"/> value.</param>
public MicrosoftSqlServerTypeMapAttribute(SqlDbType dbType)
{
DbType = dbType;
ParameterType = typeof(SqlParameter);
}
: base(typeof(SqlParameter), nameof(SqlParameter.SqlDbType), dbType)
{ }

/// <summary>
/// Gets a <see cref="SqlDbType"/> that is currently mapped.
/// </summary>
public SqlDbType DbType { get; }

/// <summary>
/// Gets the represented <see cref="Type"/> of the <see cref="SqlParameter"/>.
/// </summary>
public Type ParameterType { get; }
public SqlDbType DbType => (SqlDbType)Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@ namespace RepoDb.Attributes
/// <summary>
/// An attribute used to define a mapping of .NET CLR <see cref="Type"/> into its equivalent <see cref="SqlDbType"/> value.
/// </summary>
public class SystemSqlServerTypeMapAttribute : Attribute
public class SystemSqlServerTypeMapAttribute : ParameterPropertyValueSetterAttribute
{
/// <summary>
/// Creates a new instance of <see cref="SystemSqlServerTypeMapAttribute"/> class.
/// Creates a new instance of <see cref="MicrosoftSqlServerTypeMapAttribute"/> class.
/// </summary>
/// <param name="dbType">A target <see cref="SqlDbType"/> value.</param>
public SystemSqlServerTypeMapAttribute(SqlDbType dbType)
{
DbType = dbType;
ParameterType = typeof(SqlParameter);
}
: base(typeof(SqlParameter), nameof(SqlParameter.SqlDbType), dbType)
{ }

/// <summary>
/// Gets a <see cref="SqlDbType"/> that is currently mapped.
/// </summary>
public SqlDbType DbType { get; }

/// <summary>
/// Gets the represented <see cref="Type"/> of the <see cref="SqlParameter"/>.
/// </summary>
public Type ParameterType { get; }
public SqlDbType DbType => (SqlDbType)Value;
}
}

0 comments on commit b0940a9

Please sign in to comment.