SQLiteInventoryStore.cs 26 KB

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