MSSQLAssetData.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. AssetBase asset = null;
  80. Dictionary<string, string> param = new Dictionary<string, string>();
  81. param["id"] = assetID.ToString();
  82. IDbCommand result = database.Query("SELECT * FROM assets WHERE id = @id", param);
  83. IDataReader reader = result.ExecuteReader();
  84. asset = database.getAssetRow(reader);
  85. reader.Close();
  86. result.Dispose();
  87. return asset;
  88. }
  89. /// <summary>
  90. /// Create asset in database
  91. /// </summary>
  92. /// <param name="asset">the asset</param>
  93. override public void CreateAsset(AssetBase asset)
  94. {
  95. if (ExistsAsset((LLUUID) asset.FullID))
  96. {
  97. return;
  98. }
  99. SqlCommand cmd =
  100. new SqlCommand(
  101. "INSERT INTO assets ([id], [name], [description], [assetType], [local], [temporary], [data])" +
  102. " VALUES " +
  103. "(@id, @name, @description, @assetType, @local, @temporary, @data)",
  104. database.getConnection());
  105. using (cmd)
  106. {
  107. //SqlParameter p = cmd.Parameters.Add("id", SqlDbType.NVarChar);
  108. //p.Value = asset.FullID.ToString();
  109. cmd.Parameters.AddWithValue("id", asset.FullID.ToString());
  110. cmd.Parameters.AddWithValue("name", asset.Name);
  111. cmd.Parameters.AddWithValue("description", asset.Description);
  112. SqlParameter e = cmd.Parameters.Add("assetType", SqlDbType.TinyInt);
  113. e.Value = asset.Type;
  114. SqlParameter g = cmd.Parameters.Add("local", SqlDbType.TinyInt);
  115. g.Value = asset.Local;
  116. SqlParameter h = cmd.Parameters.Add("temporary", SqlDbType.TinyInt);
  117. h.Value = asset.Temporary;
  118. SqlParameter i = cmd.Parameters.Add("data", SqlDbType.Image);
  119. i.Value = asset.Data;
  120. try
  121. {
  122. cmd.ExecuteNonQuery();
  123. }
  124. catch (Exception)
  125. {
  126. throw;
  127. }
  128. cmd.Dispose();
  129. }
  130. }
  131. /// <summary>
  132. /// Update asset in database
  133. /// </summary>
  134. /// <param name="asset">the asset</param>
  135. override public void UpdateAsset(AssetBase asset)
  136. {
  137. SqlCommand command = new SqlCommand("UPDATE assets set id = @id, " +
  138. "name = @name, " +
  139. "description = @description," +
  140. "assetType = @assetType," +
  141. "local = @local," +
  142. "temporary = @temporary," +
  143. "data = @data where " +
  144. "id = @keyId;", database.getConnection());
  145. SqlParameter param1 = new SqlParameter("@id", asset.FullID.ToString());
  146. SqlParameter param2 = new SqlParameter("@name", asset.Name);
  147. SqlParameter param3 = new SqlParameter("@description", asset.Description);
  148. SqlParameter param4 = new SqlParameter("@assetType", asset.Type);
  149. SqlParameter param6 = new SqlParameter("@local", asset.Local);
  150. SqlParameter param7 = new SqlParameter("@temporary", asset.Temporary);
  151. SqlParameter param8 = new SqlParameter("@data", asset.Data);
  152. SqlParameter param9 = new SqlParameter("@keyId", asset.FullID.ToString());
  153. command.Parameters.Add(param1);
  154. command.Parameters.Add(param2);
  155. command.Parameters.Add(param3);
  156. command.Parameters.Add(param4);
  157. command.Parameters.Add(param6);
  158. command.Parameters.Add(param7);
  159. command.Parameters.Add(param8);
  160. command.Parameters.Add(param9);
  161. try
  162. {
  163. command.ExecuteNonQuery();
  164. }
  165. catch (Exception e)
  166. {
  167. m_log.Error(e.ToString());
  168. }
  169. }
  170. /// <summary>
  171. /// Check if asset exist in database
  172. /// </summary>
  173. /// <param name="uuid"></param>
  174. /// <returns>true if exist.</returns>
  175. override public bool ExistsAsset(LLUUID uuid)
  176. {
  177. if (FetchAsset(uuid) != null)
  178. {
  179. return true;
  180. }
  181. return false;
  182. }
  183. #endregion
  184. #region IPlugin Members
  185. override public void Dispose() { }
  186. /// <summary>
  187. /// <para>Initialises asset interface</para>
  188. /// <para>
  189. /// TODO: this would allow you to pass in connnect info as
  190. /// a string instead of file, if someone writes the support
  191. /// </para>
  192. /// </summary>
  193. /// <param name="connect">connect string</param>
  194. override public void Initialise(string connect)
  195. {
  196. Initialise();
  197. }
  198. /// <summary>
  199. /// Initialises asset interface
  200. /// </summary>
  201. /// <remarks>it use mssql_connection.ini</remarks>
  202. override public void Initialise()
  203. {
  204. IniFile GridDataMySqlFile = new IniFile("mssql_connection.ini");
  205. string settingDataSource = GridDataMySqlFile.ParseFileReadValue("data_source");
  206. string settingInitialCatalog = GridDataMySqlFile.ParseFileReadValue("initial_catalog");
  207. string settingPersistSecurityInfo = GridDataMySqlFile.ParseFileReadValue("persist_security_info");
  208. string settingUserId = GridDataMySqlFile.ParseFileReadValue("user_id");
  209. string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
  210. database =
  211. new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId,
  212. settingPassword);
  213. TestTables();
  214. }
  215. /// <summary>
  216. /// Database provider version.
  217. /// </summary>
  218. override public string Version
  219. {
  220. get { return database.getVersion(); }
  221. }
  222. /// <summary>
  223. /// The name of this DB provider.
  224. /// </summary>
  225. override public string Name
  226. {
  227. get { return "MSSQL Asset storage engine"; }
  228. }
  229. #endregion
  230. }
  231. }