MSSQLAssetData.cs 12 KB

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