MSSQLInventoryData.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 OpenSim 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.Data;
  30. using System.Data.SqlClient;
  31. using System.Reflection;
  32. using libsecondlife;
  33. using log4net;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Data.MSSQL
  36. {
  37. /// <summary>
  38. /// A MySQL interface for the inventory server
  39. /// </summary>
  40. public class MSSQLInventoryData : IInventoryData
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// The database manager
  45. /// </summary>
  46. private MSSQLManager database;
  47. /// <summary>
  48. /// Loads and initialises this database plugin
  49. /// </summary>
  50. public void Initialise(string connect)
  51. {
  52. // TODO: actually use the provided connect string
  53. Initialise();
  54. }
  55. public void Initialise()
  56. {
  57. IniFile GridDataMySqlFile = new IniFile("mssql_connection.ini");
  58. string settingDataSource = GridDataMySqlFile.ParseFileReadValue("data_source");
  59. string settingInitialCatalog = GridDataMySqlFile.ParseFileReadValue("initial_catalog");
  60. string settingPersistSecurityInfo = GridDataMySqlFile.ParseFileReadValue("persist_security_info");
  61. string settingUserId = GridDataMySqlFile.ParseFileReadValue("user_id");
  62. string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
  63. database =
  64. new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId,
  65. settingPassword);
  66. TestTables();
  67. }
  68. #region Test and initialization code
  69. private void UpgradeFoldersTable(string tableName)
  70. {
  71. // null as the version, indicates that the table didn't exist
  72. if (tableName == null)
  73. {
  74. database.ExecuteResourceSql("CreateFoldersTable.sql");
  75. //database.ExecuteResourceSql("UpgradeFoldersTableToVersion2.sql");
  76. return;
  77. }
  78. }
  79. private void UpgradeItemsTable(string tableName)
  80. {
  81. // null as the version, indicates that the table didn't exist
  82. if (tableName == null)
  83. {
  84. database.ExecuteResourceSql("CreateItemsTable.sql");
  85. //database.ExecuteResourceSql("UpgradeItemsTableToVersion2.sql");
  86. return;
  87. }
  88. }
  89. private void TestTables()
  90. {
  91. Dictionary<string, string> tableList = new Dictionary<string, string>();
  92. tableList["inventoryfolders"] = null;
  93. tableList["inventoryitems"] = null;
  94. database.GetTableVersion(tableList);
  95. UpgradeFoldersTable(tableList["inventoryfolders"]);
  96. UpgradeItemsTable(tableList["inventoryitems"]);
  97. }
  98. #endregion
  99. /// <summary>
  100. /// The name of this DB provider
  101. /// </summary>
  102. /// <returns>Name of DB provider</returns>
  103. public string getName()
  104. {
  105. return "MSSQL Inventory Data Interface";
  106. }
  107. /// <summary>
  108. /// Closes this DB provider
  109. /// </summary>
  110. public void Close()
  111. {
  112. // Do nothing.
  113. }
  114. /// <summary>
  115. /// Returns the version of this DB provider
  116. /// </summary>
  117. /// <returns>A string containing the DB provider</returns>
  118. public string getVersion()
  119. {
  120. return database.getVersion();
  121. }
  122. /// <summary>
  123. /// Returns a list of items in a specified folder
  124. /// </summary>
  125. /// <param name="folderID">The folder to search</param>
  126. /// <returns>A list containing inventory items</returns>
  127. public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID)
  128. {
  129. try
  130. {
  131. lock (database)
  132. {
  133. List<InventoryItemBase> items = new List<InventoryItemBase>();
  134. Dictionary<string, string> param = new Dictionary<string, string>();
  135. param["parentFolderID"] = folderID.ToString();
  136. IDbCommand result =
  137. database.Query("SELECT * FROM inventoryitems WHERE parentFolderID = @parentFolderID", param);
  138. IDataReader reader = result.ExecuteReader();
  139. while (reader.Read())
  140. items.Add(readInventoryItem(reader));
  141. reader.Close();
  142. result.Dispose();
  143. return items;
  144. }
  145. }
  146. catch (Exception e)
  147. {
  148. database.Reconnect();
  149. m_log.Error(e.ToString());
  150. return null;
  151. }
  152. }
  153. /// <summary>
  154. /// Returns a list of the root folders within a users inventory
  155. /// </summary>
  156. /// <param name="user">The user whos inventory is to be searched</param>
  157. /// <returns>A list of folder objects</returns>
  158. public List<InventoryFolderBase> getUserRootFolders(LLUUID user)
  159. {
  160. try
  161. {
  162. lock (database)
  163. {
  164. Dictionary<string, string> param = new Dictionary<string, string>();
  165. param["uuid"] = user.ToString();
  166. param["zero"] = LLUUID.Zero.ToString();
  167. IDbCommand result =
  168. database.Query(
  169. "SELECT * FROM inventoryfolders WHERE parentFolderID = @zero AND agentID = @uuid", param);
  170. IDataReader reader = result.ExecuteReader();
  171. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  172. while (reader.Read())
  173. items.Add(readInventoryFolder(reader));
  174. reader.Close();
  175. result.Dispose();
  176. return items;
  177. }
  178. }
  179. catch (Exception e)
  180. {
  181. database.Reconnect();
  182. m_log.Error(e.ToString());
  183. return null;
  184. }
  185. }
  186. // see InventoryItemBase.getUserRootFolder
  187. public InventoryFolderBase getUserRootFolder(LLUUID user)
  188. {
  189. try
  190. {
  191. lock (database)
  192. {
  193. Dictionary<string, string> param = new Dictionary<string, string>();
  194. param["uuid"] = user.ToString();
  195. param["zero"] = LLUUID.Zero.ToString();
  196. IDbCommand result =
  197. database.Query(
  198. "SELECT * FROM inventoryfolders WHERE parentFolderID = @zero AND agentID = @uuid", param);
  199. IDataReader reader = result.ExecuteReader();
  200. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  201. while (reader.Read())
  202. items.Add(readInventoryFolder(reader));
  203. InventoryFolderBase rootFolder = null;
  204. // There should only ever be one root folder for a user. However, if there's more
  205. // than one we'll simply use the first one rather than failing. It would be even
  206. // nicer to print some message to this effect, but this feels like it's too low a
  207. // to put such a message out, and it's too minor right now to spare the time to
  208. // suitably refactor.
  209. if (items.Count > 0)
  210. {
  211. rootFolder = items[0];
  212. }
  213. reader.Close();
  214. result.Dispose();
  215. return rootFolder;
  216. }
  217. }
  218. catch (Exception e)
  219. {
  220. database.Reconnect();
  221. m_log.Error(e.ToString());
  222. return null;
  223. }
  224. }
  225. /// <summary>
  226. /// Returns a list of folders in a users inventory contained within the specified folder
  227. /// </summary>
  228. /// <param name="parentID">The folder to search</param>
  229. /// <returns>A list of inventory folders</returns>
  230. public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID)
  231. {
  232. try
  233. {
  234. lock (database)
  235. {
  236. Dictionary<string, string> param = new Dictionary<string, string>();
  237. param["parentFolderID"] = parentID.ToString();
  238. IDbCommand result =
  239. database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = @parentFolderID", param);
  240. IDataReader reader = result.ExecuteReader();
  241. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  242. while (reader.Read())
  243. items.Add(readInventoryFolder(reader));
  244. reader.Close();
  245. result.Dispose();
  246. return items;
  247. }
  248. }
  249. catch (Exception e)
  250. {
  251. database.Reconnect();
  252. m_log.Error(e.ToString());
  253. return null;
  254. }
  255. }
  256. /// <summary>
  257. /// Reads a one item from an SQL result
  258. /// </summary>
  259. /// <param name="reader">The SQL Result</param>
  260. /// <returns>the item read</returns>
  261. private static InventoryItemBase readInventoryItem(IDataReader reader)
  262. {
  263. try
  264. {
  265. InventoryItemBase item = new InventoryItemBase();
  266. item.ID = new LLUUID((string) reader["inventoryID"]);
  267. item.AssetID = new LLUUID((string) reader["assetID"]);
  268. item.AssetType = (int) reader["assetType"];
  269. item.Folder = new LLUUID((string) reader["parentFolderID"]);
  270. item.Owner = new LLUUID((string) reader["avatarID"]);
  271. item.Name = (string) reader["inventoryName"];
  272. item.Description = (string) reader["inventoryDescription"];
  273. item.NextPermissions = Convert.ToUInt32(reader["inventoryNextPermissions"]);
  274. item.CurrentPermissions = Convert.ToUInt32(reader["inventoryCurrentPermissions"]);
  275. item.InvType = (int) reader["invType"];
  276. item.Creator = new LLUUID((string) reader["creatorID"]);
  277. item.BasePermissions = Convert.ToUInt32(reader["inventoryBasePermissions"]);
  278. item.EveryOnePermissions = Convert.ToUInt32(reader["inventoryEveryOnePermissions"]);
  279. item.SalePrice = (int) reader["salePrice"];
  280. item.SaleType = Convert.ToByte(reader["saleType"]);
  281. item.CreationDate = (int) reader["creationDate"];
  282. item.GroupID = new LLUUID(reader["groupID"].ToString());
  283. item.GroupOwned = Convert.ToBoolean(reader["groupOwned"]);
  284. item.Flags = (uint) reader["flags"];
  285. return item;
  286. }
  287. catch (SqlException e)
  288. {
  289. m_log.Error(e.ToString());
  290. }
  291. return null;
  292. }
  293. /// <summary>
  294. /// Returns a specified inventory item
  295. /// </summary>
  296. /// <param name="item">The item to return</param>
  297. /// <returns>An inventory item</returns>
  298. public InventoryItemBase getInventoryItem(LLUUID itemID)
  299. {
  300. try
  301. {
  302. lock (database)
  303. {
  304. Dictionary<string, string> param = new Dictionary<string, string>();
  305. param["inventoryID"] = itemID.ToString();
  306. IDbCommand result =
  307. database.Query("SELECT * FROM inventoryitems WHERE inventoryID = @inventoryID", param);
  308. IDataReader reader = result.ExecuteReader();
  309. InventoryItemBase item = null;
  310. if (reader.Read())
  311. item = readInventoryItem(reader);
  312. reader.Close();
  313. result.Dispose();
  314. return item;
  315. }
  316. }
  317. catch (Exception e)
  318. {
  319. database.Reconnect();
  320. m_log.Error(e.ToString());
  321. }
  322. return null;
  323. }
  324. /// <summary>
  325. /// Reads a list of inventory folders returned by a query.
  326. /// </summary>
  327. /// <param name="reader">A MySQL Data Reader</param>
  328. /// <returns>A List containing inventory folders</returns>
  329. protected static InventoryFolderBase readInventoryFolder(IDataReader reader)
  330. {
  331. try
  332. {
  333. InventoryFolderBase folder = new InventoryFolderBase();
  334. folder.Owner = new LLUUID((string) reader["agentID"]);
  335. folder.ParentID = new LLUUID((string) reader["parentFolderID"]);
  336. folder.ID = new LLUUID((string) reader["folderID"]);
  337. folder.Name = (string) reader["folderName"];
  338. folder.Type = (short) reader["type"];
  339. folder.Version = (ushort) ((int) reader["version"]);
  340. return folder;
  341. }
  342. catch (Exception e)
  343. {
  344. m_log.Error(e.ToString());
  345. }
  346. return null;
  347. }
  348. /// <summary>
  349. /// Returns a specified inventory folder
  350. /// </summary>
  351. /// <param name="folder">The folder to return</param>
  352. /// <returns>A folder class</returns>
  353. public InventoryFolderBase getInventoryFolder(LLUUID folderID)
  354. {
  355. try
  356. {
  357. lock (database)
  358. {
  359. Dictionary<string, string> param = new Dictionary<string, string>();
  360. param["uuid"] = folderID.ToString();
  361. IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE folderID = @uuid", param);
  362. IDataReader reader = result.ExecuteReader();
  363. reader.Read();
  364. InventoryFolderBase folder = readInventoryFolder(reader);
  365. reader.Close();
  366. result.Dispose();
  367. return folder;
  368. }
  369. }
  370. catch (Exception e)
  371. {
  372. database.Reconnect();
  373. m_log.Error(e.ToString());
  374. return null;
  375. }
  376. }
  377. /// <summary>
  378. /// Adds a specified item to the database
  379. /// </summary>
  380. /// <param name="item">The inventory item</param>
  381. public void addInventoryItem(InventoryItemBase item)
  382. {
  383. if (getInventoryItem(item.ID) != null)
  384. {
  385. updateInventoryItem(item);
  386. return;
  387. }
  388. string sql = "INSERT INTO inventoryitems";
  389. sql +=
  390. "([inventoryID], [assetID], [assetType], [parentFolderID], [avatarID], [inventoryName]"
  391. + ", [inventoryDescription], [inventoryNextPermissions], [inventoryCurrentPermissions]"
  392. + ", [invType], [creatorID], [inventoryBasePermissions], [inventoryEveryOnePermissions]"
  393. + ", [salePrice], [saleType], [creationDate], [groupID], [groupOwned], [flags]) VALUES ";
  394. sql +=
  395. "(@inventoryID, @assetID, @assetType, @parentFolderID, @avatarID, @inventoryName, @inventoryDescription"
  396. + ", @inventoryNextPermissions, @inventoryCurrentPermissions, @invType, @creatorID"
  397. + ", @inventoryBasePermissions, @inventoryEveryOnePermissions, @salePrice, @saleType"
  398. + ", @creationDate, @groupID, @groupOwned, @flags);";
  399. try
  400. {
  401. Dictionary<string, string> param = new Dictionary<string, string>();
  402. param["inventoryID"] = item.ID.ToString();
  403. param["assetID"] = item.AssetID.ToString();
  404. param["assetType"] = item.AssetType.ToString();
  405. param["parentFolderID"] = item.Folder.ToString();
  406. param["avatarID"] = item.Owner.ToString();
  407. param["inventoryName"] = item.Name;
  408. param["inventoryDescription"] = item.Description;
  409. param["inventoryNextPermissions"] = item.NextPermissions.ToString();
  410. param["inventoryCurrentPermissions"] = item.CurrentPermissions.ToString();
  411. param["invType"] = Convert.ToString(item.InvType);
  412. param["creatorID"] = item.Creator.ToString();
  413. param["inventoryBasePermissions"] = Convert.ToString(item.BasePermissions);
  414. param["inventoryEveryOnePermissions"] = Convert.ToString(item.EveryOnePermissions);
  415. param["salePrice"] = Convert.ToString(item.SalePrice);
  416. param["saleType"] = Convert.ToString(item.SaleType);
  417. param["creationDate"] = Convert.ToString(item.CreationDate);
  418. param["groupID"] = item.GroupID.ToString();
  419. param["groupOwned"] = Convert.ToString(item.GroupOwned);
  420. param["flags"] = Convert.ToString(item.Flags);
  421. IDbCommand result = database.Query(sql, param);
  422. result.ExecuteNonQuery();
  423. result.Dispose();
  424. }
  425. catch (SqlException e)
  426. {
  427. m_log.Error(e.ToString());
  428. }
  429. }
  430. /// <summary>
  431. /// Updates the specified inventory item
  432. /// </summary>
  433. /// <param name="item">Inventory item to update</param>
  434. public void updateInventoryItem(InventoryItemBase item)
  435. {
  436. SqlCommand command = new SqlCommand("UPDATE inventoryitems set inventoryID = @inventoryID, " +
  437. "assetID = @assetID, " +
  438. "assetType = @assetType" +
  439. "parentFolderID = @parentFolderID" +
  440. "avatarID = @avatarID" +
  441. "inventoryName = @inventoryName" +
  442. "inventoryDescription = @inventoryDescription" +
  443. "inventoryNextPermissions = @inventoryNextPermissions" +
  444. "inventoryCurrentPermissions = @inventoryCurrentPermissions" +
  445. "invType = @invType" +
  446. "creatorID = @creatorID" +
  447. "inventoryBasePermissions = @inventoryBasePermissions" +
  448. "inventoryEveryOnePermissions = @inventoryEveryOnePermissions) where " +
  449. "inventoryID = @keyInventoryID;", database.getConnection());
  450. SqlParameter param1 = new SqlParameter("@inventoryID", item.ID.ToString());
  451. SqlParameter param2 = new SqlParameter("@assetID", item.AssetID);
  452. SqlParameter param3 = new SqlParameter("@assetType", item.AssetType);
  453. SqlParameter param4 = new SqlParameter("@parentFolderID", item.Folder);
  454. SqlParameter param5 = new SqlParameter("@avatarID", item.Owner);
  455. SqlParameter param6 = new SqlParameter("@inventoryName", item.Name);
  456. SqlParameter param7 = new SqlParameter("@inventoryDescription", item.Description);
  457. SqlParameter param8 = new SqlParameter("@inventoryNextPermissions", item.NextPermissions);
  458. SqlParameter param9 = new SqlParameter("@inventoryCurrentPermissions", item.CurrentPermissions);
  459. SqlParameter param10 = new SqlParameter("@invType", item.InvType);
  460. SqlParameter param11 = new SqlParameter("@creatorID", item.Creator);
  461. SqlParameter param12 = new SqlParameter("@inventoryBasePermissions", item.BasePermissions);
  462. SqlParameter param13 = new SqlParameter("@inventoryEveryOnePermissions", item.EveryOnePermissions);
  463. SqlParameter param14 = new SqlParameter("@keyInventoryID", item.ID.ToString());
  464. command.Parameters.Add(param1);
  465. command.Parameters.Add(param2);
  466. command.Parameters.Add(param3);
  467. command.Parameters.Add(param4);
  468. command.Parameters.Add(param5);
  469. command.Parameters.Add(param6);
  470. command.Parameters.Add(param7);
  471. command.Parameters.Add(param8);
  472. command.Parameters.Add(param9);
  473. command.Parameters.Add(param10);
  474. command.Parameters.Add(param11);
  475. command.Parameters.Add(param12);
  476. command.Parameters.Add(param13);
  477. command.Parameters.Add(param14);
  478. try
  479. {
  480. command.ExecuteNonQuery();
  481. }
  482. catch (Exception e)
  483. {
  484. m_log.Error(e.ToString());
  485. }
  486. }
  487. /// <summary>
  488. ///
  489. /// </summary>
  490. /// <param name="item"></param>
  491. public void deleteInventoryItem(LLUUID itemID)
  492. {
  493. try
  494. {
  495. Dictionary<string, string> param = new Dictionary<string, string>();
  496. param["uuid"] = itemID.ToString();
  497. IDbCommand cmd = database.Query("DELETE FROM inventoryitems WHERE inventoryID=@uuid", param);
  498. cmd.ExecuteNonQuery();
  499. cmd.Dispose();
  500. }
  501. catch (SqlException e)
  502. {
  503. database.Reconnect();
  504. m_log.Error(e.ToString());
  505. }
  506. }
  507. /// <summary>
  508. /// Creates a new inventory folder
  509. /// </summary>
  510. /// <param name="folder">Folder to create</param>
  511. public void addInventoryFolder(InventoryFolderBase folder)
  512. {
  513. string sql =
  514. "INSERT INTO inventoryfolders ([folderID], [agentID], [parentFolderID], [folderName], [type], [version]) VALUES ";
  515. sql += "(@folderID, @agentID, @parentFolderID, @folderName, @type, @version);";
  516. Dictionary<string, string> param = new Dictionary<string, string>();
  517. param["folderID"] = folder.ID.ToString();
  518. param["agentID"] = folder.Owner.ToString();
  519. param["parentFolderID"] = folder.ParentID.ToString();
  520. param["folderName"] = folder.Name;
  521. param["type"] = Convert.ToString(folder.Type);
  522. param["version"] = Convert.ToString(folder.Version);
  523. try
  524. {
  525. IDbCommand result = database.Query(sql, param);
  526. result.ExecuteNonQuery();
  527. result.Dispose();
  528. }
  529. catch (Exception e)
  530. {
  531. m_log.Error(e.ToString());
  532. }
  533. }
  534. /// <summary>
  535. /// Updates an inventory folder
  536. /// </summary>
  537. /// <param name="folder">Folder to update</param>
  538. public void updateInventoryFolder(InventoryFolderBase folder)
  539. {
  540. SqlCommand command = new SqlCommand("UPDATE inventoryfolders set folderID = @folderID, " +
  541. "agentID = @agentID, " +
  542. "parentFolderID = @parentFolderID," +
  543. "folderName = @folderName," +
  544. "type = @type," +
  545. "version = @version where " +
  546. "folderID = @keyFolderID;", database.getConnection());
  547. SqlParameter param1 = new SqlParameter("@folderID", folder.ID.ToString());
  548. SqlParameter param2 = new SqlParameter("@agentID", folder.Owner.ToString());
  549. SqlParameter param3 = new SqlParameter("@parentFolderID", folder.ParentID.ToString());
  550. SqlParameter param4 = new SqlParameter("@folderName", folder.Name);
  551. SqlParameter param5 = new SqlParameter("@type", folder.Type);
  552. SqlParameter param6 = new SqlParameter("@version", folder.Version);
  553. SqlParameter param7 = new SqlParameter("@keyFolderID", folder.ID.ToString());
  554. command.Parameters.Add(param1);
  555. command.Parameters.Add(param2);
  556. command.Parameters.Add(param3);
  557. command.Parameters.Add(param4);
  558. command.Parameters.Add(param5);
  559. command.Parameters.Add(param6);
  560. command.Parameters.Add(param7);
  561. try
  562. {
  563. command.ExecuteNonQuery();
  564. }
  565. catch (Exception e)
  566. {
  567. m_log.Error(e.ToString());
  568. }
  569. }
  570. /// <summary>
  571. /// Updates an inventory folder
  572. /// </summary>
  573. /// <param name="folder">Folder to update</param>
  574. public void moveInventoryFolder(InventoryFolderBase folder)
  575. {
  576. SqlCommand command = new SqlCommand("UPDATE inventoryfolders set folderID = @folderID, " +
  577. "parentFolderID = @parentFolderID," +
  578. "folderID = @keyFolderID;", database.getConnection());
  579. SqlParameter param1 = new SqlParameter("@folderID", folder.ID.ToString());
  580. SqlParameter param2 = new SqlParameter("@parentFolderID", folder.ParentID.ToString());
  581. SqlParameter param3 = new SqlParameter("@keyFolderID", folder.ID.ToString());
  582. command.Parameters.Add(param1);
  583. command.Parameters.Add(param2);
  584. command.Parameters.Add(param3);
  585. try
  586. {
  587. command.ExecuteNonQuery();
  588. }
  589. catch (Exception e)
  590. {
  591. m_log.Error(e.ToString());
  592. }
  593. }
  594. /// <summary>
  595. /// Append a list of all the child folders of a parent folder
  596. /// </summary>
  597. /// <param name="folders">list where folders will be appended</param>
  598. /// <param name="parentID">ID of parent</param>
  599. protected void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID)
  600. {
  601. List<InventoryFolderBase> subfolderList = getInventoryFolders(parentID);
  602. foreach (InventoryFolderBase f in subfolderList)
  603. folders.Add(f);
  604. }
  605. // See IInventoryData
  606. public List<InventoryFolderBase> getFolderHierarchy(LLUUID parentID)
  607. {
  608. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  609. getInventoryFolders(ref folders, parentID);
  610. for (int i = 0; i < folders.Count; i++)
  611. getInventoryFolders(ref folders, folders[i].ID);
  612. return folders;
  613. }
  614. protected void deleteOneFolder(LLUUID folderID)
  615. {
  616. try
  617. {
  618. Dictionary<string, string> param = new Dictionary<string, string>();
  619. param["folderID"] = folderID.ToString();
  620. IDbCommand cmd = database.Query("DELETE FROM inventoryfolders WHERE folderID=@folderID", param);
  621. cmd.ExecuteNonQuery();
  622. cmd.Dispose();
  623. }
  624. catch (SqlException e)
  625. {
  626. database.Reconnect();
  627. m_log.Error(e.ToString());
  628. }
  629. }
  630. protected void deleteItemsInFolder(LLUUID folderID)
  631. {
  632. try
  633. {
  634. Dictionary<string, string> param = new Dictionary<string, string>();
  635. param["parentFolderID"] = folderID.ToString();
  636. IDbCommand cmd =
  637. database.Query("DELETE FROM inventoryitems WHERE parentFolderID=@parentFolderID", param);
  638. cmd.ExecuteNonQuery();
  639. cmd.Dispose();
  640. }
  641. catch (SqlException e)
  642. {
  643. database.Reconnect();
  644. m_log.Error(e.ToString());
  645. }
  646. }
  647. /// <summary>
  648. /// Delete an inventory folder
  649. /// </summary>
  650. /// <param name="folderId">Id of folder to delete</param>
  651. public void deleteInventoryFolder(LLUUID folderID)
  652. {
  653. lock (database)
  654. {
  655. List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID);
  656. //Delete all sub-folders
  657. foreach (InventoryFolderBase f in subFolders)
  658. {
  659. deleteOneFolder(f.ID);
  660. deleteItemsInFolder(f.ID);
  661. }
  662. //Delete the actual row
  663. deleteOneFolder(folderID);
  664. deleteItemsInFolder(folderID);
  665. }
  666. }
  667. }
  668. }