SQLiteInventoryStore.cs 35 KB

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