Adding System.Data..., System.ServiceModel..., and System.Web...
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Common / CommandTrees / DbQueryCommandTree.cs
1
2 //---------------------------------------------------------------------
3 // <copyright file="DbQueryCommandTree.cs" company="Microsoft">
4 //      Copyright (c) Microsoft Corporation.  All rights reserved.
5 // </copyright>
6 //
7 // @owner  [....]
8 // @backupOwner [....]
9 //---------------------------------------------------------------------
10
11 using System;
12 using System.Collections.Generic;
13
14 using System.Data.Metadata.Edm;
15 using System.Data.Common.CommandTrees.Internal;
16 using System.Linq;
17 using System.Diagnostics;
18
19 namespace System.Data.Common.CommandTrees
20 {
21     /// <summary>
22     /// Represents a query operation expressed as a canonical command tree.
23     /// </summary>
24     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db")]
25     public sealed class DbQueryCommandTree : DbCommandTree
26     {
27         // Query expression
28         private readonly DbExpression _query;
29
30         // Parameter information (will be retrieved from the query expression of the command tree during construction)
31         private System.Collections.ObjectModel.ReadOnlyCollection<DbParameterReferenceExpression> _parameters;
32
33         private DbQueryCommandTree(MetadataWorkspace metadata,
34                                    DataSpace dataSpace, 
35                                    DbExpression query,
36                                    bool validate)
37             : base(metadata, dataSpace)
38         {
39             // Ensure the query expression is non-null
40             EntityUtil.CheckArgumentNull(query, "query");
41
42             if (validate)
43             {
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");
47
48                 this._parameters = validator.Parameters.Select(paramInfo => paramInfo.Value).ToList().AsReadOnly();
49             }
50             this._query = query;
51         }
52
53         /// <summary>
54         /// Constructs a new DbQueryCommandTree that uses the specified metadata workspace.
55         /// </summary>
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)
63         {
64             
65         }
66
67         /// <summary>
68         /// Gets a <see cref="DbExpression"/> that defines the logic of the query.
69         /// </summary>
70         public DbExpression Query
71         {
72             get { return this._query; }
73         }
74                         
75         internal override DbCommandTreeKind CommandTreeKind
76         {
77             get { return DbCommandTreeKind.Query; }
78         }
79
80         internal override IEnumerable<KeyValuePair<string, TypeUsage>> GetParameters()
81         {
82             if (this._parameters == null)
83             {
84                 this._parameters = ParameterRetriever.GetParameters(this);
85             }
86             return this._parameters.Select(p => new KeyValuePair<string, TypeUsage>(p.ParameterName, p.ResultType));
87         }
88
89         internal override void DumpStructure(ExpressionDumper dumper)
90         {
91             if (this.Query != null)
92             {
93                 dumper.Dump(this.Query, "Query");
94             }
95         }
96
97         internal override string PrintTree(ExpressionPrinter printer)
98         {
99             return printer.Print(this); 
100         }
101
102         internal static DbQueryCommandTree FromValidExpression(MetadataWorkspace metadata, DataSpace dataSpace, DbExpression query)
103         {
104 #if DEBUG
105             return new DbQueryCommandTree(metadata, dataSpace, query);
106 #else
107             return new DbQueryCommandTree(metadata, dataSpace, query, false);
108 #endif
109         }
110     }
111 }