MySQLInventoryData.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. 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. // TODO: this is to handle a case where NULLs creep in there, which we are not sure is indemic to the system, or legacy. It would be nice to live fix these.
  272. if (reader["creatorID"] == null)
  273. {
  274. item.CreatorId = UUID.Zero.ToString();
  275. }
  276. else
  277. {
  278. item.CreatorId = (string)reader["creatorID"];
  279. }
  280. // Be a bit safer in parsing these because the
  281. // database doesn't enforce them to be not null, and
  282. // the inventory still works if these are weird in the
  283. // db
  284. UUID Owner = UUID.Zero;
  285. UUID GroupID = UUID.Zero;
  286. UUID.TryParse((string)reader["avatarID"], out Owner);
  287. UUID.TryParse((string)reader["groupID"], out GroupID);
  288. item.Owner = Owner;
  289. item.GroupID = GroupID;
  290. // Rest of the parsing. If these UUID's fail, we're dead anyway
  291. item.ID = new UUID((string) reader["inventoryID"]);
  292. item.AssetID = new UUID((string) reader["assetID"]);
  293. item.AssetType = (int) reader["assetType"];
  294. item.Folder = new UUID((string) reader["parentFolderID"]);
  295. item.Name = (string) reader["inventoryName"];
  296. item.Description = (string) reader["inventoryDescription"];
  297. item.NextPermissions = (uint) reader["inventoryNextPermissions"];
  298. item.CurrentPermissions = (uint) reader["inventoryCurrentPermissions"];
  299. item.InvType = (int) reader["invType"];
  300. item.BasePermissions = (uint) reader["inventoryBasePermissions"];
  301. item.EveryOnePermissions = (uint) reader["inventoryEveryOnePermissions"];
  302. item.GroupPermissions = (uint) reader["inventoryGroupPermissions"];
  303. item.SalePrice = (int) reader["salePrice"];
  304. item.SaleType = Convert.ToByte(reader["saleType"]);
  305. item.CreationDate = (int) reader["creationDate"];
  306. item.GroupOwned = Convert.ToBoolean(reader["groupOwned"]);
  307. item.Flags = (uint) reader["flags"];
  308. return item;
  309. }
  310. catch (MySqlException e)
  311. {
  312. m_log.Error(e.ToString());
  313. }
  314. return null;
  315. }
  316. /// <summary>
  317. /// Returns a specified inventory item
  318. /// </summary>
  319. /// <param name="item">The item to return</param>
  320. /// <returns>An inventory item</returns>
  321. public InventoryItemBase getInventoryItem(UUID itemID)
  322. {
  323. try
  324. {
  325. lock (database)
  326. {
  327. database.CheckConnection();
  328. MySqlCommand result =
  329. new MySqlCommand("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", database.Connection);
  330. result.Parameters.AddWithValue("?uuid", itemID.ToString());
  331. MySqlDataReader reader = result.ExecuteReader();
  332. InventoryItemBase item = null;
  333. if (reader.Read())
  334. item = readInventoryItem(reader);
  335. reader.Close();
  336. result.Dispose();
  337. return item;
  338. }
  339. }
  340. catch (Exception e)
  341. {
  342. database.Reconnect();
  343. m_log.Error(e.ToString());
  344. }
  345. return null;
  346. }
  347. /// <summary>
  348. /// Reads a list of inventory folders returned by a query.
  349. /// </summary>
  350. /// <param name="reader">A MySQL Data Reader</param>
  351. /// <returns>A List containing inventory folders</returns>
  352. protected static InventoryFolderBase readInventoryFolder(MySqlDataReader reader)
  353. {
  354. try
  355. {
  356. InventoryFolderBase folder = new InventoryFolderBase();
  357. folder.Owner = new UUID((string) reader["agentID"]);
  358. folder.ParentID = new UUID((string) reader["parentFolderID"]);
  359. folder.ID = new UUID((string) reader["folderID"]);
  360. folder.Name = (string) reader["folderName"];
  361. folder.Type = (short) reader["type"];
  362. folder.Version = (ushort) ((int) reader["version"]);
  363. return folder;
  364. }
  365. catch (Exception e)
  366. {
  367. m_log.Error(e.ToString());
  368. }
  369. return null;
  370. }
  371. /// <summary>
  372. /// Returns a specified inventory folder
  373. /// </summary>
  374. /// <param name="folder">The folder to return</param>
  375. /// <returns>A folder class</returns>
  376. public InventoryFolderBase getInventoryFolder(UUID folderID)
  377. {
  378. try
  379. {
  380. lock (database)
  381. {
  382. database.CheckConnection();
  383. MySqlCommand result =
  384. new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", database.Connection);
  385. result.Parameters.AddWithValue("?uuid", folderID.ToString());
  386. MySqlDataReader reader = result.ExecuteReader();
  387. reader.Read();
  388. InventoryFolderBase folder = readInventoryFolder(reader);
  389. reader.Close();
  390. result.Dispose();
  391. return folder;
  392. }
  393. }
  394. catch (Exception e)
  395. {
  396. database.Reconnect();
  397. m_log.Error(e.ToString());
  398. return null;
  399. }
  400. }
  401. /// <summary>
  402. /// Adds a specified item to the database
  403. /// </summary>
  404. /// <param name="item">The inventory item</param>
  405. public void addInventoryItem(InventoryItemBase item)
  406. {
  407. string sql =
  408. "REPLACE INTO inventoryitems (inventoryID, assetID, assetType, parentFolderID, avatarID, inventoryName"
  409. + ", inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions, invType"
  410. + ", creatorID, inventoryBasePermissions, inventoryEveryOnePermissions, inventoryGroupPermissions, salePrice, saleType"
  411. + ", creationDate, groupID, groupOwned, flags) VALUES ";
  412. sql +=
  413. "(?inventoryID, ?assetID, ?assetType, ?parentFolderID, ?avatarID, ?inventoryName, ?inventoryDescription"
  414. + ", ?inventoryNextPermissions, ?inventoryCurrentPermissions, ?invType, ?creatorID"
  415. + ", ?inventoryBasePermissions, ?inventoryEveryOnePermissions, ?inventoryGroupPermissions, ?salePrice, ?saleType, ?creationDate"
  416. + ", ?groupID, ?groupOwned, ?flags)";
  417. string itemName = item.Name;
  418. if (item.Name.Length > 64)
  419. {
  420. itemName = item.Name.Substring(0, 64);
  421. m_log.Warn("[INVENTORY DB]: Name field truncated from " + item.Name.Length + " to " + itemName.Length + " characters on add item");
  422. }
  423. string itemDesc = item.Description;
  424. if (item.Description.Length > 128)
  425. {
  426. itemDesc = item.Description.Substring(0, 128);
  427. m_log.Warn("[INVENTORY DB]: Description field truncated from " + item.Description.Length + " to " + itemDesc.Length + " characters on add item");
  428. }
  429. try
  430. {
  431. database.CheckConnection();
  432. MySqlCommand result = new MySqlCommand(sql, database.Connection);
  433. result.Parameters.AddWithValue("?inventoryID", item.ID.ToString());
  434. result.Parameters.AddWithValue("?assetID", item.AssetID.ToString());
  435. result.Parameters.AddWithValue("?assetType", item.AssetType.ToString());
  436. result.Parameters.AddWithValue("?parentFolderID", item.Folder.ToString());
  437. result.Parameters.AddWithValue("?avatarID", item.Owner.ToString());
  438. result.Parameters.AddWithValue("?inventoryName", itemName);
  439. result.Parameters.AddWithValue("?inventoryDescription", itemDesc);
  440. result.Parameters.AddWithValue("?inventoryNextPermissions", item.NextPermissions.ToString());
  441. result.Parameters.AddWithValue("?inventoryCurrentPermissions",
  442. item.CurrentPermissions.ToString());
  443. result.Parameters.AddWithValue("?invType", item.InvType);
  444. result.Parameters.AddWithValue("?creatorID", item.CreatorId);
  445. result.Parameters.AddWithValue("?inventoryBasePermissions", item.BasePermissions);
  446. result.Parameters.AddWithValue("?inventoryEveryOnePermissions", item.EveryOnePermissions);
  447. result.Parameters.AddWithValue("?inventoryGroupPermissions", item.GroupPermissions);
  448. result.Parameters.AddWithValue("?salePrice", item.SalePrice);
  449. result.Parameters.AddWithValue("?saleType", item.SaleType);
  450. result.Parameters.AddWithValue("?creationDate", item.CreationDate);
  451. result.Parameters.AddWithValue("?groupID", item.GroupID);
  452. result.Parameters.AddWithValue("?groupOwned", item.GroupOwned);
  453. result.Parameters.AddWithValue("?flags", item.Flags);
  454. lock (database)
  455. {
  456. result.ExecuteNonQuery();
  457. }
  458. result.Dispose();
  459. result = new MySqlCommand("update inventoryfolders set version=version+1 where folderID = ?folderID", database.Connection);
  460. result.Parameters.AddWithValue("?folderID", item.Folder.ToString
  461. ());
  462. lock (database)
  463. {
  464. result.ExecuteNonQuery();
  465. }
  466. result.Dispose();
  467. }
  468. catch (MySqlException e)
  469. {
  470. m_log.Error(e.ToString());
  471. }
  472. }
  473. /// <summary>
  474. /// Updates the specified inventory item
  475. /// </summary>
  476. /// <param name="item">Inventory item to update</param>
  477. public void updateInventoryItem(InventoryItemBase item)
  478. {
  479. addInventoryItem(item);
  480. }
  481. /// <summary>
  482. /// Detele the specified inventory item
  483. /// </summary>
  484. /// <param name="item">The inventory item UUID to delete</param>
  485. public void deleteInventoryItem(UUID itemID)
  486. {
  487. try
  488. {
  489. database.CheckConnection();
  490. MySqlCommand cmd =
  491. new MySqlCommand("DELETE FROM inventoryitems WHERE inventoryID=?uuid", database.Connection);
  492. cmd.Parameters.AddWithValue("?uuid", itemID.ToString());
  493. lock (database)
  494. {
  495. cmd.ExecuteNonQuery();
  496. }
  497. }
  498. catch (MySqlException e)
  499. {
  500. database.Reconnect();
  501. m_log.Error(e.ToString());
  502. }
  503. }
  504. public InventoryItemBase queryInventoryItem(UUID itemID)
  505. {
  506. return getInventoryItem(itemID);
  507. }
  508. public InventoryFolderBase queryInventoryFolder(UUID folderID)
  509. {
  510. return getInventoryFolder(folderID);
  511. }
  512. /// <summary>
  513. /// Creates a new inventory folder
  514. /// </summary>
  515. /// <param name="folder">Folder to create</param>
  516. public void addInventoryFolder(InventoryFolderBase folder)
  517. {
  518. string sql =
  519. "REPLACE INTO inventoryfolders (folderID, agentID, parentFolderID, folderName, type, version) VALUES ";
  520. sql += "(?folderID, ?agentID, ?parentFolderID, ?folderName, ?type, ?version)";
  521. string folderName = folder.Name;
  522. if (folderName.Length > 64)
  523. {
  524. folderName = folderName.Substring(0, 64);
  525. m_log.Warn("[INVENTORY DB]: Name field truncated from " + folder.Name.Length + " to " + folderName.Length + " characters on add folder");
  526. }
  527. database.CheckConnection();
  528. MySqlCommand cmd = new MySqlCommand(sql, database.Connection);
  529. cmd.Parameters.AddWithValue("?folderID", folder.ID.ToString());
  530. cmd.Parameters.AddWithValue("?agentID", folder.Owner.ToString());
  531. cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString());
  532. cmd.Parameters.AddWithValue("?folderName", folderName);
  533. cmd.Parameters.AddWithValue("?type", (short) folder.Type);
  534. cmd.Parameters.AddWithValue("?version", folder.Version);
  535. try
  536. {
  537. lock (database)
  538. {
  539. cmd.ExecuteNonQuery();
  540. }
  541. }
  542. catch (Exception e)
  543. {
  544. m_log.Error(e.ToString());
  545. }
  546. }
  547. /// <summary>
  548. /// Updates an inventory folder
  549. /// </summary>
  550. /// <param name="folder">Folder to update</param>
  551. public void updateInventoryFolder(InventoryFolderBase folder)
  552. {
  553. addInventoryFolder(folder);
  554. }
  555. /// <summary>
  556. /// Move an inventory folder
  557. /// </summary>
  558. /// <param name="folder">Folder to move</param>
  559. /// <remarks>UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID</remarks>
  560. public void moveInventoryFolder(InventoryFolderBase folder)
  561. {
  562. string sql =
  563. "UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID";
  564. database.CheckConnection();
  565. MySqlCommand cmd = new MySqlCommand(sql, database.Connection);
  566. cmd.Parameters.AddWithValue("?folderID", folder.ID.ToString());
  567. cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString());
  568. try
  569. {
  570. lock (database)
  571. {
  572. cmd.ExecuteNonQuery();
  573. }
  574. }
  575. catch (Exception e)
  576. {
  577. m_log.Error(e.ToString());
  578. }
  579. }
  580. /// <summary>
  581. /// Append a list of all the child folders of a parent folder
  582. /// </summary>
  583. /// <param name="folders">list where folders will be appended</param>
  584. /// <param name="parentID">ID of parent</param>
  585. protected void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
  586. {
  587. List<InventoryFolderBase> subfolderList = getInventoryFolders(parentID);
  588. foreach (InventoryFolderBase f in subfolderList)
  589. folders.Add(f);
  590. }
  591. /// <summary>
  592. /// See IInventoryDataPlugin
  593. /// </summary>
  594. /// <param name="parentID"></param>
  595. /// <returns></returns>
  596. public List<InventoryFolderBase> getFolderHierarchy(UUID parentID)
  597. {
  598. /* Note: There are subtle changes between this implementation of getFolderHierarchy and the previous one
  599. * - We will only need to hit the database twice instead of n times.
  600. * - We assume the database is well-formed - no stranded/dangling folders, all folders in heirarchy owned
  601. * by the same person, each user only has 1 inventory heirarchy
  602. * - The returned list is not ordered, instead of breadth-first ordered
  603. There are basically 2 usage cases for getFolderHeirarchy:
  604. 1) Getting the user's entire inventory heirarchy when they log in
  605. 2) Finding a subfolder heirarchy to delete when emptying the trash.
  606. This implementation will pull all inventory folders from the database, and then prune away any folder that
  607. is not part of the requested sub-heirarchy. The theory is that it is cheaper to make 1 request from the
  608. database than to make n requests. This pays off only if requested heirarchy is large.
  609. By making this choice, we are making the worst case better at the cost of making the best case worse.
  610. This way is generally better because we don't have to rebuild the connection/sql query per subfolder,
  611. even if we end up getting more data from the SQL server than we need.
  612. - Francis
  613. */
  614. try
  615. {
  616. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  617. Dictionary<UUID, List<InventoryFolderBase>> hashtable
  618. = new Dictionary<UUID, List<InventoryFolderBase>>(); ;
  619. List<InventoryFolderBase> parentFolder = new List<InventoryFolderBase>();
  620. lock (database)
  621. {
  622. MySqlCommand result;
  623. MySqlDataReader reader;
  624. bool buildResultsFromHashTable = false;
  625. database.CheckConnection();
  626. /* Fetch the parent folder from the database to determine the agent ID, and if
  627. * we're querying the root of the inventory folder tree */
  628. result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid",
  629. database.Connection);
  630. result.Parameters.AddWithValue("?uuid", parentID.ToString());
  631. reader = result.ExecuteReader();
  632. while (reader.Read()) // Should be at most 1 result
  633. parentFolder.Add(readInventoryFolder(reader));
  634. reader.Close();
  635. result.Dispose();
  636. if (parentFolder.Count >= 1) // No result means parent folder does not exist
  637. {
  638. if (parentFolder[0].ParentID == UUID.Zero) // We are querying the root folder
  639. {
  640. /* Get all of the agent's folders from the database, put them in a list and return it */
  641. result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE agentID = ?uuid",
  642. database.Connection);
  643. result.Parameters.AddWithValue("?uuid", parentFolder[0].Owner.ToString());
  644. reader = result.ExecuteReader();
  645. while (reader.Read())
  646. {
  647. InventoryFolderBase curFolder = readInventoryFolder(reader);
  648. if (curFolder.ID != parentID) // Do not need to add the root node of the tree to the list
  649. folders.Add(curFolder);
  650. }
  651. reader.Close();
  652. result.Dispose();
  653. } // if we are querying the root folder
  654. else // else we are querying a subtree of the inventory folder tree
  655. {
  656. /* Get all of the agent's folders from the database, put them all in a hash table
  657. * indexed by their parent ID */
  658. result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE agentID = ?uuid",
  659. database.Connection);
  660. result.Parameters.AddWithValue("?uuid", parentFolder[0].Owner.ToString());
  661. reader = result.ExecuteReader();
  662. while (reader.Read())
  663. {
  664. InventoryFolderBase curFolder = readInventoryFolder(reader);
  665. if (hashtable.ContainsKey(curFolder.ParentID)) // Current folder already has a sibling
  666. hashtable[curFolder.ParentID].Add(curFolder); // append to sibling list
  667. else // else current folder has no known (yet) siblings
  668. {
  669. List<InventoryFolderBase> siblingList = new List<InventoryFolderBase>();
  670. siblingList.Add(curFolder);
  671. // Current folder has no known (yet) siblings
  672. hashtable.Add(curFolder.ParentID, siblingList);
  673. }
  674. } // while more items to read from the database
  675. reader.Close();
  676. result.Dispose();
  677. // Set flag so we know we need to build the results from the hash table after
  678. // we unlock the database
  679. buildResultsFromHashTable = true;
  680. } // else we are querying a subtree of the inventory folder tree
  681. } // if folder parentID exists
  682. if (buildResultsFromHashTable)
  683. {
  684. /* We have all of the user's folders stored in a hash table indexed by their parent ID
  685. * and we need to return the requested subtree. We will build the requested subtree
  686. * by performing a breadth-first-search on the hash table */
  687. if (hashtable.ContainsKey(parentID))
  688. folders.AddRange(hashtable[parentID]);
  689. for (int i = 0; i < folders.Count; i++) // **Note: folders.Count is *not* static
  690. if (hashtable.ContainsKey(folders[i].ID))
  691. folders.AddRange(hashtable[folders[i].ID]);
  692. }
  693. } // lock (database)
  694. return folders;
  695. }
  696. catch (Exception e)
  697. {
  698. database.Reconnect();
  699. m_log.Error(e.ToString());
  700. return null;
  701. }
  702. }
  703. /// <summary>
  704. /// Delete a folder from database
  705. /// </summary>
  706. /// <param name="folderID">the folder UUID</param>
  707. protected void deleteOneFolder(UUID folderID)
  708. {
  709. try
  710. {
  711. database.CheckConnection();
  712. MySqlCommand cmd =
  713. new MySqlCommand("DELETE FROM inventoryfolders WHERE folderID=?uuid", database.Connection);
  714. cmd.Parameters.AddWithValue("?uuid", folderID.ToString());
  715. lock (database)
  716. {
  717. cmd.ExecuteNonQuery();
  718. }
  719. }
  720. catch (MySqlException e)
  721. {
  722. database.Reconnect();
  723. m_log.Error(e.ToString());
  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. database.CheckConnection();
  735. MySqlCommand cmd =
  736. new MySqlCommand("DELETE FROM inventoryitems WHERE parentFolderID=?uuid", database.Connection);
  737. cmd.Parameters.AddWithValue("?uuid", folderID.ToString());
  738. lock (database)
  739. {
  740. cmd.ExecuteNonQuery();
  741. }
  742. }
  743. catch (MySqlException e)
  744. {
  745. database.Reconnect();
  746. m_log.Error(e.ToString());
  747. }
  748. }
  749. /// <summary>
  750. /// Deletes an inventory folder
  751. /// </summary>
  752. /// <param name="folderId">Id of folder to delete</param>
  753. public void deleteInventoryFolder(UUID folderID)
  754. {
  755. List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID);
  756. //Delete all sub-folders
  757. foreach (InventoryFolderBase f in subFolders)
  758. {
  759. deleteOneFolder(f.ID);
  760. deleteItemsInFolder(f.ID);
  761. }
  762. //Delete the actual row
  763. deleteOneFolder(folderID);
  764. deleteItemsInFolder(folderID);
  765. }
  766. public List<InventoryItemBase> fetchActiveGestures(UUID avatarID)
  767. {
  768. MySqlDataReader result = null;
  769. MySqlCommand sqlCmd = null;
  770. lock (database)
  771. {
  772. try
  773. {
  774. database.CheckConnection();
  775. sqlCmd = new MySqlCommand(
  776. "SELECT * FROM inventoryitems WHERE avatarId = ?uuid AND assetType = ?type and flags = 1",
  777. database.Connection);
  778. sqlCmd.Parameters.AddWithValue("?uuid", avatarID.ToString());
  779. sqlCmd.Parameters.AddWithValue("?type", (int)AssetType.Gesture);
  780. result = sqlCmd.ExecuteReader();
  781. List<InventoryItemBase> list = new List<InventoryItemBase>();
  782. while (result.Read())
  783. {
  784. InventoryItemBase item = readInventoryItem(result);
  785. if (item != null)
  786. list.Add(item);
  787. }
  788. return list;
  789. }
  790. catch (Exception e)
  791. {
  792. database.Reconnect();
  793. m_log.Error(e.ToString());
  794. return null;
  795. }
  796. finally
  797. {
  798. if (result != null) result.Close();
  799. if (sqlCmd != null) sqlCmd.Dispose();
  800. }
  801. }
  802. }
  803. }
  804. }