1
0

MySQLInventoryData.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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.Reflection;
  30. using log4net;
  31. using MySql.Data.MySqlClient;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Data;
  35. namespace OpenSim.Data.MySQL
  36. {
  37. /// <summary>
  38. /// A MySQL interface for the inventory server
  39. /// </summary>
  40. public class MySQLInventoryData : IInventoryDataPlugin
  41. {
  42. private static readonly ILog m_log
  43. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private string m_connectionString;
  45. private object m_dbLock = new object();
  46. public string Version { get { return "1.0.0.0"; } }
  47. public void Initialise()
  48. {
  49. m_log.Info("[MySQLInventoryData]: " + Name + " cannot be default-initialized!");
  50. throw new PluginNotInitialisedException (Name);
  51. }
  52. /// <summary>
  53. /// <para>Initialises Inventory interface</para>
  54. /// <para>
  55. /// <list type="bullet">
  56. /// <item>Loads and initialises the MySQL storage plugin</item>
  57. /// <item>warns and uses the obsolete mysql_connection.ini if connect string is empty.</item>
  58. /// <item>Check for migration</item>
  59. /// </list>
  60. /// </para>
  61. /// </summary>
  62. /// <param name="connect">connect string</param>
  63. public void Initialise(string connect)
  64. {
  65. m_connectionString = connect;
  66. // This actually does the roll forward assembly stuff
  67. Assembly assem = GetType().Assembly;
  68. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  69. {
  70. dbcon.Open();
  71. Migration m = new Migration(dbcon, assem, "InventoryStore");
  72. m.Update();
  73. }
  74. }
  75. /// <summary>
  76. /// The name of this DB provider
  77. /// </summary>
  78. /// <returns>Name of DB provider</returns>
  79. public string Name
  80. {
  81. get { return "MySQL Inventory Data Interface"; }
  82. }
  83. /// <summary>
  84. /// Closes this DB provider
  85. /// </summary>
  86. /// <remarks>do nothing</remarks>
  87. public void Dispose()
  88. {
  89. // Do nothing.
  90. }
  91. /// <summary>
  92. /// Returns a list of items in a specified folder
  93. /// </summary>
  94. /// <param name="folderID">The folder to search</param>
  95. /// <returns>A list containing inventory items</returns>
  96. public List<InventoryItemBase> getInventoryInFolder(UUID folderID)
  97. {
  98. try
  99. {
  100. lock (m_dbLock)
  101. {
  102. List<InventoryItemBase> items = new List<InventoryItemBase>();
  103. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  104. {
  105. dbcon.Open();
  106. using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", dbcon))
  107. {
  108. result.Parameters.AddWithValue("?uuid", folderID.ToString());
  109. using (MySqlDataReader reader = result.ExecuteReader())
  110. {
  111. while (reader.Read())
  112. {
  113. // A null item (because something went wrong) breaks everything in the folder
  114. InventoryItemBase item = readInventoryItem(reader);
  115. if (item != null)
  116. items.Add(item);
  117. }
  118. return items;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. catch (Exception e)
  125. {
  126. m_log.Error(e.Message, e);
  127. return null;
  128. }
  129. }
  130. /// <summary>
  131. /// Returns a list of the root folders within a users inventory
  132. /// </summary>
  133. /// <param name="user">The user whose inventory is to be searched</param>
  134. /// <returns>A list of folder objects</returns>
  135. public List<InventoryFolderBase> getUserRootFolders(UUID user)
  136. {
  137. try
  138. {
  139. lock (m_dbLock)
  140. {
  141. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  142. {
  143. dbcon.Open();
  144. using (MySqlCommand result = new MySqlCommand(
  145. "SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", dbcon))
  146. {
  147. result.Parameters.AddWithValue("?uuid", user.ToString());
  148. result.Parameters.AddWithValue("?zero", UUID.Zero.ToString());
  149. using (MySqlDataReader reader = result.ExecuteReader())
  150. {
  151. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  152. while (reader.Read())
  153. items.Add(readInventoryFolder(reader));
  154. return items;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. catch (Exception e)
  161. {
  162. m_log.Error(e.Message, e);
  163. return null;
  164. }
  165. }
  166. /// <summary>
  167. /// see <see cref="InventoryItemBase.getUserRootFolder"/>
  168. /// </summary>
  169. /// <param name="user">The user UUID</param>
  170. /// <returns></returns>
  171. public InventoryFolderBase getUserRootFolder(UUID user)
  172. {
  173. try
  174. {
  175. lock (m_dbLock)
  176. {
  177. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  178. {
  179. dbcon.Open();
  180. using (MySqlCommand result = new MySqlCommand(
  181. "SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", dbcon))
  182. {
  183. result.Parameters.AddWithValue("?uuid", user.ToString());
  184. result.Parameters.AddWithValue("?zero", UUID.Zero.ToString());
  185. using (MySqlDataReader reader = result.ExecuteReader())
  186. {
  187. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  188. while (reader.Read())
  189. items.Add(readInventoryFolder(reader));
  190. InventoryFolderBase rootFolder = null;
  191. // There should only ever be one root folder for a user. However, if there's more
  192. // than one we'll simply use the first one rather than failing. It would be even
  193. // nicer to print some message to this effect, but this feels like it's too low a
  194. // to put such a message out, and it's too minor right now to spare the time to
  195. // suitably refactor.
  196. if (items.Count > 0)
  197. rootFolder = items[0];
  198. return rootFolder;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. catch (Exception e)
  205. {
  206. m_log.Error(e.Message, e);
  207. return null;
  208. }
  209. }
  210. /// <summary>
  211. /// Return a list of folders in a users inventory contained within the specified folder.
  212. /// This method is only used in tests - in normal operation the user always have one,
  213. /// and only one, root folder.
  214. /// </summary>
  215. /// <param name="parentID">The folder to search</param>
  216. /// <returns>A list of inventory folders</returns>
  217. public List<InventoryFolderBase> getInventoryFolders(UUID parentID)
  218. {
  219. try
  220. {
  221. lock (m_dbLock)
  222. {
  223. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  224. {
  225. dbcon.Open();
  226. using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", dbcon))
  227. {
  228. result.Parameters.AddWithValue("?uuid", parentID.ToString());
  229. using (MySqlDataReader reader = result.ExecuteReader())
  230. {
  231. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  232. while (reader.Read())
  233. items.Add(readInventoryFolder(reader));
  234. return items;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. catch (Exception e)
  241. {
  242. m_log.Error(e.Message, e);
  243. return null;
  244. }
  245. }
  246. /// <summary>
  247. /// Reads a one item from an SQL result
  248. /// </summary>
  249. /// <param name="reader">The SQL Result</param>
  250. /// <returns>the item read</returns>
  251. private static InventoryItemBase readInventoryItem(MySqlDataReader reader)
  252. {
  253. try
  254. {
  255. InventoryItemBase item = new InventoryItemBase();
  256. // TODO: this is to handle a case where NULLs creep in there, which we are not sure is endemic to the system, or legacy. It would be nice to live fix these.
  257. // (DBGuid.FromDB() reads db NULLs as well, returns UUID.Zero)
  258. item.CreatorId = reader["creatorID"].ToString();
  259. // Be a bit safer in parsing these because the
  260. // database doesn't enforce them to be not null, and
  261. // the inventory still works if these are weird in the
  262. // db
  263. // (Empty is Ok, but "weird" will throw!)
  264. item.Owner = DBGuid.FromDB(reader["avatarID"]);
  265. item.GroupID = DBGuid.FromDB(reader["groupID"]);
  266. // Rest of the parsing. If these UUID's fail, we're dead anyway
  267. item.ID = DBGuid.FromDB(reader["inventoryID"]);
  268. item.AssetID = DBGuid.FromDB(reader["assetID"]);
  269. item.AssetType = (int) reader["assetType"];
  270. item.Folder = DBGuid.FromDB(reader["parentFolderID"]);
  271. item.Name = (string)(reader["inventoryName"] ?? String.Empty);
  272. item.Description = (string)(reader["inventoryDescription"] ?? String.Empty);
  273. item.NextPermissions = (uint) reader["inventoryNextPermissions"];
  274. item.CurrentPermissions = (uint) reader["inventoryCurrentPermissions"];
  275. item.InvType = (int) reader["invType"];
  276. item.BasePermissions = (uint) reader["inventoryBasePermissions"];
  277. item.EveryOnePermissions = (uint) reader["inventoryEveryOnePermissions"];
  278. item.GroupPermissions = (uint) reader["inventoryGroupPermissions"];
  279. item.SalePrice = (int) reader["salePrice"];
  280. item.SaleType = unchecked((byte)(Convert.ToSByte(reader["saleType"])));
  281. item.CreationDate = (int) reader["creationDate"];
  282. item.GroupOwned = Convert.ToBoolean(reader["groupOwned"]);
  283. item.Flags = (uint) reader["flags"];
  284. return item;
  285. }
  286. catch (MySqlException e)
  287. {
  288. m_log.Error(e.ToString());
  289. }
  290. return null;
  291. }
  292. /// <summary>
  293. /// Returns a specified inventory item
  294. /// </summary>
  295. /// <param name="item">The item to return</param>
  296. /// <returns>An inventory item</returns>
  297. public InventoryItemBase getInventoryItem(UUID itemID)
  298. {
  299. try
  300. {
  301. lock (m_dbLock)
  302. {
  303. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  304. {
  305. dbcon.Open();
  306. using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", dbcon))
  307. {
  308. result.Parameters.AddWithValue("?uuid", itemID.ToString());
  309. using (MySqlDataReader reader = result.ExecuteReader())
  310. {
  311. InventoryItemBase item = null;
  312. if (reader.Read())
  313. item = readInventoryItem(reader);
  314. return item;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. catch (Exception e)
  321. {
  322. m_log.Error(e.Message, e);
  323. }
  324. return null;
  325. }
  326. /// <summary>
  327. /// Reads a list of inventory folders returned by a query.
  328. /// </summary>
  329. /// <param name="reader">A MySQL Data Reader</param>
  330. /// <returns>A List containing inventory folders</returns>
  331. protected static InventoryFolderBase readInventoryFolder(MySqlDataReader reader)
  332. {
  333. try
  334. {
  335. InventoryFolderBase folder = new InventoryFolderBase();
  336. folder.Owner = DBGuid.FromDB(reader["agentID"]);
  337. folder.ParentID = DBGuid.FromDB(reader["parentFolderID"]);
  338. folder.ID = DBGuid.FromDB(reader["folderID"]);
  339. folder.Name = (string) reader["folderName"];
  340. folder.Type = (short) reader["type"];
  341. folder.Version = (ushort) ((int) reader["version"]);
  342. return folder;
  343. }
  344. catch (Exception e)
  345. {
  346. m_log.Error(e.Message, e);
  347. }
  348. return null;
  349. }
  350. /// <summary>
  351. /// Returns a specified inventory folder
  352. /// </summary>
  353. /// <param name="folderID">The folder to return</param>
  354. /// <returns>A folder class</returns>
  355. public InventoryFolderBase getInventoryFolder(UUID folderID)
  356. {
  357. try
  358. {
  359. lock (m_dbLock)
  360. {
  361. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  362. {
  363. dbcon.Open();
  364. using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", dbcon))
  365. {
  366. result.Parameters.AddWithValue("?uuid", folderID.ToString());
  367. using (MySqlDataReader reader = result.ExecuteReader())
  368. {
  369. InventoryFolderBase folder = null;
  370. if (reader.Read())
  371. folder = readInventoryFolder(reader);
  372. return folder;
  373. }
  374. }
  375. }
  376. }
  377. }
  378. catch (Exception e)
  379. {
  380. m_log.Error(e.Message, e);
  381. return null;
  382. }
  383. }
  384. /// <summary>
  385. /// Adds a specified item to the database
  386. /// </summary>
  387. /// <param name="item">The inventory item</param>
  388. public void addInventoryItem(InventoryItemBase item)
  389. {
  390. string sql =
  391. "REPLACE INTO inventoryitems (inventoryID, assetID, assetType, parentFolderID, avatarID, inventoryName"
  392. + ", inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions, invType"
  393. + ", creatorID, inventoryBasePermissions, inventoryEveryOnePermissions, inventoryGroupPermissions, salePrice, saleType"
  394. + ", creationDate, groupID, groupOwned, flags) VALUES ";
  395. sql +=
  396. "(?inventoryID, ?assetID, ?assetType, ?parentFolderID, ?avatarID, ?inventoryName, ?inventoryDescription"
  397. + ", ?inventoryNextPermissions, ?inventoryCurrentPermissions, ?invType, ?creatorID"
  398. + ", ?inventoryBasePermissions, ?inventoryEveryOnePermissions, ?inventoryGroupPermissions, ?salePrice, ?saleType, ?creationDate"
  399. + ", ?groupID, ?groupOwned, ?flags)";
  400. string itemName = item.Name;
  401. if (item.Name.Length > 64)
  402. {
  403. itemName = item.Name.Substring(0, 64);
  404. m_log.Warn("[INVENTORY DB]: Name field truncated from " + item.Name.Length + " to " + itemName.Length + " characters on add item");
  405. }
  406. string itemDesc = item.Description;
  407. if (item.Description.Length > 128)
  408. {
  409. itemDesc = item.Description.Substring(0, 128);
  410. m_log.Warn("[INVENTORY DB]: Description field truncated from " + item.Description.Length + " to " + itemDesc.Length + " characters on add item");
  411. }
  412. try
  413. {
  414. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  415. {
  416. dbcon.Open();
  417. using (MySqlCommand result = new MySqlCommand(sql, dbcon))
  418. {
  419. result.Parameters.AddWithValue("?inventoryID", item.ID.ToString());
  420. result.Parameters.AddWithValue("?assetID", item.AssetID.ToString());
  421. result.Parameters.AddWithValue("?assetType", item.AssetType.ToString());
  422. result.Parameters.AddWithValue("?parentFolderID", item.Folder.ToString());
  423. result.Parameters.AddWithValue("?avatarID", item.Owner.ToString());
  424. result.Parameters.AddWithValue("?inventoryName", itemName);
  425. result.Parameters.AddWithValue("?inventoryDescription", itemDesc);
  426. result.Parameters.AddWithValue("?inventoryNextPermissions", item.NextPermissions.ToString());
  427. result.Parameters.AddWithValue("?inventoryCurrentPermissions",
  428. item.CurrentPermissions.ToString());
  429. result.Parameters.AddWithValue("?invType", item.InvType);
  430. result.Parameters.AddWithValue("?creatorID", item.CreatorId);
  431. result.Parameters.AddWithValue("?inventoryBasePermissions", item.BasePermissions);
  432. result.Parameters.AddWithValue("?inventoryEveryOnePermissions", item.EveryOnePermissions);
  433. result.Parameters.AddWithValue("?inventoryGroupPermissions", item.GroupPermissions);
  434. result.Parameters.AddWithValue("?salePrice", item.SalePrice);
  435. result.Parameters.AddWithValue("?saleType", unchecked((sbyte)item.SaleType));
  436. result.Parameters.AddWithValue("?creationDate", item.CreationDate);
  437. result.Parameters.AddWithValue("?groupID", item.GroupID);
  438. result.Parameters.AddWithValue("?groupOwned", item.GroupOwned);
  439. result.Parameters.AddWithValue("?flags", item.Flags);
  440. lock (m_dbLock)
  441. result.ExecuteNonQuery();
  442. result.Dispose();
  443. }
  444. using (MySqlCommand result = new MySqlCommand("update inventoryfolders set version=version+1 where folderID = ?folderID", dbcon))
  445. {
  446. result.Parameters.AddWithValue("?folderID", item.Folder.ToString());
  447. lock (m_dbLock)
  448. result.ExecuteNonQuery();
  449. }
  450. }
  451. }
  452. catch (MySqlException e)
  453. {
  454. m_log.Error(e.ToString());
  455. }
  456. }
  457. /// <summary>
  458. /// Updates the specified inventory item
  459. /// </summary>
  460. /// <param name="item">Inventory item to update</param>
  461. public void updateInventoryItem(InventoryItemBase item)
  462. {
  463. addInventoryItem(item);
  464. }
  465. /// <summary>
  466. /// Detele the specified inventory item
  467. /// </summary>
  468. /// <param name="item">The inventory item UUID to delete</param>
  469. public void deleteInventoryItem(UUID itemID)
  470. {
  471. try
  472. {
  473. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  474. {
  475. dbcon.Open();
  476. using (MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE inventoryID=?uuid", dbcon))
  477. {
  478. cmd.Parameters.AddWithValue("?uuid", itemID.ToString());
  479. lock (m_dbLock)
  480. cmd.ExecuteNonQuery();
  481. }
  482. }
  483. }
  484. catch (MySqlException e)
  485. {
  486. m_log.Error(e.Message, e);
  487. }
  488. }
  489. public InventoryItemBase queryInventoryItem(UUID itemID)
  490. {
  491. return getInventoryItem(itemID);
  492. }
  493. public InventoryFolderBase queryInventoryFolder(UUID folderID)
  494. {
  495. return getInventoryFolder(folderID);
  496. }
  497. /// <summary>
  498. /// Creates a new inventory folder
  499. /// </summary>
  500. /// <param name="folder">Folder to create</param>
  501. public void addInventoryFolder(InventoryFolderBase folder)
  502. {
  503. string sql =
  504. "REPLACE INTO inventoryfolders (folderID, agentID, parentFolderID, folderName, type, version) VALUES ";
  505. sql += "(?folderID, ?agentID, ?parentFolderID, ?folderName, ?type, ?version)";
  506. string folderName = folder.Name;
  507. if (folderName.Length > 64)
  508. {
  509. folderName = folderName.Substring(0, 64);
  510. m_log.Warn("[INVENTORY DB]: Name field truncated from " + folder.Name.Length + " to " + folderName.Length + " characters on add folder");
  511. }
  512. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  513. {
  514. dbcon.Open();
  515. using (MySqlCommand cmd = new MySqlCommand(sql, dbcon))
  516. {
  517. cmd.Parameters.AddWithValue("?folderID", folder.ID.ToString());
  518. cmd.Parameters.AddWithValue("?agentID", folder.Owner.ToString());
  519. cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString());
  520. cmd.Parameters.AddWithValue("?folderName", folderName);
  521. cmd.Parameters.AddWithValue("?type", folder.Type);
  522. cmd.Parameters.AddWithValue("?version", folder.Version);
  523. try
  524. {
  525. lock (m_dbLock)
  526. {
  527. cmd.ExecuteNonQuery();
  528. }
  529. }
  530. catch (Exception e)
  531. {
  532. m_log.Error(e.ToString());
  533. }
  534. }
  535. }
  536. }
  537. /// <summary>
  538. /// Updates an inventory folder
  539. /// </summary>
  540. /// <param name="folder">Folder to update</param>
  541. public void updateInventoryFolder(InventoryFolderBase folder)
  542. {
  543. addInventoryFolder(folder);
  544. }
  545. /// <summary>
  546. /// Move an inventory folder
  547. /// </summary>
  548. /// <param name="folder">Folder to move</param>
  549. /// <remarks>UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID</remarks>
  550. public void moveInventoryFolder(InventoryFolderBase folder)
  551. {
  552. string sql =
  553. "UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID";
  554. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  555. {
  556. dbcon.Open();
  557. using (MySqlCommand cmd = new MySqlCommand(sql, dbcon))
  558. {
  559. cmd.Parameters.AddWithValue("?folderID", folder.ID.ToString());
  560. cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString());
  561. try
  562. {
  563. lock (m_dbLock)
  564. {
  565. cmd.ExecuteNonQuery();
  566. }
  567. }
  568. catch (Exception e)
  569. {
  570. m_log.Error(e.ToString());
  571. }
  572. }
  573. }
  574. }
  575. /// <summary>
  576. /// Append a list of all the child folders of a parent folder
  577. /// </summary>
  578. /// <param name="folders">list where folders will be appended</param>
  579. /// <param name="parentID">ID of parent</param>
  580. protected void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
  581. {
  582. List<InventoryFolderBase> subfolderList = getInventoryFolders(parentID);
  583. foreach (InventoryFolderBase f in subfolderList)
  584. folders.Add(f);
  585. }
  586. /// <summary>
  587. /// See IInventoryDataPlugin
  588. /// </summary>
  589. /// <param name="parentID"></param>
  590. /// <returns></returns>
  591. public List<InventoryFolderBase> getFolderHierarchy(UUID parentID)
  592. {
  593. /* Note: There are subtle changes between this implementation of getFolderHierarchy and the previous one
  594. * - We will only need to hit the database twice instead of n times.
  595. * - We assume the database is well-formed - no stranded/dangling folders, all folders in heirarchy owned
  596. * by the same person, each user only has 1 inventory heirarchy
  597. * - The returned list is not ordered, instead of breadth-first ordered
  598. There are basically 2 usage cases for getFolderHeirarchy:
  599. 1) Getting the user's entire inventory heirarchy when they log in
  600. 2) Finding a subfolder heirarchy to delete when emptying the trash.
  601. This implementation will pull all inventory folders from the database, and then prune away any folder that
  602. is not part of the requested sub-heirarchy. The theory is that it is cheaper to make 1 request from the
  603. database than to make n requests. This pays off only if requested heirarchy is large.
  604. By making this choice, we are making the worst case better at the cost of making the best case worse.
  605. This way is generally better because we don't have to rebuild the connection/sql query per subfolder,
  606. even if we end up getting more data from the SQL server than we need.
  607. - Francis
  608. */
  609. try
  610. {
  611. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  612. Dictionary<UUID, List<InventoryFolderBase>> hashtable = new Dictionary<UUID, List<InventoryFolderBase>>(); ;
  613. List<InventoryFolderBase> parentFolder = new List<InventoryFolderBase>();
  614. bool buildResultsFromHashTable = false;
  615. lock (m_dbLock)
  616. {
  617. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  618. {
  619. dbcon.Open();
  620. /* Fetch the parent folder from the database to determine the agent ID, and if
  621. * we're querying the root of the inventory folder tree */
  622. using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", dbcon))
  623. {
  624. result.Parameters.AddWithValue("?uuid", parentID.ToString());
  625. using (MySqlDataReader reader = result.ExecuteReader())
  626. {
  627. // Should be at most 1 result
  628. while (reader.Read())
  629. parentFolder.Add(readInventoryFolder(reader));
  630. }
  631. }
  632. if (parentFolder.Count >= 1) // No result means parent folder does not exist
  633. {
  634. if (parentFolder[0].ParentID == UUID.Zero) // We are querying the root folder
  635. {
  636. /* Get all of the agent's folders from the database, put them in a list and return it */
  637. using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE agentID = ?uuid", dbcon))
  638. {
  639. result.Parameters.AddWithValue("?uuid", parentFolder[0].Owner.ToString());
  640. using (MySqlDataReader reader = result.ExecuteReader())
  641. {
  642. while (reader.Read())
  643. {
  644. InventoryFolderBase curFolder = readInventoryFolder(reader);
  645. if (curFolder.ID != parentID) // Do not need to add the root node of the tree to the list
  646. folders.Add(curFolder);
  647. }
  648. }
  649. }
  650. } // if we are querying the root folder
  651. else // else we are querying a subtree of the inventory folder tree
  652. {
  653. /* Get all of the agent's folders from the database, put them all in a hash table
  654. * indexed by their parent ID */
  655. using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE agentID = ?uuid", dbcon))
  656. {
  657. result.Parameters.AddWithValue("?uuid", parentFolder[0].Owner.ToString());
  658. using (MySqlDataReader reader = result.ExecuteReader())
  659. {
  660. while (reader.Read())
  661. {
  662. InventoryFolderBase curFolder = readInventoryFolder(reader);
  663. if (hashtable.ContainsKey(curFolder.ParentID)) // Current folder already has a sibling
  664. hashtable[curFolder.ParentID].Add(curFolder); // append to sibling list
  665. else // else current folder has no known (yet) siblings
  666. {
  667. List<InventoryFolderBase> siblingList = new List<InventoryFolderBase>();
  668. siblingList.Add(curFolder);
  669. // Current folder has no known (yet) siblings
  670. hashtable.Add(curFolder.ParentID, siblingList);
  671. }
  672. } // while more items to read from the database
  673. }
  674. }
  675. // Set flag so we know we need to build the results from the hash table after
  676. // we unlock the database
  677. buildResultsFromHashTable = true;
  678. } // else we are querying a subtree of the inventory folder tree
  679. } // if folder parentID exists
  680. if (buildResultsFromHashTable)
  681. {
  682. /* We have all of the user's folders stored in a hash table indexed by their parent ID
  683. * and we need to return the requested subtree. We will build the requested subtree
  684. * by performing a breadth-first-search on the hash table */
  685. if (hashtable.ContainsKey(parentID))
  686. folders.AddRange(hashtable[parentID]);
  687. for (int i = 0; i < folders.Count; i++) // **Note: folders.Count is *not* static
  688. if (hashtable.ContainsKey(folders[i].ID))
  689. folders.AddRange(hashtable[folders[i].ID]);
  690. }
  691. }
  692. } // lock (database)
  693. return folders;
  694. }
  695. catch (Exception e)
  696. {
  697. m_log.Error(e.Message, e);
  698. return null;
  699. }
  700. }
  701. /// <summary>
  702. /// Delete a folder from database
  703. /// </summary>
  704. /// <param name="folderID">the folder UUID</param>
  705. protected void deleteOneFolder(UUID folderID)
  706. {
  707. try
  708. {
  709. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  710. {
  711. dbcon.Open();
  712. // System folders can never be deleted. Period.
  713. using (MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryfolders WHERE folderID=?uuid and type=-1", dbcon))
  714. {
  715. cmd.Parameters.AddWithValue("?uuid", folderID.ToString());
  716. lock (m_dbLock)
  717. cmd.ExecuteNonQuery();
  718. }
  719. }
  720. }
  721. catch (MySqlException e)
  722. {
  723. m_log.Error(e.Message, e);
  724. }
  725. }
  726. /// <summary>
  727. /// Delete all item in a folder
  728. /// </summary>
  729. /// <param name="folderID">the folder UUID</param>
  730. protected void deleteItemsInFolder(UUID folderID)
  731. {
  732. try
  733. {
  734. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  735. {
  736. dbcon.Open();
  737. using (MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE parentFolderID=?uuid", dbcon))
  738. {
  739. cmd.Parameters.AddWithValue("?uuid", folderID.ToString());
  740. lock (m_dbLock)
  741. cmd.ExecuteNonQuery();
  742. }
  743. }
  744. }
  745. catch (MySqlException e)
  746. {
  747. m_log.Error(e.ToString());
  748. }
  749. }
  750. /// <summary>
  751. /// Deletes an inventory folder
  752. /// </summary>
  753. /// <param name="folderId">Id of folder to delete</param>
  754. public void deleteInventoryFolder(UUID folderID)
  755. {
  756. List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID);
  757. //Delete all sub-folders
  758. foreach (InventoryFolderBase f in subFolders)
  759. {
  760. deleteOneFolder(f.ID);
  761. deleteItemsInFolder(f.ID);
  762. }
  763. //Delete the actual row
  764. deleteOneFolder(folderID);
  765. deleteItemsInFolder(folderID);
  766. }
  767. public List<InventoryItemBase> fetchActiveGestures(UUID avatarID)
  768. {
  769. lock (m_dbLock)
  770. {
  771. try
  772. {
  773. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  774. {
  775. dbcon.Open();
  776. using (MySqlCommand sqlCmd = new MySqlCommand(
  777. "SELECT * FROM inventoryitems WHERE avatarId = ?uuid AND assetType = ?type and flags & 1", dbcon))
  778. {
  779. sqlCmd.Parameters.AddWithValue("?uuid", avatarID.ToString());
  780. sqlCmd.Parameters.AddWithValue("?type", (int)AssetType.Gesture);
  781. using (MySqlDataReader result = sqlCmd.ExecuteReader())
  782. {
  783. List<InventoryItemBase> list = new List<InventoryItemBase>();
  784. while (result.Read())
  785. {
  786. InventoryItemBase item = readInventoryItem(result);
  787. if (item != null)
  788. list.Add(item);
  789. }
  790. return list;
  791. }
  792. }
  793. }
  794. }
  795. catch (Exception e)
  796. {
  797. m_log.Error(e.Message, e);
  798. return null;
  799. }
  800. }
  801. }
  802. }
  803. }