MSSQLAssetData.cs 9.6 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.Collections.Generic;
  29. using System.Data;
  30. using System.Data.SqlClient;
  31. using System.Reflection;
  32. using libsecondlife;
  33. using log4net;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Data.MSSQL
  36. {
  37. /// <summary>
  38. /// A MSSQL Interface for the Asset server
  39. /// </summary>
  40. internal class MSSQLAssetData : AssetDataBase
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private MSSQLManager database;
  44. #region IAssetProvider Members
  45. /// <summary>
  46. /// Migration method
  47. /// <list type="bullet">
  48. /// <item>Execute "CreateAssetsTable.sql" if tableName == null</item>
  49. /// </list>
  50. /// </summary>
  51. /// <param name="tableName">Name of table</param>
  52. private void UpgradeAssetsTable(string tableName)
  53. {
  54. // null as the version, indicates that the table didn't exist
  55. if (tableName == null)
  56. {
  57. m_log.Info("[ASSET DB]: Creating new database tables");
  58. database.ExecuteResourceSql("CreateAssetsTable.sql");
  59. return;
  60. }
  61. }
  62. /// <summary>
  63. /// Ensure that the assets related tables exists and are at the latest version
  64. /// </summary>
  65. private void TestTables()
  66. {
  67. Dictionary<string, string> tableList = new Dictionary<string, string>();
  68. tableList["assets"] = null;
  69. database.GetTableVersion(tableList);
  70. UpgradeAssetsTable(tableList["assets"]);
  71. }
  72. /// <summary>
  73. /// Fetch Asset from database
  74. /// </summary>
  75. /// <param name="assetID">the asset UUID</param>
  76. /// <returns></returns>
  77. override public AssetBase FetchAsset(LLUUID assetID)
  78. {
  79. Dictionary<string, string> param = new Dictionary<string, string>();
  80. param["id"] = assetID.ToString();
  81. using (IDbCommand result = database.Query("SELECT * FROM assets WHERE id = @id", param))
  82. using (IDataReader reader = result.ExecuteReader())
  83. {
  84. return database.getAssetRow(reader);
  85. }
  86. }
  87. /// <summary>
  88. /// Create asset in database
  89. /// </summary>
  90. /// <param name="asset">the asset</param>
  91. override public void CreateAsset(AssetBase asset)
  92. {
  93. if (ExistsAsset((LLUUID) asset.FullID))
  94. {
  95. return;
  96. }
  97. using (AutoClosingSqlCommand cmd =
  98. database.Query(
  99. "INSERT INTO assets ([id], [name], [description], [assetType], [local], [temporary], [data])" +
  100. " VALUES " +
  101. "(@id, @name, @description, @assetType, @local, @temporary, @data)"))
  102. {
  103. //SqlParameter p = cmd.Parameters.Add("id", SqlDbType.NVarChar);
  104. //p.Value = asset.FullID.ToString();
  105. cmd.Parameters.AddWithValue("id", asset.FullID.ToString());
  106. cmd.Parameters.AddWithValue("name", asset.Name);
  107. cmd.Parameters.AddWithValue("description", asset.Description);
  108. SqlParameter e = cmd.Parameters.Add("assetType", SqlDbType.TinyInt);
  109. e.Value = asset.Type;
  110. SqlParameter g = cmd.Parameters.Add("local", SqlDbType.TinyInt);
  111. g.Value = asset.Local;
  112. SqlParameter h = cmd.Parameters.Add("temporary", SqlDbType.TinyInt);
  113. h.Value = asset.Temporary;
  114. SqlParameter i = cmd.Parameters.Add("data", SqlDbType.Image);
  115. i.Value = asset.Data;
  116. cmd.ExecuteNonQuery();
  117. }
  118. }
  119. /// <summary>
  120. /// Update asset in database
  121. /// </summary>
  122. /// <param name="asset">the asset</param>
  123. override public void UpdateAsset(AssetBase asset)
  124. {
  125. using (IDbCommand command = database.Query("UPDATE assets set id = @id, " +
  126. "name = @name, " +
  127. "description = @description," +
  128. "assetType = @assetType," +
  129. "local = @local," +
  130. "temporary = @temporary," +
  131. "data = @data where " +
  132. "id = @keyId;"))
  133. {
  134. SqlParameter param1 = new SqlParameter("@id", asset.FullID.ToString());
  135. SqlParameter param2 = new SqlParameter("@name", asset.Name);
  136. SqlParameter param3 = new SqlParameter("@description", asset.Description);
  137. SqlParameter param4 = new SqlParameter("@assetType", asset.Type);
  138. SqlParameter param6 = new SqlParameter("@local", asset.Local);
  139. SqlParameter param7 = new SqlParameter("@temporary", asset.Temporary);
  140. SqlParameter param8 = new SqlParameter("@data", asset.Data);
  141. SqlParameter param9 = new SqlParameter("@keyId", asset.FullID.ToString());
  142. command.Parameters.Add(param1);
  143. command.Parameters.Add(param2);
  144. command.Parameters.Add(param3);
  145. command.Parameters.Add(param4);
  146. command.Parameters.Add(param6);
  147. command.Parameters.Add(param7);
  148. command.Parameters.Add(param8);
  149. command.Parameters.Add(param9);
  150. try
  151. {
  152. command.ExecuteNonQuery();
  153. }
  154. catch (Exception e)
  155. {
  156. m_log.Error(e.ToString());
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// Check if asset exist in database
  162. /// </summary>
  163. /// <param name="uuid"></param>
  164. /// <returns>true if exist.</returns>
  165. override public bool ExistsAsset(LLUUID uuid)
  166. {
  167. if (FetchAsset(uuid) != null)
  168. {
  169. return true;
  170. }
  171. return false;
  172. }
  173. #endregion
  174. #region IPlugin Members
  175. override public void Dispose() { }
  176. /// <summary>
  177. /// <para>Initialises asset interface</para>
  178. /// <para>
  179. /// TODO: this would allow you to pass in connnect info as
  180. /// a string instead of file, if someone writes the support
  181. /// </para>
  182. /// </summary>
  183. /// <param name="connect">connect string</param>
  184. override public void Initialise(string connect)
  185. {
  186. Initialise();
  187. }
  188. /// <summary>
  189. /// Initialises asset interface
  190. /// </summary>
  191. /// <remarks>it use mssql_connection.ini</remarks>
  192. override public void Initialise()
  193. {
  194. IniFile gridDataMSSqlFile = new IniFile("mssql_connection.ini");
  195. string settingDataSource = gridDataMSSqlFile.ParseFileReadValue("data_source");
  196. string settingInitialCatalog = gridDataMSSqlFile.ParseFileReadValue("initial_catalog");
  197. string settingPersistSecurityInfo = gridDataMSSqlFile.ParseFileReadValue("persist_security_info");
  198. string settingUserId = gridDataMSSqlFile.ParseFileReadValue("user_id");
  199. string settingPassword = gridDataMSSqlFile.ParseFileReadValue("password");
  200. database =
  201. new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId,
  202. settingPassword);
  203. TestTables();
  204. }
  205. /// <summary>
  206. /// Database provider version.
  207. /// </summary>
  208. override public string Version
  209. {
  210. get { return database.getVersion(); }
  211. }
  212. /// <summary>
  213. /// The name of this DB provider.
  214. /// </summary>
  215. override public string Name
  216. {
  217. get { return "MSSQL Asset storage engine"; }
  218. }
  219. #endregion
  220. }
  221. }