MSSQLInventoryData.cs 33 KB

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