29 using System.Collections.Generic;
30 using System.Reflection;
32 using MySql.Data.MySqlClient;
34 using OpenSim.Framework;
37 namespace OpenSim.Data.MySQL
44 private static readonly ILog m_log
45 = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47 private string m_connectionString;
48 private object m_dbLock =
new object();
50 public string Version {
get {
return "1.0.0.0"; } }
54 m_log.Info(
"[MySQLInventoryData]: " + Name +
" cannot be default-initialized!");
71 m_connectionString = connect;
74 Assembly assem = GetType().Assembly;
76 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
90 get {
return "MySQL Inventory Data Interface"; }
113 List<InventoryItemBase> items =
new List<InventoryItemBase>();
115 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
119 using (MySqlCommand result =
new MySqlCommand(
"SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", dbcon))
121 result.Parameters.AddWithValue(
"?uuid", folderID.ToString());
123 using (MySqlDataReader reader = result.ExecuteReader())
125 while (reader.Read())
141 m_log.Error(e.Message, e);
157 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
161 using (MySqlCommand result =
new MySqlCommand(
162 "SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", dbcon))
164 result.Parameters.AddWithValue(
"?uuid", user.ToString());
165 result.Parameters.AddWithValue(
"?zero", UUID.Zero.ToString());
167 using (MySqlDataReader reader = result.ExecuteReader())
169 List<InventoryFolderBase> items =
new List<InventoryFolderBase>();
170 while (reader.Read())
171 items.Add(readInventoryFolder(reader));
181 m_log.Error(e.Message, e);
198 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
202 using (MySqlCommand result =
new MySqlCommand(
203 "SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", dbcon))
205 result.Parameters.AddWithValue(
"?uuid", user.ToString());
206 result.Parameters.AddWithValue(
"?zero", UUID.Zero.ToString());
208 using (MySqlDataReader reader = result.ExecuteReader())
210 List<InventoryFolderBase> items =
new List<InventoryFolderBase>();
211 while (reader.Read())
212 items.Add(readInventoryFolder(reader));
222 rootFolder = items[0];
232 m_log.Error(e.Message, e);
250 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
254 using (MySqlCommand result =
new MySqlCommand(
"SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", dbcon))
256 result.Parameters.AddWithValue(
"?uuid", parentID.ToString());
257 using (MySqlDataReader reader = result.ExecuteReader())
259 List<InventoryFolderBase> items =
new List<InventoryFolderBase>();
261 while (reader.Read())
262 items.Add(readInventoryFolder(reader));
272 m_log.Error(e.Message, e);
290 item.CreatorId = reader[
"creatorID"].ToString();
298 item.Owner = DBGuid.FromDB(reader[
"avatarID"]);
299 item.GroupID = DBGuid.FromDB(reader[
"groupID"]);
302 item.ID = DBGuid.FromDB(reader[
"inventoryID"]);
303 item.AssetID = DBGuid.FromDB(reader[
"assetID"]);
304 item.AssetType = (int) reader[
"assetType"];
305 item.Folder = DBGuid.FromDB(reader[
"parentFolderID"]);
306 item.Name = (string)(reader[
"inventoryName"] ?? String.Empty);
307 item.Description = (string)(reader[
"inventoryDescription"] ?? String.Empty);
308 item.NextPermissions = (uint) reader[
"inventoryNextPermissions"];
309 item.CurrentPermissions = (uint) reader[
"inventoryCurrentPermissions"];
310 item.InvType = (int) reader[
"invType"];
311 item.BasePermissions = (uint) reader[
"inventoryBasePermissions"];
312 item.EveryOnePermissions = (uint) reader[
"inventoryEveryOnePermissions"];
313 item.GroupPermissions = (uint) reader[
"inventoryGroupPermissions"];
314 item.SalePrice = (int) reader[
"salePrice"];
315 item.SaleType = unchecked((byte)(Convert.ToSByte(reader[
"saleType"])));
316 item.CreationDate = (int) reader[
"creationDate"];
317 item.GroupOwned = Convert.ToBoolean(reader[
"groupOwned"]);
318 item.Flags = (uint) reader[
"flags"];
322 catch (MySqlException e)
324 m_log.Error(e.ToString());
341 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
345 using (MySqlCommand result =
new MySqlCommand(
"SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", dbcon))
347 result.Parameters.AddWithValue(
"?uuid", itemID.ToString());
349 using (MySqlDataReader reader = result.ExecuteReader())
353 item = readInventoryItem(reader);
363 m_log.Error(e.Message, e);
378 folder.Owner = DBGuid.FromDB(reader[
"agentID"]);
379 folder.ParentID = DBGuid.FromDB(reader[
"parentFolderID"]);
380 folder.ID = DBGuid.FromDB(reader[
"folderID"]);
381 folder.Name = (string) reader[
"folderName"];
382 folder.Type = (short) reader[
"type"];
383 folder.Version = (ushort) ((
int) reader[
"version"]);
388 m_log.Error(e.Message, e);
406 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
410 using (MySqlCommand result =
new MySqlCommand(
"SELECT * FROM inventoryfolders WHERE folderID = ?uuid", dbcon))
412 result.Parameters.AddWithValue(
"?uuid", folderID.ToString());
414 using (MySqlDataReader reader = result.ExecuteReader())
418 folder = readInventoryFolder(reader);
428 m_log.Error(e.Message, e);
440 "REPLACE INTO inventoryitems (inventoryID, assetID, assetType, parentFolderID, avatarID, inventoryName"
441 +
", inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions, invType"
442 +
", creatorID, inventoryBasePermissions, inventoryEveryOnePermissions, inventoryGroupPermissions, salePrice, saleType"
443 +
", creationDate, groupID, groupOwned, flags) VALUES ";
445 "(?inventoryID, ?assetID, ?assetType, ?parentFolderID, ?avatarID, ?inventoryName, ?inventoryDescription"
446 +
", ?inventoryNextPermissions, ?inventoryCurrentPermissions, ?invType, ?creatorID"
447 +
", ?inventoryBasePermissions, ?inventoryEveryOnePermissions, ?inventoryGroupPermissions, ?salePrice, ?saleType, ?creationDate"
448 +
", ?groupID, ?groupOwned, ?flags)";
450 string itemName = item.Name;
451 if (item.
Name.Length > 64)
453 itemName = item.Name.Substring(0, 64);
454 m_log.Warn(
"[INVENTORY DB]: Name field truncated from " + item.Name.Length +
" to " + itemName.Length +
" characters on add item");
457 string itemDesc = item.Description;
460 itemDesc = item.Description.Substring(0, 128);
461 m_log.Warn(
"[INVENTORY DB]: Description field truncated from " + item.Description.Length +
" to " + itemDesc.Length +
" characters on add item");
466 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
470 using (MySqlCommand result =
new MySqlCommand(sql, dbcon))
472 result.Parameters.AddWithValue(
"?inventoryID", item.ID.ToString());
473 result.Parameters.AddWithValue(
"?assetID", item.AssetID.ToString());
474 result.Parameters.AddWithValue(
"?assetType", item.AssetType.ToString());
475 result.Parameters.AddWithValue(
"?parentFolderID", item.Folder.ToString());
476 result.Parameters.AddWithValue(
"?avatarID", item.Owner.ToString());
477 result.Parameters.AddWithValue(
"?inventoryName", itemName);
478 result.Parameters.AddWithValue(
"?inventoryDescription", itemDesc);
479 result.Parameters.AddWithValue(
"?inventoryNextPermissions", item.NextPermissions.ToString());
480 result.Parameters.AddWithValue(
"?inventoryCurrentPermissions",
481 item.CurrentPermissions.ToString());
482 result.Parameters.AddWithValue(
"?invType", item.InvType);
483 result.Parameters.AddWithValue(
"?creatorID", item.CreatorId);
484 result.Parameters.AddWithValue(
"?inventoryBasePermissions", item.BasePermissions);
485 result.Parameters.AddWithValue(
"?inventoryEveryOnePermissions", item.EveryOnePermissions);
486 result.Parameters.AddWithValue(
"?inventoryGroupPermissions", item.GroupPermissions);
487 result.Parameters.AddWithValue(
"?salePrice", item.SalePrice);
488 result.Parameters.AddWithValue(
"?saleType", unchecked((sbyte)item.
SaleType));
489 result.Parameters.AddWithValue(
"?creationDate", item.CreationDate);
490 result.Parameters.AddWithValue(
"?groupID", item.GroupID);
491 result.Parameters.AddWithValue(
"?groupOwned", item.GroupOwned);
492 result.Parameters.AddWithValue(
"?flags", item.Flags);
495 result.ExecuteNonQuery();
500 using (MySqlCommand result =
new MySqlCommand(
"update inventoryfolders set version=version+1 where folderID = ?folderID", dbcon))
502 result.Parameters.AddWithValue(
"?folderID", item.Folder.ToString());
505 result.ExecuteNonQuery();
509 catch (MySqlException e)
511 m_log.Error(e.ToString());
521 addInventoryItem(item);
532 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
536 using (MySqlCommand cmd =
new MySqlCommand(
"DELETE FROM inventoryitems WHERE inventoryID=?uuid", dbcon))
538 cmd.Parameters.AddWithValue(
"?uuid", itemID.ToString());
541 cmd.ExecuteNonQuery();
545 catch (MySqlException e)
547 m_log.Error(e.Message, e);
553 return getInventoryItem(itemID);
558 return getInventoryFolder(folderID);
568 "REPLACE INTO inventoryfolders (folderID, agentID, parentFolderID, folderName, type, version) VALUES ";
569 sql +=
"(?folderID, ?agentID, ?parentFolderID, ?folderName, ?type, ?version)";
571 string folderName = folder.Name;
572 if (folderName.Length > 64)
574 folderName = folderName.Substring(0, 64);
575 m_log.Warn(
"[INVENTORY DB]: Name field truncated from " + folder.Name.Length +
" to " + folderName.Length +
" characters on add folder");
578 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
582 using (MySqlCommand cmd =
new MySqlCommand(sql, dbcon))
584 cmd.Parameters.AddWithValue(
"?folderID", folder.ID.ToString());
585 cmd.Parameters.AddWithValue(
"?agentID", folder.Owner.ToString());
586 cmd.Parameters.AddWithValue(
"?parentFolderID", folder.ParentID.ToString());
587 cmd.Parameters.AddWithValue(
"?folderName", folderName);
588 cmd.Parameters.AddWithValue(
"?type", folder.Type);
589 cmd.Parameters.AddWithValue(
"?version", folder.Version);
595 cmd.ExecuteNonQuery();
600 m_log.Error(e.ToString());
612 addInventoryFolder(folder);
623 "UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID";
625 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
629 using (MySqlCommand cmd =
new MySqlCommand(sql, dbcon))
631 cmd.Parameters.AddWithValue(
"?folderID", folder.ID.ToString());
632 cmd.Parameters.AddWithValue(
"?parentFolderID", folder.ParentID.ToString());
638 cmd.ExecuteNonQuery();
643 m_log.Error(e.ToString());
656 List<InventoryFolderBase> subfolderList = getInventoryFolders(parentID);
688 List<InventoryFolderBase> folders =
new List<InventoryFolderBase>();
689 Dictionary<UUID, List<InventoryFolderBase>> hashtable =
new Dictionary<UUID, List<InventoryFolderBase>>(); ;
690 List<InventoryFolderBase> parentFolder =
new List<InventoryFolderBase>();
691 bool buildResultsFromHashTable =
false;
695 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
701 using (MySqlCommand result =
new MySqlCommand(
"SELECT * FROM inventoryfolders WHERE folderID = ?uuid", dbcon))
703 result.Parameters.AddWithValue(
"?uuid", parentID.ToString());
705 using (MySqlDataReader reader = result.ExecuteReader())
708 while (reader.Read())
709 parentFolder.Add(readInventoryFolder(reader));
713 if (parentFolder.Count >= 1)
715 if (parentFolder[0].
ParentID == UUID.Zero)
718 using (MySqlCommand result =
new MySqlCommand(
"SELECT * FROM inventoryfolders WHERE agentID = ?uuid", dbcon))
720 result.Parameters.AddWithValue(
"?uuid", parentFolder[0].Owner.ToString());
722 using (MySqlDataReader reader = result.ExecuteReader())
724 while (reader.Read())
727 if (curFolder.
ID != parentID)
728 folders.Add(curFolder);
737 using (MySqlCommand result =
new MySqlCommand(
"SELECT * FROM inventoryfolders WHERE agentID = ?uuid", dbcon))
739 result.Parameters.AddWithValue(
"?uuid", parentFolder[0].Owner.ToString());
741 using (MySqlDataReader reader = result.ExecuteReader())
743 while (reader.Read())
746 if (hashtable.ContainsKey(curFolder.
ParentID))
747 hashtable[curFolder.
ParentID].Add(curFolder);
750 List<InventoryFolderBase> siblingList =
new List<InventoryFolderBase>();
751 siblingList.Add(curFolder);
753 hashtable.Add(curFolder.ParentID, siblingList);
761 buildResultsFromHashTable =
true;
766 if (buildResultsFromHashTable)
771 if (hashtable.ContainsKey(parentID))
772 folders.AddRange(hashtable[parentID]);
773 for (
int i = 0; i < folders.Count; i++)
774 if (hashtable.ContainsKey(folders[i].ID))
775 folders.AddRange(hashtable[folders[i].ID]);
784 m_log.Error(e.Message, e);
797 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
802 using (MySqlCommand cmd =
new MySqlCommand(
"DELETE FROM inventoryfolders WHERE folderID=?uuid and type=-1", dbcon))
804 cmd.Parameters.AddWithValue(
"?uuid", folderID.ToString());
807 cmd.ExecuteNonQuery();
811 catch (MySqlException e)
813 m_log.Error(e.Message, e);
825 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
829 using (MySqlCommand cmd =
new MySqlCommand(
"DELETE FROM inventoryitems WHERE parentFolderID=?uuid", dbcon))
831 cmd.Parameters.AddWithValue(
"?uuid", folderID.ToString());
834 cmd.ExecuteNonQuery();
838 catch (MySqlException e)
840 m_log.Error(e.ToString());
850 List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID);
855 deleteOneFolder(f.
ID);
856 deleteItemsInFolder(f.
ID);
860 deleteOneFolder(folderID);
861 deleteItemsInFolder(folderID);
870 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
874 using (MySqlCommand sqlCmd =
new MySqlCommand(
875 "SELECT * FROM inventoryitems WHERE avatarId = ?uuid AND assetType = ?type and flags & 1", dbcon))
877 sqlCmd.Parameters.AddWithValue(
"?uuid", avatarID.ToString());
878 sqlCmd.Parameters.AddWithValue(
"?type", (int)AssetType.Gesture);
880 using (MySqlDataReader result = sqlCmd.ExecuteReader())
882 List<InventoryItemBase> list =
new List<InventoryItemBase>();
883 while (result.Read())
896 m_log.Error(e.Message, e);
void updateInventoryFolder(InventoryFolderBase folder)
Updates an inventory folder
InventoryFolderBase queryInventoryFolder(UUID folderID)
A MySQL interface for the inventory server
void deleteItemsInFolder(UUID folderID)
Delete all item in a folder
void addInventoryItem(InventoryItemBase item)
Adds a specified item to the database
void getInventoryFolders(ref List< InventoryFolderBase > folders, UUID parentID)
Append a list of all the child folders of a parent folder
InventoryFolderBase getUserRootFolder(UUID user)
see InventoryItemBase.getUserRootFolder
void addInventoryFolder(InventoryFolderBase folder)
Creates a new inventory folder
InventoryItemBase getInventoryItem(UUID itemID)
Returns a specified inventory item
List< InventoryFolderBase > getUserRootFolders(UUID user)
Returns a list of the root folders within a users inventory
void deleteInventoryItem(UUID itemID)
Detele the specified inventory item
void moveInventoryFolder(InventoryFolderBase folder)
Move an inventory folder
void updateInventoryItem(InventoryItemBase item)
Updates the specified inventory item
Inventory Item - contains all the properties associated with an individual inventory piece...
Exception thrown if Initialise has been called, but failed.
List< InventoryItemBase > fetchActiveGestures(UUID avatarID)
Returns all activated gesture-items in the inventory of the specified avatar.
InventoryFolderBase getInventoryFolder(UUID folderID)
Returns a specified inventory folder
UUID ID
A UUID containing the ID for the inventory node itself
List< InventoryFolderBase > getInventoryFolders(UUID parentID)
Return a list of folders in a users inventory contained within the specified folder. This method is only used in tests - in normal operation the user always have one, and only one, root folder.
void Initialise()
Default-initialises the plugin
List< InventoryItemBase > getInventoryInFolder(UUID folderID)
Returns a list of items in a specified folder
virtual string Name
The name of the node (64 characters or less)
void deleteOneFolder(UUID folderID)
Delete a folder from database
static InventoryFolderBase readInventoryFolder(MySqlDataReader reader)
Reads a list of inventory folders returned by a query.
void deleteInventoryFolder(UUID folderID)
Deletes an inventory folder
void Dispose()
Closes this DB provider
void Initialise(string connect)
List< InventoryFolderBase > getFolderHierarchy(UUID parentID)
See IInventoryDataPlugin
InventoryItemBase queryInventoryItem(UUID itemID)
An interface for accessing inventory data from a storage server