PageRenderTime 44ms CodeModel.GetById 30ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq/Vendor/Implementation/Vendor.cs

https://github.com/ztfuqingvip/mono
C# | 222 lines | 103 code | 19 blank | 100 comment | 13 complexity | b65a0cd95f8d4ce6137c6521682fdd5a MD5 | raw file
  1#region MIT license
  2// 
  3// MIT license
  4//
  5// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
  6// 
  7// Permission is hereby granted, free of charge, to any person obtaining a copy
  8// of this software and associated documentation files (the "Software"), to deal
  9// in the Software without restriction, including without limitation the rights
 10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11// copies of the Software, and to permit persons to whom the Software is
 12// furnished to do so, subject to the following conditions:
 13// 
 14// The above copyright notice and this permission notice shall be included in
 15// all copies or substantial portions of the Software.
 16// 
 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23// THE SOFTWARE.
 24// 
 25#endregion
 26
 27using System;
 28using System.Reflection;
 29using System.Collections.Generic;
 30using System.Data;
 31using System.Linq;
 32
 33#if MONO_STRICT
 34using System.Data.Linq;
 35#else
 36using DbLinq.Data.Linq;
 37#endif
 38
 39using Data = DbLinq.Data;
 40
 41using IExecuteResult = System.Data.Linq.IExecuteResult;
 42using System.Text;
 43
 44namespace DbLinq.Vendor.Implementation
 45{
 46    /// <summary>
 47    /// some IVendor functionality is the same for many vendors,
 48    /// implemented here as virtual functions.
 49    /// </summary>
 50#if !MONO_STRICT
 51    public
 52#endif
 53    abstract partial class Vendor : IVendor
 54    {
 55        /// <summary>
 56        /// Pings requested DB, true is result is OK.
 57        /// May throw a connection exception (DataContext.DatabaseExists() handles this)
 58        /// </summary>
 59        /// <param name="dataContext"></param>
 60        /// <returns></returns>
 61        public virtual bool Ping(DataContext dataContext)
 62        {
 63            return dataContext.ExecuteCommand("SELECT 11") == 11;
 64        }
 65
 66        /// <summary>
 67        /// Component used to provide SQL Parts
 68        /// </summary>
 69        /// <value></value>
 70        public abstract ISqlProvider SqlProvider { get; }
 71
 72        /// <summary>
 73        /// Performs bulk insert.
 74        /// Please notice that PKs may not be updated
 75        /// </summary>
 76        /// <typeparam name="T"></typeparam>
 77        /// <param name="table"></param>
 78        /// <param name="rows"></param>
 79        /// <param name="pageSize"></param>
 80        /// <param name="transaction"></param>
 81        public virtual void BulkInsert<T>(Table<T> table, List<T> rows, int pageSize, IDbTransaction transaction) where T : class
 82        {
 83            throw new NotImplementedException();
 84        }
 85
 86        /// <summary>
 87        /// VendorName represents the database being handled by this vendor 'Oracle' or 'MySql'
 88        /// </summary>
 89        /// <value></value>
 90        public abstract string VendorName { get; }
 91
 92        /// <summary>
 93        /// Executes a stored procedure/function call
 94        /// </summary>
 95        /// <param name="context"></param>
 96        /// <param name="method"></param>
 97        /// <param name="sqlParams"></param>
 98        /// <returns></returns>
 99        public abstract IExecuteResult ExecuteMethodCall(DataContext context, MethodInfo method, params object[] sqlParams);
100
101        /// <summary>
102        /// Creates the data adapter.
103        /// </summary>
104        /// <param name="dataContext">The data context.</param>
105        /// <returns></returns>
106        protected virtual IDbDataAdapter CreateDataAdapter(DataContext dataContext)
107        {
108            return dataContext.CreateDataAdapter();
109        }
110
111        /// <summary>
112        /// Gets the connection string server part name.
113        /// </summary>
114        /// <value>The connection string server.</value>
115        protected virtual string ConnectionStringServer { get { return "server"; } }
116        /// <summary>
117        /// Gets the connection string user part name.
118        /// </summary>
119        /// <value>The connection string user.</value>
120        protected virtual string ConnectionStringUser { get { return "user id"; } }
121        /// <summary>
122        /// Gets the connection string password part name.
123        /// </summary>
124        /// <value>The connection string password.</value>
125        protected virtual string ConnectionStringPassword { get { return "password"; } }
126        /// <summary>
127        /// Gets the connection string database part name.
128        /// </summary>
129        /// <value>The connection string database.</value>
130        protected virtual string ConnectionStringDatabase { get { return "database"; } }
131
132        /// <summary>
133        /// Adds the connection string part.
134        /// </summary>
135        /// <param name="parts">The parts.</param>
136        /// <param name="name">The name.</param>
137        /// <param name="value">The value.</param>
138        protected void AppendConnectionString(StringBuilder connectionString, string name, string value)
139        {
140            if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(name))
141                connectionString.AppendFormat("{0}={1};", name, value);
142        }
143
144        protected virtual void AppendDatabase(StringBuilder connectionString, string databaseName)
145        {
146            AppendConnectionString(connectionString, ConnectionStringDatabase, databaseName);
147        }
148
149        protected virtual void AppendPassword(StringBuilder connectionString, string password)
150        {
151            AppendConnectionString(connectionString, ConnectionStringPassword, password);
152        }
153
154        protected virtual void AppendServer(StringBuilder connectionString, string host)
155        {
156            // A majority of databases want a server/host port number as a separate key:
157            //      http://www.connectionstrings.com/postgre-sql
158            //      http://www.connectionstrings.com/firebird
159            //      http://www.connectionstrings.com/mysql
160            // So make this the default.
161            if (host == null)
162                return;
163            var colonIdx = host.IndexOf(':');
164            string port = colonIdx < 0 || host.Length == (colonIdx + 1) ? null : host.Substring(colonIdx + 1);
165            if (colonIdx >= 0)
166                host = host.Substring(0, colonIdx);
167            AppendConnectionString(connectionString, ConnectionStringServer, host);
168            if (port != null)
169                AppendConnectionString(connectionString, "Port", port);
170        }
171
172        protected virtual void AppendUser(StringBuilder connectionString, string userName)
173        {
174            AppendConnectionString(connectionString, ConnectionStringUser, userName);
175        }
176
177        /// <summary>
178        /// Builds a connection string given the input parameters
179        /// </summary>
180        /// <param name="host">Server host</param>
181        /// <param name="databaseName">Database (or schema) name</param>
182        /// <param name="userName">Login user name</param>
183        /// <param name="password">Login password</param>
184        /// <returns></returns>
185        public virtual string BuildConnectionString(string host, string databaseName, string userName, string password)
186        {
187            var connectionString = new StringBuilder();
188            AppendServer(connectionString, host);
189            AppendDatabase(connectionString, databaseName);
190            AppendUser(connectionString, userName);
191            AppendPassword(connectionString, password);
192            return connectionString.ToString();
193        }
194
195        /// <summary>
196        /// called from DataContext ctor, which needs to create an IDbConnection, given an IVendor
197        /// </summary>
198        public IDbConnection CreateDbConnection(string connectionString)
199        {
200            var reConnectionType = new System.Text.RegularExpressions.Regex(@"DbLinqConnectionType=([^;]*);?");
201            string connTypeVal = null;
202            if (!reConnectionType.IsMatch(connectionString))
203            {
204                connTypeVal = "System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
205            }
206            else
207            {
208                var    match        = reConnectionType.Match(connectionString);
209                connTypeVal         = match.Groups[1].Value;
210                connectionString    = reConnectionType.Replace(connectionString, "");
211            }
212
213            var    connType     = Type.GetType(connTypeVal);
214            if (connType == null)
215                throw new ArgumentException(string.Format(
216                        "Could not load the specified DbLinqConnectionType `{0}'.",
217                        connTypeVal),
218                    "connectionString");
219            return (IDbConnection)Activator.CreateInstance(connType, connectionString);
220        }
221    }
222}