SQLiteInventoryStore.cs 36 KB

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