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