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