MSSQLAssetData.cs 8.9 KB

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