MySQLInventoryData.cs 38 KB

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