SQLiteInventoryStore.cs 29 KB

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