2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Data / Linq / Sugar / SelectQuery.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
8 // of this software and associated documentation files (the "Software"), to deal\r
9 // in the Software without restriction, including without limitation the rights\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
11 // copies of the Software, and to permit persons to whom the Software is\r
12 // furnished to do so, subject to the following conditions:\r
13 // \r
14 // The above copyright notice and this permission notice shall be included in\r
15 // all copies or substantial portions of the Software.\r
16 // \r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 \r
27 using System;\r
28 using System.Collections.Generic;\r
29 using System.Data;\r
30 using DbLinq.Data.Linq.Database;\r
31 \r
32 #if MONO_STRICT\r
33 using System.Data.Linq.Sql;\r
34 using System.Data.Linq.Sugar.Expressions;\r
35 using MappingContext = System.Data.Linq.Mapping.MappingContext;\r
36 #else\r
37 using DbLinq.Data.Linq.Sql;\r
38 using DbLinq.Data.Linq.Sugar.Expressions;\r
39 using MappingContext = DbLinq.Data.Linq.Mapping.MappingContext;\r
40 #endif\r
41 \r
42 #if MONO_STRICT\r
43 namespace System.Data.Linq.Sugar\r
44 #else\r
45 namespace DbLinq.Data.Linq.Sugar\r
46 #endif\r
47 {\r
48     /// <summary>\r
49     /// Represents a linq query, parsed and compiled, to be sent to database\r
50     /// This instance is immutable, since it can be stored in a cache\r
51     /// </summary>\r
52     internal class SelectQuery : AbstractQuery\r
53     {\r
54         /// <summary>\r
55         /// Parameters to be sent as SQL parameters\r
56         /// </summary>\r
57         public IList<InputParameterExpression> InputParameters { get; private set; }\r
58 \r
59         /// <summary>\r
60         /// Expression that creates a row object\r
61         /// Use GetRowObjectCreator() to access the object with type safety\r
62         /// </summary>\r
63         internal Delegate RowObjectCreator { get; private set; }\r
64 \r
65         /// <summary>\r
66         /// Returns the row object creator, strongly typed\r
67         /// </summary>\r
68         /// <typeparam name="T"></typeparam>\r
69         /// <returns></returns>\r
70         public Func<IDataRecord, MappingContext, T> GetRowObjectCreator<T>()\r
71         {\r
72             return (Func<IDataRecord, MappingContext, T>)RowObjectCreator;\r
73         }\r
74 \r
75         /// <summary>\r
76         /// Used on scalar calls, like First()\r
77         /// </summary>\r
78         public string ExecuteMethodName { get; private set; }\r
79 \r
80         public SelectQuery(DataContext dataContext, SqlStatement sql, IList<InputParameterExpression> parameters,\r
81                      Delegate rowObjectCreator, string executeMethodName)\r
82             : base(dataContext, sql)\r
83         {\r
84             InputParameters = parameters;\r
85             RowObjectCreator = rowObjectCreator;\r
86             ExecuteMethodName = executeMethodName;\r
87         }\r
88 \r
89         public override ITransactionalCommand GetCommand()\r
90         {\r
91             var dbCommand = base.GetCommand(false);\r
92             foreach (var parameter in InputParameters)\r
93             {\r
94                 var dbParameter = dbCommand.Command.CreateParameter();\r
95                 dbParameter.ParameterName = DataContext.Vendor.SqlProvider.GetParameterName(parameter.Alias);\r
96                 dbParameter.Value = parameter.GetValue();\r
97                 dbCommand.Command.Parameters.Add(dbParameter);\r
98             }\r
99             return dbCommand;\r
100         }\r
101     }\r
102 }\r