SQLiteInventoryStore.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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.Reflection;
  31. using OpenMetaverse;
  32. using log4net;
  33. using Mono.Data.SqliteClient;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Data.SQLite
  36. {
  37. /// <summary>
  38. /// An Inventory Interface to the SQLite database
  39. /// </summary>
  40. public class SQLiteInventoryStore : SQLiteUtil, IInventoryDataPlugin
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private const string invItemsSelect = "select * from inventoryitems";
  44. private const string invFoldersSelect = "select * from inventoryfolders";
  45. private SqliteConnection conn;
  46. private DataSet ds;
  47. private SqliteDataAdapter invItemsDa;
  48. private SqliteDataAdapter invFoldersDa;
  49. public void Initialise()
  50. {
  51. m_log.Info("[SQLiteInventoryData]: " + Name + " cannot be default-initialized!");
  52. throw new PluginNotInitialisedException (Name);
  53. }
  54. /// <summary>
  55. /// <list type="bullet">
  56. /// <item>Initialises Inventory interface</item>
  57. /// <item>Loads and initialises a new SQLite connection and maintains it.</item>
  58. /// <item>use default URI if connect string string is empty.</item>
  59. /// </list>
  60. /// </summary>
  61. /// <param name="dbconnect">connect string</param>
  62. public void Initialise(string dbconnect)
  63. {
  64. if (dbconnect == string.Empty)
  65. {
  66. dbconnect = "URI=file:inventoryStore.db,version=3";
  67. }
  68. m_log.Info("[INVENTORY DB]: Sqlite - connecting: " + dbconnect);
  69. conn = new SqliteConnection(dbconnect);
  70. conn.Open();
  71. Assembly assem = GetType().Assembly;
  72. Migration m = new Migration(conn, assem, "InventoryStore");
  73. m.Update();
  74. SqliteCommand itemsSelectCmd = new SqliteCommand(invItemsSelect, conn);
  75. invItemsDa = new SqliteDataAdapter(itemsSelectCmd);
  76. // SqliteCommandBuilder primCb = new SqliteCommandBuilder(primDa);
  77. SqliteCommand foldersSelectCmd = new SqliteCommand(invFoldersSelect, conn);
  78. invFoldersDa = new SqliteDataAdapter(foldersSelectCmd);
  79. ds = new DataSet();
  80. ds.Tables.Add(createInventoryFoldersTable());
  81. invFoldersDa.Fill(ds.Tables["inventoryfolders"]);
  82. setupFoldersCommands(invFoldersDa, conn);
  83. m_log.Info("[INVENTORY DB]: Populated Inventory Folders Definitions");
  84. ds.Tables.Add(createInventoryItemsTable());
  85. invItemsDa.Fill(ds.Tables["inventoryitems"]);
  86. setupItemsCommands(invItemsDa, conn);
  87. m_log.Info("[INVENTORY DB]: Populated Inventory Items Definitions");
  88. ds.AcceptChanges();
  89. }
  90. /// <summary>
  91. /// Closes the inventory interface
  92. /// </summary>
  93. public void Dispose()
  94. {
  95. if (conn != null)
  96. {
  97. conn.Close();
  98. conn = null;
  99. }
  100. if (invItemsDa != null)
  101. {
  102. invItemsDa.Dispose();
  103. invItemsDa = null;
  104. }
  105. if (invFoldersDa != null)
  106. {
  107. invFoldersDa.Dispose();
  108. invFoldersDa = null;
  109. }
  110. if (ds != null)
  111. {
  112. ds.Dispose();
  113. ds = null;
  114. }
  115. }
  116. /// <summary>
  117. ///
  118. /// </summary>
  119. /// <param name="row"></param>
  120. /// <returns></returns>
  121. public InventoryItemBase buildItem(DataRow row)
  122. {
  123. InventoryItemBase item = new InventoryItemBase();
  124. item.ID = new UUID((string) row["UUID"]);
  125. item.AssetID = new UUID((string) row["assetID"]);
  126. item.AssetType = Convert.ToInt32(row["assetType"]);
  127. item.InvType = Convert.ToInt32(row["invType"]);
  128. item.Folder = new UUID((string) row["parentFolderID"]);
  129. item.Owner = new UUID((string) row["avatarID"]);
  130. item.Creator = new UUID((string) row["creatorsID"]);
  131. item.Name = (string) row["inventoryName"];
  132. item.Description = (string) row["inventoryDescription"];
  133. item.NextPermissions = Convert.ToUInt32(row["inventoryNextPermissions"]);
  134. item.CurrentPermissions = Convert.ToUInt32(row["inventoryCurrentPermissions"]);
  135. item.BasePermissions = Convert.ToUInt32(row["inventoryBasePermissions"]);
  136. item.EveryOnePermissions = Convert.ToUInt32(row["inventoryEveryOnePermissions"]);
  137. item.GroupPermissions = Convert.ToUInt32(row["inventoryGroupPermissions"]);
  138. // new fields
  139. if (!Convert.IsDBNull(row["salePrice"]))
  140. item.SalePrice = Convert.ToInt32(row["salePrice"]);
  141. if (!Convert.IsDBNull(row["saleType"]))
  142. item.SaleType = Convert.ToByte(row["saleType"]);
  143. if (!Convert.IsDBNull(row["creationDate"]))
  144. item.CreationDate = Convert.ToInt32(row["creationDate"]);
  145. if (!Convert.IsDBNull(row["groupID"]))
  146. item.GroupID = new UUID((string)row["groupID"]);
  147. if (!Convert.IsDBNull(row["groupOwned"]))
  148. item.GroupOwned = Convert.ToBoolean(row["groupOwned"]);
  149. if (!Convert.IsDBNull(row["Flags"]))
  150. item.Flags = Convert.ToUInt32(row["Flags"]);
  151. return item;
  152. }
  153. /// <summary>
  154. ///
  155. /// </summary>
  156. /// <param name="row"></param>
  157. /// <param name="item"></param>
  158. private static void fillItemRow(DataRow row, InventoryItemBase item)
  159. {
  160. row["UUID"] = Util.ToRawUuidString(item.ID);
  161. row["assetID"] = Util.ToRawUuidString(item.AssetID);
  162. row["assetType"] = item.AssetType;
  163. row["invType"] = item.InvType;
  164. row["parentFolderID"] = Util.ToRawUuidString(item.Folder);
  165. row["avatarID"] = Util.ToRawUuidString(item.Owner);
  166. row["creatorsID"] = Util.ToRawUuidString(item.Creator);
  167. row["inventoryName"] = item.Name;
  168. row["inventoryDescription"] = item.Description;
  169. row["inventoryNextPermissions"] = item.NextPermissions;
  170. row["inventoryCurrentPermissions"] = item.CurrentPermissions;
  171. row["inventoryBasePermissions"] = item.BasePermissions;
  172. row["inventoryEveryOnePermissions"] = item.EveryOnePermissions;
  173. row["inventoryGroupPermissions"] = item.GroupPermissions;
  174. // new fields
  175. row["salePrice"] = item.SalePrice;
  176. row["saleType"] = item.SaleType;
  177. row["creationDate"] = item.CreationDate;
  178. row["groupID"] = item.GroupID;
  179. row["groupOwned"] = item.GroupOwned;
  180. row["flags"] = item.Flags;
  181. }
  182. /// <summary>
  183. /// Add inventory folder
  184. /// </summary>
  185. /// <param name="folder">Folder base</param>
  186. /// <param name="add">true=create folder. false=update existing folder</param>
  187. /// <remarks>nasty</remarks>
  188. private void addFolder(InventoryFolderBase folder, bool add)
  189. {
  190. lock (ds)
  191. {
  192. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  193. DataRow inventoryRow = inventoryFolderTable.Rows.Find(Util.ToRawUuidString(folder.ID));
  194. if (inventoryRow == null)
  195. {
  196. if (! add)
  197. m_log.ErrorFormat("Interface Misuse: Attempting to Update non-existant inventory folder: {0}", folder.ID);
  198. inventoryRow = inventoryFolderTable.NewRow();
  199. fillFolderRow(inventoryRow, folder);
  200. inventoryFolderTable.Rows.Add(inventoryRow);
  201. }
  202. else
  203. {
  204. if (add)
  205. m_log.ErrorFormat("Interface Misuse: Attempting to Add inventory folder that already exists: {0}", folder.ID);
  206. fillFolderRow(inventoryRow, folder);
  207. }
  208. invFoldersDa.Update(ds, "inventoryfolders");
  209. }
  210. }
  211. /// <summary>
  212. /// Move an inventory folder
  213. /// </summary>
  214. /// <param name="folder">folder base</param>
  215. private void moveFolder(InventoryFolderBase folder)
  216. {
  217. lock (ds)
  218. {
  219. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  220. DataRow inventoryRow = inventoryFolderTable.Rows.Find(Util.ToRawUuidString(folder.ID));
  221. if (inventoryRow == null)
  222. {
  223. inventoryRow = inventoryFolderTable.NewRow();
  224. fillFolderRow(inventoryRow, folder);
  225. inventoryFolderTable.Rows.Add(inventoryRow);
  226. }
  227. else
  228. {
  229. moveFolderRow(inventoryRow, folder);
  230. }
  231. invFoldersDa.Update(ds, "inventoryfolders");
  232. }
  233. }
  234. /// <summary>
  235. /// add an item in inventory
  236. /// </summary>
  237. /// <param name="item">the item</param>
  238. /// <param name="add">true=add item ; false=update existing item</param>
  239. private void addItem(InventoryItemBase item, bool add)
  240. {
  241. lock (ds)
  242. {
  243. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  244. DataRow inventoryRow = inventoryItemTable.Rows.Find(Util.ToRawUuidString(item.ID));
  245. if (inventoryRow == null)
  246. {
  247. if (! add)
  248. m_log.ErrorFormat("[INVENTORY DB]: Interface Misuse: Attempting to Update non-existant inventory item: {0}", item.ID);
  249. inventoryRow = inventoryItemTable.NewRow();
  250. fillItemRow(inventoryRow, item);
  251. inventoryItemTable.Rows.Add(inventoryRow);
  252. }
  253. else
  254. {
  255. if (add)
  256. m_log.ErrorFormat("[INVENTORY DB]: Interface Misuse: Attempting to Add inventory item that already exists: {0}", item.ID);
  257. fillItemRow(inventoryRow, item);
  258. }
  259. invItemsDa.Update(ds, "inventoryitems");
  260. }
  261. }
  262. /// <summary>
  263. /// TODO : DataSet commit
  264. /// </summary>
  265. public void Shutdown()
  266. {
  267. // TODO: DataSet commit
  268. }
  269. /// <summary>
  270. /// The name of this DB provider
  271. /// </summary>
  272. /// <returns>Name of DB provider</returns>
  273. public string Name
  274. {
  275. get { return "SQLite Inventory Data Interface"; }
  276. }
  277. /// <summary>
  278. /// Returns the version of this DB provider
  279. /// </summary>
  280. /// <returns>A string containing the DB provider version</returns>
  281. public string Version
  282. {
  283. get
  284. {
  285. Module module = GetType().Module;
  286. // string dllName = module.Assembly.ManifestModule.Name;
  287. Version dllVersion = module.Assembly.GetName().Version;
  288. return
  289. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  290. dllVersion.Revision);
  291. }
  292. }
  293. /// <summary>
  294. /// Returns a list of inventory items contained within the specified folder
  295. /// </summary>
  296. /// <param name="folderID">The UUID of the target folder</param>
  297. /// <returns>A List of InventoryItemBase items</returns>
  298. public List<InventoryItemBase> getInventoryInFolder(UUID folderID)
  299. {
  300. lock (ds)
  301. {
  302. List<InventoryItemBase> retval = new List<InventoryItemBase>();
  303. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  304. string selectExp = "parentFolderID = '" + Util.ToRawUuidString(folderID) + "'";
  305. DataRow[] rows = inventoryItemTable.Select(selectExp);
  306. foreach (DataRow row in rows)
  307. {
  308. retval.Add(buildItem(row));
  309. }
  310. return retval;
  311. }
  312. }
  313. /// <summary>
  314. /// Returns a list of the root folders within a users inventory
  315. /// </summary>
  316. /// <param name="user">The user whos inventory is to be searched</param>
  317. /// <returns>A list of folder objects</returns>
  318. public List<InventoryFolderBase> getUserRootFolders(UUID user)
  319. {
  320. return new List<InventoryFolderBase>();
  321. }
  322. // see InventoryItemBase.getUserRootFolder
  323. public InventoryFolderBase getUserRootFolder(UUID user)
  324. {
  325. lock (ds)
  326. {
  327. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  328. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  329. string selectExp = "agentID = '" + Util.ToRawUuidString(user) + "' AND parentID = '" +
  330. Util.ToRawUuidString(UUID.Zero) + "'";
  331. DataRow[] rows = inventoryFolderTable.Select(selectExp);
  332. foreach (DataRow row in rows)
  333. {
  334. folders.Add(buildFolder(row));
  335. }
  336. // There should only ever be one root folder for a user. However, if there's more
  337. // than one we'll simply use the first one rather than failing. It would be even
  338. // nicer to print some message to this effect, but this feels like it's too low a
  339. // to put such a message out, and it's too minor right now to spare the time to
  340. // suitably refactor.
  341. if (folders.Count > 0)
  342. {
  343. return folders[0];
  344. }
  345. return null;
  346. }
  347. }
  348. /// <summary>
  349. /// Append a list of all the child folders of a parent folder
  350. /// </summary>
  351. /// <param name="folders">list where folders will be appended</param>
  352. /// <param name="parentID">ID of parent</param>
  353. protected void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
  354. {
  355. lock (ds)
  356. {
  357. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  358. string selectExp = "parentID = '" + Util.ToRawUuidString(parentID) + "'";
  359. DataRow[] rows = inventoryFolderTable.Select(selectExp);
  360. foreach (DataRow row in rows)
  361. {
  362. folders.Add(buildFolder(row));
  363. }
  364. }
  365. }
  366. /// <summary>
  367. /// Returns a list of inventory folders contained in the folder 'parentID'
  368. /// </summary>
  369. /// <param name="parentID">The folder to get subfolders for</param>
  370. /// <returns>A list of inventory folders</returns>
  371. public List<InventoryFolderBase> getInventoryFolders(UUID parentID)
  372. {
  373. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  374. getInventoryFolders(ref folders, parentID);
  375. return folders;
  376. }
  377. /// <summary>
  378. /// See IInventoryDataPlugin
  379. /// </summary>
  380. /// <param name="parentID"></param>
  381. /// <returns></returns>
  382. public List<InventoryFolderBase> getFolderHierarchy(UUID parentID)
  383. {
  384. /* Note: There are subtle changes between this implementation of getFolderHierarchy and the previous one
  385. * - We will only need to hit the database twice instead of n times.
  386. * - We assume the database is well-formed - no stranded/dangling folders, all folders in heirarchy owned
  387. * by the same person, each user only has 1 inventory heirarchy
  388. * - The returned list is not ordered, instead of breadth-first ordered
  389. There are basically 2 usage cases for getFolderHeirarchy:
  390. 1) Getting the user's entire inventory heirarchy when they log in
  391. 2) Finding a subfolder heirarchy to delete when emptying the trash.
  392. This implementation will pull all inventory folders from the database, and then prune away any folder that
  393. is not part of the requested sub-heirarchy. The theory is that it is cheaper to make 1 request from the
  394. database than to make n requests. This pays off only if requested heirarchy is large.
  395. By making this choice, we are making the worst case better at the cost of making the best case worse
  396. - Francis
  397. */
  398. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  399. DataRow[] folderRows = null, parentRow;
  400. InventoryFolderBase parentFolder = null;
  401. lock (ds)
  402. {
  403. /* Fetch the parent folder from the database to determine the agent ID.
  404. * Then fetch all inventory folders for that agent from the agent ID.
  405. */
  406. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  407. string selectExp = "UUID = '" + Util.ToRawUuidString(parentID) + "'";
  408. parentRow = inventoryFolderTable.Select(selectExp); // Assume at most 1 result
  409. if (parentRow.GetLength(0) >= 1) // No result means parent folder does not exist
  410. {
  411. parentFolder = buildFolder(parentRow[0]);
  412. UUID agentID = parentFolder.Owner;
  413. selectExp = "agentID = '" + Util.ToRawUuidString(agentID) + "'";
  414. folderRows = inventoryFolderTable.Select(selectExp);
  415. }
  416. if (folderRows != null && folderRows.GetLength(0) >= 1) // No result means parent folder does not exist
  417. { // or has no children
  418. /* if we're querying the root folder, just return an unordered list of all folders in the user's
  419. * inventory
  420. */
  421. if (parentFolder.ParentID == UUID.Zero)
  422. {
  423. foreach (DataRow row in folderRows)
  424. {
  425. InventoryFolderBase curFolder = buildFolder(row);
  426. if (curFolder.ID != parentID) // Return all folders except the parent folder of heirarchy
  427. folders.Add(buildFolder(row));
  428. }
  429. } // If requesting root folder
  430. /* else we are querying a non-root folder. We currently have a list of all of the user's folders,
  431. * we must construct a list of all folders in the heirarchy below parentID.
  432. * Our first step will be to construct a hash table of all folders, indexed by parent ID.
  433. * Once we have constructed the hash table, we will do a breadth-first traversal on the tree using the
  434. * hash table to find child folders.
  435. */
  436. else
  437. { // Querying a non-root folder
  438. // Build a hash table of all user's inventory folders, indexed by each folder's parent ID
  439. Dictionary<UUID, List<InventoryFolderBase>> hashtable =
  440. new Dictionary<UUID, List<InventoryFolderBase>>(folderRows.GetLength(0));
  441. foreach (DataRow row in folderRows)
  442. {
  443. InventoryFolderBase curFolder = buildFolder(row);
  444. if (curFolder.ParentID != UUID.Zero) // Discard root of tree - not needed
  445. {
  446. if (hashtable.ContainsKey(curFolder.ParentID))
  447. {
  448. // Current folder already has a sibling - append to sibling list
  449. hashtable[curFolder.ParentID].Add(curFolder);
  450. }
  451. else
  452. {
  453. List<InventoryFolderBase> siblingList = new List<InventoryFolderBase>();
  454. siblingList.Add(curFolder);
  455. // Current folder has no known (yet) siblings
  456. hashtable.Add(curFolder.ParentID, siblingList);
  457. }
  458. }
  459. } // For all inventory folders
  460. // Note: Could release the ds lock here - we don't access folderRows or the database anymore.
  461. // This is somewhat of a moot point as the callers of this function usually lock db anyways.
  462. if (hashtable.ContainsKey(parentID)) // if requested folder does have children
  463. folders.AddRange(hashtable[parentID]);
  464. // BreadthFirstSearch build inventory tree **Note: folders.Count is *not* static
  465. for (int i = 0; i < folders.Count; i++)
  466. if (hashtable.ContainsKey(folders[i].ID))
  467. folders.AddRange(hashtable[folders[i].ID]);
  468. } // if requesting a subfolder heirarchy
  469. } // if folder parentID exists and has children
  470. } // lock ds
  471. return folders;
  472. }
  473. /// <summary>
  474. /// Returns an inventory item by its UUID
  475. /// </summary>
  476. /// <param name="item">The UUID of the item to be returned</param>
  477. /// <returns>A class containing item information</returns>
  478. public InventoryItemBase getInventoryItem(UUID item)
  479. {
  480. lock (ds)
  481. {
  482. DataRow row = ds.Tables["inventoryitems"].Rows.Find(Util.ToRawUuidString(item));
  483. if (row != null)
  484. {
  485. return buildItem(row);
  486. }
  487. else
  488. {
  489. return null;
  490. }
  491. }
  492. }
  493. /// <summary>
  494. /// Returns a specified inventory folder by its UUID
  495. /// </summary>
  496. /// <param name="folder">The UUID of the folder to be returned</param>
  497. /// <returns>A class containing folder information</returns>
  498. public InventoryFolderBase getInventoryFolder(UUID folder)
  499. {
  500. // TODO: Deep voodoo here. If you enable this code then
  501. // multi region breaks. No idea why, but I figured it was
  502. // better to leave multi region at this point. It does mean
  503. // that you don't get to see system textures why creating
  504. // clothes and the like. :(
  505. lock (ds)
  506. {
  507. DataRow row = ds.Tables["inventoryfolders"].Rows.Find(Util.ToRawUuidString(folder));
  508. if (row != null)
  509. {
  510. return buildFolder(row);
  511. }
  512. else
  513. {
  514. return null;
  515. }
  516. }
  517. }
  518. /// <summary>
  519. /// Creates a new inventory item based on item
  520. /// </summary>
  521. /// <param name="item">The item to be created</param>
  522. public void addInventoryItem(InventoryItemBase item)
  523. {
  524. addItem(item, true);
  525. }
  526. /// <summary>
  527. /// Updates an inventory item with item (updates based on ID)
  528. /// </summary>
  529. /// <param name="item">The updated item</param>
  530. public void updateInventoryItem(InventoryItemBase item)
  531. {
  532. addItem(item, false);
  533. }
  534. /// <summary>
  535. /// Delete an inventory item
  536. /// </summary>
  537. /// <param name="item">The item UUID</param>
  538. public void deleteInventoryItem(UUID itemID)
  539. {
  540. lock (ds)
  541. {
  542. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  543. DataRow inventoryRow = inventoryItemTable.Rows.Find(Util.ToRawUuidString(itemID));
  544. if (inventoryRow != null)
  545. {
  546. inventoryRow.Delete();
  547. }
  548. invItemsDa.Update(ds, "inventoryitems");
  549. }
  550. }
  551. /// <summary>
  552. /// Delete all items in the specified folder
  553. /// </summary>
  554. /// <param name="folderId">id of the folder, whose item content should be deleted</param>
  555. /// <todo>this is horribly inefficient, but I don't want to ruin the overall structure of this implementation</todo>
  556. private void deleteItemsInFolder(UUID folderId)
  557. {
  558. List<InventoryItemBase> items = getInventoryInFolder(folderId);
  559. foreach (InventoryItemBase i in items)
  560. deleteInventoryItem(i.ID);
  561. }
  562. /// <summary>
  563. /// Adds a new folder specified by folder
  564. /// </summary>
  565. /// <param name="folder">The inventory folder</param>
  566. public void addInventoryFolder(InventoryFolderBase folder)
  567. {
  568. addFolder(folder, true);
  569. }
  570. /// <summary>
  571. /// Updates a folder based on its ID with folder
  572. /// </summary>
  573. /// <param name="folder">The inventory folder</param>
  574. public void updateInventoryFolder(InventoryFolderBase folder)
  575. {
  576. addFolder(folder, false);
  577. }
  578. /// <summary>
  579. /// Moves a folder based on its ID with folder
  580. /// </summary>
  581. /// <param name="folder">The inventory folder</param>
  582. public void moveInventoryFolder(InventoryFolderBase folder)
  583. {
  584. moveFolder(folder);
  585. }
  586. /// <summary>
  587. /// Delete a folder
  588. /// </summary>
  589. /// <remarks>
  590. /// This will clean-up any child folders and child items as well
  591. /// </remarks>
  592. /// <param name="folderID">the folder UUID</param>
  593. public void deleteInventoryFolder(UUID folderID)
  594. {
  595. lock (ds)
  596. {
  597. List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID);
  598. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  599. DataRow inventoryRow;
  600. //Delete all sub-folders
  601. foreach (InventoryFolderBase f in subFolders)
  602. {
  603. inventoryRow = inventoryFolderTable.Rows.Find(Util.ToRawUuidString(f.ID));
  604. if (inventoryRow != null)
  605. {
  606. deleteItemsInFolder(f.ID);
  607. inventoryRow.Delete();
  608. }
  609. }
  610. //Delete the actual row
  611. inventoryRow = inventoryFolderTable.Rows.Find(Util.ToRawUuidString(folderID));
  612. if (inventoryRow != null)
  613. {
  614. deleteItemsInFolder(folderID);
  615. inventoryRow.Delete();
  616. }
  617. invFoldersDa.Update(ds, "inventoryfolders");
  618. }
  619. }
  620. /***********************************************************************
  621. *
  622. * Data Table definitions
  623. *
  624. **********************************************************************/
  625. /// <summary>
  626. /// Create the "inventoryitems" table
  627. /// </summary>
  628. private static DataTable createInventoryItemsTable()
  629. {
  630. DataTable inv = new DataTable("inventoryitems");
  631. createCol(inv, "UUID", typeof (String)); //inventoryID
  632. createCol(inv, "assetID", typeof (String));
  633. createCol(inv, "assetType", typeof (Int32));
  634. createCol(inv, "invType", typeof (Int32));
  635. createCol(inv, "parentFolderID", typeof (String));
  636. createCol(inv, "avatarID", typeof (String));
  637. createCol(inv, "creatorsID", typeof (String));
  638. createCol(inv, "inventoryName", typeof (String));
  639. createCol(inv, "inventoryDescription", typeof (String));
  640. // permissions
  641. createCol(inv, "inventoryNextPermissions", typeof (Int32));
  642. createCol(inv, "inventoryCurrentPermissions", typeof (Int32));
  643. createCol(inv, "inventoryBasePermissions", typeof (Int32));
  644. createCol(inv, "inventoryEveryOnePermissions", typeof (Int32));
  645. createCol(inv, "inventoryGroupPermissions", typeof (Int32));
  646. // sale info
  647. createCol(inv, "salePrice", typeof(Int32));
  648. createCol(inv, "saleType", typeof(Byte));
  649. // creation date
  650. createCol(inv, "creationDate", typeof(Int32));
  651. // group info
  652. createCol(inv, "groupID", typeof(String));
  653. createCol(inv, "groupOwned", typeof(Boolean));
  654. // Flags
  655. createCol(inv, "flags", typeof(UInt32));
  656. inv.PrimaryKey = new DataColumn[] { inv.Columns["UUID"] };
  657. return inv;
  658. }
  659. /// <summary>
  660. /// Creates the "inventoryfolders" table
  661. /// </summary>
  662. /// <returns></returns>
  663. private static DataTable createInventoryFoldersTable()
  664. {
  665. DataTable fol = new DataTable("inventoryfolders");
  666. createCol(fol, "UUID", typeof (String)); //folderID
  667. createCol(fol, "name", typeof (String));
  668. createCol(fol, "agentID", typeof (String));
  669. createCol(fol, "parentID", typeof (String));
  670. createCol(fol, "type", typeof (Int32));
  671. createCol(fol, "version", typeof (Int32));
  672. fol.PrimaryKey = new DataColumn[] {fol.Columns["UUID"]};
  673. return fol;
  674. }
  675. /// <summary>
  676. ///
  677. /// </summary>
  678. /// <param name="da"></param>
  679. /// <param name="conn"></param>
  680. private void setupItemsCommands(SqliteDataAdapter da, SqliteConnection conn)
  681. {
  682. lock (ds)
  683. {
  684. da.InsertCommand = createInsertCommand("inventoryitems", ds.Tables["inventoryitems"]);
  685. da.InsertCommand.Connection = conn;
  686. da.UpdateCommand = createUpdateCommand("inventoryitems", "UUID=:UUID", ds.Tables["inventoryitems"]);
  687. da.UpdateCommand.Connection = conn;
  688. SqliteCommand delete = new SqliteCommand("delete from inventoryitems where UUID = :UUID");
  689. delete.Parameters.Add(createSqliteParameter("UUID", typeof(String)));
  690. delete.Connection = conn;
  691. da.DeleteCommand = delete;
  692. }
  693. }
  694. /// <summary>
  695. ///
  696. /// </summary>
  697. /// <param name="da"></param>
  698. /// <param name="conn"></param>
  699. private void setupFoldersCommands(SqliteDataAdapter da, SqliteConnection conn)
  700. {
  701. lock (ds)
  702. {
  703. da.InsertCommand = createInsertCommand("inventoryfolders", ds.Tables["inventoryfolders"]);
  704. da.InsertCommand.Connection = conn;
  705. da.UpdateCommand = createUpdateCommand("inventoryfolders", "UUID=:UUID", ds.Tables["inventoryfolders"]);
  706. da.UpdateCommand.Connection = conn;
  707. SqliteCommand delete = new SqliteCommand("delete from inventoryfolders where UUID = :UUID");
  708. delete.Parameters.Add(createSqliteParameter("UUID", typeof(String)));
  709. delete.Connection = conn;
  710. da.DeleteCommand = delete;
  711. }
  712. }
  713. /// <summary>
  714. ///
  715. /// </summary>
  716. /// <param name="row"></param>
  717. /// <returns></returns>
  718. private static InventoryFolderBase buildFolder(DataRow row)
  719. {
  720. InventoryFolderBase folder = new InventoryFolderBase();
  721. folder.ID = new UUID((string) row["UUID"]);
  722. folder.Name = (string) row["name"];
  723. folder.Owner = new UUID((string) row["agentID"]);
  724. folder.ParentID = new UUID((string) row["parentID"]);
  725. folder.Type = Convert.ToInt16(row["type"]);
  726. folder.Version = Convert.ToUInt16(row["version"]);
  727. return folder;
  728. }
  729. /// <summary>
  730. ///
  731. /// </summary>
  732. /// <param name="row"></param>
  733. /// <param name="folder"></param>
  734. private static void fillFolderRow(DataRow row, InventoryFolderBase folder)
  735. {
  736. row["UUID"] = Util.ToRawUuidString(folder.ID);
  737. row["name"] = folder.Name;
  738. row["agentID"] = Util.ToRawUuidString(folder.Owner);
  739. row["parentID"] = Util.ToRawUuidString(folder.ParentID);
  740. row["type"] = folder.Type;
  741. row["version"] = folder.Version;
  742. }
  743. /// <summary>
  744. ///
  745. /// </summary>
  746. /// <param name="row"></param>
  747. /// <param name="folder"></param>
  748. private static void moveFolderRow(DataRow row, InventoryFolderBase folder)
  749. {
  750. row["UUID"] = Util.ToRawUuidString(folder.ID);
  751. row["parentID"] = Util.ToRawUuidString(folder.ParentID);
  752. }
  753. public List<InventoryItemBase> fetchActiveGestures (UUID avatarID)
  754. {
  755. lock (ds)
  756. {
  757. List<InventoryItemBase> items = new List<InventoryItemBase>();
  758. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  759. string selectExp = "avatarID = '" + Util.ToRawUuidString(avatarID) + "' AND assetType = " +
  760. (int)AssetType.Gesture + " AND flags = 1";
  761. m_log.DebugFormat("[SQL]: sql = " + selectExp);
  762. DataRow[] rows = inventoryItemTable.Select(selectExp);
  763. foreach (DataRow row in rows)
  764. {
  765. items.Add(buildItem(row));
  766. }
  767. return items;
  768. }
  769. }
  770. }
  771. }