MSSQLManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. return SqlDbType.VarChar;
  103. }
  104. /// <summary>
  105. /// Creates value for parameter.
  106. /// </summary>
  107. /// <param name="value">The value.</param>
  108. /// <returns></returns>
  109. private static object CreateParameterValue(object value)
  110. {
  111. Type valueType = value.GetType();
  112. if (valueType == typeof(UUID)) //TODO check if this works
  113. {
  114. return ((UUID) value).Guid;
  115. }
  116. if (valueType == typeof(UUID))
  117. {
  118. return ((UUID)value).Guid;
  119. }
  120. if (valueType == typeof(bool))
  121. {
  122. return (bool)value ? 1 : 0;
  123. }
  124. if (valueType == typeof(Byte[]))
  125. {
  126. return value;
  127. }
  128. if (valueType == typeof(int))
  129. {
  130. return value;
  131. }
  132. return value;
  133. }
  134. /// <summary>
  135. /// Create a parameter for a command
  136. /// </summary>
  137. /// <param name="parameterName">Name of the parameter.</param>
  138. /// <param name="parameterObject">parameter object.</param>
  139. /// <returns></returns>
  140. internal SqlParameter CreateParameter(string parameterName, object parameterObject)
  141. {
  142. return CreateParameter(parameterName, parameterObject, false);
  143. }
  144. /// <summary>
  145. /// Creates the parameter for a command.
  146. /// </summary>
  147. /// <param name="parameterName">Name of the parameter.</param>
  148. /// <param name="parameterObject">parameter object.</param>
  149. /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param>
  150. /// <returns></returns>
  151. internal SqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut)
  152. {
  153. //Tweak so we dont always have to add @ sign
  154. if (!parameterName.StartsWith("@")) parameterName = "@" + parameterName;
  155. //HACK if object is null, it is turned into a string, there are no nullable type till now
  156. if (parameterObject == null) parameterObject = "";
  157. SqlParameter parameter = new SqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
  158. if (parameterOut)
  159. {
  160. parameter.Direction = ParameterDirection.Output;
  161. }
  162. else
  163. {
  164. parameter.Direction = ParameterDirection.Input;
  165. parameter.Value = CreateParameterValue(parameterObject);
  166. }
  167. return parameter;
  168. }
  169. /// <summary>
  170. /// Checks if we need to do some migrations to the database
  171. /// </summary>
  172. /// <param name="migrationStore">migrationStore.</param>
  173. public void CheckMigration(string migrationStore)
  174. {
  175. using (SqlConnection connection = new SqlConnection(connectionString))
  176. {
  177. connection.Open();
  178. Assembly assem = GetType().Assembly;
  179. MSSQLMigration migration = new MSSQLMigration(connection, assem, migrationStore);
  180. migration.Update();
  181. }
  182. }
  183. /// <summary>
  184. /// Returns the version of this DB provider
  185. /// </summary>
  186. /// <returns>A string containing the DB provider</returns>
  187. public string getVersion()
  188. {
  189. Module module = GetType().Module;
  190. // string dllName = module.Assembly.ManifestModule.Name;
  191. Version dllVersion = module.Assembly.GetName().Version;
  192. return
  193. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  194. dllVersion.Revision);
  195. }
  196. }
  197. }