Skip to content

Commit

Permalink
Use new parameters API in Connection.CreateExpiredJob
Browse files Browse the repository at this point in the history
  • Loading branch information
odinserj committed Jun 24, 2024
1 parent eef4acc commit 18e5935
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
5 changes: 2 additions & 3 deletions src/Hangfire.SqlServer/SqlCommandBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,17 @@ public static DbCommand Create(
public static DbCommand AddParameter(
[NotNull] this DbCommand command,
[NotNull] string parameterName,
[NotNull] object value,
[CanBeNull] object value,
DbType dbType,
[CanBeNull] int? size = null)
{
if (command == null) throw new ArgumentNullException(nameof(command));
if (parameterName == null) throw new ArgumentNullException(nameof(parameterName));
if (value == null) throw new ArgumentNullException(nameof(value));

var parameter = command.CreateParameter();
parameter.ParameterName = parameterName;
parameter.DbType = dbType;
parameter.Value = value;
parameter.Value = value ?? DBNull.Value;

if (size.HasValue) parameter.Size = size.Value;

Expand Down
82 changes: 48 additions & 34 deletions src/Hangfire.SqlServer/SqlServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ output inserted.Id
var invocationData = InvocationData.SerializeJob(job);
var payload = invocationData.SerializePayload(excludeArguments: true);

var queryParameters = new DynamicParameters();
queryParameters.Add("@invocationData", payload, DbType.String, size: -1);
queryParameters.Add("@arguments", invocationData.Arguments, DbType.String, size: -1);
queryParameters.Add("@createdAt", createdAt, DbType.DateTime);
queryParameters.Add("@expireAt", createdAt.Add(expireIn), DbType.DateTime);
Action<DbCommand> queryParameters = cmd => cmd
.AddParameter("@invocationData", payload, DbType.String, size: -1)
.AddParameter("@arguments", invocationData.Arguments, DbType.String, size: -1)
.AddParameter("@createdAt", createdAt, DbType.DateTime)
.AddParameter("@expireAt", createdAt.Add(expireIn), DbType.DateTime);

Action<DbCommand> additionalParameters = null;

var parametersArray = parameters.ToArray();

Expand All @@ -122,8 +124,9 @@ output inserted.Id
select @jobId = scope_identity(); select @jobId;
insert into [{schemaName}].JobParameter (JobId, Name, Value) values (@jobId, @name, @value);
commit tran;");
queryParameters.Add("@name", parametersArray[0].Key, DbType.String, size: 40);
queryParameters.Add("@value", parametersArray[0].Value, DbType.String, size: -1);
additionalParameters = cmd => cmd
.AddParameter("@name", parametersArray[0].Key, DbType.String, size: 40)
.AddParameter("@value", parametersArray[0].Value, DbType.String, size: -1);
}
else if (parametersArray.Length == 2)
{
Expand All @@ -134,10 +137,11 @@ output inserted.Id
select @jobId = scope_identity(); select @jobId;
insert into [{schemaName}].JobParameter (JobId, Name, Value) values (@jobId, @name1, @value1), (@jobId, @name2, @value2);
commit tran;");
queryParameters.Add("@name1", parametersArray[0].Key, DbType.String, size: 40);
queryParameters.Add("@value1", parametersArray[0].Value, DbType.String, size: -1);
queryParameters.Add("@name2", parametersArray[1].Key, DbType.String, size: 40);
queryParameters.Add("@value2", parametersArray[1].Value, DbType.String, size: -1);
additionalParameters = cmd => cmd
.AddParameter("@name1", parametersArray[0].Key, DbType.String, size: 40)
.AddParameter("@value1", parametersArray[0].Value, DbType.String, size: -1)
.AddParameter("@name2", parametersArray[1].Key, DbType.String, size: 40)
.AddParameter("@value2", parametersArray[1].Value, DbType.String, size: -1);
}
else if (parametersArray.Length == 3)
{
Expand All @@ -148,12 +152,13 @@ output inserted.Id
select @jobId = scope_identity(); select @jobId;
insert into [{schemaName}].JobParameter (JobId, Name, Value) values (@jobId, @name1, @value1), (@jobId, @name2, @value2), (@jobId, @name3, @value3);
commit tran;");
queryParameters.Add("@name1", parametersArray[0].Key, DbType.String, size: 40);
queryParameters.Add("@value1", parametersArray[0].Value, DbType.String, size: -1);
queryParameters.Add("@name2", parametersArray[1].Key, DbType.String, size: 40);
queryParameters.Add("@value2", parametersArray[1].Value, DbType.String, size: -1);
queryParameters.Add("@name3", parametersArray[2].Key, DbType.String, size: 40);
queryParameters.Add("@value3", parametersArray[2].Value, DbType.String, size: -1);
additionalParameters = cmd => cmd
.AddParameter("@name1", parametersArray[0].Key, DbType.String, size: 40)
.AddParameter("@value1", parametersArray[0].Value, DbType.String, size: -1)
.AddParameter("@name2", parametersArray[1].Key, DbType.String, size: 40)
.AddParameter("@value2", parametersArray[1].Value, DbType.String, size: -1)
.AddParameter("@name3", parametersArray[2].Key, DbType.String, size: 40)
.AddParameter("@value3", parametersArray[2].Value, DbType.String, size: -1);
}
else if (parametersArray.Length == 4)
{
Expand All @@ -164,29 +169,38 @@ output inserted.Id
select @jobId = scope_identity(); select @jobId;
insert into [{schemaName}].JobParameter (JobId, Name, Value) values (@jobId, @name1, @value1), (@jobId, @name2, @value2), (@jobId, @name3, @value3), (@jobId, @name4, @value4);
commit tran;");
queryParameters.Add("@name1", parametersArray[0].Key, DbType.String, size: 40);
queryParameters.Add("@value1", parametersArray[0].Value, DbType.String, size: -1);
queryParameters.Add("@name2", parametersArray[1].Key, DbType.String, size: 40);
queryParameters.Add("@value2", parametersArray[1].Value, DbType.String, size: -1);
queryParameters.Add("@name3", parametersArray[2].Key, DbType.String, size: 40);
queryParameters.Add("@value3", parametersArray[2].Value, DbType.String, size: -1);
queryParameters.Add("@name4", parametersArray[3].Key, DbType.String, size: 40);
queryParameters.Add("@value4", parametersArray[3].Value, DbType.String, size: -1);
additionalParameters = cmd => cmd
.AddParameter("@name1", parametersArray[0].Key, DbType.String, size: 40)
.AddParameter("@value1", parametersArray[0].Value, DbType.String, size: -1)
.AddParameter("@name2", parametersArray[1].Key, DbType.String, size: 40)
.AddParameter("@value2", parametersArray[1].Value, DbType.String, size: -1)
.AddParameter("@name3", parametersArray[2].Key, DbType.String, size: 40)
.AddParameter("@value3", parametersArray[2].Value, DbType.String, size: -1)
.AddParameter("@name4", parametersArray[3].Key, DbType.String, size: 40)
.AddParameter("@value4", parametersArray[3].Value, DbType.String, size: -1);
}

return _storage.UseConnection(_dedicatedConnection, static (storage, connection, ctx) => connection
.ExecuteScalar<long>(ctx.Key, ctx.Value, commandTimeout: storage.CommandTimeout)
.ToString(CultureInfo.InvariantCulture),
new KeyValuePair<string, DynamicParameters>(queryString, queryParameters));
return _storage.UseConnection(_dedicatedConnection, static (storage, connection, ctx) =>
{
using var command = connection.Create(ctx.Key, timeout: storage.CommandTimeout);
ctx.Value.Key(command);
ctx.Value.Value?.Invoke(command);

return ((long)command.ExecuteScalar()).ToString(CultureInfo.InvariantCulture);
},
new KeyValuePair<string, KeyValuePair<Action<DbCommand>, Action<DbCommand>>>(
queryString,
new KeyValuePair<Action<DbCommand>, Action<DbCommand>>(queryParameters, additionalParameters)));
}

return _storage.UseTransaction(_dedicatedConnection, static (storage, connection, transaction, triple) =>
{
var jobId = connection.ExecuteScalar<long>(
triple.Item1,
triple.Item2,
transaction,
commandTimeout: storage.CommandTimeout).ToString(CultureInfo.InvariantCulture);
using var jobCommand = connection.Create(triple.Item1, timeout: storage.CommandTimeout);
triple.Item2(jobCommand);

jobCommand.Transaction = transaction;

var jobId = ((long)jobCommand.ExecuteScalar()).ToString(CultureInfo.InvariantCulture);

var query = storage.GetQueryFromTemplate(static schemaName =>
$@"insert into [{schemaName}].JobParameter (JobId, Name, Value) values (@jobId, @name, @value)");
Expand Down

0 comments on commit 18e5935

Please sign in to comment.