Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Common / CommandTrees / DbFunctionCommandTree.cs
1 //---------------------------------------------------------------------
2 // <copyright file="DbFunctionCommandTree.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner  [....]
7 // @backupOwner [....]
8 //---------------------------------------------------------------------
9
10 using System;
11 using System.Collections.Generic;
12
13 using System.Data.Metadata.Edm;
14 using System.Data.Common.CommandTrees.Internal;
15 using System.Linq;
16
17 namespace System.Data.Common.CommandTrees
18 {
19
20     /// <summary>
21     /// Represents a function invocation expressed as a canonical command tree
22     /// </summary>
23     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db")]
24     public sealed class DbFunctionCommandTree : DbCommandTree
25     {
26         private readonly EdmFunction _edmFunction;
27         private readonly TypeUsage _resultType;
28         private readonly System.Collections.ObjectModel.ReadOnlyCollection<string> _parameterNames;
29         private readonly System.Collections.ObjectModel.ReadOnlyCollection<TypeUsage> _parameterTypes;
30
31         /// <summary>
32         /// Constructs a new DbFunctionCommandTree that uses the specified metadata workspace, data space and function metadata
33         /// </summary>
34         /// <param name="metadata">The metadata workspace that the command tree should use.</param>
35         /// <param name="dataSpace">The logical 'space' that metadata in the expressions used in this command tree must belong to.</param>
36         /// <param name="edmFunction"></param>
37         /// <param name="resultType"></param>
38         /// <param name="parameters"></param>
39         /// <exception cref="ArgumentNullException"><paramref name="metadata"/>, <paramref name="dataSpace"/> or <paramref name="edmFunction"/> is null</exception>
40         /// <exception cref="ArgumentException"><paramref name="dataSpace"/> does not represent a valid data space or
41         /// <paramref name="edmFunction">is a composable function</paramref></exception>
42         /*CQT_PUBLIC_API(*/internal/*)*/ DbFunctionCommandTree(MetadataWorkspace metadata, DataSpace dataSpace, EdmFunction edmFunction, TypeUsage resultType, IEnumerable<KeyValuePair<string, TypeUsage>> parameters)
43             : base(metadata, dataSpace)
44         {
45             EntityUtil.CheckArgumentNull(edmFunction, "edmFunction");
46
47             _edmFunction = edmFunction;
48             _resultType = resultType;
49
50             List<string> paramNames = new List<string>();
51             List<TypeUsage> paramTypes = new List<TypeUsage>();
52             if (parameters != null)
53             {
54                 foreach (KeyValuePair<string, TypeUsage> paramInfo in parameters)
55                 {
56                     paramNames.Add(paramInfo.Key);
57                     paramTypes.Add(paramInfo.Value);
58                 }
59             }
60
61             _parameterNames = paramNames.AsReadOnly();
62             _parameterTypes = paramTypes.AsReadOnly();
63         }
64
65         /// <summary>
66         /// Gets the <see cref="EdmFunction"/> that represents the function to invoke
67         /// </summary>
68         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Edm")]
69         public EdmFunction EdmFunction
70         {
71             get
72             {
73                 return _edmFunction;
74             }
75         }
76
77         /// <summary>
78         /// Gets the result type of the function; currently constrained to be a Collection of
79         /// RowTypes. Unlike typical RowType instance, merely indicates name/type not parameter
80         /// order.
81         /// </summary>
82         public TypeUsage ResultType
83         {
84             get
85             {
86                 return _resultType;
87             }
88         }
89
90         internal override DbCommandTreeKind CommandTreeKind
91         {
92             get { return DbCommandTreeKind.Function; }
93         }
94
95         internal override IEnumerable<KeyValuePair<string, TypeUsage>> GetParameters()
96         {
97             for (int idx = 0; idx < this._parameterNames.Count; idx++)
98             {
99                 yield return new KeyValuePair<string, TypeUsage>(this._parameterNames[idx], this._parameterTypes[idx]);
100             }
101         }
102
103         internal override void DumpStructure(ExpressionDumper dumper)
104         {
105             if (this.EdmFunction != null)
106             {
107                 dumper.Dump(this.EdmFunction);
108             }
109         }
110
111         internal override string PrintTree(ExpressionPrinter printer)
112         {
113             return printer.Print(this);
114         }
115     }
116 }