public override void Commit() { _storage.UseTransaction(connection => { foreach (var command in _commandQueue) { command(connection); } }); }
public override void SetRangeInHash(string key, IEnumerable <KeyValuePair <string, string> > keyValuePairs) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (keyValuePairs == null) { throw new ArgumentNullException(nameof(keyValuePairs)); } // const string sql = @" //;merge [HangFire.Hash] with (holdlock) as Target //using (VALUES (@key, @field, @value)) as Source ([Key], Field, Value) //on Target.[Key] = Source.[Key] and Target.Field = Source.Field //when matched then update set Value = Source.Value //when not matched then insert ([Key], Field, Value) values (Source.[Key], Source.Field, Source.Value);"; _storage.UseTransaction((connection, transaction) => { string tableName = $"[{_storage.SchemaName}.Hash]"; var selectSqlStr = $"select * from {tableName} where [Key] = @key and Field = @field"; var insertSqlStr = $"insert into {tableName} ([Key], Field, Value) values (@key, @field, @value)"; var updateSqlStr = $"update {tableName} set Value = @value where [Key] = @key and Field = @field"; foreach (var keyValuePair in keyValuePairs) { var fetchedHash = connection.Query <SqlHash>(selectSqlStr, new { key = key, field = keyValuePair.Key }, transaction); if (!fetchedHash.Any()) { connection.Execute(insertSqlStr, new { key = key, field = keyValuePair.Key, value = keyValuePair.Value }, transaction); } else { connection.Execute(updateSqlStr, new { key = key, field = keyValuePair.Key, value = keyValuePair.Value }, transaction); } } }); }