5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\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
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\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
28 using System.Collections.Generic;
\r
30 using DbLinq.Data.Linq.Database;
\r
33 using System.Data.Linq;
\r
35 using DbLinq.Data.Linq;
\r
38 using DbLinq.Data.Linq.Mapping;
\r
39 using DbLinq.Data.Linq.Sql;
\r
40 using DbLinq.Data.Linq.Sugar.Expressions;
\r
42 namespace DbLinq.Data.Linq.Sugar
\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
48 internal class SelectQuery : AbstractQuery
\r
51 /// Parameters to be sent as SQL parameters
\r
53 public IList<InputParameterExpression> InputParameters { get; private set; }
\r
56 /// Expression that creates a row object
\r
57 /// Use GetRowObjectCreator() to access the object with type safety
\r
59 internal Delegate RowObjectCreator { get; private set; }
\r
62 /// Returns the row object creator, strongly typed
\r
64 /// <typeparam name="T"></typeparam>
\r
65 /// <returns></returns>
\r
66 public Func<IDataRecord, MappingContext, T> GetRowObjectCreator<T>()
\r
68 return (Func<IDataRecord, MappingContext, T>)RowObjectCreator;
\r
72 /// Used on scalar calls, like First()
\r
74 public string ExecuteMethodName { get; private set; }
\r
76 public SelectQuery(DataContext dataContext, SqlStatement sql, IList<InputParameterExpression> parameters,
\r
77 Delegate rowObjectCreator, string executeMethodName)
\r
78 : base(dataContext, sql)
\r
80 InputParameters = parameters;
\r
81 RowObjectCreator = rowObjectCreator;
\r
82 ExecuteMethodName = executeMethodName;
\r
85 public override ITransactionalCommand GetCommand()
\r
87 IDbDataParameter dbParameter;
\r
88 var dbCommand = base.GetCommand(false);
\r
89 foreach (var parameter in InputParameters)
\r
91 if (parameter.Type.IsArray)
\r
94 foreach (object p in (Array)parameter.GetValue())
\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
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