MySQLInventoryData.cs 39 KB

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