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
31 namespace DbLinq.Util
\r
34 /// Executes a given SQL command, with parameter and delegate
\r
37 // TODO: once R# is fixed with internal extension methods problems, switch to full internal
\r
38 public // DataCommand is used by vendors
\r
40 static class DataCommand
\r
43 /// Executes a provided SQL command, with parameter and callback for each row
\r
45 /// <typeparam name="T">Row type</typeparam>
\r
46 /// <param name="conn">Connection to database</param>
\r
47 /// <param name="sql">SQL string</param>
\r
48 /// <param name="dbParameterName">Optional parameter name (null to ignore), like ':db'</param>
\r
49 /// <param name="db">Optional parameter value</param>
\r
50 /// <param name="readDelegate">Function called for each row, returning an instance created for row data</param>
\r
51 /// <returns></returns>
\r
52 public static List<T> Find<T>(IDbConnection conn, string sql, string dbParameterName, string db, Func<IDataReader, T> readDelegate)
\r
54 using (IDbCommand command = conn.CreateCommand())
\r
56 command.CommandText = sql;
\r
57 if (dbParameterName != null)
\r
59 var parameter = command.CreateParameter();
\r
60 parameter.ParameterName = dbParameterName;
\r
61 parameter.Value = db;
\r
62 command.Parameters.Add(parameter);
\r
64 using (var rdr = command.ExecuteReader())
\r
66 var list = new List<T>();
\r
69 var t = readDelegate(rdr);
\r
70 // we may have a value type, here
\r
71 if (!Equals(t, default(T)))
\r
80 /// Executes a provided SQL command, with parameter and callback for each row
\r
82 /// <typeparam name="T">Row type</typeparam>
\r
83 /// <param name="conn">Connection to database</param>
\r
84 /// <param name="sql">SQL string</param>
\r
85 /// <param name="readDelegate">Function called for each row, returning an instance created for row data</param>
\r
86 /// <returns></returns>
\r
87 public static List<T> Find<T>(IDbConnection conn, string sql, Func<IDataReader, T> readDelegate)
\r
89 return Find<T>(conn, sql, null, null, readDelegate);
\r