MSSQLAssetData.cs 8.7 KB

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