/* * Copyright (c) Contributors, http://opensimulator.org/ * See CONTRIBUTORS.TXT for a full list of copyright holders. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the OpenSimulator Project nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ using System; using System.Collections.Generic; using System.Reflection; using log4net; using MySql.Data.MySqlClient; using OpenMetaverse; using OpenSim.Framework; namespace OpenSim.Data.MySQL { /// /// A MySQL interface for the inventory server /// public class MySQLInventoryData : IInventoryDataPlugin { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private string m_connectionString; private object m_dbLock = new object(); public string Version { get { return "1.0.0.0"; } } public void Initialise() { m_log.Info("[MySQLInventoryData]: " + Name + " cannot be default-initialized!"); throw new PluginNotInitialisedException (Name); } /// /// Initialises Inventory interface /// /// /// Loads and initialises the MySQL storage plugin /// warns and uses the obsolete mysql_connection.ini if connect string is empty. /// Check for migration /// /// /// /// connect string public void Initialise(string connect) { m_connectionString = connect; // This actually does the roll forward assembly stuff Assembly assem = GetType().Assembly; using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); Migration m = new Migration(dbcon, assem, "InventoryStore"); m.Update(); } } /// /// The name of this DB provider /// /// Name of DB provider public string Name { get { return "MySQL Inventory Data Interface"; } } /// /// Closes this DB provider /// /// do nothing public void Dispose() { // Do nothing. } /// /// Returns a list of items in a specified folder /// /// The folder to search /// A list containing inventory items public List getInventoryInFolder(UUID folderID) { try { lock (m_dbLock) { List items = new List(); using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", folderID.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { while (reader.Read()) { // A null item (because something went wrong) breaks everything in the folder InventoryItemBase item = readInventoryItem(reader); if (item != null) items.Add(item); } return items; } } } } } catch (Exception e) { m_log.Error(e.Message, e); return null; } } /// /// Returns a list of the root folders within a users inventory /// /// The user whos inventory is to be searched /// A list of folder objects public List getUserRootFolders(UUID user) { try { lock (m_dbLock) { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand result = new MySqlCommand( "SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", user.ToString()); result.Parameters.AddWithValue("?zero", UUID.Zero.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { List items = new List(); while (reader.Read()) items.Add(readInventoryFolder(reader)); return items; } } } } } catch (Exception e) { m_log.Error(e.Message, e); return null; } } /// /// see /// /// The user UUID /// public InventoryFolderBase getUserRootFolder(UUID user) { try { lock (m_dbLock) { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand result = new MySqlCommand( "SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", user.ToString()); result.Parameters.AddWithValue("?zero", UUID.Zero.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { List items = new List(); while (reader.Read()) items.Add(readInventoryFolder(reader)); InventoryFolderBase rootFolder = null; // There should only ever be one root folder for a user. However, if there's more // than one we'll simply use the first one rather than failing. It would be even // nicer to print some message to this effect, but this feels like it's too low a // to put such a message out, and it's too minor right now to spare the time to // suitably refactor. if (items.Count > 0) rootFolder = items[0]; return rootFolder; } } } } } catch (Exception e) { m_log.Error(e.Message, e); return null; } } /// /// 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. /// /// The folder to search /// A list of inventory folders public List getInventoryFolders(UUID parentID) { try { lock (m_dbLock) { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", parentID.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { List items = new List(); while (reader.Read()) items.Add(readInventoryFolder(reader)); return items; } } } } } catch (Exception e) { m_log.Error(e.Message, e); return null; } } /// /// Reads a one item from an SQL result /// /// The SQL Result /// the item read private static InventoryItemBase readInventoryItem(MySqlDataReader reader) { try { InventoryItemBase item = new InventoryItemBase(); // TODO: this is to handle a case where NULLs creep in there, which we are not sure is indemic to the system, or legacy. It would be nice to live fix these. if (reader["creatorID"] == null) { item.CreatorId = UUID.Zero.ToString(); } else { item.CreatorId = (string)reader["creatorID"]; } // Be a bit safer in parsing these because the // database doesn't enforce them to be not null, and // the inventory still works if these are weird in the // db UUID Owner = UUID.Zero; UUID GroupID = UUID.Zero; UUID.TryParse((string)reader["avatarID"], out Owner); UUID.TryParse((string)reader["groupID"], out GroupID); item.Owner = Owner; item.GroupID = GroupID; // Rest of the parsing. If these UUID's fail, we're dead anyway item.ID = new UUID((string) reader["inventoryID"]); item.AssetID = new UUID((string) reader["assetID"]); item.AssetType = (int) reader["assetType"]; item.Folder = new UUID((string) reader["parentFolderID"]); item.Name = (string)(reader["inventoryName"] ?? String.Empty); item.Description = (string)(reader["inventoryDescription"] ?? String.Empty); item.NextPermissions = (uint) reader["inventoryNextPermissions"]; item.CurrentPermissions = (uint) reader["inventoryCurrentPermissions"]; item.InvType = (int) reader["invType"]; item.BasePermissions = (uint) reader["inventoryBasePermissions"]; item.EveryOnePermissions = (uint) reader["inventoryEveryOnePermissions"]; item.GroupPermissions = (uint) reader["inventoryGroupPermissions"]; item.SalePrice = (int) reader["salePrice"]; item.SaleType = unchecked((byte)(Convert.ToSByte(reader["saleType"]))); item.CreationDate = (int) reader["creationDate"]; item.GroupOwned = Convert.ToBoolean(reader["groupOwned"]); item.Flags = (uint) reader["flags"]; return item; } catch (MySqlException e) { m_log.Error(e.ToString()); } return null; } /// /// Returns a specified inventory item /// /// The item to return /// An inventory item public InventoryItemBase getInventoryItem(UUID itemID) { try { lock (m_dbLock) { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", itemID.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { InventoryItemBase item = null; if (reader.Read()) item = readInventoryItem(reader); return item; } } } } } catch (Exception e) { m_log.Error(e.Message, e); } return null; } /// /// Reads a list of inventory folders returned by a query. /// /// A MySQL Data Reader /// A List containing inventory folders protected static InventoryFolderBase readInventoryFolder(MySqlDataReader reader) { try { InventoryFolderBase folder = new InventoryFolderBase(); folder.Owner = new UUID((string) reader["agentID"]); folder.ParentID = new UUID((string) reader["parentFolderID"]); folder.ID = new UUID((string) reader["folderID"]); folder.Name = (string) reader["folderName"]; folder.Type = (short) reader["type"]; folder.Version = (ushort) ((int) reader["version"]); return folder; } catch (Exception e) { m_log.Error(e.Message, e); } return null; } /// /// Returns a specified inventory folder /// /// The folder to return /// A folder class public InventoryFolderBase getInventoryFolder(UUID folderID) { try { lock (m_dbLock) { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", folderID.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { InventoryFolderBase folder = null; if (reader.Read()) folder = readInventoryFolder(reader); return folder; } } } } } catch (Exception e) { m_log.Error(e.Message, e); return null; } } /// /// Adds a specified item to the database /// /// The inventory item public void addInventoryItem(InventoryItemBase item) { string sql = "REPLACE INTO inventoryitems (inventoryID, assetID, assetType, parentFolderID, avatarID, inventoryName" + ", inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions, invType" + ", creatorID, inventoryBasePermissions, inventoryEveryOnePermissions, inventoryGroupPermissions, salePrice, saleType" + ", creationDate, groupID, groupOwned, flags) VALUES "; sql += "(?inventoryID, ?assetID, ?assetType, ?parentFolderID, ?avatarID, ?inventoryName, ?inventoryDescription" + ", ?inventoryNextPermissions, ?inventoryCurrentPermissions, ?invType, ?creatorID" + ", ?inventoryBasePermissions, ?inventoryEveryOnePermissions, ?inventoryGroupPermissions, ?salePrice, ?saleType, ?creationDate" + ", ?groupID, ?groupOwned, ?flags)"; string itemName = item.Name; if (item.Name.Length > 64) { itemName = item.Name.Substring(0, 64); m_log.Warn("[INVENTORY DB]: Name field truncated from " + item.Name.Length + " to " + itemName.Length + " characters on add item"); } string itemDesc = item.Description; if (item.Description.Length > 128) { itemDesc = item.Description.Substring(0, 128); m_log.Warn("[INVENTORY DB]: Description field truncated from " + item.Description.Length + " to " + itemDesc.Length + " characters on add item"); } try { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); MySqlCommand result = new MySqlCommand(sql, dbcon); result.Parameters.AddWithValue("?inventoryID", item.ID.ToString()); result.Parameters.AddWithValue("?assetID", item.AssetID.ToString()); result.Parameters.AddWithValue("?assetType", item.AssetType.ToString()); result.Parameters.AddWithValue("?parentFolderID", item.Folder.ToString()); result.Parameters.AddWithValue("?avatarID", item.Owner.ToString()); result.Parameters.AddWithValue("?inventoryName", itemName); result.Parameters.AddWithValue("?inventoryDescription", itemDesc); result.Parameters.AddWithValue("?inventoryNextPermissions", item.NextPermissions.ToString()); result.Parameters.AddWithValue("?inventoryCurrentPermissions", item.CurrentPermissions.ToString()); result.Parameters.AddWithValue("?invType", item.InvType); result.Parameters.AddWithValue("?creatorID", item.CreatorId); result.Parameters.AddWithValue("?inventoryBasePermissions", item.BasePermissions); result.Parameters.AddWithValue("?inventoryEveryOnePermissions", item.EveryOnePermissions); result.Parameters.AddWithValue("?inventoryGroupPermissions", item.GroupPermissions); result.Parameters.AddWithValue("?salePrice", item.SalePrice); result.Parameters.AddWithValue("?saleType", unchecked((sbyte)item.SaleType)); result.Parameters.AddWithValue("?creationDate", item.CreationDate); result.Parameters.AddWithValue("?groupID", item.GroupID); result.Parameters.AddWithValue("?groupOwned", item.GroupOwned); result.Parameters.AddWithValue("?flags", item.Flags); lock (m_dbLock) { result.ExecuteNonQuery(); } result.Dispose(); result = new MySqlCommand("update inventoryfolders set version=version+1 where folderID = ?folderID", dbcon); result.Parameters.AddWithValue("?folderID", item.Folder.ToString()); lock (m_dbLock) { result.ExecuteNonQuery(); } result.Dispose(); } } catch (MySqlException e) { m_log.Error(e.ToString()); } } /// /// Updates the specified inventory item /// /// Inventory item to update public void updateInventoryItem(InventoryItemBase item) { addInventoryItem(item); } /// /// Detele the specified inventory item /// /// The inventory item UUID to delete public void deleteInventoryItem(UUID itemID) { try { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE inventoryID=?uuid", dbcon); cmd.Parameters.AddWithValue("?uuid", itemID.ToString()); lock (m_dbLock) { cmd.ExecuteNonQuery(); } } } catch (MySqlException e) { m_log.Error(e.Message, e); } } public InventoryItemBase queryInventoryItem(UUID itemID) { return getInventoryItem(itemID); } public InventoryFolderBase queryInventoryFolder(UUID folderID) { return getInventoryFolder(folderID); } /// /// Creates a new inventory folder /// /// Folder to create public void addInventoryFolder(InventoryFolderBase folder) { string sql = "REPLACE INTO inventoryfolders (folderID, agentID, parentFolderID, folderName, type, version) VALUES "; sql += "(?folderID, ?agentID, ?parentFolderID, ?folderName, ?type, ?version)"; string folderName = folder.Name; if (folderName.Length > 64) { folderName = folderName.Substring(0, 64); m_log.Warn("[INVENTORY DB]: Name field truncated from " + folder.Name.Length + " to " + folderName.Length + " characters on add folder"); } using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); MySqlCommand cmd = new MySqlCommand(sql, dbcon); cmd.Parameters.AddWithValue("?folderID", folder.ID.ToString()); cmd.Parameters.AddWithValue("?agentID", folder.Owner.ToString()); cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString()); cmd.Parameters.AddWithValue("?folderName", folderName); cmd.Parameters.AddWithValue("?type", folder.Type); cmd.Parameters.AddWithValue("?version", folder.Version); try { lock (m_dbLock) { cmd.ExecuteNonQuery(); } } catch (Exception e) { m_log.Error(e.ToString()); } } } /// /// Updates an inventory folder /// /// Folder to update public void updateInventoryFolder(InventoryFolderBase folder) { addInventoryFolder(folder); } /// /// Move an inventory folder /// /// Folder to move /// UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID public void moveInventoryFolder(InventoryFolderBase folder) { string sql = "UPDATE inventoryfolders SET parentFolderID=?parentFolderID WHERE folderID=?folderID"; using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); MySqlCommand cmd = new MySqlCommand(sql, dbcon); cmd.Parameters.AddWithValue("?folderID", folder.ID.ToString()); cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString()); try { lock (m_dbLock) { cmd.ExecuteNonQuery(); } } catch (Exception e) { m_log.Error(e.ToString()); } } } /// /// Append a list of all the child folders of a parent folder /// /// list where folders will be appended /// ID of parent protected void getInventoryFolders(ref List folders, UUID parentID) { List subfolderList = getInventoryFolders(parentID); foreach (InventoryFolderBase f in subfolderList) folders.Add(f); } /// /// See IInventoryDataPlugin /// /// /// public List getFolderHierarchy(UUID parentID) { /* Note: There are subtle changes between this implementation of getFolderHierarchy and the previous one * - We will only need to hit the database twice instead of n times. * - We assume the database is well-formed - no stranded/dangling folders, all folders in heirarchy owned * by the same person, each user only has 1 inventory heirarchy * - The returned list is not ordered, instead of breadth-first ordered There are basically 2 usage cases for getFolderHeirarchy: 1) Getting the user's entire inventory heirarchy when they log in 2) Finding a subfolder heirarchy to delete when emptying the trash. This implementation will pull all inventory folders from the database, and then prune away any folder that is not part of the requested sub-heirarchy. The theory is that it is cheaper to make 1 request from the database than to make n requests. This pays off only if requested heirarchy is large. By making this choice, we are making the worst case better at the cost of making the best case worse. This way is generally better because we don't have to rebuild the connection/sql query per subfolder, even if we end up getting more data from the SQL server than we need. - Francis */ try { List folders = new List(); Dictionary> hashtable = new Dictionary>(); ; List parentFolder = new List(); bool buildResultsFromHashTable = false; lock (m_dbLock) { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); /* Fetch the parent folder from the database to determine the agent ID, and if * we're querying the root of the inventory folder tree */ using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", parentID.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { // Should be at most 1 result while (reader.Read()) parentFolder.Add(readInventoryFolder(reader)); } } if (parentFolder.Count >= 1) // No result means parent folder does not exist { if (parentFolder[0].ParentID == UUID.Zero) // We are querying the root folder { /* Get all of the agent's folders from the database, put them in a list and return it */ using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE agentID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", parentFolder[0].Owner.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { while (reader.Read()) { InventoryFolderBase curFolder = readInventoryFolder(reader); if (curFolder.ID != parentID) // Do not need to add the root node of the tree to the list folders.Add(curFolder); } } } } // if we are querying the root folder else // else we are querying a subtree of the inventory folder tree { /* Get all of the agent's folders from the database, put them all in a hash table * indexed by their parent ID */ using (MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE agentID = ?uuid", dbcon)) { result.Parameters.AddWithValue("?uuid", parentFolder[0].Owner.ToString()); using (MySqlDataReader reader = result.ExecuteReader()) { while (reader.Read()) { InventoryFolderBase curFolder = readInventoryFolder(reader); if (hashtable.ContainsKey(curFolder.ParentID)) // Current folder already has a sibling hashtable[curFolder.ParentID].Add(curFolder); // append to sibling list else // else current folder has no known (yet) siblings { List siblingList = new List(); siblingList.Add(curFolder); // Current folder has no known (yet) siblings hashtable.Add(curFolder.ParentID, siblingList); } } // while more items to read from the database } } // Set flag so we know we need to build the results from the hash table after // we unlock the database buildResultsFromHashTable = true; } // else we are querying a subtree of the inventory folder tree } // if folder parentID exists if (buildResultsFromHashTable) { /* We have all of the user's folders stored in a hash table indexed by their parent ID * and we need to return the requested subtree. We will build the requested subtree * by performing a breadth-first-search on the hash table */ if (hashtable.ContainsKey(parentID)) folders.AddRange(hashtable[parentID]); for (int i = 0; i < folders.Count; i++) // **Note: folders.Count is *not* static if (hashtable.ContainsKey(folders[i].ID)) folders.AddRange(hashtable[folders[i].ID]); } } } // lock (database) return folders; } catch (Exception e) { m_log.Error(e.Message, e); return null; } } /// /// Delete a folder from database /// /// the folder UUID protected void deleteOneFolder(UUID folderID) { try { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryfolders WHERE folderID=?uuid", dbcon)) { cmd.Parameters.AddWithValue("?uuid", folderID.ToString()); lock (m_dbLock) cmd.ExecuteNonQuery(); } } } catch (MySqlException e) { m_log.Error(e.Message, e); } } /// /// Delete all item in a folder /// /// the folder UUID protected void deleteItemsInFolder(UUID folderID) { try { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE parentFolderID=?uuid", dbcon)) { cmd.Parameters.AddWithValue("?uuid", folderID.ToString()); lock (m_dbLock) cmd.ExecuteNonQuery(); } } } catch (MySqlException e) { m_log.Error(e.ToString()); } } /// /// Deletes an inventory folder /// /// Id of folder to delete public void deleteInventoryFolder(UUID folderID) { List subFolders = getFolderHierarchy(folderID); //Delete all sub-folders foreach (InventoryFolderBase f in subFolders) { deleteOneFolder(f.ID); deleteItemsInFolder(f.ID); } //Delete the actual row deleteOneFolder(folderID); deleteItemsInFolder(folderID); } public List fetchActiveGestures(UUID avatarID) { lock (m_dbLock) { try { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { dbcon.Open(); using (MySqlCommand sqlCmd = new MySqlCommand( "SELECT * FROM inventoryitems WHERE avatarId = ?uuid AND assetType = ?type and flags = 1", dbcon)) { sqlCmd.Parameters.AddWithValue("?uuid", avatarID.ToString()); sqlCmd.Parameters.AddWithValue("?type", (int)AssetType.Gesture); using (MySqlDataReader result = sqlCmd.ExecuteReader()) { List list = new List(); while (result.Read()) { InventoryItemBase item = readInventoryItem(result); if (item != null) list.Add(item); } return list; } } } } catch (Exception e) { m_log.Error(e.Message, e); return null; } } } } }