// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. namespace System.Data.Entity.Infrastructure { using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data.Entity.Internal; using System.Data.Entity.Utilities; using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; /// /// Represents a SQL query for non-entities that is created from a /// and is executed using the connection from that context. /// Instances of this class are obtained from the instance. /// The query is not executed when this object is created; it is executed /// each time it is enumerated, for example by using foreach. /// SQL queries for entities are created using . /// See for a generic version of this class. /// [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] [SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface")] public class DbRawSqlQuery : IEnumerable, IListSource #if !NET40 , IDbAsyncEnumerable #endif { #region Constructors and fields private readonly InternalSqlQuery _internalQuery; /// /// Initializes a new instance of the class. /// /// The internal query. internal DbRawSqlQuery(InternalSqlQuery internalQuery) { _internalQuery = internalQuery; } #endregion #region AsStreaming /// /// Returns a new query that will stream the results instead of buffering. /// /// A new query with AsStreaming applied. public DbRawSqlQuery AsStreaming() { return new DbRawSqlQuery(_internalQuery.AsStreaming()); } #endregion #region IEnumerable implementation /// /// Returns an which when enumerated will execute the SQL query against the database. /// /// /// An object that can be used to iterate through the elements. /// public IEnumerator GetEnumerator() { return _internalQuery.GetEnumerator(); } #endregion #region IDbAsyncEnumerable implementation #if !NET40 /// /// Returns an which when enumerated will execute the SQL query against the database. /// /// /// An object that can be used to iterate through the elements. /// [SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")] IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator() { return _internalQuery.GetAsyncEnumerator(); } #endif #endregion #region Access to IDbAsyncEnumerable extensions #if !NET40 public Task ForEachAsync(Action action) { Check.NotNull(action, "action"); return ((IDbAsyncEnumerable)this).ForEachAsync(action, CancellationToken.None); } public Task ForEachAsync(Action action, CancellationToken cancellationToken) { Check.NotNull(action, "action"); return ((IDbAsyncEnumerable)this).ForEachAsync(action, cancellationToken); } [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] public Task> ToListAsync() { return ((IDbAsyncEnumerable)this).ToListAsync(); } [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] public Task> ToListAsync(CancellationToken cancellationToken) { return ((IDbAsyncEnumerable)this).ToListAsync(cancellationToken); } #endif #endregion #region ToString /// /// Returns a that contains the SQL string that was set /// when the query was created. The parameters are not included. /// /// /// A that represents this instance. /// public override string ToString() { return _internalQuery.ToString(); } #endregion #region Access to internal query /// /// Gets the internal query. /// /// The internal query. internal InternalSqlQuery InternalQuery { get { return _internalQuery; } } #endregion #region IListSource implementation /// /// Returns false. /// /// /// false . /// [SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")] bool IListSource.ContainsListCollection { get { // Note that _internalQuery will always return false; return _internalQuery.ContainsListCollection; } } /// /// Throws an exception indicating that binding directly to a store query is not supported. /// /// Never returns; always throws. [SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")] IList IListSource.GetList() { // Note that _internalQuery will always throw; return _internalQuery.GetList(); } #endregion #region Hidden Object methods [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { return base.Equals(obj); } [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { return base.GetHashCode(); } [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] [EditorBrowsable(EditorBrowsableState.Never)] public new Type GetType() { return base.GetType(); } #endregion } }