2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Util / DataCommand.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 \r
31 namespace DbLinq.Util\r
32 {\r
33     /// <summary>\r
34     /// Executes a given SQL command, with parameter and delegate\r
35     /// </summary>\r
36 #if !MONO_STRICT\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
39 #endif\r
40     static class DataCommand\r
41     {\r
42         /// <summary>\r
43         /// Executes a provided SQL command, with parameter and callback for each row\r
44         /// </summary>\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
53         {\r
54             using (IDbCommand command = conn.CreateCommand())\r
55             {\r
56                 command.CommandText = sql;\r
57                 if (dbParameterName != null)\r
58                 {\r
59                     var parameter = command.CreateParameter();\r
60                     parameter.ParameterName = dbParameterName;\r
61                     parameter.Value = db;\r
62                     command.Parameters.Add(parameter);\r
63                 }\r
64                 using (var rdr = command.ExecuteReader())\r
65                 {\r
66                     var list = new List<T>();\r
67                     while (rdr.Read())\r
68                     {\r
69                         var t = readDelegate(rdr);\r
70                         // we may have a value type, here\r
71                         if (!Equals(t, default(T)))\r
72                             list.Add(t);\r
73                     }\r
74                     return list;\r
75                 }\r
76             }\r
77         }\r
78 \r
79         /// <summary>\r
80         /// Executes a provided SQL command, with parameter and callback for each row\r
81         /// </summary>\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
88         {\r
89             return Find<T>(conn, sql, null, null, readDelegate);\r
90         }\r
91     }\r
92 }\r