29 using System.Collections.Generic;
31 using System.Reflection;
34 using OpenSim.Framework;
37 namespace OpenSim.Data.PGSQL
44 private const string _migrationStore =
"InventoryStore";
46 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
52 private string m_connectionString;
54 #region IPlugin members
56 [Obsolete(
"Cannot be default-initialized!")]
59 m_log.Info(
"[PGSQLInventoryData]: " + Name +
" cannot be default-initialized!");
70 m_connectionString = connectionString;
74 database.CheckMigration(_migrationStore);
83 get {
return "PGSQL Inventory Data Interface"; }
100 get {
return database.getVersion(); }
105 #region Folder methods
114 if (user == UUID.Zero)
115 return new List<InventoryFolderBase>();
117 return getInventoryFolders(UUID.Zero, user);
127 List<InventoryFolderBase> items = getUserRootFolders(user);
138 rootFolder = items[0];
151 return getInventoryFolders(parentID, UUID.Zero);
161 string sql =
"SELECT * FROM inventoryfolders WHERE \"folderID\" = :folderID";
162 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
163 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
165 cmd.Parameters.Add(database.CreateParameter(
"folderID", folderID));
167 using (NpgsqlDataReader reader = cmd.ExecuteReader())
171 return readInventoryFolder(reader);
175 m_log.InfoFormat(
"[INVENTORY DB] : Found no inventory folder with ID : {0}", folderID);
198 List<InventoryFolderBase> folders =
new List<InventoryFolderBase>();
200 if (parentID == UUID.Zero)
203 string sql =
"SELECT * FROM inventoryfolders WHERE \"parentFolderID\" = :parentID";
204 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
205 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
207 cmd.Parameters.Add(database.CreateParameter(
"parentID", parentID));
209 folders.AddRange(getInventoryFolders(cmd));
211 List<InventoryFolderBase> tempFolders =
new List<InventoryFolderBase>();
215 tempFolders.AddRange(getFolderHierarchy(folderBase.
ID, cmd));
217 if (tempFolders.Count > 0)
219 folders.AddRange(tempFolders);
231 string sql =
"INSERT INTO inventoryfolders (\"folderID\", \"agentID\", \"parentFolderID\", \"folderName\", type, version) " +
232 " VALUES (:folderID, :agentID, :parentFolderID, :folderName, :type, :version);";
234 string folderName = folder.Name;
235 if (folderName.Length > 64)
237 folderName = folderName.Substring(0, 64);
238 m_log.Warn(
"[INVENTORY DB]: Name field truncated from " + folder.Name.Length.ToString() +
" to " + folderName.Length +
" characters on add");
240 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
241 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
243 cmd.Parameters.Add(database.CreateParameter(
"folderID", folder.ID));
244 cmd.Parameters.Add(database.CreateParameter(
"agentID", folder.Owner));
245 cmd.Parameters.Add(database.CreateParameter(
"parentFolderID", folder.ParentID));
246 cmd.Parameters.Add(database.CreateParameter(
"folderName", folderName));
247 cmd.Parameters.Add(database.CreateParameter(
"type", folder.Type));
248 cmd.Parameters.Add(database.CreateParameter(
"version", folder.Version));
252 cmd.ExecuteNonQuery();
256 m_log.ErrorFormat(
"[INVENTORY DB]: Error : {0}", e.Message);
267 string sql =
@"UPDATE inventoryfolders SET ""agentID"" = :agentID,
268 ""parentFolderID"" = :parentFolderID,
269 ""folderName"" = :folderName,
272 WHERE folderID = :folderID";
274 string folderName = folder.Name;
275 if (folderName.Length > 64)
277 folderName = folderName.Substring(0, 64);
278 m_log.Warn(
"[INVENTORY DB]: Name field truncated from " + folder.Name.Length.ToString() +
" to " + folderName.Length +
" characters on update");
280 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
281 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
283 cmd.Parameters.Add(database.CreateParameter(
"folderID", folder.ID));
284 cmd.Parameters.Add(database.CreateParameter(
"agentID", folder.Owner));
285 cmd.Parameters.Add(database.CreateParameter(
"parentFolderID", folder.ParentID));
286 cmd.Parameters.Add(database.CreateParameter(
"folderName", folderName));
287 cmd.Parameters.Add(database.CreateParameter(
"type", folder.Type));
288 cmd.Parameters.Add(database.CreateParameter(
"version", folder.Version));
292 cmd.ExecuteNonQuery();
296 m_log.ErrorFormat(
"[INVENTORY DB]: Error : {0}", e.Message);
307 string sql =
@"UPDATE inventoryfolders SET ""parentFolderID"" = :parentFolderID WHERE ""folderID"" = :folderID";
308 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
309 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
311 cmd.Parameters.Add(database.CreateParameter(
"parentFolderID", folder.ParentID));
312 cmd.Parameters.Add(database.CreateParameter(
"folderID", folder.ID));
316 cmd.ExecuteNonQuery();
320 m_log.ErrorFormat(
"[INVENTORY DB]: Error : {0}", e.Message);
331 string sql =
@"SELECT * FROM inventoryfolders WHERE ""parentFolderID"" = :parentID";
333 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
334 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
336 List<InventoryFolderBase> subFolders;
337 cmd.Parameters.Add(database.CreateParameter(
"parentID", UUID.Zero));
339 subFolders = getFolderHierarchy(folderID, cmd);
345 DeleteOneFolder(f.
ID, conn);
346 DeleteItemsInFolder(f.
ID, conn);
350 DeleteOneFolder(folderID, conn);
351 DeleteItemsInFolder(folderID, conn);
366 string sql =
@"SELECT * FROM inventoryitems WHERE ""parentFolderID"" = :parentFolderID";
367 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
368 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
370 cmd.Parameters.Add(database.CreateParameter(
"parentFolderID", folderID));
372 List<InventoryItemBase> items =
new List<InventoryItemBase>();
374 using (NpgsqlDataReader reader = cmd.ExecuteReader())
376 while (reader.Read())
378 items.Add(readInventoryItem(reader));
392 string sql =
@"SELECT * FROM inventoryitems WHERE ""inventoryID"" = :inventoryID";
393 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
394 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
396 cmd.Parameters.Add(database.CreateParameter(
"inventoryID", itemID));
398 using (NpgsqlDataReader reader = cmd.ExecuteReader())
402 return readInventoryItem(reader);
407 m_log.InfoFormat(
"[INVENTORY DB]: Found no inventory item with ID : {0}", itemID);
417 if (getInventoryItem(item.
ID) != null)
419 updateInventoryItem(item);
423 string sql =
@"INSERT INTO inventoryitems
424 (""inventoryID"", ""assetID"", ""assetType"", ""parentFolderID"", ""avatarID"", ""inventoryName"",
425 ""inventoryDescription"", ""inventoryNextPermissions"", ""inventoryCurrentPermissions"",
426 ""invType"", ""creatorID"", ""inventoryBasePermissions"", ""inventoryEveryOnePermissions"", ""inventoryGroupPermissions"",
427 ""salePrice"", ""SaleType"", ""creationDate"", ""groupID"", ""groupOwned"", flags)
429 (:inventoryID, :assetID, :assetType, :parentFolderID, :avatarID, :inventoryName, :inventoryDescription,
430 :inventoryNextPermissions, :inventoryCurrentPermissions, :invType, :creatorID,
431 :inventoryBasePermissions, :inventoryEveryOnePermissions, :inventoryGroupPermissions, :SalePrice, :SaleType,
432 :creationDate, :groupID, :groupOwned, :flags)";
434 string itemName = item.Name;
435 if (item.
Name.Length > 64)
437 itemName = item.Name.Substring(0, 64);
438 m_log.Warn(
"[INVENTORY DB]: Name field truncated from " + item.Name.Length.ToString() +
" to " + itemName.Length.ToString() +
" characters");
441 string itemDesc = item.Description;
444 itemDesc = item.Description.Substring(0, 128);
445 m_log.Warn(
"[INVENTORY DB]: Description field truncated from " + item.Description.Length.ToString() +
" to " + itemDesc.Length.ToString() +
" characters");
448 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
449 using (NpgsqlCommand command =
new NpgsqlCommand(sql, conn))
451 command.Parameters.Add(database.CreateParameter(
"inventoryID", item.ID));
452 command.Parameters.Add(database.CreateParameter(
"assetID", item.AssetID));
453 command.Parameters.Add(database.CreateParameter(
"assetType", item.AssetType));
454 command.Parameters.Add(database.CreateParameter(
"parentFolderID", item.Folder));
455 command.Parameters.Add(database.CreateParameter(
"avatarID", item.Owner));
456 command.Parameters.Add(database.CreateParameter(
"inventoryName", itemName));
457 command.Parameters.Add(database.CreateParameter(
"inventoryDescription", itemDesc));
458 command.Parameters.Add(database.CreateParameter(
"inventoryNextPermissions", item.NextPermissions));
459 command.Parameters.Add(database.CreateParameter(
"inventoryCurrentPermissions", item.CurrentPermissions));
460 command.Parameters.Add(database.CreateParameter(
"invType", item.InvType));
461 command.Parameters.Add(database.CreateParameter(
"creatorID", item.CreatorId));
462 command.Parameters.Add(database.CreateParameter(
"inventoryBasePermissions", item.BasePermissions));
463 command.Parameters.Add(database.CreateParameter(
"inventoryEveryOnePermissions", item.EveryOnePermissions));
464 command.Parameters.Add(database.CreateParameter(
"inventoryGroupPermissions", item.GroupPermissions));
465 command.Parameters.Add(database.CreateParameter(
"SalePrice", item.SalePrice));
466 command.Parameters.Add(database.CreateParameter(
"SaleType", item.SaleType));
467 command.Parameters.Add(database.CreateParameter(
"creationDate", item.CreationDate));
468 command.Parameters.Add(database.CreateParameter(
"groupID", item.GroupID));
469 command.Parameters.Add(database.CreateParameter(
"groupOwned", item.GroupOwned));
470 command.Parameters.Add(database.CreateParameter(
"flags", item.Flags));
474 command.ExecuteNonQuery();
478 m_log.Error(
"[INVENTORY DB]: Error inserting item :" + e.Message);
482 sql =
@"UPDATE inventoryfolders SET version = version + 1 WHERE ""folderID"" = @folderID";
483 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
484 using (NpgsqlCommand command =
new NpgsqlCommand(sql, conn))
486 command.Parameters.Add(database.CreateParameter(
"folderID", item.Folder.ToString()));
490 command.ExecuteNonQuery();
494 m_log.Error(
"[INVENTORY DB] Error updating inventory folder for new item :" + e.Message);
505 string sql =
@"UPDATE inventoryitems SET ""assetID"" = :assetID,
506 ""assetType"" = :assetType,
507 ""parentFolderID"" = :parentFolderID,
508 ""avatarID"" = :avatarID,
509 ""inventoryName"" = :inventoryName,
510 ""inventoryDescription"" = :inventoryDescription,
511 ""inventoryNextPermissions"" = :inventoryNextPermissions,
512 ""inventoryCurrentPermissions"" = :inventoryCurrentPermissions,
513 ""invType"" = :invType,
514 ""creatorID"" = :creatorID,
515 ""inventoryBasePermissions"" = :inventoryBasePermissions,
516 ""inventoryEveryOnePermissions"" = :inventoryEveryOnePermissions,
517 ""inventoryGroupPermissions"" = :inventoryGroupPermissions,
518 ""salePrice"" = :SalePrice,
519 ""saleType"" = :SaleType,
520 ""creationDate"" = :creationDate,
521 ""groupID"" = :groupID,
522 ""groupOwned"" = :groupOwned,
524 WHERE ""inventoryID"" = :inventoryID";
526 string itemName = item.Name;
527 if (item.
Name.Length > 64)
529 itemName = item.Name.Substring(0, 64);
530 m_log.Warn(
"[INVENTORY DB]: Name field truncated from " + item.Name.Length.ToString() +
" to " + itemName.Length.ToString() +
" characters on update");
533 string itemDesc = item.Description;
536 itemDesc = item.Description.Substring(0, 128);
537 m_log.Warn(
"[INVENTORY DB]: Description field truncated from " + item.Description.Length.ToString() +
" to " + itemDesc.Length.ToString() +
" characters on update");
540 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
541 using (NpgsqlCommand command =
new NpgsqlCommand(sql, conn))
543 command.Parameters.Add(database.CreateParameter(
"inventoryID", item.ID));
544 command.Parameters.Add(database.CreateParameter(
"assetID", item.AssetID));
545 command.Parameters.Add(database.CreateParameter(
"assetType", item.AssetType));
546 command.Parameters.Add(database.CreateParameter(
"parentFolderID", item.Folder));
547 command.Parameters.Add(database.CreateParameter(
"avatarID", item.Owner));
548 command.Parameters.Add(database.CreateParameter(
"inventoryName", itemName));
549 command.Parameters.Add(database.CreateParameter(
"inventoryDescription", itemDesc));
550 command.Parameters.Add(database.CreateParameter(
"inventoryNextPermissions", item.NextPermissions));
551 command.Parameters.Add(database.CreateParameter(
"inventoryCurrentPermissions", item.CurrentPermissions));
552 command.Parameters.Add(database.CreateParameter(
"invType", item.InvType));
553 command.Parameters.Add(database.CreateParameter(
"creatorID", item.CreatorId));
554 command.Parameters.Add(database.CreateParameter(
"inventoryBasePermissions", item.BasePermissions));
555 command.Parameters.Add(database.CreateParameter(
"inventoryEveryOnePermissions", item.EveryOnePermissions));
556 command.Parameters.Add(database.CreateParameter(
"inventoryGroupPermissions", item.GroupPermissions));
557 command.Parameters.Add(database.CreateParameter(
"SalePrice", item.SalePrice));
558 command.Parameters.Add(database.CreateParameter(
"SaleType", item.SaleType));
559 command.Parameters.Add(database.CreateParameter(
"creationDate", item.CreationDate));
560 command.Parameters.Add(database.CreateParameter(
"groupID", item.GroupID));
561 command.Parameters.Add(database.CreateParameter(
"groupOwned", item.GroupOwned));
562 command.Parameters.Add(database.CreateParameter(
"flags", item.Flags));
566 command.ExecuteNonQuery();
570 m_log.Error(
"[INVENTORY DB]: Error updating item :" + e.Message);
583 string sql =
@"DELETE FROM inventoryitems WHERE ""inventoryID""=:inventoryID";
584 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
585 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
587 cmd.Parameters.Add(database.CreateParameter(
"inventoryID", itemID));
591 cmd.ExecuteNonQuery();
595 m_log.Error(
"[INVENTORY DB]: Error deleting item :" + e.Message);
602 return getInventoryItem(itemID);
607 return getInventoryFolder(folderID);
619 string sql =
@"SELECT * FROM inventoryitems WHERE ""avatarID"" = :uuid AND ""assetType"" = :assetType and flags = 1";
620 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
621 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
623 cmd.Parameters.Add(database.CreateParameter(
"uuid", avatarID));
624 cmd.Parameters.Add(database.CreateParameter(
"assetType", (int)AssetType.Gesture));
626 using (NpgsqlDataReader reader = cmd.ExecuteReader())
628 List<InventoryItemBase> gestureList =
new List<InventoryItemBase>();
629 while (reader.Read())
631 gestureList.Add(readInventoryItem(reader));
640 #region Private methods
647 private void DeleteItemsInFolder(UUID folderID, NpgsqlConnection connection)
649 using (NpgsqlCommand command =
new NpgsqlCommand(
@"DELETE FROM inventoryitems WHERE ""folderID""=:folderID", connection))
651 command.Parameters.Add(database.CreateParameter(
"folderID", folderID));
655 command.ExecuteNonQuery();
659 m_log.Error(
"[INVENTORY DB] Error deleting item :" + e.Message);
670 private static List<InventoryFolderBase> getFolderHierarchy(UUID parentID, NpgsqlCommand command)
672 command.Parameters[
"parentID"].Value = parentID.Guid;
674 List<InventoryFolderBase> folders = getInventoryFolders(command);
676 if (folders.Count > 0)
678 List<InventoryFolderBase> tempFolders =
new List<InventoryFolderBase>();
682 tempFolders.AddRange(getFolderHierarchy(folderBase.
ID, command));
685 if (tempFolders.Count > 0)
687 folders.AddRange(tempFolders);
699 private List<InventoryFolderBase> getInventoryFolders(UUID parentID, UUID user)
701 string sql =
@"SELECT * FROM inventoryfolders WHERE ""parentFolderID"" = :parentID AND ""agentID"" = :uuid";
702 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
703 using (NpgsqlCommand command =
new NpgsqlCommand(sql, conn))
705 if (user ==
UUID.Zero)
707 command.Parameters.Add(database.CreateParameter(
"uuid",
"%"));
711 command.Parameters.Add(database.CreateParameter(
"uuid", user));
713 command.Parameters.Add(database.CreateParameter(
"parentID", parentID));
715 return getInventoryFolders(command);
724 private static List<InventoryFolderBase> getInventoryFolders(NpgsqlCommand command)
726 using (NpgsqlDataReader reader = command.ExecuteReader())
729 List<InventoryFolderBase> items =
new List<InventoryFolderBase>();
730 while (reader.Read())
732 items.Add(readInventoryFolder(reader));
748 folder.Owner = DBGuid.FromDB(reader[
"agentID"]);
749 folder.ParentID = DBGuid.FromDB(reader[
"parentFolderID"]);
750 folder.ID = DBGuid.FromDB(reader[
"folderID"]);
751 folder.Name = (string)reader[
"folderName"];
752 folder.Type = (short)reader[
"type"];
753 folder.Version = Convert.ToUInt16(reader[
"version"]);
759 m_log.Error(
"[INVENTORY DB] Error reading inventory folder :" + e.Message);
776 item.ID = DBGuid.FromDB(reader[
"inventoryID"]);
777 item.AssetID = DBGuid.FromDB(reader[
"assetID"]);
778 item.AssetType = Convert.ToInt32(reader[
"assetType"].ToString());
779 item.Folder = DBGuid.FromDB(reader[
"parentFolderID"]);
780 item.Owner = DBGuid.FromDB(reader[
"avatarID"]);
781 item.Name = reader[
"inventoryName"].ToString();
782 item.Description = reader[
"inventoryDescription"].ToString();
783 item.NextPermissions = Convert.ToUInt32(reader[
"inventoryNextPermissions"]);
784 item.CurrentPermissions = Convert.ToUInt32(reader[
"inventoryCurrentPermissions"]);
785 item.InvType = Convert.ToInt32(reader[
"invType"].ToString());
786 item.CreatorId = reader[
"creatorID"].ToString();
787 item.BasePermissions = Convert.ToUInt32(reader[
"inventoryBasePermissions"]);
788 item.EveryOnePermissions = Convert.ToUInt32(reader[
"inventoryEveryOnePermissions"]);
789 item.GroupPermissions = Convert.ToUInt32(reader[
"inventoryGroupPermissions"]);
790 item.SalePrice = Convert.ToInt32(reader[
"salePrice"]);
791 item.SaleType = Convert.ToByte(reader[
"saleType"]);
792 item.CreationDate = Convert.ToInt32(reader[
"creationDate"]);
793 item.GroupID = DBGuid.FromDB(reader[
"groupID"]);
794 item.GroupOwned = Convert.ToBoolean(reader[
"groupOwned"]);
795 item.Flags = Convert.ToUInt32(reader[
"flags"]);
799 catch (NpgsqlException e)
801 m_log.Error(
"[INVENTORY DB]: Error reading inventory item :" + e.Message);
812 private void DeleteOneFolder(UUID folderID, NpgsqlConnection connection)
816 using (NpgsqlCommand command =
new NpgsqlCommand(
@"DELETE FROM inventoryfolders WHERE ""folderID""=:folderID and type=-1", connection))
818 command.Parameters.Add(database.CreateParameter(
"folderID", folderID));
820 command.ExecuteNonQuery();
823 catch (NpgsqlException e)
825 m_log.Error(
"[INVENTORY DB]: Error deleting folder :" + e.Message);
InventoryFolderBase getInventoryFolder(UUID folderID)
Returns a specified inventory folder
void deleteInventoryFolder(UUID folderID)
Delete an inventory folder
static InventoryFolderBase readInventoryFolder(NpgsqlDataReader reader)
Reads a list of inventory folders returned by a query.
List< InventoryFolderBase > getUserRootFolders(UUID user)
Returns a list of the root folders within a users inventory
void updateInventoryFolder(InventoryFolderBase folder)
Updates an inventory folder
A PGSQL interface for the inventory server
List< InventoryItemBase > fetchActiveGestures(UUID avatarID)
Returns all activated gesture-items in the inventory of the specified avatar.
void Initialise()
Default-initialises the plugin
void addInventoryItem(InventoryItemBase item)
Adds a specified item to the database
A management class for the MS SQL Storage Engine
List< InventoryItemBase > getInventoryInFolder(UUID folderID)
Returns a list of items in a specified folder
List< InventoryFolderBase > getInventoryFolders(UUID parentID)
Returns a list of folders in a users inventory contained within the specified folder ...
void deleteInventoryItem(UUID itemID)
Delete an item in inventory database
void addInventoryFolder(InventoryFolderBase folder)
Creates a new inventory folder
Inventory Item - contains all the properties associated with an individual inventory piece...
Exception thrown if Initialise has been called, but failed.
void Initialise(string connectionString)
Loads and initialises the PGSQL inventory storage interface
void moveInventoryFolder(InventoryFolderBase folder)
Updates an inventory folder
InventoryFolderBase queryInventoryFolder(UUID folderID)
void updateInventoryItem(InventoryItemBase item)
Updates the specified inventory item
UUID ID
A UUID containing the ID for the inventory node itself
InventoryItemBase queryInventoryItem(UUID itemID)
void Dispose()
Closes this DB provider
virtual string Name
The name of the node (64 characters or less)
InventoryFolderBase getUserRootFolder(UUID user)
see InventoryItemBase.getUserRootFolder
List< InventoryFolderBase > getFolderHierarchy(UUID parentID)
Returns all child folders in the hierarchy from the parent folder and down. Does not return the paren...
InventoryItemBase getInventoryItem(UUID itemID)
Returns a specified inventory item
An interface for accessing inventory data from a storage server