2017-02-08 14:52:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-27 13:55:37 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2017-02-08 14:52:24 +00:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Flow.SQL
|
|
|
|
|
{
|
|
|
|
|
/*
|
2018-12-19 20:41:19 +00:00
|
|
|
|
Assumes the following table layout (table name configurable and may include schema):
|
2017-02-08 14:52:24 +00:00
|
|
|
|
|
|
|
|
|
|
2018-12-19 20:41:19 +00:00
|
|
|
|
create table Flow
|
2017-02-08 14:52:24 +00:00
|
|
|
|
(
|
|
|
|
|
FlowID uniqueidentifier not null,
|
|
|
|
|
CreationTime datetime2(3) not null,
|
2017-07-27 14:02:03 +00:00
|
|
|
|
StateJson nvarchar(max) null,
|
2017-02-08 14:52:24 +00:00
|
|
|
|
|
|
|
|
|
constraint PK_Flow primary key clustered (FlowID)
|
|
|
|
|
);
|
|
|
|
|
*/
|
2017-08-14 11:58:01 +00:00
|
|
|
|
public class SqlConnectionFlowRepository : IFlowRepository
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly string connectionString;
|
2018-12-19 20:41:19 +00:00
|
|
|
|
private readonly string tableName;
|
2017-02-08 14:52:24 +00:00
|
|
|
|
|
|
|
|
|
|
2018-12-19 20:41:19 +00:00
|
|
|
|
public SqlConnectionFlowRepository(string connectionString, string tableName = "Flow")
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
|
|
|
|
this.connectionString = connectionString;
|
2018-12-19 20:41:19 +00:00
|
|
|
|
this.tableName = tableName;
|
2017-02-08 14:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-08-14 11:58:01 +00:00
|
|
|
|
public async Task<List<KeyValuePair<Guid, T>>> GetStates<T>()
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
|
|
|
|
using (var connection = await GetConnection())
|
|
|
|
|
{
|
2018-12-19 20:41:19 +00:00
|
|
|
|
var flowQuery = new SqlCommand($"select FlowID, StateJson from {tableName}", connection);
|
2017-02-08 14:52:24 +00:00
|
|
|
|
var flowReader = await flowQuery.ExecuteReaderAsync();
|
2017-07-27 13:55:37 +00:00
|
|
|
|
|
|
|
|
|
var result = new List<KeyValuePair<Guid, T>>();
|
2017-02-08 14:52:24 +00:00
|
|
|
|
|
|
|
|
|
while (await flowReader.ReadAsync())
|
|
|
|
|
{
|
2017-07-27 13:55:37 +00:00
|
|
|
|
var flowID = flowReader.GetGuid(0);
|
|
|
|
|
var stateJson = flowReader.GetString(1);
|
|
|
|
|
|
|
|
|
|
var state = JsonConvert.DeserializeObject<T>(stateJson);
|
|
|
|
|
result.Add(new KeyValuePair<Guid, T>(flowID, state));
|
2017-02-08 14:52:24 +00:00
|
|
|
|
}
|
2017-07-27 13:55:37 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
2017-02-08 14:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 20:41:19 +00:00
|
|
|
|
public async Task CreateState<T>(Guid flowID, T state, DateTime timestamp)
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
2018-12-19 20:41:19 +00:00
|
|
|
|
using (var connection = await GetConnection())
|
|
|
|
|
{
|
|
|
|
|
var query = new SqlCommand($"insert into {tableName} (FlowID, StateJson, CreationTime)" +
|
|
|
|
|
"values (@FlowID, @StateJson, @CreationTime)",
|
|
|
|
|
connection);
|
2017-07-27 13:55:37 +00:00
|
|
|
|
|
2018-12-19 20:41:19 +00:00
|
|
|
|
var flowIDParam = query.Parameters.Add("@FlowID", SqlDbType.UniqueIdentifier);
|
|
|
|
|
var stateJsonParam = query.Parameters.Add("@StateJson", SqlDbType.NVarChar);
|
|
|
|
|
var creationTimeParam = query.Parameters.Add("@CreationTime", SqlDbType.DateTime2);
|
|
|
|
|
|
|
|
|
|
flowIDParam.Value = flowID;
|
|
|
|
|
stateJsonParam.Value = JsonConvert.SerializeObject(state);
|
|
|
|
|
creationTimeParam.Value = timestamp;
|
|
|
|
|
|
|
|
|
|
await query.ExecuteNonQueryAsync();
|
|
|
|
|
}
|
2017-02-08 14:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 20:41:19 +00:00
|
|
|
|
public async Task UpdateState<T>(Guid flowID, T state)
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
2018-12-19 20:41:19 +00:00
|
|
|
|
using (var connection = await GetConnection())
|
|
|
|
|
{
|
|
|
|
|
var query = new SqlCommand($"update {tableName} set StateJson = @StateJson where FlowID = @FlowID", connection);
|
|
|
|
|
|
|
|
|
|
var flowIDParam = query.Parameters.Add("@FlowID", SqlDbType.UniqueIdentifier);
|
|
|
|
|
var stateJsonParam = query.Parameters.Add("@StateJson", SqlDbType.NVarChar);
|
|
|
|
|
|
|
|
|
|
flowIDParam.Value = flowID;
|
|
|
|
|
stateJsonParam.Value = JsonConvert.SerializeObject(state);
|
|
|
|
|
|
|
|
|
|
await query.ExecuteNonQueryAsync();
|
|
|
|
|
}
|
2017-02-08 14:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 20:41:19 +00:00
|
|
|
|
public async Task DeleteState(Guid flowID)
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
2018-12-19 20:41:19 +00:00
|
|
|
|
using (var connection = await GetConnection())
|
|
|
|
|
{
|
|
|
|
|
var query = new SqlCommand($"delete from {tableName} where FlowID = @FlowID", connection);
|
|
|
|
|
|
|
|
|
|
var flowIDParam = query.Parameters.Add("@FlowID", SqlDbType.UniqueIdentifier);
|
|
|
|
|
flowIDParam.Value = flowID;
|
|
|
|
|
|
|
|
|
|
await query.ExecuteNonQueryAsync();
|
|
|
|
|
}
|
2017-02-08 14:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<SqlConnection> GetConnection()
|
|
|
|
|
{
|
|
|
|
|
var connection = new SqlConnection(connectionString);
|
|
|
|
|
await connection.OpenAsync();
|
|
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|