SQLiteInventoryStore.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. if (inventoryRow != null) //MySQL doesn't throw an exception here, so sqlite shouldn't either.
  263. inventoryRow["version"] = (int)inventoryRow["version"] + 1;
  264. invFoldersDa.Update(ds, "inventoryfolders");
  265. }
  266. }
  267. /// <summary>
  268. /// TODO : DataSet commit
  269. /// </summary>
  270. public void Shutdown()
  271. {
  272. // TODO: DataSet commit
  273. }
  274. /// <summary>
  275. /// The name of this DB provider
  276. /// </summary>
  277. /// <returns>Name of DB provider</returns>
  278. public string Name
  279. {
  280. get { return "SQLite Inventory Data Interface"; }
  281. }
  282. /// <summary>
  283. /// Returns the version of this DB provider
  284. /// </summary>
  285. /// <returns>A string containing the DB provider version</returns>
  286. public string Version
  287. {
  288. get
  289. {
  290. Module module = GetType().Module;
  291. // string dllName = module.Assembly.ManifestModule.Name;
  292. Version dllVersion = module.Assembly.GetName().Version;
  293. return
  294. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  295. dllVersion.Revision);
  296. }
  297. }
  298. /// <summary>
  299. /// Returns a list of inventory items contained within the specified folder
  300. /// </summary>
  301. /// <param name="folderID">The UUID of the target folder</param>
  302. /// <returns>A List of InventoryItemBase items</returns>
  303. public List<InventoryItemBase> getInventoryInFolder(UUID folderID)
  304. {
  305. lock (ds)
  306. {
  307. List<InventoryItemBase> retval = new List<InventoryItemBase>();
  308. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  309. string selectExp = "parentFolderID = '" + folderID + "'";
  310. DataRow[] rows = inventoryItemTable.Select(selectExp);
  311. foreach (DataRow row in rows)
  312. {
  313. retval.Add(buildItem(row));
  314. }
  315. return retval;
  316. }
  317. }
  318. /// <summary>
  319. /// Returns a list of the root folders within a users inventory
  320. /// </summary>
  321. /// <param name="user">The user whos inventory is to be searched</param>
  322. /// <returns>A list of folder objects</returns>
  323. public List<InventoryFolderBase> getUserRootFolders(UUID user)
  324. {
  325. return new List<InventoryFolderBase>();
  326. }
  327. // see InventoryItemBase.getUserRootFolder
  328. public InventoryFolderBase getUserRootFolder(UUID user)
  329. {
  330. lock (ds)
  331. {
  332. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  333. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  334. string selectExp = "agentID = '" + user + "' AND parentID = '" + UUID.Zero + "'";
  335. DataRow[] rows = inventoryFolderTable.Select(selectExp);
  336. foreach (DataRow row in rows)
  337. {
  338. folders.Add(buildFolder(row));
  339. }
  340. // There should only ever be one root folder for a user. However, if there's more
  341. // than one we'll simply use the first one rather than failing. It would be even
  342. // nicer to print some message to this effect, but this feels like it's too low a
  343. // to put such a message out, and it's too minor right now to spare the time to
  344. // suitably refactor.
  345. if (folders.Count > 0)
  346. {
  347. return folders[0];
  348. }
  349. return null;
  350. }
  351. }
  352. /// <summary>
  353. /// Append a list of all the child folders of a parent folder
  354. /// </summary>
  355. /// <param name="folders">list where folders will be appended</param>
  356. /// <param name="parentID">ID of parent</param>
  357. protected void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
  358. {
  359. lock (ds)
  360. {
  361. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  362. string selectExp = "parentID = '" + parentID + "'";
  363. DataRow[] rows = inventoryFolderTable.Select(selectExp);
  364. foreach (DataRow row in rows)
  365. {
  366. folders.Add(buildFolder(row));
  367. }
  368. }
  369. }
  370. /// <summary>
  371. /// Returns a list of inventory folders contained in the folder 'parentID'
  372. /// </summary>
  373. /// <param name="parentID">The folder to get subfolders for</param>
  374. /// <returns>A list of inventory folders</returns>
  375. public List<InventoryFolderBase> getInventoryFolders(UUID parentID)
  376. {
  377. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  378. getInventoryFolders(ref folders, parentID);
  379. return folders;
  380. }
  381. /// <summary>
  382. /// See IInventoryDataPlugin
  383. /// </summary>
  384. /// <param name="parentID"></param>
  385. /// <returns></returns>
  386. public List<InventoryFolderBase> getFolderHierarchy(UUID parentID)
  387. {
  388. /* Note: There are subtle changes between this implementation of getFolderHierarchy and the previous one
  389. * - We will only need to hit the database twice instead of n times.
  390. * - We assume the database is well-formed - no stranded/dangling folders, all folders in heirarchy owned
  391. * by the same person, each user only has 1 inventory heirarchy
  392. * - The returned list is not ordered, instead of breadth-first ordered
  393. There are basically 2 usage cases for getFolderHeirarchy:
  394. 1) Getting the user's entire inventory heirarchy when they log in
  395. 2) Finding a subfolder heirarchy to delete when emptying the trash.
  396. This implementation will pull all inventory folders from the database, and then prune away any folder that
  397. is not part of the requested sub-heirarchy. The theory is that it is cheaper to make 1 request from the
  398. database than to make n requests. This pays off only if requested heirarchy is large.
  399. By making this choice, we are making the worst case better at the cost of making the best case worse
  400. - Francis
  401. */
  402. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  403. DataRow[] folderRows = null, parentRow;
  404. InventoryFolderBase parentFolder = null;
  405. lock (ds)
  406. {
  407. /* Fetch the parent folder from the database to determine the agent ID.
  408. * Then fetch all inventory folders for that agent from the agent ID.
  409. */
  410. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  411. string selectExp = "UUID = '" + parentID + "'";
  412. parentRow = inventoryFolderTable.Select(selectExp); // Assume at most 1 result
  413. if (parentRow.GetLength(0) >= 1) // No result means parent folder does not exist
  414. {
  415. parentFolder = buildFolder(parentRow[0]);
  416. UUID agentID = parentFolder.Owner;
  417. selectExp = "agentID = '" + agentID + "'";
  418. folderRows = inventoryFolderTable.Select(selectExp);
  419. }
  420. if (folderRows != null && folderRows.GetLength(0) >= 1) // No result means parent folder does not exist
  421. { // or has no children
  422. /* if we're querying the root folder, just return an unordered list of all folders in the user's
  423. * inventory
  424. */
  425. if (parentFolder.ParentID == UUID.Zero)
  426. {
  427. foreach (DataRow row in folderRows)
  428. {
  429. InventoryFolderBase curFolder = buildFolder(row);
  430. if (curFolder.ID != parentID) // Return all folders except the parent folder of heirarchy
  431. folders.Add(buildFolder(row));
  432. }
  433. } // If requesting root folder
  434. /* else we are querying a non-root folder. We currently have a list of all of the user's folders,
  435. * we must construct a list of all folders in the heirarchy below parentID.
  436. * Our first step will be to construct a hash table of all folders, indexed by parent ID.
  437. * Once we have constructed the hash table, we will do a breadth-first traversal on the tree using the
  438. * hash table to find child folders.
  439. */
  440. else
  441. { // Querying a non-root folder
  442. // Build a hash table of all user's inventory folders, indexed by each folder's parent ID
  443. Dictionary<UUID, List<InventoryFolderBase>> hashtable =
  444. new Dictionary<UUID, List<InventoryFolderBase>>(folderRows.GetLength(0));
  445. foreach (DataRow row in folderRows)
  446. {
  447. InventoryFolderBase curFolder = buildFolder(row);
  448. if (curFolder.ParentID != UUID.Zero) // Discard root of tree - not needed
  449. {
  450. if (hashtable.ContainsKey(curFolder.ParentID))
  451. {
  452. // Current folder already has a sibling - append to sibling list
  453. hashtable[curFolder.ParentID].Add(curFolder);
  454. }
  455. else
  456. {
  457. List<InventoryFolderBase> siblingList = new List<InventoryFolderBase>();
  458. siblingList.Add(curFolder);
  459. // Current folder has no known (yet) siblings
  460. hashtable.Add(curFolder.ParentID, siblingList);
  461. }
  462. }
  463. } // For all inventory folders
  464. // Note: Could release the ds lock here - we don't access folderRows or the database anymore.
  465. // This is somewhat of a moot point as the callers of this function usually lock db anyways.
  466. if (hashtable.ContainsKey(parentID)) // if requested folder does have children
  467. folders.AddRange(hashtable[parentID]);
  468. // BreadthFirstSearch build inventory tree **Note: folders.Count is *not* static
  469. for (int i = 0; i < folders.Count; i++)
  470. if (hashtable.ContainsKey(folders[i].ID))
  471. folders.AddRange(hashtable[folders[i].ID]);
  472. } // if requesting a subfolder heirarchy
  473. } // if folder parentID exists and has children
  474. } // lock ds
  475. return folders;
  476. }
  477. /// <summary>
  478. /// Returns an inventory item by its UUID
  479. /// </summary>
  480. /// <param name="item">The UUID of the item to be returned</param>
  481. /// <returns>A class containing item information</returns>
  482. public InventoryItemBase getInventoryItem(UUID item)
  483. {
  484. lock (ds)
  485. {
  486. DataRow row = ds.Tables["inventoryitems"].Rows.Find(item.ToString());
  487. if (row != null)
  488. {
  489. return buildItem(row);
  490. }
  491. else
  492. {
  493. return null;
  494. }
  495. }
  496. }
  497. /// <summary>
  498. /// Returns a specified inventory folder by its UUID
  499. /// </summary>
  500. /// <param name="folder">The UUID of the folder to be returned</param>
  501. /// <returns>A class containing folder information</returns>
  502. public InventoryFolderBase getInventoryFolder(UUID folder)
  503. {
  504. // TODO: Deep voodoo here. If you enable this code then
  505. // multi region breaks. No idea why, but I figured it was
  506. // better to leave multi region at this point. It does mean
  507. // that you don't get to see system textures why creating
  508. // clothes and the like. :(
  509. lock (ds)
  510. {
  511. DataRow row = ds.Tables["inventoryfolders"].Rows.Find(folder.ToString());
  512. if (row != null)
  513. {
  514. return buildFolder(row);
  515. }
  516. else
  517. {
  518. return null;
  519. }
  520. }
  521. }
  522. /// <summary>
  523. /// Creates a new inventory item based on item
  524. /// </summary>
  525. /// <param name="item">The item to be created</param>
  526. public void addInventoryItem(InventoryItemBase item)
  527. {
  528. addItem(item, true);
  529. }
  530. /// <summary>
  531. /// Updates an inventory item with item (updates based on ID)
  532. /// </summary>
  533. /// <param name="item">The updated item</param>
  534. public void updateInventoryItem(InventoryItemBase item)
  535. {
  536. addItem(item, false);
  537. }
  538. /// <summary>
  539. /// Delete an inventory item
  540. /// </summary>
  541. /// <param name="item">The item UUID</param>
  542. public void deleteInventoryItem(UUID itemID)
  543. {
  544. lock (ds)
  545. {
  546. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  547. DataRow inventoryRow = inventoryItemTable.Rows.Find(itemID.ToString());
  548. if (inventoryRow != null)
  549. {
  550. inventoryRow.Delete();
  551. }
  552. invItemsDa.Update(ds, "inventoryitems");
  553. }
  554. }
  555. public InventoryItemBase queryInventoryItem(UUID itemID)
  556. {
  557. return getInventoryItem(itemID);
  558. }
  559. public InventoryFolderBase queryInventoryFolder(UUID folderID)
  560. {
  561. return getInventoryFolder(folderID);
  562. }
  563. /// <summary>
  564. /// Delete all items in the specified folder
  565. /// </summary>
  566. /// <param name="folderId">id of the folder, whose item content should be deleted</param>
  567. /// <todo>this is horribly inefficient, but I don't want to ruin the overall structure of this implementation</todo>
  568. private void deleteItemsInFolder(UUID folderId)
  569. {
  570. List<InventoryItemBase> items = getInventoryInFolder(folderId);
  571. foreach (InventoryItemBase i in items)
  572. deleteInventoryItem(i.ID);
  573. }
  574. /// <summary>
  575. /// Adds a new folder specified by folder
  576. /// </summary>
  577. /// <param name="folder">The inventory folder</param>
  578. public void addInventoryFolder(InventoryFolderBase folder)
  579. {
  580. addFolder(folder, true);
  581. }
  582. /// <summary>
  583. /// Updates a folder based on its ID with folder
  584. /// </summary>
  585. /// <param name="folder">The inventory folder</param>
  586. public void updateInventoryFolder(InventoryFolderBase folder)
  587. {
  588. addFolder(folder, false);
  589. }
  590. /// <summary>
  591. /// Moves a folder based on its ID with folder
  592. /// </summary>
  593. /// <param name="folder">The inventory folder</param>
  594. public void moveInventoryFolder(InventoryFolderBase folder)
  595. {
  596. moveFolder(folder);
  597. }
  598. /// <summary>
  599. /// Delete a folder
  600. /// </summary>
  601. /// <remarks>
  602. /// This will clean-up any child folders and child items as well
  603. /// </remarks>
  604. /// <param name="folderID">the folder UUID</param>
  605. public void deleteInventoryFolder(UUID folderID)
  606. {
  607. lock (ds)
  608. {
  609. List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID);
  610. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  611. DataRow inventoryRow;
  612. //Delete all sub-folders
  613. foreach (InventoryFolderBase f in subFolders)
  614. {
  615. inventoryRow = inventoryFolderTable.Rows.Find(f.ID.ToString());
  616. if (inventoryRow != null)
  617. {
  618. deleteItemsInFolder(f.ID);
  619. inventoryRow.Delete();
  620. }
  621. }
  622. //Delete the actual row
  623. inventoryRow = inventoryFolderTable.Rows.Find(folderID.ToString());
  624. if (inventoryRow != null)
  625. {
  626. deleteItemsInFolder(folderID);
  627. inventoryRow.Delete();
  628. }
  629. invFoldersDa.Update(ds, "inventoryfolders");
  630. }
  631. }
  632. /***********************************************************************
  633. *
  634. * Data Table definitions
  635. *
  636. **********************************************************************/
  637. /// <summary>
  638. /// Create the "inventoryitems" table
  639. /// </summary>
  640. private static DataTable createInventoryItemsTable()
  641. {
  642. DataTable inv = new DataTable("inventoryitems");
  643. createCol(inv, "UUID", typeof (String)); //inventoryID
  644. createCol(inv, "assetID", typeof (String));
  645. createCol(inv, "assetType", typeof (Int32));
  646. createCol(inv, "invType", typeof (Int32));
  647. createCol(inv, "parentFolderID", typeof (String));
  648. createCol(inv, "avatarID", typeof (String));
  649. createCol(inv, "creatorsID", typeof (String));
  650. createCol(inv, "inventoryName", typeof (String));
  651. createCol(inv, "inventoryDescription", typeof (String));
  652. // permissions
  653. createCol(inv, "inventoryNextPermissions", typeof (Int32));
  654. createCol(inv, "inventoryCurrentPermissions", typeof (Int32));
  655. createCol(inv, "inventoryBasePermissions", typeof (Int32));
  656. createCol(inv, "inventoryEveryOnePermissions", typeof (Int32));
  657. createCol(inv, "inventoryGroupPermissions", typeof (Int32));
  658. // sale info
  659. createCol(inv, "salePrice", typeof(Int32));
  660. createCol(inv, "saleType", typeof(Byte));
  661. // creation date
  662. createCol(inv, "creationDate", typeof(Int32));
  663. // group info
  664. createCol(inv, "groupID", typeof(String));
  665. createCol(inv, "groupOwned", typeof(Boolean));
  666. // Flags
  667. createCol(inv, "flags", typeof(UInt32));
  668. inv.PrimaryKey = new DataColumn[] { inv.Columns["UUID"] };
  669. return inv;
  670. }
  671. /// <summary>
  672. /// Creates the "inventoryfolders" table
  673. /// </summary>
  674. /// <returns></returns>
  675. private static DataTable createInventoryFoldersTable()
  676. {
  677. DataTable fol = new DataTable("inventoryfolders");
  678. createCol(fol, "UUID", typeof (String)); //folderID
  679. createCol(fol, "name", typeof (String));
  680. createCol(fol, "agentID", typeof (String));
  681. createCol(fol, "parentID", typeof (String));
  682. createCol(fol, "type", typeof (Int32));
  683. createCol(fol, "version", typeof (Int32));
  684. fol.PrimaryKey = new DataColumn[] {fol.Columns["UUID"]};
  685. return fol;
  686. }
  687. /// <summary>
  688. ///
  689. /// </summary>
  690. /// <param name="da"></param>
  691. /// <param name="conn"></param>
  692. private void setupItemsCommands(SqliteDataAdapter da, SqliteConnection conn)
  693. {
  694. lock (ds)
  695. {
  696. da.InsertCommand = createInsertCommand("inventoryitems", ds.Tables["inventoryitems"]);
  697. da.InsertCommand.Connection = conn;
  698. da.UpdateCommand = createUpdateCommand("inventoryitems", "UUID=:UUID", ds.Tables["inventoryitems"]);
  699. da.UpdateCommand.Connection = conn;
  700. SqliteCommand delete = new SqliteCommand("delete from inventoryitems where UUID = :UUID");
  701. delete.Parameters.Add(createSqliteParameter("UUID", typeof(String)));
  702. delete.Connection = conn;
  703. da.DeleteCommand = delete;
  704. }
  705. }
  706. /// <summary>
  707. ///
  708. /// </summary>
  709. /// <param name="da"></param>
  710. /// <param name="conn"></param>
  711. private void setupFoldersCommands(SqliteDataAdapter da, SqliteConnection conn)
  712. {
  713. lock (ds)
  714. {
  715. da.InsertCommand = createInsertCommand("inventoryfolders", ds.Tables["inventoryfolders"]);
  716. da.InsertCommand.Connection = conn;
  717. da.UpdateCommand = createUpdateCommand("inventoryfolders", "UUID=:UUID", ds.Tables["inventoryfolders"]);
  718. da.UpdateCommand.Connection = conn;
  719. SqliteCommand delete = new SqliteCommand("delete from inventoryfolders 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="row"></param>
  729. /// <returns></returns>
  730. private static InventoryFolderBase buildFolder(DataRow row)
  731. {
  732. InventoryFolderBase folder = new InventoryFolderBase();
  733. folder.ID = new UUID((string) row["UUID"]);
  734. folder.Name = (string) row["name"];
  735. folder.Owner = new UUID((string) row["agentID"]);
  736. folder.ParentID = new UUID((string) row["parentID"]);
  737. folder.Type = Convert.ToInt16(row["type"]);
  738. folder.Version = Convert.ToUInt16(row["version"]);
  739. return folder;
  740. }
  741. /// <summary>
  742. ///
  743. /// </summary>
  744. /// <param name="row"></param>
  745. /// <param name="folder"></param>
  746. private static void fillFolderRow(DataRow row, InventoryFolderBase folder)
  747. {
  748. row["UUID"] = folder.ID.ToString();
  749. row["name"] = folder.Name;
  750. row["agentID"] = folder.Owner.ToString();
  751. row["parentID"] = folder.ParentID.ToString();
  752. row["type"] = folder.Type;
  753. row["version"] = folder.Version;
  754. }
  755. /// <summary>
  756. ///
  757. /// </summary>
  758. /// <param name="row"></param>
  759. /// <param name="folder"></param>
  760. private static void moveFolderRow(DataRow row, InventoryFolderBase folder)
  761. {
  762. row["UUID"] = folder.ID.ToString();
  763. row["parentID"] = folder.ParentID.ToString();
  764. }
  765. public List<InventoryItemBase> fetchActiveGestures (UUID avatarID)
  766. {
  767. lock (ds)
  768. {
  769. List<InventoryItemBase> items = new List<InventoryItemBase>();
  770. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  771. string selectExp
  772. = "avatarID = '" + avatarID + "' AND assetType = " + (int)AssetType.Gesture + " AND flags = 1";
  773. //m_log.DebugFormat("[SQL]: sql = " + selectExp);
  774. DataRow[] rows = inventoryItemTable.Select(selectExp);
  775. foreach (DataRow row in rows)
  776. {
  777. items.Add(buildItem(row));
  778. }
  779. return items;
  780. }
  781. }
  782. }
  783. }