Skip to content

Commit

Permalink
Fixes for the issue #836
Browse files Browse the repository at this point in the history
  • Loading branch information
mikependon committed Aug 28, 2021
1 parent 12a5df4 commit b7b30ab
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 11 deletions.
121 changes: 118 additions & 3 deletions RepoDb.Core/RepoDb/DirectionalQueryField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,98 @@ public DirectionalQueryField(string fieldName,
: this(fieldName,
Operation.Equal,
null,
null,
direction)
{
Type = type;
}

/// <summary>
/// Creates a new instance of <see cref="DirectionalQueryField"/> object.
/// </summary>
/// <param name="fieldName">The name of the field for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(string fieldName,
object value,
ParameterDirection direction)
: this(fieldName,
Operation.Equal,
value,
null,
direction)
{ }

/// <summary>
/// Creates a new instance of <see cref="DirectionalQueryField"/> object.
/// </summary>
/// <param name="fieldName">The name of the field for the query expression.</param>
/// <param name="operation">The operation to be used for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(string fieldName,
Operation operation,
object value,
ParameterDirection direction)
: this(fieldName,
operation,
value,
null,
false,
direction)
{ }

/// <summary>
/// Creates a new instance of <see cref="DirectionalQueryField"/> object.
/// </summary>
/// <param name="field">The actual field for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(Field field,
object value,
ParameterDirection direction)
: this(field,
Operation.Equal,
value,
null,
false,
direction)
{ }

/// <summary>
/// Creates a new instance of <see cref="DirectionalQueryField"/> object.
/// </summary>
/// <param name="field">The actual field for the query expression.</param>
/// <param name="operation">The operation to be used for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(Field field,
Operation operation,
object value,
ParameterDirection direction)
: this(field,
operation,
value,
null,
false,
direction)
{ }

