2 //---------------------------------------------------------------------
3 // <copyright file="DbQueryCommandTree.cs" company="Microsoft">
4 // Copyright (c) Microsoft Corporation. All rights reserved.
8 // @backupOwner Microsoft
9 //---------------------------------------------------------------------
12 using System.Collections.Generic;
14 using System.Data.Metadata.Edm;
15 using System.Data.Common.CommandTrees.Internal;
17 using System.Diagnostics;
19 namespace System.Data.Common.CommandTrees
22 /// Represents a query operation expressed as a canonical command tree.
24 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db")]
25 public sealed class DbQueryCommandTree : DbCommandTree
28 private readonly DbExpression _query;
30 // Parameter information (will be retrieved from the query expression of the command tree during construction)
31 private System.Collections.ObjectModel.ReadOnlyCollection<DbParameterReferenceExpression> _parameters;
33 private DbQueryCommandTree(MetadataWorkspace metadata,
37 : base(metadata, dataSpace)
39 // Ensure the query expression is non-null
40 EntityUtil.CheckArgumentNull(query, "query");
44 // Use the valid workspace and data space to validate the query expression
45 DbExpressionValidator validator = new DbExpressionValidator(metadata, dataSpace);
46 validator.ValidateExpression(query, "query");
48 this._parameters = validator.Parameters.Select(paramInfo => paramInfo.Value).ToList().AsReadOnly();
54 /// Constructs a new DbQueryCommandTree that uses the specified metadata workspace.
56 /// <param name="metadata">The metadata workspace that the command tree should use.</param>
57 /// <param name="dataSpace">The logical 'space' that metadata in the expressions used in this command tree must belong to.</param>
58 /// <param name="query">A <see cref="DbExpression"/> that defines the logic of the query.</param>
59 /// <exception cref="ArgumentNullException"><paramref name="metadata"/> or <paramref name="query"/> is null</exception>
60 /// <exception cref="ArgumentException"><paramref name="dataSpace"/> does not represent a valid data space</exception>
61 /*CQT_PUBLIC_API(*/internal/*)*/ DbQueryCommandTree(MetadataWorkspace metadata, DataSpace dataSpace, DbExpression query)
62 : this(metadata, dataSpace, query, true)
68 /// Gets a <see cref="DbExpression"/> that defines the logic of the query.
70 public DbExpression Query
72 get { return this._query; }
75 internal override DbCommandTreeKind CommandTreeKind
77 get { return DbCommandTreeKind.Query; }
80 internal override IEnumerable<KeyValuePair<string, TypeUsage>> GetParameters()
82 if (this._parameters == null)
84 this._parameters = ParameterRetriever.GetParameters(this);
86 return this._parameters.Select(p => new KeyValuePair<string, TypeUsage>(p.ParameterName, p.ResultType));
89 internal override void DumpStructure(ExpressionDumper dumper)
91 if (this.Query != null)
93 dumper.Dump(this.Query, "Query");
97 internal override string PrintTree(ExpressionPrinter printer)
99 return printer.Print(this);
102 internal static DbQueryCommandTree FromValidExpression(MetadataWorkspace metadata, DataSpace dataSpace, DbExpression query)
105 return new DbQueryCommandTree(metadata, dataSpace, query);
107 return new DbQueryCommandTree(metadata, dataSpace, query, false);