Skip to content

Commit

Permalink
Fixes command store Get where ContextKey in null
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Henderson committed Feb 22, 2019
1 parent 288238e commit 6245248
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/Paramore.Brighter.CommandStore.MsSql/MsSqlCommandStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public T Get<T>(Guid id, string contextKey, int timeoutInMilliseconds = -1) wher
CreateSqlParameter("ContextKey", contextKey)
};

return ExecuteCommand(command => ReadCommand<T>(command.ExecuteReader(), id), sql, timeoutInMilliseconds,
parameters);
return ExecuteCommand(command => ReadCommand<T>(command.ExecuteReader(), id), sql, timeoutInMilliseconds, parameters);
}

/// <summary>
Expand All @@ -129,8 +128,7 @@ public bool Exists<T>(Guid id, string contextKey, int timeoutInMilliseconds = -1
CreateSqlParameter("ContextKey", contextKey)
};

return ExecuteCommand(command => command.ExecuteReader().HasRows, sql, timeoutInMilliseconds,
parameters);
return ExecuteCommand(command => command.ExecuteReader().HasRows, sql, timeoutInMilliseconds, parameters);
}

/// <summary>
Expand Down Expand Up @@ -242,7 +240,7 @@ public bool Exists<T>(Guid id, string contextKey, int timeoutInMilliseconds = -1

private DbParameter CreateSqlParameter(string parameterName, object value)
{
return new SqlParameter(parameterName, value);
return new SqlParameter(parameterName, value ?? DBNull.Value);
}

private T ExecuteCommand<T>(Func<DbCommand, T> execute, string sql, int timeoutInMilliseconds,
Expand Down
16 changes: 6 additions & 10 deletions src/Paramore.Brighter.MessageStore.MsSql/MsSqlMessageStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Licence
#region Licence

/* The MIT License (MIT)
Copyright © 2014 Francesco Pighi <[email protected]>
Expand Down Expand Up @@ -104,8 +104,7 @@ public void Add(Message message, int messageStoreTimeout = -1)
/// <returns>Task&lt;Message&gt;.</returns>
public Message Get(Guid messageId, int messageStoreTimeout = -1)
{
var sql = string.Format("SELECT * FROM {0} WHERE MessageId = @MessageId",
_configuration.MessageStoreTableName);
var sql = $"SELECT * FROM {_configuration.MessageStoreTableName} WHERE MessageId = @MessageId";
var parameters = new[]
{
CreateSqlParameter("MessageId", messageId)
Expand Down Expand Up @@ -163,7 +162,7 @@ public Message Get(Guid messageId, int messageStoreTimeout = -1)
/// <returns><see cref="Task{Message}" />.</returns>
public async Task<Message> GetAsync(Guid messageId, int messageStoreTimeout = -1, CancellationToken cancellationToken = default(CancellationToken))
{
var sql = string.Format("SELECT * FROM {0} WHERE MessageId = @MessageId", _configuration.MessageStoreTableName);
var sql = $"SELECT * FROM {_configuration.MessageStoreTableName} WHERE MessageId = @MessageId";

var parameters = new[]
{
Expand Down Expand Up @@ -234,7 +233,7 @@ public IList<Message> Get(int pageSize = 100, int pageNumber = 1)
//Fold this code back in as there is only one choice
private DbParameter CreateSqlParameter(string parameterName, object value)
{
return new SqlParameter(parameterName, value);
return new SqlParameter(parameterName, value ?? DBNull.Value);

}

Expand Down Expand Up @@ -281,10 +280,7 @@ private DbConnection GetConnection()
private DbCommand InitAddDbCommand(DbConnection connection, DbParameter[] parameters)
{
var command = connection.CreateCommand();
var sql =
string.Format(
"INSERT INTO {0} (MessageId, MessageType, Topic, Timestamp, HeaderBag, Body) VALUES (@MessageId, @MessageType, @Topic, @Timestamp, @HeaderBag, @Body)",
_configuration.MessageStoreTableName);
var sql = $"INSERT INTO {_configuration.MessageStoreTableName} (MessageId, MessageType, Topic, Timestamp, HeaderBag, Body) VALUES (@MessageId, @MessageType, @Topic, @Timestamp, @HeaderBag, @Body)";
command.CommandText = sql;
command.Parameters.AddRange(parameters);
return command;
Expand Down Expand Up @@ -365,4 +361,4 @@ private void SetPagingCommandFor(DbCommand command, MsSqlMessageStoreConfigurati
command.Parameters.AddRange(parameters);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ THE SOFTWARE. */
using FluentAssertions;
using Xunit;
using Paramore.Brighter.CommandStore.MsSql;
using Paramore.Brighter.Eventsourcing.Exceptions;
using Paramore.Brighter.Tests.CommandProcessors.TestDoubles;

namespace Paramore.Brighter.Tests.CommandStore.MsSsql
Expand Down Expand Up @@ -64,6 +65,15 @@ public void When_Writing_A_Message_To_The_Command_Store()
_storedCommand.Id.Should().Be(_raisedCommand.Id);
}

[Fact]
public void When_Reading_A_Message_From_The_Command_Store_And_ContextKey_IsNull()
{
var exception = Catch.Exception(() => _storedCommand = _sqlCommandStore.Get<MyCommand>(_raisedCommand.Id, null));
//should_not_read_message
exception.Should().BeOfType<CommandNotFoundException<MyCommand>>();
}


public void Dispose()
{
_msSqlTestHelper.CleanUpDb();
Expand Down

0 comments on commit 6245248

Please sign in to comment.