SQLiteInventoryStore.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 OpenSim 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 libsecondlife;
  32. using log4net;
  33. using Mono.Data.SqliteClient;
  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, IInventoryData
  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 DataSet ds;
  46. private SqliteDataAdapter invItemsDa;
  47. private SqliteDataAdapter invFoldersDa;
  48. /// <summary>
  49. /// <list type="bullet">
  50. /// <item>Initialises Inventory interface</item>
  51. /// <item>Loads and initialises a new SQLite connection and maintains it.</item>
  52. /// <item>use default URI if connect string string is empty.</item>
  53. /// </list>
  54. /// </summary>
  55. /// <param name="dbconnect">connect string</param>
  56. public void Initialise(string dbconnect)
  57. {
  58. if (dbconnect == string.Empty)
  59. {
  60. dbconnect = "URI=file:inventoryStore.db,version=3";
  61. }
  62. m_log.Info("[INVENTORY DB]: Sqlite - connecting: " + dbconnect);
  63. SqliteConnection conn = new SqliteConnection(dbconnect);
  64. conn.Open();
  65. Assembly assem = GetType().Assembly;
  66. Migration m = new Migration(conn, assem, "InventoryStore");
  67. // TODO: remove this line after changeset 6000
  68. TestTables(conn, m);
  69. m.Update();
  70. SqliteCommand itemsSelectCmd = new SqliteCommand(invItemsSelect, conn);
  71. invItemsDa = new SqliteDataAdapter(itemsSelectCmd);
  72. // SqliteCommandBuilder primCb = new SqliteCommandBuilder(primDa);
  73. SqliteCommand foldersSelectCmd = new SqliteCommand(invFoldersSelect, conn);
  74. invFoldersDa = new SqliteDataAdapter(foldersSelectCmd);
  75. ds = new DataSet();
  76. ds.Tables.Add(createInventoryFoldersTable());
  77. invFoldersDa.Fill(ds.Tables["inventoryfolders"]);
  78. setupFoldersCommands(invFoldersDa, conn);
  79. m_log.Info("[INVENTORY DB]: Populated Inventory Folders Definitions");
  80. ds.Tables.Add(createInventoryItemsTable());
  81. invItemsDa.Fill(ds.Tables["inventoryitems"]);
  82. setupItemsCommands(invItemsDa, conn);
  83. m_log.Info("[INVENTORY DB]: Populated Inventory Items Definitions");
  84. ds.AcceptChanges();
  85. }
  86. /// <summary>
  87. ///
  88. /// </summary>
  89. /// <param name="row"></param>
  90. /// <returns></returns>
  91. public InventoryItemBase buildItem(DataRow row)
  92. {
  93. InventoryItemBase item = new InventoryItemBase();
  94. item.ID = new LLUUID((string) row["UUID"]);
  95. item.AssetID = new LLUUID((string) row["assetID"]);
  96. item.AssetType = Convert.ToInt32(row["assetType"]);
  97. item.InvType = Convert.ToInt32(row["invType"]);
  98. item.Folder = new LLUUID((string) row["parentFolderID"]);
  99. item.Owner = new LLUUID((string) row["avatarID"]);
  100. item.Creator = new LLUUID((string) row["creatorsID"]);
  101. item.Name = (string) row["inventoryName"];
  102. item.Description = (string) row["inventoryDescription"];
  103. item.NextPermissions = Convert.ToUInt32(row["inventoryNextPermissions"]);
  104. item.CurrentPermissions = Convert.ToUInt32(row["inventoryCurrentPermissions"]);
  105. item.BasePermissions = Convert.ToUInt32(row["inventoryBasePermissions"]);
  106. item.EveryOnePermissions = Convert.ToUInt32(row["inventoryEveryOnePermissions"]);
  107. // new fields
  108. if (!Convert.IsDBNull(row["salePrice"]))
  109. item.SalePrice = Convert.ToInt32(row["salePrice"]);
  110. if (!Convert.IsDBNull(row["saleType"]))
  111. item.SaleType = Convert.ToByte(row["saleType"]);
  112. if (!Convert.IsDBNull(row["creationDate"]))
  113. item.CreationDate = Convert.ToInt32(row["creationDate"]);
  114. if (!Convert.IsDBNull(row["groupID"]))
  115. item.GroupID = new LLUUID((string)row["groupID"]);
  116. if (!Convert.IsDBNull(row["groupOwned"]))
  117. item.GroupOwned = Convert.ToBoolean(row["groupOwned"]);
  118. if (!Convert.IsDBNull(row["Flags"]))
  119. item.Flags = Convert.ToUInt32(row["Flags"]);
  120. return item;
  121. }
  122. /// <summary>
  123. ///
  124. /// </summary>
  125. /// <param name="row"></param>
  126. /// <param name="item"></param>
  127. private static void fillItemRow(DataRow row, InventoryItemBase item)
  128. {
  129. row["UUID"] = Util.ToRawUuidString(item.ID);
  130. row["assetID"] = Util.ToRawUuidString(item.AssetID);
  131. row["assetType"] = item.AssetType;
  132. row["invType"] = item.InvType;
  133. row["parentFolderID"] = Util.ToRawUuidString(item.Folder);
  134. row["avatarID"] = Util.ToRawUuidString(item.Owner);
  135. row["creatorsID"] = Util.ToRawUuidString(item.Creator);
  136. row["inventoryName"] = item.Name;
  137. row["inventoryDescription"] = item.Description;
  138. row["inventoryNextPermissions"] = item.NextPermissions;
  139. row["inventoryCurrentPermissions"] = item.CurrentPermissions;
  140. row["inventoryBasePermissions"] = item.BasePermissions;
  141. row["inventoryEveryOnePermissions"] = item.EveryOnePermissions;
  142. // new fields
  143. row["salePrice"] = item.SalePrice;
  144. row["saleType"] = item.SaleType;
  145. row["creationDate"] = item.CreationDate;
  146. row["groupID"] = item.GroupID;
  147. row["groupOwned"] = item.GroupOwned;
  148. row["flags"] = item.Flags;
  149. }
  150. /// <summary>
  151. /// Add inventory folder
  152. /// </summary>
  153. /// <param name="folder">Folder base</param>
  154. /// <param name="add">true=create folder. false=update existing folder</param>
  155. /// <remarks>nasty</remarks>
  156. private void addFolder(InventoryFolderBase folder, bool add)
  157. {
  158. lock (ds)
  159. {
  160. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  161. DataRow inventoryRow = inventoryFolderTable.Rows.Find(Util.ToRawUuidString(folder.ID));
  162. if (inventoryRow == null)
  163. {
  164. if (! add)
  165. m_log.ErrorFormat("Interface Misuse: Attempting to Update non-existant inventory folder: {0}", folder.ID);
  166. inventoryRow = inventoryFolderTable.NewRow();
  167. fillFolderRow(inventoryRow, folder);
  168. inventoryFolderTable.Rows.Add(inventoryRow);
  169. }
  170. else
  171. {
  172. if (add)
  173. m_log.ErrorFormat("Interface Misuse: Attempting to Add inventory folder that already exists: {0}", folder.ID);
  174. fillFolderRow(inventoryRow, folder);
  175. }
  176. invFoldersDa.Update(ds, "inventoryfolders");
  177. }
  178. }
  179. /// <summary>
  180. /// Move an inventory folder
  181. /// </summary>
  182. /// <param name="folder">folder base</param>
  183. private void moveFolder(InventoryFolderBase folder)
  184. {
  185. lock (ds)
  186. {
  187. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  188. DataRow inventoryRow = inventoryFolderTable.Rows.Find(Util.ToRawUuidString(folder.ID));
  189. if (inventoryRow == null)
  190. {
  191. inventoryRow = inventoryFolderTable.NewRow();
  192. fillFolderRow(inventoryRow, folder);
  193. inventoryFolderTable.Rows.Add(inventoryRow);
  194. }
  195. else
  196. {
  197. moveFolderRow(inventoryRow, folder);
  198. }
  199. invFoldersDa.Update(ds, "inventoryfolders");
  200. }
  201. }
  202. /// <summary>
  203. /// add an item in inventory
  204. /// </summary>
  205. /// <param name="item">the item</param>
  206. /// <param name="add">true=add item ; false=update existing item</param>
  207. private void addItem(InventoryItemBase item, bool add)
  208. {
  209. lock (ds)
  210. {
  211. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  212. DataRow inventoryRow = inventoryItemTable.Rows.Find(Util.ToRawUuidString(item.ID));
  213. if (inventoryRow == null)
  214. {
  215. if (! add)
  216. m_log.ErrorFormat("[INVENTORY DB]: Interface Misuse: Attempting to Update non-existant inventory item: {0}", item.ID);
  217. inventoryRow = inventoryItemTable.NewRow();
  218. fillItemRow(inventoryRow, item);
  219. inventoryItemTable.Rows.Add(inventoryRow);
  220. }
  221. else
  222. {
  223. if (add)
  224. m_log.ErrorFormat("[INVENTORY DB]: Interface Misuse: Attempting to Add inventory item that already exists: {0}", item.ID);
  225. fillItemRow(inventoryRow, item);
  226. }
  227. invItemsDa.Update(ds, "inventoryitems");
  228. }
  229. }
  230. /// <summary>
  231. /// TODO : DataSet commit
  232. /// </summary>
  233. public void Shutdown()
  234. {
  235. // TODO: DataSet commit
  236. }
  237. /// <summary>
  238. /// Closes the inventory interface
  239. /// </summary>
  240. public void Close()
  241. {
  242. }
  243. /// <summary>
  244. /// The name of this DB provider
  245. /// </summary>
  246. /// <returns>Name of DB provider</returns>
  247. public string getName()
  248. {
  249. return "SQLite Inventory Data Interface";
  250. }
  251. /// <summary>
  252. /// Returns the version of this DB provider
  253. /// </summary>
  254. /// <returns>A string containing the DB provider version</returns>
  255. public string getVersion()
  256. {
  257. Module module = GetType().Module;
  258. // string dllName = module.Assembly.ManifestModule.Name;
  259. Version dllVersion = module.Assembly.GetName().Version;
  260. return
  261. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  262. dllVersion.Revision);
  263. }
  264. /// <summary>
  265. /// Returns a list of inventory items contained within the specified folder
  266. /// </summary>
  267. /// <param name="folderID">The UUID of the target folder</param>
  268. /// <returns>A List of InventoryItemBase items</returns>
  269. public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID)
  270. {
  271. lock (ds)
  272. {
  273. List<InventoryItemBase> retval = new List<InventoryItemBase>();
  274. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  275. string selectExp = "parentFolderID = '" + Util.ToRawUuidString(folderID) + "'";
  276. DataRow[] rows = inventoryItemTable.Select(selectExp);
  277. foreach (DataRow row in rows)
  278. {
  279. retval.Add(buildItem(row));
  280. }
  281. return retval;
  282. }
  283. }
  284. /// <summary>
  285. /// Returns a list of the root folders within a users inventory
  286. /// </summary>
  287. /// <param name="user">The user whos inventory is to be searched</param>
  288. /// <returns>A list of folder objects</returns>
  289. public List<InventoryFolderBase> getUserRootFolders(LLUUID user)
  290. {
  291. return new List<InventoryFolderBase>();
  292. }
  293. // see InventoryItemBase.getUserRootFolder
  294. public InventoryFolderBase getUserRootFolder(LLUUID user)
  295. {
  296. lock (ds)
  297. {
  298. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  299. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  300. string selectExp = "agentID = '" + Util.ToRawUuidString(user) + "' AND parentID = '" +
  301. Util.ToRawUuidString(LLUUID.Zero) + "'";
  302. DataRow[] rows = inventoryFolderTable.Select(selectExp);
  303. foreach (DataRow row in rows)
  304. {
  305. folders.Add(buildFolder(row));
  306. }
  307. // There should only ever be one root folder for a user. However, if there's more
  308. // than one we'll simply use the first one rather than failing. It would be even
  309. // nicer to print some message to this effect, but this feels like it's too low a
  310. // to put such a message out, and it's too minor right now to spare the time to
  311. // suitably refactor.
  312. if (folders.Count > 0)
  313. {
  314. return folders[0];
  315. }
  316. return null;
  317. }
  318. }
  319. /// <summary>
  320. /// Append a list of all the child folders of a parent folder
  321. /// </summary>
  322. /// <param name="folders">list where folders will be appended</param>
  323. /// <param name="parentID">ID of parent</param>
  324. protected void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID)
  325. {
  326. lock (ds)
  327. {
  328. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  329. string selectExp = "parentID = '" + Util.ToRawUuidString(parentID) + "'";
  330. DataRow[] rows = inventoryFolderTable.Select(selectExp);
  331. foreach (DataRow row in rows)
  332. {
  333. folders.Add(buildFolder(row));
  334. }
  335. }
  336. }
  337. /// <summary>
  338. /// Returns a list of inventory folders contained in the folder 'parentID'
  339. /// </summary>
  340. /// <param name="parentID">The folder to get subfolders for</param>
  341. /// <returns>A list of inventory folders</returns>
  342. public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID)
  343. {
  344. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  345. getInventoryFolders(ref folders, Util.ToRawUuidString(parentID));
  346. return folders;
  347. }
  348. /// <summary>
  349. /// See IInventoryData
  350. /// </summary>
  351. /// <param name="parentID"></param>
  352. /// <returns></returns>
  353. public List<InventoryFolderBase> getFolderHierarchy(LLUUID parentID)
  354. {
  355. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  356. getInventoryFolders(ref folders, Util.ToRawUuidString(parentID));
  357. for (int i = 0; i < folders.Count; i++)
  358. getInventoryFolders(ref folders, Util.ToRawUuidString(folders[i].ID));
  359. return folders;
  360. }
  361. /// <summary>
  362. /// Returns an inventory item by its UUID
  363. /// </summary>
  364. /// <param name="item">The UUID of the item to be returned</param>
  365. /// <returns>A class containing item information</returns>
  366. public InventoryItemBase getInventoryItem(LLUUID item)
  367. {
  368. lock (ds)
  369. {
  370. DataRow row = ds.Tables["inventoryitems"].Rows.Find(Util.ToRawUuidString(item));
  371. if (row != null)
  372. {
  373. return buildItem(row);
  374. }
  375. else
  376. {
  377. return null;
  378. }
  379. }
  380. }
  381. /// <summary>
  382. /// Returns a specified inventory folder by its UUID
  383. /// </summary>
  384. /// <param name="folder">The UUID of the folder to be returned</param>
  385. /// <returns>A class containing folder information</returns>
  386. public InventoryFolderBase getInventoryFolder(LLUUID folder)
  387. {
  388. // TODO: Deep voodoo here. If you enable this code then
  389. // multi region breaks. No idea why, but I figured it was
  390. // better to leave multi region at this point. It does mean
  391. // that you don't get to see system textures why creating
  392. // clothes and the like. :(
  393. lock (ds)
  394. {
  395. DataRow row = ds.Tables["inventoryfolders"].Rows.Find(Util.ToRawUuidString(folder));
  396. if (row != null)
  397. {
  398. return buildFolder(row);
  399. }
  400. else
  401. {
  402. return null;
  403. }
  404. }
  405. }
  406. /// <summary>
  407. /// Creates a new inventory item based on item
  408. /// </summary>
  409. /// <param name="item">The item to be created</param>
  410. public void addInventoryItem(InventoryItemBase item)
  411. {
  412. addItem(item, true);
  413. }
  414. /// <summary>
  415. /// Updates an inventory item with item (updates based on ID)
  416. /// </summary>
  417. /// <param name="item">The updated item</param>
  418. public void updateInventoryItem(InventoryItemBase item)
  419. {
  420. addItem(item, false);
  421. }
  422. /// <summary>
  423. /// Delete an inventory item
  424. /// </summary>
  425. /// <param name="item">The item UUID</param>
  426. public void deleteInventoryItem(LLUUID itemID)
  427. {
  428. lock (ds)
  429. {
  430. DataTable inventoryItemTable = ds.Tables["inventoryitems"];
  431. DataRow inventoryRow = inventoryItemTable.Rows.Find(Util.ToRawUuidString(itemID));
  432. if (inventoryRow != null)
  433. {
  434. inventoryRow.Delete();
  435. }
  436. invItemsDa.Update(ds, "inventoryitems");
  437. }
  438. }
  439. /// <summary>
  440. /// Delete all items in the specified folder
  441. /// </summary>
  442. /// <param name="folderId">id of the folder, whose item content should be deleted</param>
  443. /// <todo>this is horribly inefficient, but I don't want to ruin the overall structure of this implementation</todo>
  444. private void deleteItemsInFolder(LLUUID folderId)
  445. {
  446. List<InventoryItemBase> items = getInventoryInFolder(Util.ToRawUuidString(folderId));
  447. foreach (InventoryItemBase i in items)
  448. deleteInventoryItem(Util.ToRawUuidString(i.ID));
  449. }
  450. /// <summary>
  451. /// Adds a new folder specified by folder
  452. /// </summary>
  453. /// <param name="folder">The inventory folder</param>
  454. public void addInventoryFolder(InventoryFolderBase folder)
  455. {
  456. addFolder(folder, true);
  457. }
  458. /// <summary>
  459. /// Updates a folder based on its ID with folder
  460. /// </summary>
  461. /// <param name="folder">The inventory folder</param>
  462. public void updateInventoryFolder(InventoryFolderBase folder)
  463. {
  464. addFolder(folder, false);
  465. }
  466. /// <summary>
  467. /// Moves a folder based on its ID with folder
  468. /// </summary>
  469. /// <param name="folder">The inventory folder</param>
  470. public void moveInventoryFolder(InventoryFolderBase folder)
  471. {
  472. moveFolder(folder);
  473. }
  474. /// <summary>
  475. /// Delete a folder
  476. /// </summary>
  477. /// <remarks>
  478. /// This will clean-up any child folders and child items as well
  479. /// </remarks>
  480. /// <param name="folderID">the folder UUID</param>
  481. public void deleteInventoryFolder(LLUUID folderID)
  482. {
  483. lock (ds)
  484. {
  485. List<InventoryFolderBase> subFolders = getFolderHierarchy(Util.ToRawUuidString(folderID));
  486. DataTable inventoryFolderTable = ds.Tables["inventoryfolders"];
  487. DataRow inventoryRow;
  488. //Delete all sub-folders
  489. foreach (InventoryFolderBase f in subFolders)
  490. {
  491. inventoryRow = inventoryFolderTable.Rows.Find(Util.ToRawUuidString(f.ID));
  492. if (inventoryRow != null)
  493. {
  494. deleteItemsInFolder(Util.ToRawUuidString(f.ID));
  495. inventoryRow.Delete();
  496. }
  497. }
  498. //Delete the actual row
  499. inventoryRow = inventoryFolderTable.Rows.Find(Util.ToRawUuidString(folderID));
  500. if (inventoryRow != null)
  501. {
  502. deleteItemsInFolder(Util.ToRawUuidString(folderID));
  503. inventoryRow.Delete();
  504. }
  505. invFoldersDa.Update(ds, "inventoryfolders");
  506. }
  507. }
  508. /***********************************************************************
  509. *
  510. * Data Table definitions
  511. *
  512. **********************************************************************/
  513. /// <summary>
  514. /// Create the "inventoryitems" table
  515. /// </summary>
  516. private static DataTable createInventoryItemsTable()
  517. {
  518. DataTable inv = new DataTable("inventoryitems");
  519. createCol(inv, "UUID", typeof (String)); //inventoryID
  520. createCol(inv, "assetID", typeof (String));
  521. createCol(inv, "assetType", typeof (Int32));
  522. createCol(inv, "invType", typeof (Int32));
  523. createCol(inv, "parentFolderID", typeof (String));
  524. createCol(inv, "avatarID", typeof (String));
  525. createCol(inv, "creatorsID", typeof (String));
  526. createCol(inv, "inventoryName", typeof (String));
  527. createCol(inv, "inventoryDescription", typeof (String));
  528. // permissions
  529. createCol(inv, "inventoryNextPermissions", typeof (Int32));
  530. createCol(inv, "inventoryCurrentPermissions", typeof (Int32));
  531. createCol(inv, "inventoryBasePermissions", typeof (Int32));
  532. createCol(inv, "inventoryEveryOnePermissions", typeof (Int32));
  533. // sale info
  534. createCol(inv, "salePrice", typeof(Int32));
  535. createCol(inv, "saleType", typeof(Byte));
  536. // creation date
  537. createCol(inv, "creationDate", typeof(Int32));
  538. // group info
  539. createCol(inv, "groupID", typeof(String));
  540. createCol(inv, "groupOwned", typeof(Boolean));
  541. // Flags
  542. createCol(inv, "flags", typeof(UInt32));
  543. inv.PrimaryKey = new DataColumn[] { inv.Columns["UUID"] };
  544. return inv;
  545. }
  546. /// <summary>
  547. /// Creates the "inventoryfolders" table
  548. /// </summary>
  549. /// <returns></returns>
  550. private static DataTable createInventoryFoldersTable()
  551. {
  552. DataTable fol = new DataTable("inventoryfolders");
  553. createCol(fol, "UUID", typeof (String)); //folderID
  554. createCol(fol, "name", typeof (String));
  555. createCol(fol, "agentID", typeof (String));
  556. createCol(fol, "parentID", typeof (String));
  557. createCol(fol, "type", typeof (Int32));
  558. createCol(fol, "version", typeof (Int32));
  559. fol.PrimaryKey = new DataColumn[] {fol.Columns["UUID"]};
  560. return fol;
  561. }
  562. /// <summary>
  563. ///
  564. /// </summary>
  565. /// <param name="da"></param>
  566. /// <param name="conn"></param>
  567. private void setupItemsCommands(SqliteDataAdapter da, SqliteConnection conn)
  568. {
  569. lock (ds)
  570. {
  571. da.InsertCommand = createInsertCommand("inventoryitems", ds.Tables["inventoryitems"]);
  572. da.InsertCommand.Connection = conn;
  573. da.UpdateCommand = createUpdateCommand("inventoryitems", "UUID=:UUID", ds.Tables["inventoryitems"]);
  574. da.UpdateCommand.Connection = conn;
  575. SqliteCommand delete = new SqliteCommand("delete from inventoryitems where UUID = :UUID");
  576. delete.Parameters.Add(createSqliteParameter("UUID", typeof(String)));
  577. delete.Connection = conn;
  578. da.DeleteCommand = delete;
  579. }
  580. }
  581. /// <summary>
  582. ///
  583. /// </summary>
  584. /// <param name="da"></param>
  585. /// <param name="conn"></param>
  586. private void setupFoldersCommands(SqliteDataAdapter da, SqliteConnection conn)
  587. {
  588. lock (ds)
  589. {
  590. da.InsertCommand = createInsertCommand("inventoryfolders", ds.Tables["inventoryfolders"]);
  591. da.InsertCommand.Connection = conn;
  592. da.UpdateCommand = createUpdateCommand("inventoryfolders", "UUID=:UUID", ds.Tables["inventoryfolders"]);
  593. da.UpdateCommand.Connection = conn;
  594. SqliteCommand delete = new SqliteCommand("delete from inventoryfolders where UUID = :UUID");
  595. delete.Parameters.Add(createSqliteParameter("UUID", typeof(String)));
  596. delete.Connection = conn;
  597. da.DeleteCommand = delete;
  598. }
  599. }
  600. /// <summary>
  601. ///
  602. /// </summary>
  603. /// <param name="row"></param>
  604. /// <returns></returns>
  605. private static InventoryFolderBase buildFolder(DataRow row)
  606. {
  607. InventoryFolderBase folder = new InventoryFolderBase();
  608. folder.ID = new LLUUID((string) row["UUID"]);
  609. folder.Name = (string) row["name"];
  610. folder.Owner = new LLUUID((string) row["agentID"]);
  611. folder.ParentID = new LLUUID((string) row["parentID"]);
  612. folder.Type = Convert.ToInt16(row["type"]);
  613. folder.Version = Convert.ToUInt16(row["version"]);
  614. return folder;
  615. }
  616. /// <summary>
  617. ///
  618. /// </summary>
  619. /// <param name="row"></param>
  620. /// <param name="folder"></param>
  621. private static void fillFolderRow(DataRow row, InventoryFolderBase folder)
  622. {
  623. row["UUID"] = Util.ToRawUuidString(folder.ID);
  624. row["name"] = folder.Name;
  625. row["agentID"] = Util.ToRawUuidString(folder.Owner);
  626. row["parentID"] = Util.ToRawUuidString(folder.ParentID);
  627. row["type"] = folder.Type;
  628. row["version"] = folder.Version;
  629. }
  630. /// <summary>
  631. ///
  632. /// </summary>
  633. /// <param name="row"></param>
  634. /// <param name="folder"></param>
  635. private static void moveFolderRow(DataRow row, InventoryFolderBase folder)
  636. {
  637. row["UUID"] = Util.ToRawUuidString(folder.ID);
  638. row["parentID"] = Util.ToRawUuidString(folder.ParentID);
  639. }
  640. /***********************************************************************
  641. *
  642. * Test and Initialization code
  643. *
  644. **********************************************************************/
  645. /// <summary>
  646. ///
  647. /// </summary>
  648. /// <param name="conn"></param>
  649. // private static void InitDB(SqliteConnection conn)
  650. // {
  651. // string createInventoryItems = defineTable(createInventoryItemsTable());
  652. // string createInventoryFolders = defineTable(createInventoryFoldersTable());
  653. // SqliteCommand pcmd = new SqliteCommand(createInventoryItems, conn);
  654. // SqliteCommand scmd = new SqliteCommand(createInventoryFolders, conn);
  655. // pcmd.ExecuteNonQuery();
  656. // scmd.ExecuteNonQuery();
  657. // }
  658. /// <summary>
  659. ///
  660. /// </summary>
  661. /// <param name="conn"></param>
  662. /// <param name="m"></param>
  663. /// <returns></returns>
  664. private static bool TestTables(SqliteConnection conn, Migration m)
  665. {
  666. SqliteCommand invItemsSelectCmd = new SqliteCommand(invItemsSelect, conn);
  667. SqliteDataAdapter pDa = new SqliteDataAdapter(invItemsSelectCmd);
  668. SqliteCommand invFoldersSelectCmd = new SqliteCommand(invFoldersSelect, conn);
  669. SqliteDataAdapter sDa = new SqliteDataAdapter(invFoldersSelectCmd);
  670. DataSet tmpDS = new DataSet();
  671. try
  672. {
  673. pDa.Fill(tmpDS, "inventoryitems");
  674. sDa.Fill(tmpDS, "inventoryfolders");
  675. }
  676. catch (SqliteSyntaxException)
  677. {
  678. m_log.Info("[INVENTORY DB]: SQLite Database doesn't exist... creating");
  679. return false;
  680. }
  681. if (m.Version == 0)
  682. m.Version = 1;
  683. return true;
  684. // pDa.Fill(tmpDS, "inventoryitems");
  685. // sDa.Fill(tmpDS, "inventoryfolders");
  686. // // Very clumsy way of checking whether we need to upgrade the database table version and then updating. Only
  687. // // putting up with this because this code should be blown away soon by nhibernate...
  688. // conn.Open();
  689. // SqliteCommand cmd;
  690. // try
  691. // {
  692. // cmd = new SqliteCommand("select salePrice from inventoryitems limit 1;", conn);
  693. // cmd.ExecuteNonQuery();
  694. // }
  695. // catch (SqliteSyntaxException)
  696. // {
  697. // m_log.Info("[INVENTORY DB]: Upgrading sqlite inventory database to version 2");
  698. // cmd = new SqliteCommand("alter table inventoryitems add column salePrice integer default 99;", conn);
  699. // cmd.ExecuteNonQuery();
  700. // cmd = new SqliteCommand("alter table inventoryitems add column saleType integer default 0;", conn);
  701. // cmd.ExecuteNonQuery();
  702. // cmd = new SqliteCommand("alter table inventoryitems add column creationDate integer default 2000;", conn);
  703. // cmd.ExecuteNonQuery();
  704. // cmd = new SqliteCommand("alter table inventoryitems add column groupID varchar(255) default '00000000-0000-0000-0000-000000000000';", conn);
  705. // cmd.ExecuteNonQuery();
  706. // cmd = new SqliteCommand("alter table inventoryitems add column groupOwned integer default 0;", conn);
  707. // cmd.ExecuteNonQuery();
  708. // cmd = new SqliteCommand("alter table inventoryitems add column flags integer default 0;", conn);
  709. // cmd.ExecuteNonQuery();
  710. // pDa.Fill(tmpDS, "inventoryitems");
  711. // }
  712. // finally
  713. // {
  714. // conn.Close();
  715. // }
  716. // foreach (DataColumn col in createInventoryItemsTable().Columns)
  717. // {
  718. // if (! tmpDS.Tables["inventoryitems"].Columns.Contains(col.ColumnName))
  719. // {
  720. // m_log.Info("[INVENTORY DB]: Missing required column:" + col.ColumnName);
  721. // return false;
  722. // }
  723. // }
  724. // foreach (DataColumn col in createInventoryFoldersTable().Columns)
  725. // {
  726. // if (! tmpDS.Tables["inventoryfolders"].Columns.Contains(col.ColumnName))
  727. // {
  728. // m_log.Info("[INVENTORY DB]: Missing required column:" + col.ColumnName);
  729. // return false;
  730. // }
  731. // }
  732. // return true;
  733. }
  734. }
  735. }