public async Task <int> StreamJson(Stream stream, DbDataReader reader, CancellationToken token) { var count = 0; var ordinal = reader.FieldCount == 1 ? 0 : reader.GetOrdinal("data"); await stream.WriteBytes(ManagedConnectionExtensions.LeftBracket, token).ConfigureAwait(false); if (await reader.ReadAsync(token).ConfigureAwait(false)) { _statistics.TotalResults = await reader.GetFieldValueAsync <int>(_countIndex, token).ConfigureAwait(false); count++; var source = await reader.As <NpgsqlDataReader>().GetStreamAsync(ordinal, token).ConfigureAwait(false); await source.CopyStreamSkippingSOHAsync(stream, token).ConfigureAwait(false); } while (await reader.ReadAsync(token).ConfigureAwait(false)) { count++; await stream.WriteBytes(ManagedConnectionExtensions.Comma, token).ConfigureAwait(false); var source = await reader.As <NpgsqlDataReader>().GetStreamAsync(ordinal, token).ConfigureAwait(false); await source.CopyStreamSkippingSOHAsync(stream, token).ConfigureAwait(false); } await stream.WriteBytes(ManagedConnectionExtensions.RightBracket, token).ConfigureAwait(false); return(count); }
public async Task <string> HandleAsync(DbDataReader reader, IMartenSession session, CancellationToken token) { // TODO -- figure out better, more efficient ways to do this var builder = new StringWriter(); await builder.WriteAsync(_arrayPrefix); if (await reader.ReadAsync(token)) { using var text = await reader.As <NpgsqlDataReader>().GetStreamAsync(0, token); await builder.WriteAsync(await text.GetStreamReader().ReadToEndAsync()); } while (await reader.ReadAsync(token)) { using var text = await reader.As <NpgsqlDataReader>().GetStreamAsync(0, token); await builder.WriteAsync(','); await builder.WriteAsync(await text.GetStreamReader().ReadToEndAsync()); } await builder.WriteAsync(_arraySuffix); return(builder.ToString()); }
public T Resolve(DbDataReader reader, IIdentityMap map, QueryStatistics stats) { if (!typeof(T).IsSimple()) { var json = reader.As <DbDataReader>().GetTextReader(0); return(_serializer.FromJson <T>(json)); } return(reader.GetFieldValue <T>(0)); }
public async Task <T> ResolveAsync(DbDataReader reader, IIdentityMap map, QueryStatistics stats, CancellationToken token) { if (!typeof(T).IsSimple()) { var json = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(0).ConfigureAwait(false); return(_serializer.FromJson <T>(json)); } return(await reader.GetFieldValueAsync <T>(0, token).ConfigureAwait(false)); }
public async Task <IEvent> ResolveAsync(DbDataReader reader, IIdentityMap map, QueryStatistics stats, CancellationToken token) { var id = await reader.GetFieldValueAsync <Guid>(0, token).ConfigureAwait(false); var eventTypeName = await reader.GetFieldValueAsync <string>(1, token).ConfigureAwait(false); var version = await reader.GetFieldValueAsync <int>(2, token).ConfigureAwait(false); var mapping = Events.EventMappingFor(eventTypeName); if (mapping == null) { var dotnetTypeName = await reader.GetFieldValueAsync <string>(8, token).ConfigureAwait(false); if (dotnetTypeName.IsEmpty()) { throw new UnknownEventTypeException(eventTypeName); } Type type; try { type = Events.TypeForDotNetName(dotnetTypeName); } catch (ArgumentNullException) { throw new UnknownEventTypeException(dotnetTypeName); } mapping = Events.EventMappingFor(type); } var dataJson = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(3).ConfigureAwait(false); var data = _serializer.FromJson(mapping.DocumentType, dataJson).As <object>(); var sequence = await reader.GetFieldValueAsync <long>(4, token).ConfigureAwait(false); var stream = await reader.GetFieldValueAsync <Guid>(5, token).ConfigureAwait(false); var timestamp = await reader.GetFieldValueAsync <DateTimeOffset>(6, token).ConfigureAwait(false); var tenantId = await reader.GetFieldValueAsync <string>(7, token).ConfigureAwait(false); var @event = EventStream.ToEvent(data); @event.Version = version; @event.Id = id; @event.Sequence = sequence; @event.StreamId = stream; @event.Timestamp = timestamp; @event.TenantId = tenantId; return(@event); }
public async Task <T> ResolveAsync(int startingIndex, DbDataReader reader, IIdentityMap map, CancellationToken token) { var json = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(startingIndex).ConfigureAwait(false); var id = await reader.GetFieldValueAsync <object>(startingIndex + 1, token).ConfigureAwait(false); var version = await reader.GetFieldValueAsync <Guid>(3, token).ConfigureAwait(false); var typeAlias = await reader.GetFieldValueAsync <string>(startingIndex + 2, token).ConfigureAwait(false); return(map.Get <TBase>(id, _mapping.TypeFor(typeAlias), json, version) as T); }
/// <summary> /// 将DataReader转换为数据实体列表 /// </summary> /// <typeparam name="T">数据实体类型</typeparam> /// <param name="dr">要转换的数据流</param> /// <param name="ev">需要应用的事务</param> /// <returns></returns> public static List <T> ToList <T>(this DbDataReader dr, IUnitOfWork ev) where T : new() { List <T> list = new List <T>(); using (dr) { while (dr.Read()) { list.Add(dr.As <T>()); } dr.Dispose(); } return(list); }
public virtual async Task <T> FetchAsync(object id, DbDataReader reader, IIdentityMap map, CancellationToken token) { var found = await reader.ReadAsync(token).ConfigureAwait(false); if (!found) { return(null); } var version = await reader.GetFieldValueAsync <Guid>(2, token).ConfigureAwait(false); var json = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(0).ConfigureAwait(false); return(map.Get <T>(id, json, version)); }
public override async Task <T> ResolveAsync(int startingIndex, DbDataReader reader, IIdentityMap map, CancellationToken token) { if (await reader.IsDBNullAsync(startingIndex, token).ConfigureAwait(false)) { return(null); } var json = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(startingIndex).ConfigureAwait(false); var id = await reader.GetFieldValueAsync <object>(startingIndex + 1, token).ConfigureAwait(false); var typeAlias = await reader.GetFieldValueAsync <string>(startingIndex + 2, token).ConfigureAwait(false); var version = await reader.GetFieldValueAsync <Guid>(3, token).ConfigureAwait(false); return(map.Get <T>(id, _hierarchy.TypeFor(typeAlias), json, version)); }
public virtual async Task <T> ResolveAsync(int startingIndex, DbDataReader reader, IIdentityMap map, CancellationToken token) { if (await reader.IsDBNullAsync(startingIndex, token).ConfigureAwait(false)) { return(null); } var id = await reader.GetFieldValueAsync <object>(startingIndex + 1, token).ConfigureAwait(false); var version = await reader.GetFieldValueAsync <Guid>(startingIndex + 2, token).ConfigureAwait(false); using (var json = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(startingIndex).ConfigureAwait(false)) { return(map.Get <T>(id, json, version)); } }
public override async Task <T> FetchAsync(object id, DbDataReader reader, IIdentityMap map, CancellationToken token) { var found = await reader.ReadAsync(token).ConfigureAwait(false); if (!found) { return(null); } var json = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(0).ConfigureAwait(false); var typeAlias = await reader.GetFieldValueAsync <string>(2, token).ConfigureAwait(false); var actualType = _hierarchy.TypeFor(typeAlias); var version = await reader.GetFieldValueAsync <Guid>(3, token).ConfigureAwait(false); return(map.Get <T>(id, actualType, json, version)); }
public async Task <IEvent> ResolveAsync(DbDataReader reader, CancellationToken token) { var eventTypeName = await reader.GetFieldValueAsync <string>(1, token); var mapping = Events.EventMappingFor(eventTypeName); if (mapping == null) { var dotnetTypeName = await reader.GetFieldValueAsync <string>(2, token).ConfigureAwait(false); if (dotnetTypeName.IsEmpty()) { throw new UnknownEventTypeException(eventTypeName); } Type type; try { type = Events.TypeForDotNetName(dotnetTypeName); } catch (ArgumentNullException) { throw new UnknownEventTypeException(dotnetTypeName); } mapping = Events.EventMappingFor(type); } var dataJson = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(0, token); var data = _serializer.FromJson(mapping.DocumentType, dataJson); var @event = mapping.Wrap(data); await ApplyReaderDataToEventAsync(reader, @event, token); return(@event); }
public Task <int> StreamJson(Stream stream, DbDataReader reader, CancellationToken token) { return(reader.As <NpgsqlDataReader>().StreamMany(stream, token)); }
public async Task <T> ResolveAsync(DbDataReader reader, IIdentityMap map, QueryStatistics stats, CancellationToken token) { var json = await reader.As <NpgsqlDataReader>().GetTextReaderAsync(0).ConfigureAwait(false); return(map.Serializer.FromJson <T>(json)); }