MSSQLAssetData.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Data.SqlClient;
  32. using libsecondlife;
  33. using OpenSim.Framework.Console;
  34. namespace OpenSim.Framework.Data.MSSQL
  35. {
  36. internal class MSSQLAssetData : IAssetProvider
  37. {
  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. MainLog.Instance.Notice("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. 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. 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], [mediaUrl], [description], [assetType], [invType], [local], [temporary], [data])" +
  81. " VALUES " +
  82. "(@id, @name, @mediaUrl, @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("mediaUrl", asset.MediaURL);
  91. cmd.Parameters.AddWithValue("description", asset.Description);
  92. SqlParameter e = cmd.Parameters.Add("assetType", SqlDbType.TinyInt);
  93. e.Value = asset.Type;
  94. SqlParameter f = cmd.Parameters.Add("invType", SqlDbType.TinyInt);
  95. f.Value = asset.InvType;
  96. SqlParameter g = cmd.Parameters.Add("local", SqlDbType.TinyInt);
  97. g.Value = asset.Local;
  98. SqlParameter h = cmd.Parameters.Add("temporary", SqlDbType.TinyInt);
  99. h.Value = asset.Temporary;
  100. SqlParameter i = cmd.Parameters.Add("data", SqlDbType.Image);
  101. i.Value = asset.Data;
  102. try
  103. {
  104. cmd.ExecuteNonQuery();
  105. }
  106. catch (Exception)
  107. {
  108. throw;
  109. }
  110. cmd.Dispose();
  111. }
  112. }
  113. public void UpdateAsset(AssetBase asset)
  114. {
  115. SqlCommand command = new SqlCommand("UPDATE assets set id = @id, " +
  116. "name = @name, " +
  117. "mediaUrl = @mediaUrl, "+
  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("@mediaUrl", asset.MediaURL);
  128. SqlParameter param4 = new SqlParameter("@description", asset.Description);
  129. SqlParameter param5 = new SqlParameter("@assetType", Convert.ToBoolean(asset.Type));
  130. SqlParameter param6 = new SqlParameter("@invType", Convert.ToBoolean(asset.InvType));
  131. SqlParameter param7 = new SqlParameter("@local", asset.Local);
  132. SqlParameter param8 = new SqlParameter("@temporary", asset.Temporary);
  133. SqlParameter param9 = new SqlParameter("@data", asset.Data);
  134. SqlParameter param10 = new SqlParameter("@keyId", asset.FullID.ToString());
  135. command.Parameters.Add(param1);
  136. command.Parameters.Add(param2);
  137. command.Parameters.Add(param3);
  138. command.Parameters.Add(param4);
  139. command.Parameters.Add(param5);
  140. command.Parameters.Add(param6);
  141. command.Parameters.Add(param7);
  142. command.Parameters.Add(param8);
  143. command.Parameters.Add(param9);
  144. command.Parameters.Add(param10);
  145. try
  146. {
  147. command.ExecuteNonQuery();
  148. }
  149. catch (Exception e)
  150. {
  151. MainLog.Instance.Error(e.ToString());
  152. }
  153. }
  154. public bool ExistsAsset(LLUUID uuid)
  155. {
  156. if (FetchAsset(uuid) != null)
  157. {
  158. return true;
  159. }
  160. return false;
  161. }
  162. // rex, new function, fixme not implemented
  163. public List<AssetBase> GetAssetList(int vAssetType)
  164. {
  165. List<AssetBase> retvals = new List<AssetBase>();
  166. return retvals;
  167. }
  168. /// <summary>
  169. /// All writes are immediately commited to the database, so this is a no-op
  170. /// </summary>
  171. public void CommitAssets()
  172. {
  173. }
  174. // rex new function for "replace assets" functionality
  175. // TODO: actual implementation by someone, should return LLUUID of an asset
  176. // with matching type & name, or zero if not in DB
  177. public LLUUID ExistsAsset(sbyte type, string name)
  178. {
  179. return LLUUID.Zero;
  180. }
  181. #endregion
  182. #region IPlugin Members
  183. public void Initialise()
  184. {
  185. IniFile GridDataMySqlFile = new IniFile("mssql_connection.ini");
  186. string settingDataSource = GridDataMySqlFile.ParseFileReadValue("data_source");
  187. string settingInitialCatalog = GridDataMySqlFile.ParseFileReadValue("initial_catalog");
  188. string settingPersistSecurityInfo = GridDataMySqlFile.ParseFileReadValue("persist_security_info");
  189. string settingUserId = GridDataMySqlFile.ParseFileReadValue("user_id");
  190. string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
  191. database =
  192. new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId,
  193. settingPassword);
  194. TestTables();
  195. }
  196. public string Version
  197. {
  198. // get { return database.getVersion(); }
  199. get { return database.getVersion(); }
  200. }
  201. public string Name
  202. {
  203. get { return "MSSQL Asset storage engine"; }
  204. }
  205. #endregion
  206. }
  207. }