5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\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
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\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
28 using System.Reflection;
\r
29 using System.Collections.Generic;
\r
34 using DataContext = System.Data.Linq.DataContext;
\r
35 using Data = System.Data;
\r
36 using System.Data.Linq;
\r
38 using DataContext = DbLinq.Data.Linq.DataContext;
\r
39 using Data = DbLinq.Data;
\r
40 using DbLinq.Data.Linq;
\r
42 using IExecuteResult = System.Data.Linq.IExecuteResult;
\r
44 namespace DbLinq.Vendor.Implementation
\r
47 /// some IVendor functionality is the same for many vendors,
\r
48 /// implemented here as virtual functions.
\r
55 abstract partial class Vendor : IVendor
\r
58 /// Pings requested DB, true is result is OK.
\r
59 /// May throw a connection exception (DataContext.DatabaseExists() handles this)
\r
61 /// <param name="dataContext"></param>
\r
62 /// <returns></returns>
\r
63 public virtual bool Ping(DataContext dataContext)
\r
65 return dataContext.ExecuteCommand("SELECT 11") == 11;
\r
69 /// Component used to provide SQL Parts
\r
72 public abstract ISqlProvider SqlProvider { get; }
\r
75 /// Performs bulk insert.
\r
76 /// Please notice that PKs may not be updated
\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
85 throw new NotImplementedException();
\r
89 /// VendorName represents the database being handled by this vendor 'Oracle' or 'MySql'
\r
92 public abstract string VendorName { get; }
\r
95 /// Executes a stored procedure/function call
\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
104 /// Creates the data adapter.
\r
106 /// <param name="dataContext">The data context.</param>
\r
107 /// <returns></returns>
\r
108 protected virtual IDbDataAdapter CreateDataAdapter(DataContext dataContext)
\r
110 return dataContext.CreateDataAdapter();
\r
114 /// Gets the connection string server part name.
\r
116 /// <value>The connection string server.</value>
\r
117 protected virtual string ConnectionStringServer { get { return "server"; } }
\r
119 /// Gets the connection string user part name.
\r
121 /// <value>The connection string user.</value>
\r
122 protected virtual string ConnectionStringUser { get { return "user id"; } }
\r
124 /// Gets the connection string password part name.
\r
126 /// <value>The connection string password.</value>
\r
127 protected virtual string ConnectionStringPassword { get { return "password"; } }
\r
129 /// Gets the connection string database part name.
\r
131 /// <value>The connection string database.</value>
\r
132 protected virtual string ConnectionStringDatabase { get { return "database"; } }
\r
135 /// Adds the connection string part.
\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
142 if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(name))
\r
143 parts.Add(string.Format("{0}={1}", name, value));
\r
147 /// Builds a connection string given the input parameters
\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
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
165 /// called from DataContext ctor, which needs to create an IDbConnection, given an IVendor
\r
167 public IDbConnection CreateDbConnection(string connectionString)
\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
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
182 "connectionString");
\r
183 connectionString = reConnectionType.Replace(connectionString, "");
\r
184 return (IDbConnection)Activator.CreateInstance(connType, connectionString);
\r