public void Prepare_returns_a_new_instance() { var objectQueryExecutionPlanFactory = new ObjectQueryExecutionPlanFactory( Common.Internal.Materialization.MockHelper.CreateTranslator <object>()); var edmItemCollection = new EdmItemCollection(); var fakeSqlProviderManifest = FakeSqlProviderServices.Instance.GetProviderManifest("2008"); var storeItemCollection = new StoreItemCollection(FakeSqlProviderFactory.Instance, fakeSqlProviderManifest, "System.Data.SqlClient", "2008"); var mappingItemCollection = new StorageMappingItemCollection(edmItemCollection, storeItemCollection, Enumerable.Empty <XmlReader>()); var metadataWorkspace = new MetadataWorkspace( () => edmItemCollection, () => storeItemCollection, () => mappingItemCollection); var fakeSqlConnection = new FakeSqlConnection(); fakeSqlConnection.ConnectionString = "foo"; var entityConnection = new EntityConnection(metadataWorkspace, fakeSqlConnection, false); var objectContext = new ObjectContext(entityConnection); var dbExpression = new DbNullExpression(TypeUsage.Create(fakeSqlProviderManifest.GetStoreTypes().First())); var dbQueryCommandTree = new DbQueryCommandTree( metadataWorkspace, DataSpace.CSpace, dbExpression, validate: false, useDatabaseNullSemantics: true); var parameters = new List <Tuple <ObjectParameter, QueryParameterExpression> >(); var objectQueryExecutionPlan = objectQueryExecutionPlanFactory.Prepare( objectContext, dbQueryCommandTree, typeof(object), MergeOption.NoTracking, false, new Span(), parameters, aliasGenerator: null); Assert.NotNull(objectQueryExecutionPlan); }
public void Prepare_returns_a_new_instance() { var objectQueryExecutionPlanFactory = new ObjectQueryExecutionPlanFactory( Common.Internal.Materialization.MockHelper.CreateTranslator<object>()); var metadataWorkspace = new MetadataWorkspace(); var edmItemCollection = new EdmItemCollection(); metadataWorkspace.RegisterItemCollection(edmItemCollection); metadataWorkspace.RegisterItemCollection(new ObjectItemCollection()); var fakeSqlProviderManifest = new FakeSqlProviderServices().GetProviderManifest("2008"); var storeItemCollection = new StoreItemCollection(FakeSqlProviderFactory.Instance, fakeSqlProviderManifest, "2008"); metadataWorkspace.RegisterItemCollection(storeItemCollection); metadataWorkspace.RegisterItemCollection(new StorageMappingItemCollection(edmItemCollection, storeItemCollection, Enumerable.Empty<XmlReader>())); var fakeSqlConnection = new FakeSqlConnection(); fakeSqlConnection.ConnectionString = "foo"; var entityConnection = new EntityConnection(metadataWorkspace, fakeSqlConnection, false); var objectContext = new ObjectContext(entityConnection); var dbExpression = new DbNullExpression(TypeUsage.Create(fakeSqlProviderManifest.GetStoreTypes().First())); var dbQueryCommandTree = new DbQueryCommandTree(metadataWorkspace, DataSpace.CSpace, dbExpression, validate: false); var parameters = new List<Tuple<ObjectParameter, QueryParameterExpression>>(); var objectQueryExecutionPlan = objectQueryExecutionPlanFactory.Prepare(objectContext, dbQueryCommandTree, typeof(object), MergeOption.NoTracking, new Span(), parameters, aliasGenerator: null); Assert.NotNull(objectQueryExecutionPlan); }
// <summary> // Creates a new compiled query state instance // </summary> // <param name="elementType"> The element type of the new instance (the 'T' of the ObjectQuery < T > that the new state instance will back)" </param> // <param name="context"> The object context with which the new instance should be associated </param> // <param name="lambda"> // The compiled query definition, as a <see cref="LambdaExpression" /> // </param> // <param name="cacheToken"> The cache token to use when retrieving or storing the new instance's execution plan in the query cache </param> // <param name="parameterValues"> The values passed into the CompiledQuery delegate </param> internal CompiledELinqQueryState( Type elementType, ObjectContext context, LambdaExpression lambda, Guid cacheToken, object[] parameterValues, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory = null) : base(elementType, context, lambda) { DebugCheck.NotNull(parameterValues); _cacheToken = cacheToken; _parameterValues = parameterValues; EnsureParameters(); Parameters.SetReadOnly(true); _objectQueryExecutionPlanFactory = objectQueryExecutionPlanFactory ?? new ObjectQueryExecutionPlanFactory(); }
public virtual ObjectQueryExecutionPlan Prepare( ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, bool streaming, Span span, IEnumerable <Tuple <ObjectParameter, QueryParameterExpression> > compiledQueryParameters, AliasGenerator aliasGenerator) { TypeUsage resultType = tree.Query.ResultType; DbExpression newQuery; SpanIndex spanInfo; if (ObjectSpanRewriter.TryRewrite(tree, span, mergeOption, aliasGenerator, out newQuery, out spanInfo)) { tree = DbQueryCommandTree.FromValidExpression(tree.MetadataWorkspace, tree.DataSpace, newQuery, tree.UseDatabaseNullSemantics); } else { spanInfo = (SpanIndex)null; } EntityCommandDefinition commandDefinition = ObjectQueryExecutionPlanFactory.CreateCommandDefinition(context, tree); ShaperFactory resultShaperFactory = System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap(this._translator, elementType, commandDefinition.CreateColumnMap((DbDataReader)null), context.MetadataWorkspace, spanInfo, mergeOption, streaming, false); EntitySet singleEntitySet = (EntitySet)null; if (resultType.EdmType.BuiltInTypeKind == BuiltInTypeKind.CollectionType && commandDefinition.EntitySets != null) { foreach (EntitySet entitySet in commandDefinition.EntitySets) { if (entitySet != null && entitySet.ElementType.IsAssignableFrom(((CollectionType)resultType.EdmType).TypeUsage.EdmType)) { if (singleEntitySet == null) { singleEntitySet = entitySet; } else { singleEntitySet = (EntitySet)null; break; } } } } return(new ObjectQueryExecutionPlan((DbCommandDefinition)commandDefinition, resultShaperFactory, resultType, mergeOption, streaming, singleEntitySet, compiledQueryParameters)); }
private static ObjectContext CreateObjectContext( Mock<EntityConnection> entityConnectionMock = null, Mock<ObjectStateManager> objectStateManagerMock = null, Mock<MetadataWorkspace> metadataWorkspaceMock = null, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory = null, Translator translator = null, ColumnMapFactory columnMapFactory = null, DbCommand dbCommand = null) { if (objectStateManagerMock == null) { objectStateManagerMock = new Mock<ObjectStateManager>(); } if (metadataWorkspaceMock == null) { metadataWorkspaceMock = new Mock<MetadataWorkspace>(); metadataWorkspaceMock.Setup(m => m.IsItemCollectionAlreadyRegistered(DataSpace.OSpace)).Returns(true); metadataWorkspaceMock.Setup(m => m.GetItemCollection(DataSpace.OCSpace)).Returns(default(ItemCollection)); metadataWorkspaceMock.Setup(m => m.IsItemCollectionAlreadyRegistered(DataSpace.SSpace)).Returns(true); var storeItemCollection = new StoreItemCollection( GenericProviderFactory<DbProviderFactory>.Instance, new SqlProviderManifest("2008"), "System.Data.FakeSqlClient", "2008"); metadataWorkspaceMock.Setup(m => m.GetItemCollection(DataSpace.SSpace)).Returns(storeItemCollection); } if (entityConnectionMock == null) { var dbConnectionMock = new Mock<DbConnection>(); if (dbCommand != null) { dbConnectionMock.Protected().Setup<DbCommand>("CreateDbCommand").Returns(() => dbCommand); } dbConnectionMock.Setup(m => m.DataSource).Returns("fakeDb"); entityConnectionMock = new Mock<EntityConnection>(); entityConnectionMock.SetupGet(m => m.ConnectionString).Returns("BarConnection"); entityConnectionMock.SetupGet(m => m.State).Returns(() => ConnectionState.Open); entityConnectionMock.SetupGet(m => m.StoreConnection).Returns(dbConnectionMock.Object); entityConnectionMock.Setup(m => m.GetMetadataWorkspace()).Returns(metadataWorkspaceMock.Object); } var objectContextMock = new Mock<ObjectContext>(objectQueryExecutionPlanFactory, translator, columnMapFactory, null) { CallBase = true }; objectContextMock.Setup(m => m.Connection).Returns(entityConnectionMock.Object); objectContextMock.Setup(m => m.ObjectStateManager).Returns(() => objectStateManagerMock.Object); objectContextMock.Setup(m => m.MetadataWorkspace).Returns(() => metadataWorkspaceMock.Object); objectContextMock.Setup(m => m.DefaultContainerName).Returns("Bar"); return objectContextMock.Object; }