MySQLInventoryData.cs 35 KB

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