MySQLXInventoryData.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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.Text;
  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 XInventoryItem[] GetItems(string field, string[] vals)
  61. {
  62. return m_Items.Get(field, vals);
  63. }
  64. public XInventoryItem[] GetItems(string fields, string val)
  65. {
  66. return m_Items.Get(fields, val);
  67. }
  68. public bool StoreFolder(XInventoryFolder folder)
  69. {
  70. if (folder.folderName.Length > 64)
  71. folder.folderName = folder.folderName.Substring(0, 64);
  72. return m_Folders.Store(folder);
  73. }
  74. public bool StoreItem(XInventoryItem item)
  75. {
  76. if (item.inventoryName.Length > 64)
  77. item.inventoryName = item.inventoryName.Substring(0, 64);
  78. if (item.inventoryDescription.Length > 128)
  79. item.inventoryDescription = item.inventoryDescription.Substring(0, 128);
  80. return m_Items.Store(item);
  81. }
  82. public bool DeleteFolders(string field, string val)
  83. {
  84. return m_Folders.Delete(field, val);
  85. }
  86. public bool DeleteFolders(string[] fields, string[] vals)
  87. {
  88. return m_Folders.Delete(fields, vals);
  89. }
  90. public bool DeleteItems(string field, string val)
  91. {
  92. return m_Items.Delete(field, val);
  93. }
  94. public bool DeleteItems(string[] fields, string[] vals)
  95. {
  96. return m_Items.Delete(fields, vals);
  97. }
  98. public bool MoveItem(string id, string newParent)
  99. {
  100. return m_Items.MoveItem(id, newParent);
  101. }
  102. public bool MoveItems(string[] ids, string[] newParents)
  103. {
  104. return m_Items.MoveItems(ids, newParents);
  105. }
  106. public bool MoveFolder(string id, string newParent)
  107. {
  108. return m_Folders.MoveFolder(id, newParent);
  109. }
  110. public XInventoryItem[] GetActiveGestures(UUID principalID)
  111. {
  112. return m_Items.GetActiveGestures(principalID);
  113. }
  114. public int GetAssetPermissions(UUID principalID, UUID assetID)
  115. {
  116. return m_Items.GetAssetPermissions(principalID, assetID);
  117. }
  118. }
  119. public class MySqlItemHandler : MySqlInventoryHandler<XInventoryItem>
  120. {
  121. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  122. public MySqlItemHandler(string c, string t, string m) :
  123. base(c, t, m)
  124. {
  125. }
  126. public override bool Delete(string field, string val)
  127. {
  128. XInventoryItem[] retrievedItems = Get(new string[] { field }, new string[] { val });
  129. if (retrievedItems.Length == 0)
  130. return false;
  131. if (!base.Delete(field, val))
  132. return false;
  133. IncrementFolderVersion(retrievedItems[0].parentFolderID);
  134. return true;
  135. }
  136. public override bool Delete(string[] fields, string[] vals)
  137. {
  138. XInventoryItem[] retrievedItems = Get(fields, vals);
  139. if (retrievedItems.Length == 0)
  140. return false;
  141. if (!base.Delete(fields, vals))
  142. return false;
  143. HashSet<UUID> deletedItemFolderUUIDs = new HashSet<UUID>();
  144. Array.ForEach<XInventoryItem>(retrievedItems, i => deletedItemFolderUUIDs.Add(i.parentFolderID));
  145. foreach (UUID deletedItemFolderUUID in deletedItemFolderUUIDs)
  146. IncrementFolderVersion(deletedItemFolderUUID);
  147. return true;
  148. }
  149. public bool MoveItem(string id, string newParent)
  150. {
  151. XInventoryItem[] retrievedItems = Get("inventoryID", id);
  152. if (retrievedItems.Length == 0)
  153. return false;
  154. UUID oldParent = retrievedItems[0].parentFolderID;
  155. using (MySqlCommand cmd = new MySqlCommand())
  156. {
  157. cmd.CommandText = String.Format("update {0} set parentFolderID = ?ParentFolderID where inventoryID = ?InventoryID", m_Realm);
  158. cmd.Parameters.AddWithValue("?ParentFolderID", newParent);
  159. cmd.Parameters.AddWithValue("?InventoryID", id);
  160. if (ExecuteNonQuery(cmd) == 0)
  161. return false;
  162. }
  163. IncrementFolderVersion(newParent);
  164. if(oldParent.ToString() != newParent)
  165. IncrementFolderVersion(oldParent);
  166. return true;
  167. }
  168. public bool MoveItems(string[] ids, string[] newParents)
  169. {
  170. int len = ids.Length;
  171. if(len == 0)
  172. return false;
  173. MySqlConnection dbcon;
  174. MySqlCommand cmd;
  175. MySqlDataReader rdr;
  176. try
  177. {
  178. dbcon = new MySqlConnection(m_connectionString);
  179. dbcon.Open();
  180. }
  181. catch
  182. {
  183. return false;
  184. }
  185. HashSet<string> changedfolders = new HashSet<string>();
  186. try
  187. {
  188. UUID utmp;
  189. int flast = len - 1;
  190. StringBuilder sb = new StringBuilder(1024);
  191. sb.AppendFormat("select parentFolderID from {0} where inventoryID IN ('", m_Realm);
  192. for (int i = 0 ; i < len ; ++i)
  193. {
  194. sb.Append(ids[i]);
  195. if(i < flast)
  196. sb.Append("','");
  197. else
  198. sb.Append("')");
  199. }
  200. string[] oldparents = new string[len];
  201. int l = 0;
  202. using (cmd = new MySqlCommand())
  203. {
  204. cmd.CommandText = sb.ToString();
  205. cmd.Connection = dbcon;
  206. rdr = cmd.ExecuteReader();
  207. while (rdr.Read() && l < len)
  208. {
  209. if(!(rdr[0] is string))
  210. oldparents[l++] = null;
  211. else
  212. oldparents[l++] = (string)rdr[0];
  213. }
  214. rdr.Close();
  215. }
  216. if(l == 0)
  217. return false;
  218. l = 0;
  219. sb = new StringBuilder(1024);
  220. string sbformat = String.Format("insert into {0} (inventoryID,parentFolderID) values{1} on duplicate key update parentFolderID = values(parentFolderID)", m_Realm,"{0}");
  221. for(int k = 0; k < len; k++)
  222. {
  223. string oldid = oldparents[k];
  224. if(String.IsNullOrWhiteSpace(oldid) || !UUID.TryParse(oldid, out utmp))
  225. continue;
  226. string newParent = newParents[k];
  227. if(String.IsNullOrWhiteSpace(newParent) || !UUID.TryParse(newParent, out utmp))
  228. continue;
  229. string id = ids[k];
  230. if(id == oldid)
  231. continue;
  232. sb.AppendFormat("(\'{0}\',\'{1}\')",id, newParent);
  233. if(k < flast)
  234. sb.Append(",");
  235. if(!changedfolders.Contains(newParent))
  236. changedfolders.Add(newParent);
  237. if(!changedfolders.Contains(oldid))
  238. changedfolders.Add(oldid);
  239. ++l;
  240. }
  241. if(l == 0)
  242. return false;
  243. oldparents = null;
  244. newParents = null;
  245. ids = null;
  246. using (cmd = new MySqlCommand())
  247. {
  248. using(MySqlTransaction trans = dbcon.BeginTransaction())
  249. {
  250. cmd.Connection = dbcon;
  251. cmd.Transaction = trans;
  252. try
  253. {
  254. cmd.CommandText = string.Format(sbformat,sb.ToString());
  255. int r = cmd.ExecuteNonQuery();
  256. if(r == 2 * l)
  257. trans.Commit();
  258. else
  259. {
  260. // we did got insertions so need to bail out
  261. trans.Rollback();
  262. return false;
  263. }
  264. }
  265. catch
  266. {
  267. trans.Rollback();
  268. return false;
  269. }
  270. }
  271. }
  272. if(changedfolders.Count == 0)
  273. return true;
  274. sb = new StringBuilder(256);
  275. sb.Append("insert into inventoryfolders (folderID) values");
  276. l = 0;
  277. flast = changedfolders.Count - 1;
  278. foreach(UUID uu in changedfolders)
  279. {
  280. sb.AppendFormat("(\'{0}\')",uu);
  281. if(l < flast)
  282. sb.Append(",");
  283. ++l;
  284. }
  285. changedfolders = null;
  286. sb.Append(" on duplicate key update version = version + 1");
  287. using (cmd = new MySqlCommand())
  288. {
  289. using(MySqlTransaction trans = dbcon.BeginTransaction())
  290. {
  291. cmd.Connection = dbcon;
  292. cmd.Transaction = trans;
  293. cmd.CommandText = sb.ToString();
  294. try
  295. {
  296. int r = cmd.ExecuteNonQuery();
  297. if(r == 2 * l)
  298. trans.Commit();
  299. else
  300. {
  301. // we did got insertions so need to bail out
  302. trans.Rollback();
  303. }
  304. }
  305. catch
  306. {
  307. trans.Rollback();
  308. }
  309. }
  310. }
  311. }
  312. catch
  313. {
  314. return false;
  315. }
  316. finally
  317. {
  318. dbcon.Close();
  319. }
  320. return true;
  321. }
  322. public XInventoryItem[] GetActiveGestures(UUID principalID)
  323. {
  324. using (MySqlCommand cmd = new MySqlCommand())
  325. {
  326. // cmd.CommandText = String.Format("select * from inventoryitems where avatarId = ?uuid and assetType = ?type and flags & 1", m_Realm);
  327. cmd.CommandText = String.Format("select * from inventoryitems where avatarId = ?uuid and assetType = ?type and flags & 1");
  328. cmd.Parameters.AddWithValue("?uuid", principalID.ToString());
  329. cmd.Parameters.AddWithValue("?type", (int)AssetType.Gesture);
  330. return DoQuery(cmd);
  331. }
  332. }
  333. public int GetAssetPermissions(UUID principalID, UUID assetID)
  334. {
  335. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  336. {
  337. dbcon.Open();
  338. using (MySqlCommand cmd = new MySqlCommand())
  339. {
  340. cmd.Connection = dbcon;
  341. // cmd.CommandText = String.Format("select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions from inventoryitems where avatarID = ?PrincipalID and assetID = ?AssetID group by assetID", m_Realm);
  342. cmd.CommandText = String.Format("select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions from inventoryitems where avatarID = ?PrincipalID and assetID = ?AssetID group by assetID");
  343. cmd.Parameters.AddWithValue("?PrincipalID", principalID.ToString());
  344. cmd.Parameters.AddWithValue("?AssetID", assetID.ToString());
  345. using (IDataReader reader = cmd.ExecuteReader())
  346. {
  347. int perms = 0;
  348. if (reader.Read())
  349. {
  350. perms = Convert.ToInt32(reader["inventoryCurrentPermissions"]);
  351. }
  352. return perms;
  353. }
  354. }
  355. }
  356. }
  357. public override bool Store(XInventoryItem item)
  358. {
  359. if (!base.Store(item))
  360. return false;
  361. IncrementFolderVersion(item.parentFolderID);
  362. return true;
  363. }
  364. }
  365. public class MySqlFolderHandler : MySqlInventoryHandler<XInventoryFolder>
  366. {
  367. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  368. public MySqlFolderHandler(string c, string t, string m) :
  369. base(c, t, m)
  370. {
  371. }
  372. public bool MoveFolder(string id, string newParentFolderID)
  373. {
  374. XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id });
  375. if (folders.Length == 0)
  376. return false;
  377. UUID oldParentFolderUUID = folders[0].parentFolderID;
  378. using (MySqlCommand cmd = new MySqlCommand())
  379. {
  380. cmd.CommandText
  381. = String.Format(
  382. "update {0} set parentFolderID = ?ParentFolderID where folderID = ?folderID", m_Realm);
  383. cmd.Parameters.AddWithValue("?ParentFolderID", newParentFolderID);
  384. cmd.Parameters.AddWithValue("?folderID", id);
  385. if (ExecuteNonQuery(cmd) == 0)
  386. return false;
  387. }
  388. IncrementFolderVersion(oldParentFolderUUID);
  389. IncrementFolderVersion(newParentFolderID);
  390. return true;
  391. }
  392. public override bool Store(XInventoryFolder folder)
  393. {
  394. if (!base.Store(folder))
  395. return false;
  396. IncrementFolderVersion(folder.parentFolderID);
  397. return true;
  398. }
  399. }
  400. public class MySqlInventoryHandler<T> : MySQLGenericTableHandler<T> where T: class, new()
  401. {
  402. public MySqlInventoryHandler(string c, string t, string m) : base(c, t, m) {}
  403. protected bool IncrementFolderVersion(UUID folderID)
  404. {
  405. return IncrementFolderVersion(folderID.ToString());
  406. }
  407. protected bool IncrementFolderVersion(string folderID)
  408. {
  409. // m_log.DebugFormat("[MYSQL FOLDER HANDLER]: Incrementing version on folder {0}", folderID);
  410. // Util.PrintCallStack();
  411. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  412. {
  413. dbcon.Open();
  414. using (MySqlCommand cmd = new MySqlCommand())
  415. {
  416. cmd.Connection = dbcon;
  417. cmd.CommandText = String.Format("update inventoryfolders set version=version+1 where folderID = ?folderID");
  418. cmd.Parameters.AddWithValue("?folderID", folderID);
  419. try
  420. {
  421. cmd.ExecuteNonQuery();
  422. }
  423. catch (Exception)
  424. {
  425. return false;
  426. }
  427. }
  428. dbcon.Close();
  429. }
  430. return true;
  431. }
  432. }
  433. }