MSSQLManager.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using System.Data.SqlClient;
  31. using System.IO;
  32. using System.Reflection;
  33. using log4net;
  34. using OpenMetaverse;
  35. namespace OpenSim.Data.MSSQL
  36. {
  37. /// <summary>
  38. /// A management class for the MS SQL Storage Engine
  39. /// </summary>
  40. public class MSSQLManager
  41. {
  42. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// Connection string for ADO.net
  45. /// </summary>
  46. private readonly string connectionString;
  47. /// <summary>
  48. /// Initialize the manager and set the connectionstring
  49. /// </summary>
  50. /// <param name="connection"></param>
  51. public MSSQLManager(string connection)
  52. {
  53. connectionString = connection;
  54. }
  55. /// <summary>
  56. /// Type conversion to a SQLDbType functions
  57. /// </summary>
  58. /// <param name="type"></param>
  59. /// <returns></returns>
  60. internal SqlDbType DbtypeFromType(Type type)
  61. {
  62. if (type == typeof(string))
  63. {
  64. return SqlDbType.VarChar;
  65. }
  66. if (type == typeof(double))
  67. {
  68. return SqlDbType.Float;
  69. }
  70. if (type == typeof(Single))
  71. {
  72. return SqlDbType.Float;
  73. }
  74. if (type == typeof(int))
  75. {
  76. return SqlDbType.Int;
  77. }
  78. if (type == typeof(bool))
  79. {
  80. return SqlDbType.Bit;
  81. }
  82. if (type == typeof(UUID))
  83. {
  84. return SqlDbType.UniqueIdentifier;
  85. }
  86. if (type == typeof(sbyte))
  87. {
  88. return SqlDbType.Int;
  89. }
  90. if (type == typeof(Byte[]))
  91. {
  92. return SqlDbType.Image;
  93. }
  94. if (type == typeof(uint) || type == typeof(ushort))
  95. {
  96. return SqlDbType.Int;
  97. }
  98. if (type == typeof(ulong))
  99. {
  100. return SqlDbType.BigInt;
  101. }
  102. if (type == typeof(DateTime))
  103. {
  104. return SqlDbType.DateTime;
  105. }
  106. return SqlDbType.VarChar;
  107. }
  108. /// <summary>
  109. /// Creates value for parameter.
  110. /// </summary>
  111. /// <param name="value">The value.</param>
  112. /// <returns></returns>
  113. private static object CreateParameterValue(object value)
  114. {
  115. Type valueType = value.GetType();
  116. if (valueType == typeof(UUID)) //TODO check if this works
  117. {
  118. return ((UUID) value).Guid;
  119. }
  120. if (valueType == typeof(UUID))
  121. {
  122. return ((UUID)value).Guid;
  123. }
  124. if (valueType == typeof(bool))
  125. {
  126. return (bool)value ? 1 : 0;
  127. }
  128. if (valueType == typeof(Byte[]))
  129. {
  130. return value;
  131. }
  132. if (valueType == typeof(int))
  133. {
  134. return value;
  135. }
  136. return value;
  137. }
  138. /// <summary>
  139. /// Create a parameter for a command
  140. /// </summary>
  141. /// <param name="parameterName">Name of the parameter.</param>
  142. /// <param name="parameterObject">parameter object.</param>
  143. /// <returns></returns>
  144. internal SqlParameter CreateParameter(string parameterName, object parameterObject)
  145. {
  146. return CreateParameter(parameterName, parameterObject, false);
  147. }
  148. /// <summary>
  149. /// Creates the parameter for a command.
  150. /// </summary>
  151. /// <param name="parameterName">Name of the parameter.</param>
  152. /// <param name="parameterObject">parameter object.</param>
  153. /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param>
  154. /// <returns></returns>
  155. internal SqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut)
  156. {
  157. //Tweak so we dont always have to add @ sign
  158. if (!parameterName.StartsWith("@")) parameterName = "@" + parameterName;
  159. //HACK if object is null, it is turned into a string, there are no nullable type till now
  160. if (parameterObject == null) parameterObject = "";
  161. SqlParameter parameter = new SqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
  162. if (parameterOut)
  163. {
  164. parameter.Direction = ParameterDirection.Output;
  165. }
  166. else
  167. {
  168. parameter.Direction = ParameterDirection.Input;
  169. parameter.Value = CreateParameterValue(parameterObject);
  170. }
  171. return parameter;
  172. }
  173. /// <summary>
  174. /// Checks if we need to do some migrations to the database
  175. /// </summary>
  176. /// <param name="migrationStore">migrationStore.</param>
  177. public void CheckMigration(string migrationStore)
  178. {
  179. using (SqlConnection connection = new SqlConnection(connectionString))
  180. {
  181. connection.Open();
  182. Assembly assem = GetType().Assembly;
  183. MSSQLMigration migration = new MSSQLMigration(connection, assem, migrationStore);
  184. migration.Update();
  185. }
  186. }
  187. /// <summary>
  188. /// Returns the version of this DB provider
  189. /// </summary>
  190. /// <returns>A string containing the DB provider</returns>
  191. public string getVersion()
  192. {
  193. Module module = GetType().Module;
  194. // string dllName = module.Assembly.ManifestModule.Name;
  195. Version dllVersion = module.Assembly.GetName().Version;
  196. return
  197. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  198. dllVersion.Revision);
  199. }
  200. }
  201. }