public IEnumerable <Dictionary <String, Object> > Resolve(ResolveFieldContext context) { var results = new List <Dictionary <String, Object> >(); var translator = new OeGraphqlAstToODataUri(_edmModel, context); ODataUri odataUri = translator.Translate(context.Document.OriginalQuery); IEdmModel refModel = _edmModel.GetEdmModel(odataUri.Path); Db.OeDataAdapter dataAdapter = refModel.GetDataAdapter(refModel.EntityContainer); context.UserContext = dataAdapter.CreateDataContext(); try { var queryContext = new Parsers.OeQueryContext(refModel, odataUri); IAsyncEnumerator <Object> asyncEnumerator = dataAdapter.Execute(context.UserContext, queryContext).GetEnumerator(); using (var entityAsyncEnumerator = new OeGraphqlAsyncEnumerator(asyncEnumerator, queryContext.EntryFactory)) { while (entityAsyncEnumerator.MoveNext().GetAwaiter().GetResult()) { results.Add(entityAsyncEnumerator.Current); } } } finally { if (context.UserContext != null) { dataAdapter.CloseDataContext(context.UserContext); } } return(results); }
private IAsyncEnumerable <Object> GetAsyncEnumerator(IQueryable source = null) { _httpContext.Response.RegisterForDispose(this); ODataUri odataUri = OeParser.ParseUri(_edmModel, UriHelper.GetBaseUri(_httpContext.Request), UriHelper.GetUri(_httpContext.Request)); IEdmModel refModel = _edmModel.GetEdmModel(odataUri.Path); _dataAdapter = refModel.GetDataAdapter(refModel.EntityContainer); if (_dataContext == null) { _dataContext = _dataAdapter.CreateDataContext(); } var requestHeaders = (HttpRequestHeaders)_httpContext.Request.Headers; OeRequestHeaders headers = GetRequestHeaders(requestHeaders, _httpContext.Response); if (odataUri.Path.LastSegment is OperationImportSegment) { return(ExecutePost(refModel, odataUri, headers, _httpContext.Request.Body)); } else { return(ExecuteGet(refModel, odataUri, headers, source)); } }
public async Task ExecuteAsync(ODataUri odataUri, Stream requestStream, OeRequestHeaders headers, Stream responseStream, CancellationToken cancellationToken) { Object dataContext = null; try { dataContext = _dataAdapter.CreateDataContext(); using (Db.OeAsyncEnumerator asyncEnumerator = GetAsyncEnumerator(odataUri, requestStream, headers, dataContext, out bool isScalar)) { if (isScalar) { if (await asyncEnumerator.MoveNextAsync().ConfigureAwait(false) && asyncEnumerator.Current != null) { headers.ResponseContentType = OeRequestHeaders.TextDefault.ContentType; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(asyncEnumerator.Current.ToString()); responseStream.Write(buffer, 0, buffer.Length); } else headers.ResponseContentType = null; } else { OeQueryContext queryContext = CreateQueryContext(odataUri, headers.MetadataLevel); if (queryContext == null) await WriteCollectionAsync(_edmModel, odataUri, asyncEnumerator, responseStream).ConfigureAwait(false); else await Writers.OeGetWriter.SerializeAsync(queryContext, asyncEnumerator, headers.ContentType, responseStream).ConfigureAwait(false); } } } finally { if (dataContext != null) _dataAdapter.CloseDataContext(dataContext); } }
public async Task ExecuteAsync(ODataUri odataUri, OeRequestHeaders headers, Stream stream, CancellationToken cancellationToken) { OeQueryContext queryContext = CreateQueryContext(odataUri, headers.MaxPageSize, headers.NavigationNextLink, headers.MetadataLevel); Db.OeDataAdapter dataAdapter = _edmModel.GetDataAdapter(queryContext.EdmModel.EntityContainer); Object dataContext = null; try { dataContext = dataAdapter.CreateDataContext(); if (queryContext.IsCountSegment) { headers.ResponseContentType = OeRequestHeaders.TextDefault.ContentType; int count = dataAdapter.ExecuteScalar <int>(dataContext, queryContext); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(count.ToString(CultureInfo.InvariantCulture)); stream.Write(buffer, 0, buffer.Length); } else { using (Db.OeAsyncEnumerator asyncEnumerator = dataAdapter.ExecuteEnumerator(dataContext, queryContext, cancellationToken)) await Writers.OeGetWriter.SerializeAsync(queryContext, asyncEnumerator, headers.ContentType, stream).ConfigureAwait(false); } } finally { if (dataContext != null) { dataAdapter.CloseDataContext(dataContext); } } }
public static EdmModel BuildEdmModelFromEf6Model(this Db.OeDataAdapter dataAdapter) { using (var context = (DbContext)dataAdapter.CreateDataContext()) { var modelBuilder = new OeEdmModelBuilder(dataAdapter, new OeEf6EdmModelMetadataProvider(context)); return(modelBuilder.BuildEdmModel()); } }
public async Task ExecuteAsync(ODataUri odataUri, Stream?requestStream, OeRequestHeaders headers, Stream responseStream, CancellationToken cancellationToken) { Object?dataContext = null; try { dataContext = _dataAdapter.CreateDataContext(); IAsyncEnumerator <Object>?asyncEnumerator = null; try { asyncEnumerator = GetAsyncEnumerable(odataUri, requestStream, headers, dataContext, cancellationToken, out bool isScalar).GetAsyncEnumerator(cancellationToken); if (isScalar) { if (await asyncEnumerator.MoveNextAsync().ConfigureAwait(false) && asyncEnumerator.Current != null) { headers.ResponseContentType = OeRequestHeaders.TextDefault.ContentType; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(asyncEnumerator.Current.ToString()); await responseStream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false); } else { headers.ResponseContentType = null; } } else { var importSegment = (OperationImportSegment)odataUri.Path.FirstSegment; IEdmEntitySet?entitySet = OeOperationHelper.GetEntitySet(importSegment.OperationImports.Single()); if (entitySet == null) { await WriteCollectionAsync(_edmModel, odataUri, asyncEnumerator, responseStream).ConfigureAwait(false); } else { OeQueryContext queryContext = CreateQueryContext(odataUri, entitySet, headers.MetadataLevel); await Writers.OeGetWriter.SerializeAsync(queryContext, asyncEnumerator, headers.ContentType, responseStream, _serviceProvider, cancellationToken).ConfigureAwait(false); } } } finally { if (asyncEnumerator != null) { await asyncEnumerator.DisposeAsync().ConfigureAwait(false); } } } finally { if (dataContext != null) { _dataAdapter.CloseDataContext(dataContext); } } }
public static EdmModel BuildEdmModelFromEf6Model(this Db.OeDataAdapter dataAdapter) { using (var context = (DbContext)dataAdapter.CreateDataContext()) { var metadataProvider = new OeEf6EdmModelMetadataProvider(context); var modelBuilder = new OeEdmModelBuilder(metadataProvider); modelBuilder.AddEntitySetRange(dataAdapter.EntitySetMetaAdapters.GetEntitySetNamesEntityTypes()); OeDataAdapterExtension.BuildOperations(dataAdapter, modelBuilder); return(modelBuilder.BuildEdmModel()); } }
private async Task ExecuteChangeset(IReadOnlyList <OeOperationMessage> changeset, CancellationToken cancellationToken) { Object dataContext = null; try { dataContext = _dataAdapter.CreateDataContext(); foreach (OeOperationMessage operation in changeset) { AddToEntitySet(dataContext, operation); } await _dataAdapter.SaveChangesAsync(_model, dataContext, cancellationToken).ConfigureAwait(false); } finally { if (dataContext != null) { _dataAdapter.CloseDataContext(dataContext); } } }
public TDataContext GetDbContext <TDataContext>() where TDataContext : class { if (_dataContext == null) { Db.OeDataAdapter dataAdapter = _edmModel.GetDataAdapter(typeof(TDataContext)); if (dataAdapter == null) { return(null); } _dataContext = dataAdapter.CreateDataContext(); } return((TDataContext)_dataContext); }
public static EdmModel BuildEdmModelFromEfCoreModel(this Db.OeDataAdapter dataAdapter, Type[] excludeTypes, params IEdmModel[] refModels) { var context = (DbContext)dataAdapter.CreateDataContext(); try { var modelBuilder = new OeEdmModelBuilder(dataAdapter, new OeEfCoreEdmModelMetadataProvider(context.Model)); return(modelBuilder.BuildEdmModel(excludeTypes, refModels)); } finally { dataAdapter.CloseDataContext(context); } }
public static bool GetUseRelationalNulls(Db.OeDataAdapter dataAdapter) { var serviceProvider = (IInfrastructure <IServiceProvider>)dataAdapter.CreateDataContext(); try { RelationalOptionsExtension options = serviceProvider.GetService <IDbContextOptions>().Extensions.OfType <RelationalOptionsExtension>().Single(); return(options.UseRelationalNulls); } finally { dataAdapter.CloseDataContext(serviceProvider); } }
public override OrderContext CreateContext() { Db.OeDataAdapter dataAdapter = OeEdmModel.GetDataAdapter(OeEdmModel.EntityContainer); var dbContext = (DbContext)dataAdapter.CreateDataContext(); try { DbContextOptions options = TestHelper.CreateOptions <OrderContext>(dbContext); return(new OrderContext(options)); } finally { dataAdapter.CloseDataContext(dbContext); } }
public ODataResult <Model.OrderItem> WithItems(String itemIds) { List <int> ids = JArray.Parse(itemIds).Select(j => j.Value <int>()).ToList(); var edmModel = (IEdmModel)_httpContextAccessor.HttpContext.RequestServices.GetService(typeof(IEdmModel)); Db.OeDataAdapter dataAdapter = edmModel.GetDataAdapter(typeof(Model.OrderContext)); var orderContext = (Model.OrderContext)dataAdapter.CreateDataContext(); List <Model.OrderItem> orderItems = orderContext.OrderItems.Where(i => ids.Contains(i.Id)).ToList(); dataAdapter.CloseDataContext(orderContext); return(_httpContextAccessor.HttpContext.OData(orderItems)); }
public static EdmModel BuildEdmModelFromEfCorePgSqlModel(this Db.OeDataAdapter dataAdapter, String schema, params IEdmModel[] refModels) { var context = (DbContext)dataAdapter.CreateDataContext(); try { var model = (Model)context.Model; model.Relational().DefaultSchema = schema; var modelBuilder = new OeEdmModelBuilder(dataAdapter, new OeEfCoreEdmModelMetadataProvider(model)); return(modelBuilder.BuildEdmModel(refModels)); } finally { dataAdapter.CloseDataContext(context); } }
public static IEnumerable <OrderItem> BoundFunctionSingle(IEdmModel edmModel, Order order, IEnumerable <String> customerNames) { var customerNameSet = new HashSet <String>(customerNames); Db.OeDataAdapter dataAdapter = edmModel.GetDataAdapter(typeof(OrderEf6Context)); var orderContext = (OrderEf6Context)dataAdapter.CreateDataContext(); Customer customer = orderContext.Customers.Find(new Object[] { order.CustomerCountry, order.CustomerId }); if (customerNameSet.Contains(customer.Name)) { foreach (OrderItem orderItem in orderContext.OrderItems.Where(i => i.OrderId == order.Id)) { yield return(orderItem); } } }
protected static void EnsureCreated(IEdmModel edmModel) { Db.OeDataAdapter dataAdapter = edmModel.GetDataAdapter(edmModel.EntityContainer); var dbContext = (DbContext)dataAdapter.CreateDataContext(); dbContext.Database.EnsureCreated(); dataAdapter.CloseDataContext(dbContext); foreach (IEdmModel refModel in edmModel.ReferencedModels) { if (refModel.EntityContainer != null) { EnsureCreated(refModel); } } }
public async Task ExecuteAsync(ODataUri odataUri, OeRequestHeaders headers, Stream stream, CancellationToken cancellationToken) { if (_modelBoundProvider != null) { _modelBoundProvider.Validate(_edmModel, odataUri); } var queryContext = new OeQueryContext(_edmModel, odataUri) { MetadataLevel = headers.MetadataLevel }; if (queryContext.ODataUri.Path.LastSegment is OperationSegment) { using (IAsyncEnumerator <Object> asyncEnumerator = OeOperationHelper.ApplyBoundFunction(queryContext).GetEnumerator()) await Writers.OeGetWriter.SerializeAsync(queryContext, asyncEnumerator, headers.ContentType, stream, cancellationToken).ConfigureAwait(false); return; } Object dataContext = null; Db.OeDataAdapter dataAdapter = queryContext.EdmModel.GetDataAdapter(queryContext.EdmModel.EntityContainer); try { dataContext = dataAdapter.CreateDataContext(); if (odataUri.Path.LastSegment is CountSegment) { headers.ResponseContentType = OeRequestHeaders.TextDefault.ContentType; int count = dataAdapter.ExecuteScalar <int>(dataContext, queryContext); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(count.ToString(CultureInfo.InvariantCulture)); stream.Write(buffer, 0, buffer.Length); } else { using (IAsyncEnumerator <Object> asyncEnumerator = dataAdapter.Execute(dataContext, queryContext).GetEnumerator()) await Writers.OeGetWriter.SerializeAsync(queryContext, asyncEnumerator, headers.ContentType, stream, cancellationToken).ConfigureAwait(false); } } finally { if (dataContext != null) { dataAdapter.CloseDataContext(dataContext); } } }
public virtual async Task Execute <T, TResult>(QueryParametersScalar <T, TResult> parameters) { IList fromOe = await ExecuteOe <TResult>(parameters.RequestUri, false, 0).ConfigureAwait(false); ODataUri odataUri = ParseUri(parameters.RequestUri); IEdmModel dbEdmModel = TestHelper.GetEdmModel(DbEdmModel, odataUri.Path); Db.OeDataAdapter dbDataAdapter = DbEdmModel.GetDataAdapter(dbEdmModel.EntityContainer); IList fromDb; using (var dataContext = (DbContext)dbDataAdapter.CreateDataContext()) fromDb = TestHelper.ExecuteDb(dbDataAdapter.EntitySetAdapters, dataContext, parameters.Expression); Console.WriteLine(parameters.RequestUri); TestHelper.Compare(fromDb, fromOe, null); }
public async Task <IEnumerable <Dictionary <String, Object?> > > Resolve(IResolveFieldContext context) { var results = new List <Dictionary <String, Object?> >(); var translator = new OeGraphqlAstToODataUri(_edmModel, context); ODataUri odataUri = translator.Translate(context.Document.OriginalQuery); IEdmModel refModel = _edmModel.GetEdmModel(odataUri.Path); Db.OeDataAdapter dataAdapter = refModel.GetDataAdapter(refModel.EntityContainer); Object dataContext = dataAdapter.CreateDataContext(); OeGraphqlAsyncEnumerator?entityAsyncEnumerator = null; try { var queryContext = new Parsers.OeQueryContext(refModel, odataUri); IAsyncEnumerator <Object> asyncEnumerator = dataAdapter.Execute(dataContext, queryContext).GetAsyncEnumerator(); if (queryContext.EntryFactory == null) { throw new InvalidOperationException("queryContext.EntryFactory must be not null"); } entityAsyncEnumerator = new OeGraphqlAsyncEnumerator(asyncEnumerator, queryContext.EntryFactory, CancellationToken.None); while (await entityAsyncEnumerator.MoveNextAsync()) { if (entityAsyncEnumerator.Current != null) { results.Add(entityAsyncEnumerator.Current); } } } finally { if (dataContext != null) { dataAdapter.CloseDataContext(dataContext); } if (entityAsyncEnumerator != null) { await entityAsyncEnumerator.DisposeAsync().ConfigureAwait(false); } } return(results); }
public virtual async Task Execute <T, TResult>(QueryParameters <T, TResult> parameters) { IList fromOe = await ExecuteOe <TResult>(parameters.RequestUri, parameters.NavigationNextLink, parameters.PageSize).ConfigureAwait(false); ODataUri odataUri = ParseUri(parameters.RequestUri); IEdmModel dbEdmModel = TestHelper.GetEdmModel(DbEdmModel, odataUri.Path); Db.OeDataAdapter dbDataAdapter = DbEdmModel.GetDataAdapter(dbEdmModel.EntityContainer); IList fromDb; IReadOnlyList <EfInclude> includes; using (var dataContext = (DbContext)dbDataAdapter.CreateDataContext()) fromDb = TestHelper.ExecuteDb(dbDataAdapter, dataContext, parameters.Expression, out includes); Console.WriteLine(parameters.RequestUri); TestHelper.Compare(fromDb, fromOe, includes); }
public static IEnumerable <OrderItem> BoundFunctionCollection(IEdmModel edmModel, IAsyncEnumerator <Order> orders, IEnumerable <String> customerNames) { var customerNameSet = new HashSet <String>(customerNames); Db.OeDataAdapter dataAdapter = edmModel.GetDataAdapter(typeof(OrderEf6Context)); var orderContext = (OrderEf6Context)dataAdapter.CreateDataContext(); while (orders.MoveNext().GetAwaiter().GetResult()) { var order = orders.Current; Customer customer = orderContext.Customers.Find(new Object[] { order.CustomerCountry, order.CustomerId }); if (customerNameSet.Contains(customer.Name)) { foreach (OrderItem orderItem in orderContext.OrderItems.Where(i => i.OrderId == order.Id)) { yield return(orderItem); } } } }
private static Db.OeEntitySetAdapterCollection GetEntitySetAdapters(Db.OeDataAdapter dataAdapter) { Db.OeEntitySetAdapterCollection?entitySetAdapters = Volatile.Read(ref _entitySetAdapters); if (entitySetAdapters != null) { return(entitySetAdapters); } var context = (DbContext)dataAdapter.CreateDataContext(); try { Interlocked.CompareExchange(ref _entitySetAdapters, CreateEntitySetAdapters(context.Model), null); return(Volatile.Read(ref _entitySetAdapters) !); } finally { dataAdapter.CloseDataContext(context); } }
public static EdmModel BuildEdmModelFromEfCorePgSqlModel(this Db.OeDataAdapter dataAdapter, String schema) { var context = (DbContext)dataAdapter.CreateDataContext(); try { var model = (Model)context.Model; model.Relational().DefaultSchema = schema; var metadataProvider = new OeEfCoreEdmModelMetadataProvider(context.Model); var modelBuilder = new OeEdmModelBuilder(metadataProvider); modelBuilder.AddEntitySetRange(dataAdapter.EntitySetMetaAdapters.GetEntitySetNamesEntityTypes()); OeDataAdapterExtension.BuildOperations(dataAdapter, modelBuilder); return(modelBuilder.BuildEdmModel()); } finally { dataAdapter.CloseDataContext(context); } }
public static EdmModel BuildDbEdmModel(IEdmModel oeEdmModel, bool useRelationalNulls) { IEdmModel orderEdmModel; OeEfCoreDataAdapter <OrderContext> orderDataAdapter; OeEfCoreDataAdapter <Order2Context> order2DataAdapter; Db.OeDataAdapter dataAdapter = oeEdmModel.GetDataAdapter(oeEdmModel.EntityContainer); if (dataAdapter.CreateDataContext() is DbContext dbContext) { try { orderDataAdapter = new OeEfCoreDataAdapter <OrderContext>(CreateOptions <OrderContext>(dbContext)) { IsDatabaseNullHighestValue = dataAdapter.IsDatabaseNullHighestValue }; orderEdmModel = orderDataAdapter.BuildEdmModelFromEfCoreModel(); order2DataAdapter = new OeEfCoreDataAdapter <Order2Context>(CreateOptions <Order2Context>(dbContext)) { IsDatabaseNullHighestValue = dataAdapter.IsDatabaseNullHighestValue }; return(order2DataAdapter.BuildEdmModelFromEfCoreModel(orderEdmModel)); } finally { dataAdapter.CloseDataContext(dbContext); } } orderDataAdapter = new OeEfCoreDataAdapter <OrderContext>(Create(useRelationalNulls)) { IsDatabaseNullHighestValue = dataAdapter.IsDatabaseNullHighestValue }; orderEdmModel = orderDataAdapter.BuildEdmModelFromEfCoreModel(); order2DataAdapter = new OeEfCoreDataAdapter <Order2Context>(Create <Order2Context>(useRelationalNulls)) { IsDatabaseNullHighestValue = dataAdapter.IsDatabaseNullHighestValue }; return(order2DataAdapter.BuildEdmModelFromEfCoreModel(orderEdmModel)); }
private static void EnsureCreated(IEdmModel edmModel) { Db.OeDataAdapter dataAdapter = edmModel.GetDataAdapter(edmModel.EntityContainer); var dbContext = (DbContext)dataAdapter.CreateDataContext(); dbContext.Database.EnsureCreated(); if (dataAdapter.EntitySetAdapters.Find(typeof(OrderItemsView)) != null) { dbContext.Database.ExecuteSqlCommand( @"create view OrderItemsView(Name, Product) as select o.Name, i.Product from Orders o inner join OrderItems i on o.Id = i.OrderId"); } dataAdapter.CloseDataContext(dbContext); foreach (IEdmModel refModel in edmModel.ReferencedModels) { if (refModel.EntityContainer != null && refModel is EdmModel) { EnsureCreated(refModel); } } }
public virtual async Task Execute <T, TResult>(QueryParameters <T, TResult> parameters) { IList fromOe = await ExecuteOe <TResult>(parameters.RequestUri, parameters.NavigationNextLink, parameters.PageSize).ConfigureAwait(false); ODataUri odataUri = ParseUri(parameters.RequestUri); IEdmModel edmModel = EdmModel.GetEdmModel(odataUri.Path); Db.OeDataAdapter oeDataAdapter = edmModel.GetDataAdapter(edmModel.EntityContainer); Db.OeDataAdapter dbDataAdapter = ((ITestDbDataAdapter)oeDataAdapter).DbDataAdapter; IList fromDb; IReadOnlyList <IncludeVisitor.Include> includes; using (var dataContext = (DbContext)dbDataAdapter.CreateDataContext()) fromDb = TestHelper.ExecuteDb(dbDataAdapter, dataContext, parameters.Expression, out includes); //fix null navigation property Order where aggregate Order(Name) if (typeof(TResult) == typeof(Object)) { foreach (SortedDictionary <String, Object> item in fromOe) { foreach (KeyValuePair <String, Object> keyValue in item.Where(i => i.Value == null).ToList()) { PropertyInfo navigationProperty = typeof(T).GetProperty(keyValue.Key); if (navigationProperty != null && TestContractResolver.IsEntity(navigationProperty.PropertyType) && !includes.Any(i => i.Property == navigationProperty)) { item.Remove(keyValue.Key); } } } } Console.WriteLine(parameters.RequestUri); TestHelper.Compare(fromDb, fromOe, MetadataProvider, includes); }
public static int GetNestedCount(IEdmModel edmModel, Db.IOeDbEnumerator dbEnumerator) { Db.IOeDbEnumerator parentEnumerator = dbEnumerator.ParentEnumerator; ODataUri odataUri = OeNextPageLinkBuilder.GetCountODataUri(edmModel, parentEnumerator.EntryFactory, dbEnumerator.EntryFactory.NavigationSelectItem, parentEnumerator.Current); var queryContext = new OeQueryContext(edmModel, odataUri); IEdmEntitySet entitySet = (odataUri.Path.FirstSegment as EntitySetSegment).EntitySet; Db.OeDataAdapter dataAdapter = edmModel.GetDataAdapter(entitySet.Container); Object dataContext = null; try { dataContext = dataAdapter.CreateDataContext(); return(dataAdapter.ExecuteScalar <int>(dataContext, queryContext)); } finally { if (dataContext != null) { dataAdapter.CloseDataContext(dataContext); } } }
public override OrderContext CreateContext() { Db.OeDataAdapter dataAdapter = base.OeEdmModel.GetDataAdapter(typeof(OrderContext)); return((OrderContext)dataAdapter.CreateDataContext()); }
public InMemory.InMemoryOrder2Context CreateInMemoryContext() { Db.OeDataAdapter dataAdapter = base.OeEdmModel.GetDataAdapter(typeof(InMemory.InMemoryOrder2Context)); return((InMemory.InMemoryOrder2Context)dataAdapter.CreateDataContext()); }