MSSQLAssetData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 OpenSim 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.Data;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using log4net;
  32. using OpenSim.Framework;
  33. namespace OpenSim.Data.MSSQL
  34. {
  35. /// <summary>
  36. /// A MSSQL Interface for the Asset server
  37. /// </summary>
  38. internal class MSSQLAssetData : AssetDataBase
  39. {
  40. private const string _migrationStore = "AssetStore";
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private long TicksToEpoch;
  43. /// <summary>
  44. /// Database manager
  45. /// </summary>
  46. private MSSQLManager database;
  47. #region IPlugin Members
  48. override public void Dispose() { }
  49. /// <summary>
  50. /// <para>Initialises asset interface</para>
  51. /// </summary>
  52. // [Obsolete("Cannot be default-initialized!")]
  53. override public void Initialise()
  54. {
  55. m_log.Info("[MSSQLUserData]: " + Name + " cannot be default-initialized!");
  56. throw new PluginNotInitialisedException(Name);
  57. }
  58. /// <summary>
  59. /// Initialises asset interface
  60. /// </summary>
  61. /// <para>
  62. /// a string instead of file, if someone writes the support
  63. /// </para>
  64. /// <param name="connectionString">connect string</param>
  65. override public void Initialise(string connectionString)
  66. {
  67. TicksToEpoch = new System.DateTime(1970, 1, 1).Ticks;
  68. if (!string.IsNullOrEmpty(connectionString))
  69. {
  70. database = new MSSQLManager(connectionString);
  71. }
  72. else
  73. {
  74. IniFile gridDataMSSqlFile = new IniFile("mssql_connection.ini");
  75. string settingDataSource = gridDataMSSqlFile.ParseFileReadValue("data_source");
  76. string settingInitialCatalog = gridDataMSSqlFile.ParseFileReadValue("initial_catalog");
  77. string settingPersistSecurityInfo = gridDataMSSqlFile.ParseFileReadValue("persist_security_info");
  78. string settingUserId = gridDataMSSqlFile.ParseFileReadValue("user_id");
  79. string settingPassword = gridDataMSSqlFile.ParseFileReadValue("password");
  80. database =
  81. new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId,
  82. settingPassword);
  83. }
  84. //New migration to check for DB changes
  85. database.CheckMigration(_migrationStore);
  86. }
  87. /// <summary>
  88. /// Database provider version.
  89. /// </summary>
  90. override public string Version
  91. {
  92. get { return database.getVersion(); }
  93. }
  94. /// <summary>
  95. /// The name of this DB provider.
  96. /// </summary>
  97. override public string Name
  98. {
  99. get { return "MSSQL Asset storage engine"; }
  100. }
  101. #endregion
  102. #region IAssetProviderPlugin Members
  103. /// <summary>
  104. /// Fetch Asset from database
  105. /// </summary>
  106. /// <param name="assetID">the asset UUID</param>
  107. /// <returns></returns>
  108. override public AssetBase FetchAsset(UUID assetID)
  109. {
  110. using (AutoClosingSqlCommand command = database.Query("SELECT * FROM assets WHERE id = @id"))
  111. {
  112. command.Parameters.Add(database.CreateParameter("id", assetID));
  113. using (IDataReader reader = command.ExecuteReader())
  114. {
  115. if (reader.Read())
  116. {
  117. AssetBase asset = new AssetBase();
  118. // Region Main
  119. asset.FullID = new UUID((string)reader["id"]);
  120. asset.Name = (string)reader["name"];
  121. asset.Description = (string)reader["description"];
  122. asset.Type = Convert.ToSByte(reader["assetType"]);
  123. asset.Local = Convert.ToBoolean(reader["local"]);
  124. asset.Temporary = Convert.ToBoolean(reader["temporary"]);
  125. asset.Data = (byte[])reader["data"];
  126. return asset;
  127. }
  128. return null; // throw new Exception("No rows to return");
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Create asset in database
  134. /// </summary>
  135. /// <param name="asset">the asset</param>
  136. override public void CreateAsset(AssetBase asset)
  137. {
  138. if (ExistsAsset(asset.FullID))
  139. {
  140. return;
  141. }
  142. using (AutoClosingSqlCommand command = database.Query(
  143. "INSERT INTO assets ([id], [name], [description], [assetType], [local], [temporary], [create_time], [access_time], [data])" +
  144. " VALUES " +
  145. "(@id, @name, @description, @assetType, @local, @temporary, @create_time, @access_time, @data)"))
  146. {
  147. int now = (int)((System.DateTime.Now.Ticks - TicksToEpoch) / 10000000);
  148. command.Parameters.Add(database.CreateParameter("id", asset.FullID));
  149. command.Parameters.Add(database.CreateParameter("name", asset.Name));
  150. command.Parameters.Add(database.CreateParameter("description", asset.Description));
  151. command.Parameters.Add(database.CreateParameter("assetType", asset.Type));
  152. command.Parameters.Add(database.CreateParameter("local", asset.Local));
  153. command.Parameters.Add(database.CreateParameter("temporary", asset.Temporary));
  154. command.Parameters.Add(database.CreateParameter("access_time", now));
  155. command.Parameters.Add(database.CreateParameter("create_time", now));
  156. command.Parameters.Add(database.CreateParameter("data", asset.Data));
  157. command.ExecuteNonQuery();
  158. }
  159. }
  160. /// <summary>
  161. /// Update asset in database
  162. /// </summary>
  163. /// <param name="asset">the asset</param>
  164. override public void UpdateAsset(AssetBase asset)
  165. {
  166. using (AutoClosingSqlCommand command = database.Query("UPDATE assets set id = @id, " +
  167. "name = @name, " +
  168. "description = @description," +
  169. "assetType = @assetType," +
  170. "local = @local," +
  171. "temporary = @temporary," +
  172. "data = @data where " +
  173. "id = @keyId;"))
  174. {
  175. command.Parameters.Add(database.CreateParameter("id", asset.FullID));
  176. command.Parameters.Add(database.CreateParameter("name", asset.Name));
  177. command.Parameters.Add(database.CreateParameter("description", asset.Description));
  178. command.Parameters.Add(database.CreateParameter("assetType", asset.Type));
  179. command.Parameters.Add(database.CreateParameter("local", asset.Local));
  180. command.Parameters.Add(database.CreateParameter("temporary", asset.Temporary));
  181. command.Parameters.Add(database.CreateParameter("data", asset.Data));
  182. command.Parameters.Add(database.CreateParameter("@keyId", asset.FullID));
  183. try
  184. {
  185. command.ExecuteNonQuery();
  186. }
  187. catch (Exception e)
  188. {
  189. m_log.Error(e.ToString());
  190. }
  191. }
  192. }
  193. // Commented out since currently unused - this probably should be called in FetchAsset()
  194. // private void UpdateAccessTime(AssetBase asset)
  195. // {
  196. // using (AutoClosingSqlCommand cmd = database.Query("UPDATE assets SET access_time = @access_time WHERE id=@id"))
  197. // {
  198. // int now = (int)((System.DateTime.Now.Ticks - TicksToEpoch) / 10000000);
  199. // cmd.Parameters.AddWithValue("@id", asset.FullID.ToString());
  200. // cmd.Parameters.AddWithValue("@access_time", now);
  201. // try
  202. // {
  203. // cmd.ExecuteNonQuery();
  204. // }
  205. // catch (Exception e)
  206. // {
  207. // m_log.Error(e.ToString());
  208. // }
  209. // }
  210. // }
  211. /// <summary>
  212. /// Check if asset exist in database
  213. /// </summary>
  214. /// <param name="uuid"></param>
  215. /// <returns>true if exist.</returns>
  216. override public bool ExistsAsset(UUID uuid)
  217. {
  218. if (FetchAsset(uuid) != null)
  219. {
  220. return true;
  221. }
  222. return false;
  223. }
  224. #endregion
  225. }
  226. }