SQLiteInventoryStore.cs 36 KB

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