1
0

MySQLXInventoryData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.Generic;
  29. using System.Data;
  30. using log4net;
  31. using MySql.Data.MySqlClient;
  32. using OpenMetaverse;
  33. namespace OpenSim.Data.MySQL
  34. {
  35. /// <summary>
  36. /// A MySQL Interface for the Inventory Server
  37. /// </summary>
  38. public class MySQLXInventoryData : IXInventoryData
  39. {
  40. private MySqlFolderHandler m_Folders;
  41. private MySqlItemHandler m_Items;
  42. public MySQLXInventoryData(string conn, string realm)
  43. {
  44. m_Folders = new MySqlFolderHandler(conn, "inventoryfolders", "InventoryStore");
  45. m_Items = new MySqlItemHandler(conn, "inventoryitems", string.Empty);
  46. }
  47. public XInventoryFolder[] GetFolder(string field, string val)
  48. {
  49. return m_Folders.Get(field, val);
  50. }
  51. public XInventoryFolder[] GetFolders(string[] fields, string[] vals)
  52. {
  53. return m_Folders.Get(fields, vals);
  54. }
  55. public XInventoryItem[] GetItems(string[] fields, string[] vals)
  56. {
  57. return m_Items.Get(fields, vals);
  58. }
  59. public bool StoreFolder(XInventoryFolder folder)
  60. {
  61. if (folder.folderName.Length > 64)
  62. folder.folderName = folder.folderName.Substring(0, 64);
  63. return m_Folders.Store(folder);
  64. }
  65. public bool StoreItem(XInventoryItem item)
  66. {
  67. if (item.inventoryName.Length > 64)
  68. item.inventoryName = item.inventoryName[..64];
  69. if (item.inventoryDescription.Length > 128)
  70. item.inventoryDescription = item.inventoryDescription[..128];
  71. return m_Items.Store(item);
  72. }
  73. public bool DeleteFolders(string field, string val)
  74. {
  75. return m_Folders.Delete(field, val);
  76. }
  77. public bool DeleteFolders(string[] fields, string[] vals)
  78. {
  79. return m_Folders.Delete(fields, vals);
  80. }
  81. public bool DeleteItems(string field, string val)
  82. {
  83. return m_Items.Delete(field, val);
  84. }
  85. public bool DeleteItems(string[] fields, string[] vals)
  86. {
  87. return m_Items.Delete(fields, vals);
  88. }
  89. public bool MoveItem(string id, string newParent)
  90. {
  91. return m_Items.MoveItem(id, newParent);
  92. }
  93. public bool MoveFolder(string id, string newParent)
  94. {
  95. return m_Folders.MoveFolder(id, newParent);
  96. }
  97. public XInventoryItem[] GetActiveGestures(UUID principalID)
  98. {
  99. return m_Items.GetActiveGestures(principalID);
  100. }
  101. public int GetAssetPermissions(UUID principalID, UUID assetID)
  102. {
  103. return m_Items.GetAssetPermissions(principalID, assetID);
  104. }
  105. }
  106. public class MySqlItemHandler : MySqlInventoryHandler<XInventoryItem>
  107. {
  108. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  109. public MySqlItemHandler(string c, string t, string m) : base(c, t, m)
  110. {
  111. }
  112. public override bool Delete(string field, string val)
  113. {
  114. XInventoryItem[] retrievedItems = Get(new string[] { field }, new string[] { val });
  115. if (retrievedItems.Length == 0)
  116. return false;
  117. if (!base.Delete(field, val))
  118. return false;
  119. // Don't increment folder version here since Delete(string, string) calls Delete(string[], string[])
  120. //IncrementFolderVersion(retrievedItems[0].parentFolderID);
  121. return true;
  122. }
  123. public override bool Delete(string[] fields, string[] vals)
  124. {
  125. XInventoryItem[] retrievedItems = Get(fields, vals);
  126. if (retrievedItems.Length == 0)
  127. return false;
  128. if (!base.Delete(fields, vals))
  129. return false;
  130. HashSet<UUID> deletedItemFolderUUIDs = new HashSet<UUID>();
  131. Array.ForEach<XInventoryItem>(retrievedItems, i => deletedItemFolderUUIDs.Add(i.parentFolderID));
  132. foreach (UUID deletedItemFolderUUID in deletedItemFolderUUIDs)
  133. IncrementFolderVersion(deletedItemFolderUUID);
  134. return true;
  135. }
  136. public bool MoveItem(string id, string newParent)
  137. {
  138. XInventoryItem[] retrievedItems = Get(new string[] { "inventoryID" }, new string[] { id });
  139. if (retrievedItems.Length == 0)
  140. return false;
  141. UUID oldParent = retrievedItems[0].parentFolderID;
  142. using (MySqlCommand cmd = new MySqlCommand())
  143. {
  144. cmd.CommandText = String.Format("update {0} set parentFolderID = ?ParentFolderID where inventoryID = ?InventoryID", m_Realm);
  145. cmd.Parameters.AddWithValue("?ParentFolderID", newParent);
  146. cmd.Parameters.AddWithValue("?InventoryID", id);
  147. if (ExecuteNonQuery(cmd) == 0)
  148. return false;
  149. }
  150. IncrementFolderVersion(oldParent);
  151. IncrementFolderVersion(newParent);
  152. return true;
  153. }
  154. public XInventoryItem[] GetActiveGestures(UUID principalID)
  155. {
  156. using (MySqlCommand cmd = new MySqlCommand())
  157. {
  158. //cmd.CommandText = String.Format("select * from inventoryitems where avatarId = ?uuid and assetType = ?type and flags & 1", m_Realm);
  159. cmd.CommandText = "select * from inventoryitems where avatarId = ?uuid and assetType = ?type and flags & 1";
  160. cmd.Parameters.AddWithValue("?uuid", principalID.ToString());
  161. cmd.Parameters.AddWithValue("?type", (int)AssetType.Gesture);
  162. return DoQuery(cmd);
  163. }
  164. }
  165. public int GetAssetPermissions(UUID principalID, UUID assetID)
  166. {
  167. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  168. {
  169. dbcon.Open();
  170. using (MySqlCommand cmd = new MySqlCommand())
  171. {
  172. cmd.Connection = dbcon;
  173. //cmd.CommandText = String.Format("select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions from inventoryitems where avatarID = ?PrincipalID and assetID = ?AssetID group by assetID", m_Realm);
  174. cmd.CommandText = "select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions from inventoryitems where avatarID = ?PrincipalID and assetID = ?AssetID group by assetID";
  175. cmd.Parameters.AddWithValue("?PrincipalID", principalID.ToString());
  176. cmd.Parameters.AddWithValue("?AssetID", assetID.ToString());
  177. using (IDataReader reader = cmd.ExecuteReader())
  178. {
  179. int perms = 0;
  180. if (reader.Read())
  181. {
  182. perms = Convert.ToInt32(reader["inventoryCurrentPermissions"]);
  183. }
  184. return perms;
  185. }
  186. }
  187. }
  188. }
  189. public override bool Store(XInventoryItem item)
  190. {
  191. if (!base.Store(item))
  192. return false;
  193. IncrementFolderVersion(item.parentFolderID);
  194. return true;
  195. }
  196. }
  197. public class MySqlFolderHandler : MySqlInventoryHandler<XInventoryFolder>
  198. {
  199. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  200. public MySqlFolderHandler(string c, string t, string m) : base(c, t, m)
  201. {
  202. }
  203. public bool MoveFolder(string id, string newParentFolderID)
  204. {
  205. XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id });
  206. if (folders.Length == 0)
  207. return false;
  208. UUID oldParentFolderUUID = folders[0].parentFolderID;
  209. using (MySqlCommand cmd = new MySqlCommand())
  210. {
  211. cmd.CommandText = $"update {m_Realm} set parentFolderID = ?ParentFolderID where folderID = ?folderID";
  212. cmd.Parameters.AddWithValue("?ParentFolderID", newParentFolderID);
  213. cmd.Parameters.AddWithValue("?folderID", id);
  214. if (ExecuteNonQuery(cmd) == 0)
  215. return false;
  216. }
  217. IncrementFolderVersion(oldParentFolderUUID);
  218. IncrementFolderVersion(newParentFolderID);
  219. return true;
  220. }
  221. public override bool Store(XInventoryFolder folder)
  222. {
  223. if (!base.Store(folder))
  224. return false;
  225. IncrementFolderVersion(folder.parentFolderID);
  226. return true;
  227. }
  228. }
  229. public class MySqlInventoryHandler<T> : MySQLGenericTableHandler<T> where T: class, new()
  230. {
  231. public MySqlInventoryHandler(string c, string t, string m) : base(c, t, m) {}
  232. protected bool IncrementFolderVersion(UUID folderID)
  233. {
  234. return IncrementFolderVersion(folderID.ToString());
  235. }
  236. protected bool IncrementFolderVersion(string folderID)
  237. {
  238. //m_log.DebugFormat("[MYSQL FOLDER HANDLER]: Incrementing version on folder {0}", folderID);
  239. //Util.PrintCallStack();
  240. using (MySqlConnection dbcon = new(m_connectionString))
  241. {
  242. dbcon.Open();
  243. using (MySqlCommand cmd = new())
  244. {
  245. cmd.Connection = dbcon;
  246. cmd.CommandText = "update inventoryfolders set version=version+1 where folderID = ?folderID";
  247. cmd.Parameters.AddWithValue("?folderID", folderID);
  248. try
  249. {
  250. cmd.ExecuteNonQuery();
  251. }
  252. catch (Exception)
  253. {
  254. return false;
  255. }
  256. }
  257. dbcon.Close();
  258. }
  259. return true;
  260. }
  261. }
  262. }