SQLiteInventoryStore.cs 36 KB

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