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