2017-02-08 14:52:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Linq;
|
2017-07-27 13:55:37 +00:00
|
|
|
|
using System.Text;
|
2017-02-08 14:52:24 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
Assumes the following table layout (schema configurable):
|
|
|
|
|
|
|
|
|
|
|
2017-07-27 13:55:37 +00:00
|
|
|
|
create table shared.Flow
|
2017-02-08 14:52:24 +00:00
|
|
|
|
(
|
|
|
|
|
FlowID uniqueidentifier not null,
|
|
|
|
|
ServiceID int 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)
|
|
|
|
|
);
|
|
|
|
|
go;
|
|
|
|
|
*/
|
2017-07-27 13:55:37 +00:00
|
|
|
|
public class SqlConnectionFlowRepository<T> : IFlowRepository<T>
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly string connectionString;
|
|
|
|
|
private readonly int serviceId;
|
|
|
|
|
private readonly string schema;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SqlConnectionFlowRepository(string connectionString, int serviceId, string schema)
|
|
|
|
|
{
|
|
|
|
|
this.connectionString = connectionString;
|
|
|
|
|
this.serviceId = serviceId;
|
|
|
|
|
this.schema = schema;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-07-27 13:55:37 +00:00
|
|
|
|
public async Task<List<KeyValuePair<Guid, T>>> GetStates()
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
|
|
|
|
using (var connection = await GetConnection())
|
|
|
|
|
{
|
2017-07-27 13:55:37 +00:00
|
|
|
|
var flowQuery = new SqlCommand($"select FlowID, StateJson from {schema}.Flow " +
|
2017-07-27 14:02:03 +00:00
|
|
|
|
"where ServiceID = @ServiceID ",
|
|
|
|
|
connection);
|
2017-02-08 14:52:24 +00:00
|
|
|
|
var flowServiceParam = flowQuery.Parameters.Add("@ServiceID", SqlDbType.Int);
|
|
|
|
|
|
|
|
|
|
flowServiceParam.Value = serviceId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 13:55:37 +00:00
|
|
|
|
public Task CreateState(Guid flowID, T state, DateTime timestamp)
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
2017-07-27 13:55:37 +00:00
|
|
|
|
var stateJason = JsonConvert.SerializeObject(state);
|
|
|
|
|
|
2017-02-08 14:52:24 +00:00
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 13:55:37 +00:00
|
|
|
|
public Task UpdateState(Guid flowID, T state)
|
2017-02-08 14:52:24 +00:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task DeleteState(Guid flowID)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<SqlConnection> GetConnection()
|
|
|
|
|
{
|
|
|
|
|
var connection = new SqlConnection(connectionString);
|
|
|
|
|
await connection.OpenAsync();
|
|
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|