MySQLInventoryData.cs 38 KB

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