2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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;\r
34 #else\r
35 using DbLinq.Data.Linq;\r
36 #endif\r
37 \r
38 using DbLinq.Data.Linq.Mapping;\r
39 using DbLinq.Data.Linq.Sql;\r
40 using DbLinq.Data.Linq.Sugar.Expressions;\r
41 \r
42 namespace DbLinq.Data.Linq.Sugar\r
43 {\r
44     /// <summary>\r
45     /// Represents a linq query, parsed and compiled, to be sent to database\r
46     /// This instance is immutable, since it can be stored in a cache\r
47     /// </summary>\r
48     internal class SelectQuery : AbstractQuery\r
49     {\r
50         /// <summary>\r
51         /// Parameters to be sent as SQL parameters\r
52         /// </summary>\r
53         public IList<InputParameterExpression> InputParameters { get; private set; }\r
54 \r
55         /// <summary>\r
56         /// Expression that creates a row object\r
57         /// Use GetRowObjectCreator() to access the object with type safety\r
58         /// </summary>\r
59         internal Delegate RowObjectCreator { get; private set; }\r
60 \r
61         /// <summary>\r
62         /// Returns the row object creator, strongly typed\r
63         /// </summary>\r
64         /// <typeparam name="T"></typeparam>\r
65         /// <returns></returns>\r
66         public Func<IDataRecord, MappingContext, T> GetRowObjectCreator<T>()\r
67         {\r
68             return (Func<IDataRecord, MappingContext, T>)RowObjectCreator;\r
69         }\r
70 \r
71         /// <summary>\r
72         /// Used on scalar calls, like First()\r
73         /// </summary>\r
74         public string ExecuteMethodName { get; private set; }\r
75 \r
76         public SelectQuery(DataContext dataContext, SqlStatement sql, IList<InputParameterExpression> parameters,\r
77                      Delegate rowObjectCreator, string executeMethodName)\r
78             : base(dataContext, sql)\r
79         {\r
80             InputParameters = parameters;\r
81             RowObjectCreator = rowObjectCreator;\r
82             ExecuteMethodName = executeMethodName;\r
83         }\r
84 \r
85         public override ITransactionalCommand GetCommand()\r
86         {\r
87             IDbDataParameter dbParameter;\r
88             var dbCommand = base.GetCommand(false);\r
89             foreach (var parameter in InputParameters)\r
90             {\r
91                 if (parameter.Type.IsArray)\r
92                 {\r
93                     int i = 0;\r
94                     foreach (object p in (Array)parameter.GetValue())\r
95                     {\r
96                         dbParameter = dbCommand.Command.CreateParameter();\r
97                         dbParameter.ParameterName = DataContext.Vendor.SqlProvider.GetParameterName(parameter.Alias + i.ToString());\r
98                         dbParameter.Value = p;\r
99                         dbCommand.Command.Parameters.Add(dbParameter);\r
100                         ++i;\r
101                     }\r
102                 }\r
103                 else\r
104                 {\r
105                     dbParameter = dbCommand.Command.CreateParameter();\r
106                     dbParameter.ParameterName = DataContext.Vendor.SqlProvider.GetParameterName(parameter.Alias);\r
107                     dbParameter.Value = parameter.GetValue();\r
108                     dbCommand.Command.Parameters.Add(dbParameter);\r
109                 }\r
110             }\r
111             return dbCommand;\r
112         }\r
113     }\r
114 }\r