1
0

SQLiteInventoryStore.cs 36 KB

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