public async Task DeleteAsync(K id) { using (var conn = tableInfo.CreateConnection()) { await conn.ExecuteAsync(deleteSql, new { StateId = id }); } }
public async Task <IList <IEventBase <K> > > GetListAsync(K stateId, Int64 startVersion, Int64 endVersion, DateTime?startTime = null) { var originList = new List <SqlEvent>((int)(endVersion - startVersion)); await Task.Run(async() => { var tableList = await tableInfo.GetTableList(startTime); using (var conn = tableInfo.CreateConnection() as NpgsqlConnection) { await conn.OpenAsync(); foreach (var table in tableList) { var sql = $"COPY (SELECT typecode,data from {table.Name} WHERE stateid='{stateId.ToString()}' and version>{startVersion} and version<={endVersion} order by version asc) TO STDOUT (FORMAT BINARY)"; using (var reader = conn.BeginBinaryExport(sql)) { while (reader.StartRow() != -1) { originList.Add(new SqlEvent { TypeCode = reader.Read <string>(NpgsqlDbType.Varchar), Data = reader.Read <byte[]>(NpgsqlDbType.Bytea) }); } } } } }).ConfigureAwait(false); var list = new List <IEventBase <K> >(originList.Count); foreach (var origin in originList) { if (MessageTypeMapper.EventTypeDict.TryGetValue(origin.TypeCode, out var type)) { using (var ms = new MemoryStream(origin.Data)) { if (Serializer.Deserialize(type, ms) is IEventBase <K> evt) { list.Add(evt); } } } } return(list.OrderBy(v => v.Version).ToList()); }
public async Task <IList <IEventBase <K> > > GetListAsync(K stateId, Int64 startVersion, Int64 endVersion, DateTime?startTime = null) { var tableList = await tableInfo.GetTableList(startTime); var list = new List <IEventBase <K> >(); Int64 readVersion = 0; using (var conn = tableInfo.CreateConnection()) { foreach (var table in tableList) { if (!oneListSqlDict.TryGetValue(table.Name, out var sql)) { sql = $"SELECT typecode,data from {table.Name} WHERE stateid=@StateId and version>@Start and version<=@End order by version asc"; oneListSqlDict.TryAdd(table.Name, sql); } var sqlEventList = await conn.QueryAsync <SqlEvent>(sql, new { StateId = stateId.ToString(), Start = startVersion, End = endVersion }); foreach (var sqlEvent in sqlEventList) { var type = MessageTypeMapper.GetType(sqlEvent.TypeCode); using (var ms = new MemoryStream(sqlEvent.Data)) { if (Serializer.Deserialize(type, ms) is IEventBase <K> evt) { readVersion = evt.Version; if (readVersion <= endVersion) { list.Add(evt); } } } } if (readVersion >= endVersion) { break; } } } return(list); }