PageRenderTime 41ms CodeModel.GetById 9ms 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
Possible License(s): GPL-2.0, Unlicense, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0
  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. using System;
  27. using System.Reflection;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using System.Linq;
  31. #if MONO_STRICT
  32. using System.Data.Linq;
  33. #else
  34. using DbLinq.Data.Linq;
  35. #endif
  36. using Data = DbLinq.Data;
  37. using IExecuteResult = System.Data.Linq.IExecuteResult;
  38. using System.Text;
  39. namespace DbLinq.Vendor.Implementation
  40. {
  41. /// <summary>
  42. /// some IVendor functionality is the same for many vendors,
  43. /// implemented here as virtual functions.
  44. /// </summary>
  45. #if !MONO_STRICT
  46. public
  47. #endif
  48. abstract partial class Vendor : IVendor
  49. {
  50. /// <summary>
  51. /// Pings requested DB, true is result is OK.
  52. /// May throw a connection exception (DataContext.DatabaseExists() handles this)
  53. /// </summary>
  54. /// <param name="dataContext"></param>
  55. /// <returns></returns>
  56. public virtual bool Ping(DataContext dataContext)
  57. {
  58. return dataContext.ExecuteCommand("SELECT 11") == 11;
  59. }
  60. /// <summary>
  61. /// Component used to provide SQL Parts
  62. /// </summary>
  63. /// <value></value>
  64. public abstract ISqlProvider SqlProvider { get; }
  65. /// <summary>
  66. /// Performs bulk insert.
  67. /// Please notice that PKs may not be updated
  68. /// </summary>
  69. /// <typeparam name="T"></typeparam>
  70. /// <param name="table"></param>
  71. /// <param name="rows"></param>
  72. /// <param name="pageSize"></param>
  73. /// <param name="transaction"></param>
  74. public virtual void BulkInsert<T>(Table<T> table, List<T> rows, int pageSize, IDbTransaction transaction) where T : class
  75. {
  76. throw new NotImplementedException();
  77. }
  78. /// <summary>
  79. /// VendorName represents the database being handled by this vendor 'Oracle' or 'MySql'
  80. /// </summary>
  81. /// <value></value>
  82. public abstract string VendorName { get; }
  83. /// <summary>
  84. /// Executes a stored procedure/function call
  85. /// </summary>
  86. /// <param name="context"></param>
  87. /// <param name="method"></param>
  88. /// <param name="sqlParams"></param>
  89. /// <returns></returns>
  90. public abstract IExecuteResult ExecuteMethodCall(DataContext context, MethodInfo method, params object[] sqlParams);
  91. /// <summary>
  92. /// Creates the data adapter.
  93. /// </summary>
  94. /// <param name="dataContext">The data context.</param>
  95. /// <returns></returns>
  96. protected virtual IDbDataAdapter CreateDataAdapter(DataContext dataContext)
  97. {
  98. return dataContext.CreateDataAdapter();
  99. }
  100. /// <summary>
  101. /// Gets the connection string server part name.
  102. /// </summary>
  103. /// <value>The connection string server.</value>
  104. protected virtual string ConnectionStringServer { get { return "server"; } }
  105. /// <summary>
  106. /// Gets the connection string user part name.
  107. /// </summary>
  108. /// <value>The connection string user.</value>
  109. protected virtual string ConnectionStringUser { get { return "user id"; } }
  110. /// <summary>
  111. /// Gets the connection string password part name.
  112. /// </summary>
  113. /// <value>The connection string password.</value>
  114. protected virtual string ConnectionStringPassword { get { return "password"; } }
  115. /// <summary>
  116. /// Gets the connection string database part name.
  117. /// </summary>
  118. /// <value>The connection string database.</value>
  119. protected virtual string ConnectionStringDatabase { get { return "database"; } }
  120. /// <summary>
  121. /// Adds the connection string part.
  122. /// </summary>
  123. /// <param name="parts">The parts.</param>
  124. /// <param name="name">The name.</param>
  125. /// <param name="value">The value.</param>
  126. protected void AppendConnectionString(StringBuilder connectionString, string name, string value)
  127. {
  128. if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(name))
  129. connectionString.AppendFormat("{0}={1};", name, value);
  130. }
  131. protected virtual void AppendDatabase(StringBuilder connectionString, string databaseName)
  132. {
  133. AppendConnectionString(connectionString, ConnectionStringDatabase, databaseName);
  134. }
  135. protected virtual void AppendPassword(StringBuilder connectionString, string password)
  136. {
  137. AppendConnectionString(connectionString, ConnectionStringPassword, password);
  138. }
  139. protected virtual void AppendServer(StringBuilder connectionString, string host)
  140. {
  141. // A majority of databases want a server/host port number as a separate key:
  142. // http://www.connectionstrings.com/postgre-sql
  143. // http://www.connectionstrings.com/firebird
  144. // http://www.connectionstrings.com/mysql
  145. // So make this the default.
  146. if (host == null)
  147. return;
  148. var colonIdx = host.IndexOf(':');
  149. string port = colonIdx < 0 || host.Length == (colonIdx + 1) ? null : host.Substring(colonIdx + 1);
  150. if (colonIdx >= 0)
  151. host = host.Substring(0, colonIdx);
  152. AppendConnectionString(connectionString, ConnectionStringServer, host);
  153. if (port != null)
  154. AppendConnectionString(connectionString, "Port", port);
  155. }
  156. protected virtual void AppendUser(StringBuilder connectionString, string userName)
  157. {
  158. AppendConnectionString(connectionString, ConnectionStringUser, userName);
  159. }
  160. /// <summary>
  161. /// Builds a connection string given the input parameters
  162. /// </summary>
  163. /// <param name="host">Server host</param>
  164. /// <param name="databaseName">Database (or schema) name</param>
  165. /// <param name="userName">Login user name</param>
  166. /// <param name="password">Login password</param>
  167. /// <returns></returns>
  168. public virtual string BuildConnectionString(string host, string databaseName, string userName, string password)
  169. {
  170. var connectionString = new StringBuilder();
  171. AppendServer(connectionString, host);
  172. AppendDatabase(connectionString, databaseName);
  173. AppendUser(connectionString, userName);
  174. AppendPassword(connectionString, password);
  175. return connectionString.ToString();
  176. }
  177. /// <summary>
  178. /// called from DataContext ctor, which needs to create an IDbConnection, given an IVendor
  179. /// </summary>
  180. public IDbConnection CreateDbConnection(string connectionString)
  181. {
  182. var reConnectionType = new System.Text.RegularExpressions.Regex(@"DbLinqConnectionType=([^;]*);?");
  183. string connTypeVal = null;
  184. if (!reConnectionType.IsMatch(connectionString))
  185. {
  186. connTypeVal = "System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
  187. }
  188. else
  189. {
  190. var match = reConnectionType.Match(connectionString);
  191. connTypeVal = match.Groups[1].Value;
  192. connectionString = reConnectionType.Replace(connectionString, "");
  193. }
  194. var connType = Type.GetType(connTypeVal);
  195. if (connType == null)
  196. throw new ArgumentException(string.Format(
  197. "Could not load the specified DbLinqConnectionType `{0}'.",
  198. connTypeVal),
  199. "connectionString");
  200. return (IDbConnection)Activator.CreateInstance(connType, connectionString);
  201. }
  202. }
  203. }