/// <summary>
/// Creates a new instance of <see cref="DirectionalQueryField"/> object.
/// </summary>
/// <param name="fieldName">The name of the field for the query expression.</param>
/// <param name="type">The type of the parameter object.</param>
/// <param name="size">The sizeof the parameter value.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(string fieldName,
Type type,
int? size,
ParameterDirection direction)
: this(fieldName,
Operation.Equal,
null,
size,
direction)
{
Type = type;
Expand All @@ -36,13 +128,16 @@ public DirectionalQueryField(string fieldName,
/// </summary>
/// <param name="fieldName">The name of the field for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="size">The sizeof the parameter value.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(string fieldName,
object value,
int? size,
ParameterDirection direction)
: this(fieldName,
Operation.Equal,
value,
size,
direction)
{ }

Expand All @@ -52,14 +147,17 @@ public DirectionalQueryField(string fieldName,
/// <param name="fieldName">The name of the field for the query expression.</param>
/// <param name="operation">The operation to be used for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="size">The sizeof the parameter value.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(string fieldName,
Operation operation,
object value,
int? size,
ParameterDirection direction)
: this(fieldName,
operation,
value,
size,
false,
direction)
{ }
Expand All @@ -69,13 +167,16 @@ public DirectionalQueryField(string fieldName,
/// </summary>
/// <param name="field">The actual field for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="size">The sizeof the parameter value.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(Field field,
object value,
int? size,
ParameterDirection direction)
: this(field,
Operation.Equal,
value,
size,
false,
direction)
{ }
Expand All @@ -86,14 +187,17 @@ public DirectionalQueryField(Field field,
/// <param name="field">The actual field for the query expression.</param>
/// <param name="operation">The operation to be used for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="size">The sizeof the parameter value.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
public DirectionalQueryField(Field field,
Operation operation,
object value,
int? size,
ParameterDirection direction)
: this(field,
operation,
value,
size,
false,
direction)
{ }
Expand All @@ -104,16 +208,19 @@ public DirectionalQueryField(Field field,
/// <param name="fieldName">The name of the field for the query expression.</param>
/// <param name="operation">The operation to be used for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="size">The sizeof the parameter value.</param>
/// <param name="appendUnderscore">The value to identify whether the underscore prefix will be appended to the parameter name.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
internal DirectionalQueryField(string fieldName,
Operation operation,
object value,
int? size,
bool appendUnderscore,
ParameterDirection direction)
: this(new Field(fieldName),
operation,
value,
size,
appendUnderscore,
direction)
{ }
Expand All @@ -124,15 +231,18 @@ internal DirectionalQueryField(string fieldName,
/// <param name="field">The actual field for the query expression.</param>
/// <param name="operation">The operation to be used for the query expression.</param>
/// <param name="value">The value to be used for the query expression.</param>
/// <param name="size">The sizeof the parameter value.</param>
/// <param name="appendUnderscore">The value to identify whether the underscore prefix will be appended to the parameter name.</param>
/// <param name="direction">The direction to be used for the parameter object.</param>
internal DirectionalQueryField(Field field,
Operation operation,
object value,
int? size,
bool appendUnderscore,
ParameterDirection direction)
: base(field, operation, value, appendUnderscore)
{
Size = size;
Direction = direction;
}

Expand All @@ -145,10 +255,15 @@ internal DirectionalQueryField(Field field,
/// </summary>
public ParameterDirection Direction { get; }

/// <summary>
/// Gets the size of the parameter currently in used.
/// </summary>
public int? Size { get; }

/// <summary>
/// Gets the type of the parameter.
/// </summary>
internal Type Type { get; }
public Type Type { get; }

#endregion

Expand Down Expand Up @@ -185,7 +300,7 @@ public override int GetHashCode()
public override bool Equals(object obj)
{
if (obj is null) return false;

return obj.GetHashCode() == GetHashCode();
}

Expand All @@ -197,7 +312,7 @@ public override bool Equals(object obj)
public bool Equals(DirectionalQueryField other)
{
if (other is null) return false;

return other.GetHashCode() == GetHashCode();
}

Expand Down
81 changes: 73 additions & 8 deletions RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ internal static void CreateParameters(this IDbCommand command,
/// <param name="command">The command object to be used.</param>
/// <param name="name">Entity property's name.</param>
/// <param name="value">Entity property's value, maybe null.</param>
/// <param name="size">The size of the parameter.</param>
/// <param name="classProperty">
/// The entity's class property information. <br />
/// If the parameter is a dictionary, it will be null, otherwise it will not be null.
Expand All @@ -236,6 +237,7 @@ internal static void CreateParameters(this IDbCommand command,
private static IDbDataParameter CreateParameter(IDbCommand command,
string name,
object value,
int? size,
ClassProperty classProperty,
DbField dbField,
ParameterDirection? parameterDirection,
Expand Down Expand Up @@ -314,10 +316,21 @@ private static IDbDataParameter CreateParameter(IDbCommand command,
dbType = Converter.EnumDefaultDatabaseType; // use Converter.EnumDefaultDatabaseType
}

// Create the parameter
var parameter = command.CreateParameter(name, value, dbType, parameterDirection);

// Set the size
var sizeValue = size.HasValue ? size.Value :
dbField != null && dbField.Size.HasValue ? dbField.Size.Value : default;
if (sizeValue > 0)
{
parameter.Size = sizeValue;
}

/*
* Return the parameter
*/
return command.CreateParameter(name, value, dbType, parameterDirection);
return parameter;
}

/// <summary>
Expand Down Expand Up @@ -355,7 +368,15 @@ private static void CreateParametersInternal(IDbCommand command,
var name = classProperty.GetMappedName();
var dbField = GetDbField(name, dbFields);
var value = classProperty.PropertyInfo.GetValue(param);
command.Parameters.Add(CreateParameter(command, name, value, classProperty, dbField, null, null));
var parameter = CreateParameter(command,
name,
value,
dbField?.Size,
classProperty,
dbField,
null,
null);
command.Parameters.Add(parameter);
}
}

Expand Down Expand Up @@ -388,7 +409,15 @@ private static void CreateParameters(IDbCommand command,
dbField ??= GetDbField(commandParameter.Field.Name, dbFields);
classProperty = PropertyCache.Get(commandParameter.MappedToType, commandParameter.Field.Name);
}
command.Parameters.Add(CreateParameter(command, kvp.Key, value, classProperty, dbField, null, null));
var parameter = CreateParameter(command,
kvp.Key,
value,
dbField?.Size,
classProperty,
dbField,
null,
null);
command.Parameters.Add(parameter);
}
}

Expand Down Expand Up @@ -487,8 +516,18 @@ private static void CreateParameters(this IDbCommand command,
var dbField = GetDbField(fieldName, dbFields);
var value = queryField.Parameter.Value;
var classProperty = PropertyCache.Get(entityType, queryField.Field);
var (direction, fallbackType) = queryField is DirectionalQueryField n ? ((ParameterDirection?)n.Direction, n.Type) : default;
var parameter = CreateParameter(command, queryField.Parameter.Name, value, classProperty, dbField, direction, fallbackType);
var (direction, fallbackType, size) = queryField is DirectionalQueryField n ?
((ParameterDirection?)n.Direction, n.Type, n.Size ?? dbField?.Size) : default;

// Create the parameter
var parameter = CreateParameter(command,
queryField.Parameter.Name,
value,
size,
classProperty,
dbField,
direction,
fallbackType);
command.Parameters.Add(parameter);

// Set the parameter
Expand All @@ -513,7 +552,15 @@ private static void CreateParametersForInOperation(this IDbCommand command,
for (var i = 0; i < values.Count; i++)
{
var name = string.Concat(queryField.Parameter.Name, "_In_", i.ToString());
command.Parameters.Add(CreateParameter(command, name, values[i], null, dbField, null, null));
var parameter = CreateParameter(command,
name,
values[i],
dbField?.Size,
null,
dbField,
null,
null);
command.Parameters.Add(parameter);
}
}
}
Expand All @@ -533,8 +580,26 @@ private static void CreateParametersForBetweenOperation(this IDbCommand command,
.AsList();
if (values?.Count == 2)
{
command.Parameters.Add(CreateParameter(command, string.Concat(queryField.Parameter.Name, "_Left"), values[0], null, dbField, null, null));
command.Parameters.Add(CreateParameter(command, string.Concat(queryField.Parameter.Name, "_Right"), values[1], null, dbField, null, null));
// Left
var leftParameter = CreateParameter(command,
string.Concat(queryField.Parameter.Name, "_Left"),
values[0],
dbField?.Size,
null, dbField,
null,
null);
command.Parameters.Add(leftParameter);

// Right
var rightParameter = CreateParameter(command,
string.Concat(queryField.Parameter.Name, "_Right"),
values[1],
dbField?.Size,
null,
dbField,
null,
null);
command.Parameters.Add(rightParameter);
}
else
{
Expand Down

0 comments on commit b7b30ab

Please sign in to comment.