1
0

MySQLInventoryData.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 = unchecked((byte)(Convert.ToSByte(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="folderID">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. InventoryFolderBase folder = null;
  388. if (reader.Read())
  389. folder = readInventoryFolder(reader);
  390. reader.Close();
  391. result.Dispose();
  392. return folder;
  393. }
  394. }
  395. catch (Exception e)
  396. {
  397. database.Reconnect();
  398. m_log.Error(e.ToString());
  399. return null;
  400. }
  401. }
  402. /// <summary>
  403. /// Adds a specified item to the database
  404. /// </summary>
  405. /// <param name="item">The inventory item</param>
  406. public void addInventoryItem(InventoryItemBase item)
  407. {
  408. string sql =
  409. "REPLACE INTO inventoryitems (inventoryID, assetID, assetType, parentFolderID, avatarID, inventoryName"
  410. + ", inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions, invType"
  411. + ", creatorID, inventoryBasePermissions, inventoryEveryOnePermissions, inventoryGroupPermissions, salePrice, saleType"
  412. + ", creationDate, groupID, groupOwned, flags) VALUES ";
  413. sql +=
  414. "(?inventoryID, ?assetID, ?assetType, ?parentFolderID, ?avatarID, ?inventoryName, ?inventoryDescription"
  415. + ", ?inventoryNextPermissions, ?inventoryCurrentPermissions, ?invType, ?creatorID"
  416. + ", ?inventoryBasePermissions, ?inventoryEveryOnePermissions, ?inventoryGroupPermissions, ?salePrice, ?saleType, ?creationDate"
  417. + ", ?groupID, ?groupOwned, ?flags)";
  418. string itemName = item.Name;
  419. if (item.Name.Length > 64)
  420. {
  421. itemName = item.Name.Substring(0, 64);
  422. m_log.Warn("[INVENTORY DB]: Name field truncated from " + item.Name.Length + " to " + itemName.Length + " characters on add item");
  423. }
  424. string itemDesc = item.Description;
  425. if (item.Description.Length > 128)
  426. {
  427. itemDesc = item.Description.Substring(0, 128);
  428. m_log.Warn("[INVENTORY DB]: Description field truncated from " + item.Description.Length + " to " + itemDesc.Length + " characters on add item");
  429. }
  430. try
  431. {
  432. database.CheckConnection();
  433. MySqlCommand result = new MySqlCommand(sql, database.Connection);
  434. result.Parameters.AddWithValue("?inventoryID", item.ID.ToString());
  435. result.Parameters.AddWithValue("?assetID", item.AssetID.ToString());
  436. result.Parameters.AddWithValue("?assetType", item.AssetType.ToString());
  437. result.Parameters.AddWithValue("?parentFolderID", item.Folder.ToString());
  438. result.Parameters.AddWithValue("?avatarID", item.Owner.ToString());
  439. result.Parameters.AddWithValue("?inventoryName", itemName);
  440. result.Parameters.AddWithValue("?inventoryDescription", itemDesc);
  441. result.Parameters.AddWithValue("?inventoryNextPermissions", item.NextPermissions.ToString());
  442. result.Parameters.AddWithValue("?inventoryCurrentPermissions",
  443. item.CurrentPermissions.ToString());
  444. result.Parameters.AddWithValue("?invType", item.InvType);
  445. result.Parameters.AddWithValue("?creatorID", item.CreatorId);
  446. result.Parameters.AddWithValue("?inventoryBasePermissions", item.BasePermissions);
  447. result.Parameters.AddWithValue("?inventoryEveryOnePermissions", item.EveryOnePermissions);
  448. result.Parameters.AddWithValue("?inventoryGroupPermissions", item.GroupPermissions);
  449. result.Parameters.AddWithValue("?salePrice", item.SalePrice);
  450. result.Parameters.AddWithValue("?saleType", unchecked((sbyte)item.SaleType));
  451. result.Parameters.AddWithValue("?creationDate", item.CreationDate);
  452. result.Parameters.AddWithValue("?groupID", item.GroupID);
  453. result.Parameters.AddWithValue("?groupOwned", item.GroupOwned);
  454. result.Parameters.AddWithValue("?flags", item.Flags);
  455. lock (database)
  456. {
  457. result.ExecuteNonQuery();
  458. }
  459. result.Dispose();
  460. result = new MySqlCommand("update inventoryfolders set version=version+1 where folderID = ?folderID", database.Connection);
  461. result.Parameters.AddWithValue("?folderID", item.Folder.ToString
  462. ());
  463. lock (database)
  464. {
  465. result.ExecuteNonQuery();
  466. }
  467. result.Dispose();
  468. }
  469. catch (MySqlException e)
  470. {
  471. m_log.Error(e.ToString());
  472. }
  473. }
  474. /// <summary>
  475. /// Updates the specified inventory item
  476. /// </summary>
  477. /// <param name="item">Inventory item to update</param>
  478. public void updateInventoryItem(InventoryItemBase item)
  479. {
  480. addInventoryItem(item);
  481. }
  482. /// <summary>
  483. /// Detele the specified inventory item
  484. /// </summary>
  485. /// <param name="item">The inventory item UUID to delete</param>
  486. public void deleteInventoryItem(UUID itemID)
  487. {
  488. try
  489. {
  490. database.CheckConnection();
  491. MySqlCommand cmd =
  492. new MySqlCommand("DELETE FROM inventoryitems WHERE inventoryID=?uuid", database.Connection);
  493. cmd.Parameters.AddWithValue("?uuid", itemID.ToString());
  494. lock (database)
  495. {
  496. cmd.ExecuteNonQuery();
  497. }
  498. }
  499. catch (MySqlException e)
  500. {
  501. database.Reconnect();
  502. m_log.Error(e.ToString());
  503. }
  504. }
  505. public InventoryItemBase queryInventoryItem(UUID itemID)
  506. {
  507. return getInventoryItem(itemID);
  508. }
  509. public InventoryFolderBase queryInventoryFolder(UUID folderID)
  510. {
  511. return getInventoryFolder(folderID);
  512. }
  513. /// <summary>
  514. /// Creates a new inventory folder
  515. /// </summary>
  516. /// <param name="folder">Folder to create</param>
  517. public void addInventoryFolder(InventoryFolderBase folder)
  518. {
  519. string sql =
  520. "REPLACE INTO inventoryfolders (folderID, agentID, parentFolderID, folderName, type, version) VALUES ";
  521. sql += "(?folderID, ?agentID, ?parentFolderID, ?folderName, ?type, ?version)";
  522. string folderName = folder.Name;
  523. if (folderName.Length > 64)
  524. {
  525. folderName = folderName.Substring(0, 64);
  526. m_log.Warn("[INVENTORY DB]: Name field truncated from " + folder.Name.Length + " to " + folderName.Length + " characters on add folder");
  527. }
  528. database.CheckConnection();
  529. MySqlCommand cmd = new MySqlCommand(sql, database.Connection);
  530. cmd.Parameters.AddWithValue("?folderID", folder.ID.ToString());
  531. cmd.Parameters.AddWithValue("?agentID", folder.Owner.ToString());
  532. cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString());
  533. cmd.Parameters.AddWithValue("?folderName", folderName);
  534. cmd.Parameters.AddWithValue("?type", folder.Type);
  535. cmd.Parameters.AddWithValue("?version", folder.Version);
  536. try
  537. {
  538. lock (database)
  539. {
  540. cmd.ExecuteNonQuery();
  541. }
  542. }
  543. catch (Exception e)
  544. {
  545. m_log.Error(e.ToString());
  546. }
  547. }
  548. /// <summary>
  549. /// Updates an inventory folder
  550. /// </summary>
  551. /// <param name="folder">Folder to update</param>
  552. public void updateInventoryFolder(InventoryFolderBase folder)
  553. {
  554. addInventoryFolder(folder);
  555. }
  556. /// <summary>
  557. /// Move an inventory folder
  558. /// </summary>
  559. /// <param name="folder">Folder to move</param>
  560. /// <remarks>UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID</remarks>
  561. public void moveInventoryFolder(InventoryFolderBase folder)
  562. {
  563. string sql =
  564. "UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID";
  565. database.CheckConnection();
  566. MySqlCommand cmd = new MySqlCommand(sql, database.Connection);
  567. cmd.Parameters.AddWithValue("?folderID", folder.ID.ToString());
  568. cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString());
  569. try
  570. {
  571. lock (database)
  572. {
  573. cmd.ExecuteNonQuery();
  574. }
  575. }
  576. catch (Exception e)
  577. {
  578. m_log.Error(e.ToString());
  579. }
  580. }
  581. /// <summary>
  582. /// Append a list of all the child folders of a parent folder
  583. /// </summary>
  584. /// <param name="folders">list where folders will be appended</param>
  585. /// <param name="parentID">ID of parent</param>
  586. protected void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
  587. {
  588. List<InventoryFolderBase> subfolderList = getInventoryFolders(parentID);
  589. foreach (InventoryFolderBase f in subfolderList)
  590. folders.Add(f);
  591. }
  592. /// <summary>
  593. /// See IInventoryDataPlugin
  594. /// </summary>
  595. /// <param name="parentID"></param>
  596. /// <returns></returns>
  597. public List<InventoryFolderBase> getFolderHierarchy(UUID parentID)
  598. {
  599. /* Note: There are subtle changes between this implementation of getFolderHierarchy and the previous one
  600. * - We will only need to hit the database twice instead of n times.
  601. * - We assume the database is well-formed - no stranded/dangling folders, all folders in heirarchy owned
  602. * by the same person, each user only has 1 inventory heirarchy
  603. * - The returned list is not ordered, instead of breadth-first ordered
  604. There are basically 2 usage cases for getFolderHeirarchy:
  605. 1) Getting the user's entire inventory heirarchy when they log in
  606. 2) Finding a subfolder heirarchy to delete when emptying the trash.
  607. This implementation will pull all inventory folders from the database, and then prune away any folder that
  608. is not part of the requested sub-heirarchy. The theory is that it is cheaper to make 1 request from the
  609. database than to make n requests. This pays off only if requested heirarchy is large.
  610. By making this choice, we are making the worst case better at the cost of making the best case worse.
  611. This way is generally better because we don't have to rebuild the connection/sql query per subfolder,
  612. even if we end up getting more data from the SQL server than we need.
  613. - Francis
  614. */
  615. try
  616. {
  617. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  618. Dictionary<UUID, List<InventoryFolderBase>> hashtable
  619. = new Dictionary<UUID, List<InventoryFolderBase>>(); ;
  620. List<InventoryFolderBase> parentFolder = new List<InventoryFolderBase>();
  621. lock (database)
  622. {
  623. MySqlCommand result;
  624. MySqlDataReader reader;
  625. bool buildResultsFromHashTable = false;
  626. database.CheckConnection();
  627. /* Fetch the parent folder from the database to determine the agent ID, and if
  628. * we're querying the root of the inventory folder tree */
  629. result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid",
  630. database.Connection);
  631. result.Parameters.AddWithValue("?uuid", parentID.ToString());
  632. reader = result.ExecuteReader();
  633. while (reader.Read()) // Should be at most 1 result
  634. parentFolder.Add(readInventoryFolder(reader));
  635. reader.Close();
  636. result.Dispose();
  637. if (parentFolder.Count >= 1) // No result means parent folder does not exist
  638. {
  639. if (parentFolder[0].ParentID == UUID.Zero) // We are querying the root folder
  640. {
  641. /* Get all of the agent's folders from the database, put them in a list and return it */
  642. result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE agentID = ?uuid",
  643. database.Connection);
  644. result.Parameters.AddWithValue("?uuid", parentFolder[0].Owner.ToString());
  645. reader = result.ExecuteReader();
  646. while (reader.Read())
  647. {
  648. InventoryFolderBase curFolder = readInventoryFolder(reader);
  649. if (curFolder.ID != parentID) // Do not need to add the root node of the tree to the list
  650. folders.Add(curFolder);
  651. }
  652. reader.Close();
  653. result.Dispose();
  654. } // if we are querying the root folder
  655. else // else we are querying a subtree of the inventory folder tree
  656. {
  657. /* Get all of the agent's folders from the database, put them all in a hash table
  658. * indexed by their parent ID */
  659. result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE agentID = ?uuid",
  660. database.Connection);
  661. result.Parameters.AddWithValue("?uuid", parentFolder[0].Owner.ToString());
  662. reader = result.ExecuteReader();
  663. while (reader.Read())
  664. {
  665. InventoryFolderBase curFolder = readInventoryFolder(reader);
  666. if (hashtable.ContainsKey(curFolder.ParentID)) // Current folder already has a sibling
  667. hashtable[curFolder.ParentID].Add(curFolder); // append to sibling list
  668. else // else current folder has no known (yet) siblings
  669. {
  670. List<InventoryFolderBase> siblingList = new List<InventoryFolderBase>();
  671. siblingList.Add(curFolder);
  672. // Current folder has no known (yet) siblings
  673. hashtable.Add(curFolder.ParentID, siblingList);
  674. }
  675. } // while more items to read from the database
  676. reader.Close();
  677. result.Dispose();
  678. // Set flag so we know we need to build the results from the hash table after
  679. // we unlock the database
  680. buildResultsFromHashTable = true;
  681. } // else we are querying a subtree of the inventory folder tree
  682. } // if folder parentID exists
  683. if (buildResultsFromHashTable)
  684. {
  685. /* We have all of the user's folders stored in a hash table indexed by their parent ID
  686. * and we need to return the requested subtree. We will build the requested subtree
  687. * by performing a breadth-first-search on the hash table */
  688. if (hashtable.ContainsKey(parentID))
  689. folders.AddRange(hashtable[parentID]);
  690. for (int i = 0; i < folders.Count; i++) // **Note: folders.Count is *not* static
  691. if (hashtable.ContainsKey(folders[i].ID))
  692. folders.AddRange(hashtable[folders[i].ID]);
  693. }
  694. } // lock (database)
  695. return folders;
  696. }
  697. catch (Exception e)
  698. {
  699. database.Reconnect();
  700. m_log.Error(e.ToString());
  701. return null;
  702. }
  703. }
  704. /// <summary>
  705. /// Delete a folder from database
  706. /// </summary>
  707. /// <param name="folderID">the folder UUID</param>
  708. protected void deleteOneFolder(UUID folderID)
  709. {
  710. try
  711. {
  712. database.CheckConnection();
  713. MySqlCommand cmd =
  714. new MySqlCommand("DELETE FROM inventoryfolders WHERE folderID=?uuid", database.Connection);
  715. cmd.Parameters.AddWithValue("?uuid", folderID.ToString());
  716. lock (database)
  717. {
  718. cmd.ExecuteNonQuery();
  719. }
  720. }
  721. catch (MySqlException e)
  722. {
  723. database.Reconnect();
  724. m_log.Error(e.ToString());
  725. }
  726. }
  727. /// <summary>
  728. /// Delete all item in a folder
  729. /// </summary>
  730. /// <param name="folderID">the folder UUID</param>
  731. protected void deleteItemsInFolder(UUID folderID)
  732. {
  733. try
  734. {
  735. database.CheckConnection();
  736. MySqlCommand cmd =
  737. new MySqlCommand("DELETE FROM inventoryitems WHERE parentFolderID=?uuid", database.Connection);
  738. cmd.Parameters.AddWithValue("?uuid", folderID.ToString());
  739. lock (database)
  740. {
  741. cmd.ExecuteNonQuery();
  742. }
  743. }
  744. catch (MySqlException e)
  745. {
  746. database.Reconnect();
  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. MySqlDataReader result = null;
  770. MySqlCommand sqlCmd = null;
  771. lock (database)
  772. {
  773. try
  774. {
  775. database.CheckConnection();
  776. sqlCmd = new MySqlCommand(
  777. "SELECT * FROM inventoryitems WHERE avatarId = ?uuid AND assetType = ?type and flags = 1",
  778. database.Connection);
  779. sqlCmd.Parameters.AddWithValue("?uuid", avatarID.ToString());
  780. sqlCmd.Parameters.AddWithValue("?type", (int)AssetType.Gesture);
  781. result = sqlCmd.ExecuteReader();
  782. List<InventoryItemBase> list = new List<InventoryItemBase>();
  783. while (result.Read())
  784. {
  785. InventoryItemBase item = readInventoryItem(result);
  786. if (item != null)
  787. list.Add(item);
  788. }
  789. return list;
  790. }
  791. catch (Exception e)
  792. {
  793. database.Reconnect();
  794. m_log.Error(e.ToString());
  795. return null;
  796. }
  797. finally
  798. {
  799. if (result != null) result.Close();
  800. if (sqlCmd != null) sqlCmd.Dispose();
  801. }
  802. }
  803. }
  804. }
  805. }