Revert 132601, 132602 as it made the csharp console stop working
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Vendor / Implementation / Vendor.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.Reflection;\r
29 using System.Collections.Generic;\r
30 using System.Data;\r
31 using System.Linq;\r
32 \r
33 #if MONO_STRICT\r
34 using DataContext = System.Data.Linq.DataContext;\r
35 using Data = System.Data;\r
36 using System.Data.Linq;\r
37 #else\r
38 using DataContext = DbLinq.Data.Linq.DataContext;\r
39 using Data = DbLinq.Data;\r
40 using DbLinq.Data.Linq;\r
41 #endif\r
42 using IExecuteResult = System.Data.Linq.IExecuteResult;\r
43 \r
44 namespace DbLinq.Vendor.Implementation\r
45 {\r
46     /// <summary>\r
47     /// some IVendor functionality is the same for many vendors,\r
48     /// implemented here as virtual functions.\r
49     /// </summary>\r
50 #if MONO_STRICT\r
51     internal\r
52 #else\r
53     public\r
54 #endif\r
55  abstract partial class Vendor : IVendor\r
56     {\r
57         /// <summary>\r
58         /// Pings requested DB, true is result is OK.\r
59         /// May throw a connection exception (DataContext.DatabaseExists() handles this)\r
60         /// </summary>\r
61         /// <param name="dataContext"></param>\r
62         /// <returns></returns>\r
63         public virtual bool Ping(DataContext dataContext)\r
64         {\r
65             return dataContext.ExecuteCommand("SELECT 11") == 11;\r
66         }\r
67 \r
68         /// <summary>\r
69         /// Component used to provide SQL Parts\r
70         /// </summary>\r
71         /// <value></value>\r
72         public abstract ISqlProvider SqlProvider { get; }\r
73 \r
74         /// <summary>\r
75         /// Performs bulk insert.\r
76         /// Please notice that PKs may not be updated\r
77         /// </summary>\r
78         /// <typeparam name="T"></typeparam>\r
79         /// <param name="table"></param>\r
80         /// <param name="rows"></param>\r
81         /// <param name="pageSize"></param>\r
82         /// <param name="transaction"></param>\r
83         public virtual void BulkInsert<T>(Table<T> table, List<T> rows, int pageSize, IDbTransaction transaction) where T : class\r
84         {\r
85             throw new NotImplementedException();\r
86         }\r
87 \r
88         /// <summary>\r
89         /// VendorName represents the database being handled by this vendor 'Oracle' or 'MySql'\r
90         /// </summary>\r
91         /// <value></value>\r
92         public abstract string VendorName { get; }\r
93 \r
94         /// <summary>\r
95         /// Executes a stored procedure/function call\r
96         /// </summary>\r
97         /// <param name="context"></param>\r
98         /// <param name="method"></param>\r
99         /// <param name="sqlParams"></param>\r
100         /// <returns></returns>\r
101         public abstract IExecuteResult ExecuteMethodCall(DataContext context, MethodInfo method, params object[] sqlParams);\r
102 \r
103         /// <summary>\r
104         /// Creates the data adapter.\r
105         /// </summary>\r
106         /// <param name="dataContext">The data context.</param>\r
107         /// <returns></returns>\r
108         protected virtual IDbDataAdapter CreateDataAdapter(DataContext dataContext)\r
109         {\r
110             return dataContext.CreateDataAdapter();\r
111         }\r
112 \r
113         /// <summary>\r
114         /// Gets the connection string server part name.\r
115         /// </summary>\r
116         /// <value>The connection string server.</value>\r
117         protected virtual string ConnectionStringServer { get { return "server"; } }\r
118         /// <summary>\r
119         /// Gets the connection string user part name.\r
120         /// </summary>\r
121         /// <value>The connection string user.</value>\r
122         protected virtual string ConnectionStringUser { get { return "user id"; } }\r
123         /// <summary>\r
124         /// Gets the connection string password part name.\r
125         /// </summary>\r
126         /// <value>The connection string password.</value>\r
127         protected virtual string ConnectionStringPassword { get { return "password"; } }\r
128         /// <summary>\r
129         /// Gets the connection string database part name.\r
130         /// </summary>\r
131         /// <value>The connection string database.</value>\r
132         protected virtual string ConnectionStringDatabase { get { return "database"; } }\r
133 \r
134         /// <summary>\r
135         /// Adds the connection string part.\r
136         /// </summary>\r
137         /// <param name="parts">The parts.</param>\r
138         /// <param name="name">The name.</param>\r
139         /// <param name="value">The value.</param>\r
140         protected virtual void AddConnectionStringPart(IList<string> parts, string name, string value)\r
141         {\r
142             if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(name))\r
143                 parts.Add(string.Format("{0}={1}", name, value));\r
144         }\r
145 \r
146         /// <summary>\r
147         /// Builds a connection string given the input parameters\r
148         /// </summary>\r
149         /// <param name="host">Server host</param>\r
150         /// <param name="databaseName">Database (or schema) name</param>\r
151         /// <param name="userName">Login user name</param>\r
152         /// <param name="password">Login password</param>\r
153         /// <returns></returns>\r
154         public virtual string BuildConnectionString(string host, string databaseName, string userName, string password)\r
155         {\r
156             var connectionStringParts = new List<string>();\r
157             AddConnectionStringPart(connectionStringParts, ConnectionStringServer, host);\r
158             AddConnectionStringPart(connectionStringParts, ConnectionStringDatabase, databaseName);\r
159             AddConnectionStringPart(connectionStringParts, ConnectionStringUser, userName);\r
160             AddConnectionStringPart(connectionStringParts, ConnectionStringPassword, password);\r
161             return string.Join(";", connectionStringParts.ToArray());\r
162         }\r
163 \r
164         /// <summary>\r
165         /// called from DataContext ctor, which needs to create an IDbConnection, given an IVendor\r
166         /// </summary>\r
167         public IDbConnection CreateDbConnection(string connectionString)\r
168         {\r
169             var reConnectionType = new System.Text.RegularExpressions.Regex(@"DbLinqConnectionType=([^;]+)");\r
170             if (!reConnectionType.IsMatch(connectionString))\r
171                 throw new ArgumentException("No DbLinqConnectionType parameter found.  " +\r
172                     "Please specify the assembly qualified type name to use for the Connection Type.",\r
173                     "connectionString");\r
174 \r
175             var    match        = reConnectionType.Match(connectionString);\r
176             string connTypeVal  = match.Groups[1].Value;\r
177             var    connType     = Type.GetType(connTypeVal);\r
178             if (connType == null)\r
179                 throw new ArgumentException(string.Format(\r
180                         "Could not load the specified DbLinqConnectionType `{0}'.",\r
181                         connTypeVal),\r
182                     "connectionString");\r
183             connectionString = reConnectionType.Replace(connectionString, "");\r
184             return (IDbConnection)Activator.CreateInstance(connType, connectionString);\r
185         }\r
186     }\r
187 }\r