MSSQLInventoryData.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 gridDataMSSqlFile = new IniFile("mssql_connection.ini");
  66. string settingDataSource = gridDataMSSqlFile.ParseFileReadValue("data_source");
  67. string settingInitialCatalog = gridDataMSSqlFile.ParseFileReadValue("initial_catalog");
  68. string settingPersistSecurityInfo = gridDataMSSqlFile.ParseFileReadValue("persist_security_info");
  69. string settingUserId = gridDataMSSqlFile.ParseFileReadValue("user_id");
  70. string settingPassword = gridDataMSSqlFile.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. List<InventoryItemBase> items = new List<InventoryItemBase>();
  151. Dictionary<string, string> param = new Dictionary<string, string>();
  152. param["parentFolderID"] = folderID.ToString();
  153. using (IDbCommand result =
  154. database.Query("SELECT * FROM inventoryitems WHERE parentFolderID = @parentFolderID", param))
  155. using (IDataReader reader = result.ExecuteReader())
  156. {
  157. while (reader.Read())
  158. items.Add(readInventoryItem(reader));
  159. reader.Close();
  160. }
  161. return items;
  162. }
  163. catch (Exception e)
  164. {
  165. m_log.Error(e.ToString());
  166. return null;
  167. }
  168. }
  169. /// <summary>
  170. /// Returns a list of the root folders within a users inventory
  171. /// </summary>
  172. /// <param name="user">The user whos inventory is to be searched</param>
  173. /// <returns>A list of folder objects</returns>
  174. public List<InventoryFolderBase> getUserRootFolders(LLUUID user)
  175. {
  176. try
  177. {
  178. Dictionary<string, string> param = new Dictionary<string, string>();
  179. param["uuid"] = user.ToString();
  180. param["zero"] = LLUUID.Zero.ToString();
  181. using (IDbCommand result =
  182. database.Query(
  183. "SELECT * FROM inventoryfolders WHERE parentFolderID = @zero AND agentID = @uuid", param))
  184. using (IDataReader reader = result.ExecuteReader())
  185. {
  186. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  187. while (reader.Read())
  188. items.Add(readInventoryFolder(reader));
  189. return items;
  190. }
  191. }
  192. catch (Exception e)
  193. {
  194. m_log.Error(e.ToString());
  195. return null;
  196. }
  197. }
  198. /// <summary>
  199. /// see InventoryItemBase.getUserRootFolder
  200. /// </summary>
  201. /// <param name="user">the User UUID</param>
  202. /// <returns></returns>
  203. public InventoryFolderBase getUserRootFolder(LLUUID user)
  204. {
  205. try
  206. {
  207. Dictionary<string, string> param = new Dictionary<string, string>();
  208. param["uuid"] = user.ToString();
  209. param["zero"] = LLUUID.Zero.ToString();
  210. using (IDbCommand result =
  211. database.Query(
  212. "SELECT * FROM inventoryfolders WHERE parentFolderID = @zero AND agentID = @uuid", param))
  213. using (IDataReader reader = result.ExecuteReader())
  214. {
  215. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  216. while (reader.Read())
  217. items.Add(readInventoryFolder(reader));
  218. InventoryFolderBase rootFolder = null;
  219. // There should only ever be one root folder for a user. However, if there's more
  220. // than one we'll simply use the first one rather than failing. It would be even
  221. // nicer to print some message to this effect, but this feels like it's too low a
  222. // to put such a message out, and it's too minor right now to spare the time to
  223. // suitably refactor.
  224. if (items.Count > 0)
  225. {
  226. rootFolder = items[0];
  227. }
  228. return rootFolder;
  229. }
  230. }
  231. catch (Exception e)
  232. {
  233. m_log.Error(e.ToString());
  234. return null;
  235. }
  236. }
  237. /// <summary>
  238. /// Returns a list of folders in a users inventory contained within the specified folder
  239. /// </summary>
  240. /// <param name="parentID">The folder to search</param>
  241. /// <returns>A list of inventory folders</returns>
  242. public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID)
  243. {
  244. try
  245. {
  246. Dictionary<string, string> param = new Dictionary<string, string>();
  247. param["parentFolderID"] = parentID.ToString();
  248. using (IDbCommand result =
  249. database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = @parentFolderID", param))
  250. using (IDataReader reader = result.ExecuteReader())
  251. {
  252. List<InventoryFolderBase> items = new List<InventoryFolderBase>();
  253. while (reader.Read())
  254. items.Add(readInventoryFolder(reader));
  255. return items;
  256. }
  257. }
  258. catch (Exception e)
  259. {
  260. m_log.Error(e.ToString());
  261. return null;
  262. }
  263. }
  264. /// <summary>
  265. /// Reads a one item from an SQL result
  266. /// </summary>
  267. /// <param name="reader">The SQL Result</param>
  268. /// <returns>the item read</returns>
  269. private static InventoryItemBase readInventoryItem(IDataReader reader)
  270. {
  271. try
  272. {
  273. InventoryItemBase item = new InventoryItemBase();
  274. item.ID = new LLUUID((string) reader["inventoryID"]);
  275. item.AssetID = new LLUUID((string) reader["assetID"]);
  276. item.AssetType = (int) reader["assetType"];
  277. item.Folder = new LLUUID((string) reader["parentFolderID"]);
  278. item.Owner = new LLUUID((string) reader["avatarID"]);
  279. item.Name = (string) reader["inventoryName"];
  280. item.Description = (string) reader["inventoryDescription"];
  281. item.NextPermissions = ConvertInt32BitFieldToUint32((int)reader["inventoryNextPermissions"]);
  282. item.CurrentPermissions = ConvertInt32BitFieldToUint32((int)reader["inventoryCurrentPermissions"]);
  283. item.InvType = (int) reader["invType"];
  284. item.Creator = new LLUUID((string) reader["creatorID"]);
  285. item.BasePermissions = ConvertInt32BitFieldToUint32((int)reader["inventoryBasePermissions"]);
  286. item.EveryOnePermissions = ConvertInt32BitFieldToUint32((int)reader["inventoryEveryOnePermissions"]);
  287. item.SalePrice = (int) reader["salePrice"];
  288. item.SaleType = Convert.ToByte(reader["saleType"]);
  289. item.CreationDate = (int) reader["creationDate"];
  290. item.GroupID = new LLUUID(reader["groupID"].ToString());
  291. item.GroupOwned = Convert.ToBoolean(reader["groupOwned"]);
  292. item.Flags = ConvertInt32BitFieldToUint32((int)reader["flags"]);
  293. return item;
  294. }
  295. catch (SqlException e)
  296. {
  297. m_log.Error(e.ToString());
  298. }
  299. return null;
  300. }
  301. /// <summary>
  302. /// Returns a specified inventory item
  303. /// </summary>
  304. /// <param name="item">The item to return</param>
  305. /// <returns>An inventory item</returns>
  306. public InventoryItemBase getInventoryItem(LLUUID itemID)
  307. {
  308. try
  309. {
  310. Dictionary<string, string> param = new Dictionary<string, string>();
  311. param["inventoryID"] = itemID.ToString();
  312. using (IDbCommand result =
  313. database.Query("SELECT * FROM inventoryitems WHERE inventoryID = @inventoryID", param))
  314. using (IDataReader reader = result.ExecuteReader())
  315. {
  316. InventoryItemBase item = null;
  317. if (reader.Read())
  318. item = readInventoryItem(reader);
  319. return item;
  320. }
  321. }
  322. catch (Exception e)
  323. {
  324. m_log.Error(e.ToString());
  325. }
  326. return null;
  327. }
  328. /// <summary>
  329. /// Reads a list of inventory folders returned by a query.
  330. /// </summary>
  331. /// <param name="reader">A MSSQL Data Reader</param>
  332. /// <returns>A List containing inventory folders</returns>
  333. protected static InventoryFolderBase readInventoryFolder(IDataReader reader)
  334. {
  335. try
  336. {
  337. InventoryFolderBase folder = new InventoryFolderBase();
  338. folder.Owner = new LLUUID((string) reader["agentID"]);
  339. folder.ParentID = new LLUUID((string) reader["parentFolderID"]);
  340. folder.ID = new LLUUID((string) reader["folderID"]);
  341. folder.Name = (string) reader["folderName"];
  342. folder.Type = (short) reader["type"];
  343. folder.Version = Convert.ToUInt16(reader["version"]);
  344. return folder;
  345. }
  346. catch (Exception e)
  347. {
  348. m_log.Error(e.ToString());
  349. }
  350. return null;
  351. }
  352. /// <summary>
  353. /// Returns a specified inventory folder
  354. /// </summary>
  355. /// <param name="folder">The folder to return</param>
  356. /// <returns>A folder class</returns>
  357. public InventoryFolderBase getInventoryFolder(LLUUID folderID)
  358. {
  359. try
  360. {
  361. Dictionary<string, string> param = new Dictionary<string, string>();
  362. param["uuid"] = folderID.ToString();
  363. using (IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE folderID = @uuid", param))
  364. using (IDataReader reader = result.ExecuteReader())
  365. {
  366. reader.Read();
  367. InventoryFolderBase folder = readInventoryFolder(reader);
  368. return folder;
  369. }
  370. }
  371. catch (Exception e)
  372. {
  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. using (AutoClosingSqlCommand command = database.Query(sql))
  400. {
  401. command.Parameters.AddWithValue("inventoryID", item.ID.ToString());
  402. command.Parameters.AddWithValue("assetID", item.AssetID.ToString());
  403. command.Parameters.AddWithValue("assetType", item.AssetType.ToString());
  404. command.Parameters.AddWithValue("parentFolderID", item.Folder.ToString());
  405. command.Parameters.AddWithValue("avatarID", item.Owner.ToString());
  406. command.Parameters.AddWithValue("inventoryName", item.Name);
  407. command.Parameters.AddWithValue("inventoryDescription", item.Description);
  408. command.Parameters.AddWithValue("inventoryNextPermissions", ConvertUint32BitFieldToInt32(item.NextPermissions));
  409. command.Parameters.AddWithValue("inventoryCurrentPermissions", ConvertUint32BitFieldToInt32(item.CurrentPermissions));
  410. command.Parameters.AddWithValue("invType", item.InvType);
  411. command.Parameters.AddWithValue("creatorID", item.Creator.ToString());
  412. command.Parameters.AddWithValue("inventoryBasePermissions", ConvertUint32BitFieldToInt32(item.BasePermissions));
  413. command.Parameters.AddWithValue("inventoryEveryOnePermissions", ConvertUint32BitFieldToInt32(item.EveryOnePermissions));
  414. command.Parameters.AddWithValue("salePrice", item.SalePrice);
  415. command.Parameters.AddWithValue("saleType", item.SaleType);
  416. command.Parameters.AddWithValue("creationDate", item.CreationDate);
  417. command.Parameters.AddWithValue("groupID", item.GroupID.ToString());
  418. command.Parameters.AddWithValue("groupOwned", item.GroupOwned);
  419. command.Parameters.AddWithValue("flags", ConvertUint32BitFieldToInt32(item.Flags));
  420. try
  421. {
  422. command.ExecuteNonQuery();
  423. }
  424. catch (SqlException e)
  425. {
  426. m_log.Error(e.ToString());
  427. }
  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. using (AutoClosingSqlCommand command = database.Query("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," +
  449. "salePrice = @salePrice," +
  450. "saleType = @saleType," +
  451. "creationDate = @creationDate," +
  452. "groupID = @groupID," +
  453. "groupOwned = @groupOwned," +
  454. "flags = @flags where " +
  455. "inventoryID = @keyInventoryID;"))
  456. {
  457. command.Parameters.AddWithValue("inventoryID", item.ID.ToString());
  458. command.Parameters.AddWithValue("assetID", item.AssetID.ToString());
  459. command.Parameters.AddWithValue("assetType", item.AssetType.ToString());
  460. command.Parameters.AddWithValue("parentFolderID", item.Folder.ToString());
  461. command.Parameters.AddWithValue("avatarID", item.Owner.ToString());
  462. command.Parameters.AddWithValue("inventoryName", item.Name);
  463. command.Parameters.AddWithValue("inventoryDescription", item.Description);
  464. command.Parameters.AddWithValue("inventoryNextPermissions", ConvertUint32BitFieldToInt32(item.NextPermissions));
  465. command.Parameters.AddWithValue("inventoryCurrentPermissions", ConvertUint32BitFieldToInt32(item.CurrentPermissions));
  466. command.Parameters.AddWithValue("invType", item.InvType);
  467. command.Parameters.AddWithValue("creatorID", item.Creator.ToString());
  468. command.Parameters.AddWithValue("inventoryBasePermissions", ConvertUint32BitFieldToInt32(item.BasePermissions));
  469. command.Parameters.AddWithValue("inventoryEveryOnePermissions", ConvertUint32BitFieldToInt32(item.EveryOnePermissions));
  470. command.Parameters.AddWithValue("salePrice", item.SalePrice);
  471. command.Parameters.AddWithValue("saleType", item.SaleType);
  472. command.Parameters.AddWithValue("creationDate", item.CreationDate);
  473. command.Parameters.AddWithValue("groupID", item.GroupID.ToString());
  474. command.Parameters.AddWithValue("groupOwned", item.GroupOwned);
  475. command.Parameters.AddWithValue("flags", ConvertUint32BitFieldToInt32(item.Flags));
  476. command.Parameters.AddWithValue("@keyInventoryID", item.ID.ToString());
  477. try
  478. {
  479. command.ExecuteNonQuery();
  480. }
  481. catch (Exception e)
  482. {
  483. m_log.Error(e.ToString());
  484. }
  485. }
  486. }
  487. /// <summary>
  488. /// Delete an item in inventory database
  489. /// </summary>
  490. /// <param name="item">the item UUID</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. using (IDbCommand cmd = database.Query("DELETE FROM inventoryitems WHERE inventoryID=@uuid", param))
  498. {
  499. cmd.ExecuteNonQuery();
  500. }
  501. }
  502. catch (SqlException e)
  503. {
  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. using (AutoClosingSqlCommand command = database.Query(sql))
  517. {
  518. command.Parameters.AddWithValue("folderID", folder.ID.ToString());
  519. command.Parameters.AddWithValue("agentID", folder.Owner.ToString());
  520. command.Parameters.AddWithValue("parentFolderID", folder.ParentID.ToString());
  521. command.Parameters.AddWithValue("folderName", folder.Name);
  522. command.Parameters.AddWithValue("type", folder.Type);
  523. command.Parameters.AddWithValue("version", Convert.ToInt32(folder.Version));
  524. try
  525. {
  526. //IDbCommand result = database.Query(sql, param);
  527. command.ExecuteNonQuery();
  528. }
  529. catch (Exception e)
  530. {
  531. m_log.Error(e.ToString());
  532. }
  533. }
  534. }
  535. /// <summary>
  536. /// Updates an inventory folder
  537. /// </summary>
  538. /// <param name="folder">Folder to update</param>
  539. public void updateInventoryFolder(InventoryFolderBase folder)
  540. {
  541. using (IDbCommand command = database.Query("UPDATE inventoryfolders set folderID = @folderID, " +
  542. "agentID = @agentID, " +
  543. "parentFolderID = @parentFolderID," +
  544. "folderName = @folderName," +
  545. "type = @type," +
  546. "version = @version where " +
  547. "folderID = @keyFolderID;"))
  548. {
  549. SqlParameter param1 = new SqlParameter("@folderID", folder.ID.ToString());
  550. SqlParameter param2 = new SqlParameter("@agentID", folder.Owner.ToString());
  551. SqlParameter param3 = new SqlParameter("@parentFolderID", folder.ParentID.ToString());
  552. SqlParameter param4 = new SqlParameter("@folderName", folder.Name);
  553. SqlParameter param5 = new SqlParameter("@type", folder.Type);
  554. SqlParameter param6 = new SqlParameter("@version", Convert.ToInt32(folder.Version));
  555. SqlParameter param7 = new SqlParameter("@keyFolderID", folder.ID.ToString());
  556. command.Parameters.Add(param1);
  557. command.Parameters.Add(param2);
  558. command.Parameters.Add(param3);
  559. command.Parameters.Add(param4);
  560. command.Parameters.Add(param5);
  561. command.Parameters.Add(param6);
  562. command.Parameters.Add(param7);
  563. try
  564. {
  565. command.ExecuteNonQuery();
  566. }
  567. catch (Exception e)
  568. {
  569. m_log.Error(e.ToString());
  570. }
  571. }
  572. }
  573. /// <summary>
  574. /// Updates an inventory folder
  575. /// </summary>
  576. /// <param name="folder">Folder to update</param>
  577. public void moveInventoryFolder(InventoryFolderBase folder)
  578. {
  579. using (IDbCommand command = database.Query("UPDATE inventoryfolders set folderID = @folderID, " +
  580. "parentFolderID = @parentFolderID," +
  581. "folderID = @keyFolderID;"))
  582. {
  583. SqlParameter param1 = new SqlParameter("@folderID", folder.ID.ToString());
  584. SqlParameter param2 = new SqlParameter("@parentFolderID", folder.ParentID.ToString());
  585. SqlParameter param3 = new SqlParameter("@keyFolderID", folder.ID.ToString());
  586. command.Parameters.Add(param1);
  587. command.Parameters.Add(param2);
  588. command.Parameters.Add(param3);
  589. try
  590. {
  591. command.ExecuteNonQuery();
  592. }
  593. catch (Exception e)
  594. {
  595. m_log.Error(e.ToString());
  596. }
  597. }
  598. }
  599. /// <summary>
  600. /// Append a list of all the child folders of a parent folder
  601. /// </summary>
  602. /// <param name="folders">list where folders will be appended</param>
  603. /// <param name="parentID">ID of parent</param>
  604. protected void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID)
  605. {
  606. List<InventoryFolderBase> subfolderList = getInventoryFolders(parentID);
  607. foreach (InventoryFolderBase f in subfolderList)
  608. folders.Add(f);
  609. }
  610. // See IInventoryData
  611. public List<InventoryFolderBase> getFolderHierarchy(LLUUID parentID)
  612. {
  613. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  614. getInventoryFolders(ref folders, parentID);
  615. for (int i = 0; i < folders.Count; i++)
  616. getInventoryFolders(ref folders, folders[i].ID);
  617. return folders;
  618. }
  619. /// <summary>
  620. /// Delete a folder in inventory databasae
  621. /// </summary>
  622. /// <param name="folderID">the folder UUID</param>
  623. protected void deleteOneFolder(LLUUID folderID)
  624. {
  625. try
  626. {
  627. Dictionary<string, string> param = new Dictionary<string, string>();
  628. param["folderID"] = folderID.ToString();
  629. using (IDbCommand cmd = database.Query("DELETE FROM inventoryfolders WHERE folderID=@folderID", param))
  630. {
  631. cmd.ExecuteNonQuery();
  632. }
  633. }
  634. catch (SqlException e)
  635. {
  636. m_log.Error(e.ToString());
  637. }
  638. }
  639. /// <summary>
  640. /// Delete an item in inventory database
  641. /// </summary>
  642. /// <param name="folderID">the item ID</param>
  643. protected void deleteItemsInFolder(LLUUID folderID)
  644. {
  645. try
  646. {
  647. Dictionary<string, string> param = new Dictionary<string, string>();
  648. param["parentFolderID"] = folderID.ToString();
  649. using (IDbCommand cmd =
  650. database.Query("DELETE FROM inventoryitems WHERE parentFolderID=@parentFolderID", param))
  651. {
  652. cmd.ExecuteNonQuery();
  653. }
  654. }
  655. catch (SqlException e)
  656. {
  657. m_log.Error(e.ToString());
  658. }
  659. }
  660. /// <summary>
  661. /// Delete an inventory folder
  662. /// </summary>
  663. /// <param name="folderId">Id of folder to delete</param>
  664. public void deleteInventoryFolder(LLUUID folderID)
  665. {
  666. // lock (database)
  667. {
  668. List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID);
  669. //Delete all sub-folders
  670. foreach (InventoryFolderBase f in subFolders)
  671. {
  672. deleteOneFolder(f.ID);
  673. deleteItemsInFolder(f.ID);
  674. }
  675. //Delete the actual row
  676. deleteOneFolder(folderID);
  677. deleteItemsInFolder(folderID);
  678. }
  679. }
  680. }
  681. }