MySQLXInventoryData.cs 12 KB

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