2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq.Sqlite / SqliteVendor.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 using System;\r
27 using System.Linq;\r
28 using System.Collections.Generic;\r
29 using System.Text;\r
30 using System.Data;\r
31 using System.Data.Linq.Mapping;\r
32 using System.Reflection;\r
33 #if MONO_STRICT\r
34 using System.Data.Linq;\r
35 using System.Data.Linq.SqlClient;\r
36 #else\r
37 using DbLinq.Data.Linq;\r
38 using DbLinq.Data.Linq.SqlClient;\r
39 #endif\r
40 using DbLinq.Sqlite;\r
41 using DbLinq.Util;\r
42 using DbLinq.Vendor;\r
43 \r
44 namespace DbLinq.Sqlite\r
45 {\r
46     /// <summary>\r
47     /// SQLite - specific code.\r
48     /// </summary>\r
49     [Vendor(typeof(SqliteProvider))]\r
50 #if MONO_STRICT\r
51     internal\r
52 #else\r
53     public\r
54 #endif\r
55  class SqliteVendor : Vendor.Implementation.Vendor\r
56     {\r
57         public override string VendorName { get { return "SQLite"; } }\r
58 \r
59         protected readonly SqliteSqlProvider sqlProvider = new SqliteSqlProvider();\r
60         public override ISqlProvider SqlProvider { get { return sqlProvider; } }\r
61 \r
62         /// <summary>\r
63         /// call SQLite stored proc or stored function, \r
64         /// optionally return DataSet, and collect return params.\r
65         /// </summary>\r
66         public override System.Data.Linq.IExecuteResult ExecuteMethodCall(DataContext context, MethodInfo method\r
67                                                                  , params object[] inputValues)\r
68         {\r
69             if (method == null)\r
70                 throw new ArgumentNullException("L56 Null 'method' parameter");\r
71 \r
72             //check to make sure there is exactly one [FunctionEx]? that's below.\r
73             //FunctionAttribute functionAttrib = GetFunctionAttribute(method);\r
74             var functionAttrib = context.Mapping.GetFunction(method);\r
75 \r
76             ParameterInfo[] paramInfos = method.GetParameters();\r
77             //int numRequiredParams = paramInfos.Count(p => p.IsIn || p.IsRetval);\r
78             //if (numRequiredParams != inputValues.Length)\r
79             //    throw new ArgumentException("L161 Argument count mismatch");\r
80 \r
81             string sp_name = functionAttrib.MappedName;\r
82 \r
83             using (IDbCommand command = context.Connection.CreateCommand())\r
84             {\r
85                 command.CommandText = sp_name;\r
86                 //SQLiteCommand command = new SQLiteCommand("select hello0()");\r
87                 int currInputIndex = 0;\r
88 \r
89                 List<string> paramNames = new List<string>();\r
90                 for (int i = 0; i < paramInfos.Length; i++)\r
91                 {\r
92                     ParameterInfo paramInfo = paramInfos[i];\r
93 \r
94                     //TODO: check to make sure there is exactly one [Parameter]?\r
95                     ParameterAttribute paramAttrib = paramInfo.GetCustomAttributes(false).OfType<ParameterAttribute>().Single();\r
96 \r
97                     string paramName = "?" + paramAttrib.Name; //eg. '?param1'\r
98                     paramNames.Add(paramName);\r
99 \r
100                     System.Data.ParameterDirection direction = GetDirection(paramInfo, paramAttrib);\r
101                     //SQLiteDbType dbType = SQLiteTypeConversions.ParseType(paramAttrib.DbType);\r
102                     IDataParameter cmdParam = command.CreateParameter();\r
103                     cmdParam.ParameterName = paramName;\r
104                     //cmdParam.Direction = System.Data.ParameterDirection.Input;\r
105                     if (direction == ParameterDirection.Input || direction == ParameterDirection.InputOutput)\r
106                     {\r
107                         object inputValue = inputValues[currInputIndex++];\r
108                         cmdParam.Value = inputValue;\r
109                     }\r
110                     else\r
111                     {\r
112                         cmdParam.Value = null;\r
113                     }\r
114                     cmdParam.Direction = direction;\r
115                     command.Parameters.Add(cmdParam);\r
116                 }\r
117 \r
118                 if (!functionAttrib.IsComposable)\r
119                 {\r
120                     //procedures: under the hood, this seems to prepend 'CALL '\r
121                     command.CommandType = System.Data.CommandType.StoredProcedure;\r
122                 }\r
123                 else\r
124                 {\r
125                     //functions: 'SELECT myFunction()' or 'SELECT hello(?s)'\r
126                     string cmdText = "SELECT " + command.CommandText + "($args)";\r
127                     cmdText = cmdText.Replace("$args", string.Join(",", paramNames.ToArray()));\r
128                     command.CommandText = cmdText;\r
129                 }\r
130 \r
131                 if (method.ReturnType == typeof(DataSet))\r
132                 {\r
133                     //unknown shape of resultset:\r
134                     System.Data.DataSet dataSet = new DataSet();\r
135                     IDbDataAdapter adapter = CreateDataAdapter(context);\r
136                     adapter.SelectCommand = command;\r
137                     adapter.Fill(dataSet);\r
138                     List<object> outParamValues = CopyOutParams(paramInfos, command.Parameters);\r
139                     return new ProcedureResult(dataSet, outParamValues.ToArray());\r
140                 }\r
141                 else\r
142                 {\r
143                     object obj = command.ExecuteScalar();\r
144                     List<object> outParamValues = CopyOutParams(paramInfos, command.Parameters);\r
145                     return new ProcedureResult(obj, outParamValues.ToArray());\r
146                 }\r
147             }\r
148         }\r
149 \r
150         static System.Data.ParameterDirection GetDirection(ParameterInfo paramInfo, ParameterAttribute paramAttrib)\r
151         {\r
152             //strange hack to determine what's a ref, out parameter:\r
153             //http://lists.ximian.com/pipermain/mono-list/2003-March/012751.html\r
154             bool hasAmpersand = paramInfo.ParameterType.FullName.Contains('&');\r
155             if (paramInfo.IsOut)\r
156                 return System.Data.ParameterDirection.Output;\r
157             if (hasAmpersand)\r
158                 return System.Data.ParameterDirection.InputOutput;\r
159             return System.Data.ParameterDirection.Input;\r
160         }\r
161 \r
162         /// <summary>\r
163         /// Collect all Out or InOut param values, casting them to the correct .net type.\r
164         /// </summary>\r
165         private List<object> CopyOutParams(ParameterInfo[] paramInfos, IDataParameterCollection paramSet)\r
166         {\r
167             List<object> outParamValues = new List<object>();\r
168             //Type type_t = typeof(T);\r
169             int i = -1;\r
170             foreach (IDataParameter param in paramSet)\r
171             {\r
172                 i++;\r
173                 if (param.Direction == System.Data.ParameterDirection.Input)\r
174                 {\r
175                     outParamValues.Add("unused");\r
176                     continue;\r
177                 }\r
178 \r
179                 object val = param.Value;\r
180                 Type desired_type = paramInfos[i].ParameterType;\r
181 \r
182                 if (desired_type.Name.EndsWith("&"))\r
183                 {\r
184                     //for ref and out parameters, we need to tweak ref types, e.g.\r
185                     // "System.Int32&, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"\r
186                     string fullName1 = desired_type.AssemblyQualifiedName;\r
187                     string fullName2 = fullName1.Replace("&", "");\r
188                     desired_type = Type.GetType(fullName2);\r
189                 }\r
190                 try\r
191                 {\r
192                     //fi.SetValue(t, val); //fails with 'System.Decimal cannot be converted to Int32'\r
193                     //DbLinq.util.FieldUtils.SetObjectIdField(t, fi, val);\r
194                     //object val2 = DbLinq.Util.FieldUtils.CastValue(val, desired_type);\r
195                     object val2 = TypeConvert.To(val, desired_type);\r
196                     outParamValues.Add(val2);\r
197                 }\r
198                 catch (Exception)\r
199                 {\r
200                     //fails with 'System.Decimal cannot be converted to Int32'\r
201                     //Logger.Write(Level.Error, "CopyOutParams ERROR L245: failed on CastValue(): " + ex.Message);\r
202                 }\r
203             }\r
204             return outParamValues;\r
205         }\r
206     }\r
207 }\r