MSSQLXInventoryData.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 OpenSimulator 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;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using System.Data.SqlClient;
  34. using System.Reflection;
  35. using System.Text;
  36. using log4net;
  37. namespace OpenSim.Data.MSSQL
  38. {
  39. public class MSSQLXInventoryData : IXInventoryData
  40. {
  41. // private static readonly ILog m_log = LogManager.GetLogger(
  42. // MethodBase.GetCurrentMethod().DeclaringType);
  43. private MSSQLFolderHandler m_Folders;
  44. private MSSQLItemHandler m_Items;
  45. public MSSQLXInventoryData(string conn, string realm)
  46. {
  47. m_Folders = new MSSQLFolderHandler(
  48. conn, "inventoryfolders", "InventoryStore");
  49. m_Items = new MSSQLItemHandler(
  50. conn, "inventoryitems", String.Empty);
  51. }
  52. public XInventoryFolder[] GetFolders(string[] fields, string[] vals)
  53. {
  54. return m_Folders.Get(fields, vals);
  55. }
  56. public XInventoryItem[] GetItems(string[] fields, string[] vals)
  57. {
  58. return m_Items.Get(fields, vals);
  59. }
  60. public bool StoreFolder(XInventoryFolder folder)
  61. {
  62. if (folder.folderName.Length > 64)
  63. folder.folderName = folder.folderName.Substring(0, 64);
  64. return m_Folders.Store(folder);
  65. }
  66. public bool StoreItem(XInventoryItem item)
  67. {
  68. if (item.inventoryName.Length > 64)
  69. item.inventoryName = item.inventoryName.Substring(0, 64);
  70. if (item.inventoryDescription.Length > 128)
  71. item.inventoryDescription = item.inventoryDescription.Substring(0, 128);
  72. return m_Items.Store(item);
  73. }
  74. public bool DeleteFolders(string field, string val)
  75. {
  76. return m_Folders.Delete(field, val);
  77. }
  78. public bool DeleteFolders(string[] fields, string[] vals)
  79. {
  80. return m_Folders.Delete(fields, vals);
  81. }
  82. public bool DeleteItems(string field, string val)
  83. {
  84. return m_Items.Delete(field, val);
  85. }
  86. public bool DeleteItems(string[] fields, string[] vals)
  87. {
  88. return m_Items.Delete(fields, vals);
  89. }
  90. public bool MoveItem(string id, string newParent)
  91. {
  92. return m_Items.MoveItem(id, newParent);
  93. }
  94. public bool MoveFolder(string id, string newParent)
  95. {
  96. return m_Folders.MoveFolder(id, newParent);
  97. }
  98. public XInventoryItem[] GetActiveGestures(UUID principalID)
  99. {
  100. return m_Items.GetActiveGestures(principalID);
  101. }
  102. public int GetAssetPermissions(UUID principalID, UUID assetID)
  103. {
  104. return m_Items.GetAssetPermissions(principalID, assetID);
  105. }
  106. }
  107. public class MSSQLItemHandler : MSSQLGenericTableHandler<XInventoryItem>
  108. {
  109. public MSSQLItemHandler(string c, string t, string m) :
  110. base(c, t, m)
  111. {
  112. }
  113. public bool MoveItem(string id, string newParent)
  114. {
  115. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  116. {
  117. using (SqlCommand cmd = new SqlCommand())
  118. {
  119. cmd.CommandText = String.Format("update {0} set parentFolderID = @ParentFolderID where inventoryID = @InventoryID", m_Realm);
  120. cmd.Parameters.Add(m_database.CreateParameter("@ParentFolderID", newParent));
  121. cmd.Parameters.Add(m_database.CreateParameter("@InventoryID", id));
  122. cmd.Connection = conn;
  123. conn.Open();
  124. return cmd.ExecuteNonQuery() == 0 ? false : true;
  125. }
  126. }
  127. }
  128. public XInventoryItem[] GetActiveGestures(UUID principalID)
  129. {
  130. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  131. {
  132. using (SqlCommand cmd = new SqlCommand())
  133. {
  134. cmd.CommandText = String.Format("select * from inventoryitems where avatarId = @uuid and assetType = @type and flags = 1", m_Realm);
  135. cmd.Parameters.Add(m_database.CreateParameter("@uuid", principalID.ToString()));
  136. cmd.Parameters.Add(m_database.CreateParameter("@type", (int)AssetType.Gesture));
  137. cmd.Connection = conn;
  138. conn.Open();
  139. return DoQuery(cmd);
  140. }
  141. }
  142. }
  143. public int GetAssetPermissions(UUID principalID, UUID assetID)
  144. {
  145. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  146. {
  147. using (SqlCommand cmd = new SqlCommand())
  148. {
  149. cmd.CommandText = String.Format("select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions from inventoryitems where avatarID = @PrincipalID and assetID = @AssetID group by assetID", m_Realm);
  150. cmd.Parameters.Add(m_database.CreateParameter("@PrincipalID", principalID.ToString()));
  151. cmd.Parameters.Add(m_database.CreateParameter("@AssetID", assetID.ToString()));
  152. cmd.Connection = conn;
  153. conn.Open();
  154. using (SqlDataReader reader = cmd.ExecuteReader())
  155. {
  156. int perms = 0;
  157. if (reader.Read())
  158. {
  159. perms = Convert.ToInt32(reader["inventoryCurrentPermissions"]);
  160. }
  161. return perms;
  162. }
  163. }
  164. }
  165. }
  166. public override bool Store(XInventoryItem item)
  167. {
  168. if (!base.Store(item))
  169. return false;
  170. string sql = "update inventoryfolders set version=version+1 where folderID = @folderID";
  171. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  172. {
  173. using (SqlCommand cmd = new SqlCommand(sql, conn))
  174. {
  175. conn.Open();
  176. cmd.Parameters.AddWithValue("@folderID", item.parentFolderID.ToString());
  177. try
  178. {
  179. cmd.ExecuteNonQuery();
  180. }
  181. catch (Exception)
  182. {
  183. return false;
  184. }
  185. }
  186. return true;
  187. }
  188. }
  189. }
  190. public class MSSQLFolderHandler : MSSQLGenericTableHandler<XInventoryFolder>
  191. {
  192. public MSSQLFolderHandler(string c, string t, string m) :
  193. base(c, t, m)
  194. {
  195. }
  196. public bool MoveFolder(string id, string newParentFolderID)
  197. {
  198. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  199. {
  200. using (SqlCommand cmd = new SqlCommand())
  201. {
  202. cmd.CommandText = String.Format("update {0} set parentFolderID = @ParentFolderID where folderID = @folderID", m_Realm);
  203. cmd.Parameters.Add(m_database.CreateParameter("@ParentFolderID", newParentFolderID));
  204. cmd.Parameters.Add(m_database.CreateParameter("@folderID", id));
  205. cmd.Connection = conn;
  206. conn.Open();
  207. return cmd.ExecuteNonQuery() == 0 ? false : true;
  208. }
  209. }
  210. }
  211. }
  212. }