MSSQLInventoryData.cs 29 KB

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