From 9976cb93ce351f45dea77e3389e0159b866757ae Mon Sep 17 00:00:00 2001 From: AlexRa Date: Sun, 23 May 2010 11:26:53 +0300 Subject: Further corrections to MS SQL stores (now passes all tests) Besides, AssetData is slightly optimized to StoreAsset in one request ("IF EXISTS() UPDATE ... ELSE INSERT ...") The main change in the MS SQL Inventory implem. is that it now return empty list (or whatever) when called with UUID.Zero, which is consistent with how the code for other DBs work. I did no changes at all in XInventory, as there is no test set for them. --- OpenSim/Data/MSSQL/MSSQLAssetData.cs | 87 +++++++------------------------- OpenSim/Data/MSSQL/MSSQLInventoryData.cs | 51 ++++++++++++------- 2 files changed, 49 insertions(+), 89 deletions(-) (limited to 'OpenSim') diff --git a/OpenSim/Data/MSSQL/MSSQLAssetData.cs b/OpenSim/Data/MSSQL/MSSQLAssetData.cs index ec9d4f6..c7488d8 100644 --- a/OpenSim/Data/MSSQL/MSSQLAssetData.cs +++ b/OpenSim/Data/MSSQL/MSSQLAssetData.cs @@ -145,26 +145,19 @@ namespace OpenSim.Data.MSSQL /// the asset override public void StoreAsset(AssetBase asset) { - if (ExistsAsset(asset.FullID)) - UpdateAsset(asset); - else - InsertAsset(asset); - } - - - private void InsertAsset(AssetBase asset) - { - if (ExistsAsset(asset.FullID)) - { - return; - } - - string sql = @"INSERT INTO assets - ([id], [name], [description], [assetType], [local], - [temporary], [create_time], [access_time], [creatorid], [asset_flags], [data]) - VALUES - (@id, @name, @description, @assetType, @local, - @temporary, @create_time, @access_time, @creatorid, @asset_flags, @data)"; + + string sql = + @"IF EXISTS(SELECT * FROM assets WHERE id=@id) + UPDATE assets set name = @name, description = @description, assetType = @assetType, + local = @local, temporary = @temporary, creatorid = @creatorid, data = @data + WHERE id=@id + ELSE + INSERT INTO assets + ([id], [name], [description], [assetType], [local], + [temporary], [create_time], [access_time], [creatorid], [asset_flags], [data]) + VALUES + (@id, @name, @description, @assetType, @local, + @temporary, @create_time, @access_time, @creatorid, @asset_flags, @data)"; string assetName = asset.Name; if (asset.Name.Length > 64) @@ -202,58 +195,11 @@ namespace OpenSim.Data.MSSQL } catch(Exception e) { - m_log.Error("[ASSET DB]: Error inserting item :" + e.Message); + m_log.Error("[ASSET DB]: Error storing item :" + e.Message); } } } - /// - /// Update asset in m_database - /// - /// the asset - private void UpdateAsset(AssetBase asset) - { - string sql = @"UPDATE assets set name = @name, description = @description, assetType = @assetType, - local = @local, temporary = @temporary, data = @data - , creatorid = @creatorid - WHERE id = @keyId;"; - - string assetName = asset.Name; - if (asset.Name.Length > 64) - { - assetName = asset.Name.Substring(0, 64); - m_log.Warn("[ASSET DB]: Name field truncated from " + asset.Name.Length + " to " + assetName.Length + " characters on update"); - } - - string assetDescription = asset.Description; - if (asset.Description.Length > 64) - { - assetDescription = asset.Description.Substring(0, 64); - m_log.Warn("[ASSET DB]: Description field truncated from " + asset.Description.Length + " to " + assetDescription.Length + " characters on update"); - } - - using (SqlConnection conn = new SqlConnection(m_connectionString)) - using (SqlCommand command = new SqlCommand(sql, conn)) - { - command.Parameters.Add(m_database.CreateParameter("keyId", asset.FullID)); - command.Parameters.Add(m_database.CreateParameter("name", assetName)); - command.Parameters.Add(m_database.CreateParameter("description", assetDescription)); - command.Parameters.Add(m_database.CreateParameter("assetType", asset.Type)); - command.Parameters.Add(m_database.CreateParameter("local", asset.Local)); - command.Parameters.Add(m_database.CreateParameter("temporary", asset.Temporary)); - command.Parameters.Add(m_database.CreateParameter("data", asset.Data)); - command.Parameters.Add(m_database.CreateParameter("creatorid", asset.Metadata.CreatorID)); - conn.Open(); - try - { - command.ExecuteNonQuery(); - } - catch (Exception e) - { - m_log.Error(e.ToString()); - } - } - } // Commented out since currently unused - this probably should be called in GetAsset() // private void UpdateAccessTime(AssetBase asset) @@ -302,7 +248,7 @@ namespace OpenSim.Data.MSSQL string sql = @"WITH OrderedAssets AS ( SELECT id, name, description, assetType, temporary, creatorid, - Row = ROW_NUMBER() OVER (ORDER BY id) + RowNumber = ROW_NUMBER() OVER (ORDER BY id) FROM assets ) SELECT * @@ -320,12 +266,13 @@ namespace OpenSim.Data.MSSQL while (reader.Read()) { AssetMetadata metadata = new AssetMetadata(); - metadata.FullID = new UUID((Guid)reader["id"]); + metadata.FullID = DBGuid.FromDB(reader["id"]); metadata.Name = (string)reader["name"]; metadata.Description = (string)reader["description"]; metadata.Type = Convert.ToSByte(reader["assetType"]); metadata.Temporary = Convert.ToBoolean(reader["temporary"]); metadata.CreatorID = (string)reader["creatorid"]; + retList.Add(metadata); } } } diff --git a/OpenSim/Data/MSSQL/MSSQLInventoryData.cs b/OpenSim/Data/MSSQL/MSSQLInventoryData.cs index 4815700..4d06377 100644 --- a/OpenSim/Data/MSSQL/MSSQLInventoryData.cs +++ b/OpenSim/Data/MSSQL/MSSQLInventoryData.cs @@ -111,6 +111,9 @@ namespace OpenSim.Data.MSSQL /// A list of folder objects public List getUserRootFolders(UUID user) { + if (user == UUID.Zero) + return new List(); + return getInventoryFolders(UUID.Zero, user); } @@ -184,7 +187,19 @@ namespace OpenSim.Data.MSSQL //Note maybe change this to use a Dataset that loading in all folders of a user and then go throw it that way. //Note this is changed so it opens only one connection to the database and not everytime it wants to get data. + /* NOTE: the implementation below is very inefficient (makes a separate request to get subfolders for + * every found folder, recursively). Inventory code for other DBs has been already rewritten to get ALL + * inventory for a specific user at once. + * + * Meanwhile, one little thing is corrected: getFolderHierarchy(UUID.Zero) doesn't make sense and should never + * be used, so check for that and return an empty list. + */ + List folders = new List(); + + if (parentID == UUID.Zero) + return folders; + string sql = "SELECT * FROM inventoryfolders WHERE parentFolderID = @parentID"; using (SqlConnection conn = new SqlConnection(m_connectionString)) using (SqlCommand cmd = new SqlCommand(sql, conn)) @@ -249,13 +264,12 @@ namespace OpenSim.Data.MSSQL /// Folder to update public void updateInventoryFolder(InventoryFolderBase folder) { - string sql = @"UPDATE inventoryfolders SET folderID = @folderID, - agentID = @agentID, + string sql = @"UPDATE inventoryfolders SET agentID = @agentID, parentFolderID = @parentFolderID, folderName = @folderName, type = @type, version = @version - WHERE folderID = @keyFolderID"; + WHERE folderID = @folderID"; string folderName = folder.Name; if (folderName.Length > 64) @@ -272,7 +286,6 @@ namespace OpenSim.Data.MSSQL cmd.Parameters.Add(database.CreateParameter("folderName", folderName)); cmd.Parameters.Add(database.CreateParameter("type", folder.Type)); cmd.Parameters.Add(database.CreateParameter("version", folder.Version)); - cmd.Parameters.Add(database.CreateParameter("@keyFolderID", folder.ID)); conn.Open(); try { @@ -296,7 +309,7 @@ namespace OpenSim.Data.MSSQL using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.Add(database.CreateParameter("parentFolderID", folder.ParentID)); - cmd.Parameters.Add(database.CreateParameter("@folderID", folder.ID)); + cmd.Parameters.Add(database.CreateParameter("folderID", folder.ID)); conn.Open(); try { @@ -489,8 +502,7 @@ namespace OpenSim.Data.MSSQL /// Inventory item to update public void updateInventoryItem(InventoryItemBase item) { - string sql = @"UPDATE inventoryitems SET inventoryID = @inventoryID, - assetID = @assetID, + string sql = @"UPDATE inventoryitems SET assetID = @assetID, assetType = @assetType, parentFolderID = @parentFolderID, avatarID = @avatarID, @@ -502,13 +514,14 @@ namespace OpenSim.Data.MSSQL creatorID = @creatorID, inventoryBasePermissions = @inventoryBasePermissions, inventoryEveryOnePermissions = @inventoryEveryOnePermissions, + inventoryGroupPermissions = @inventoryGroupPermissions, salePrice = @salePrice, saleType = @saleType, creationDate = @creationDate, groupID = @groupID, groupOwned = @groupOwned, flags = @flags - WHERE inventoryID = @keyInventoryID"; + WHERE inventoryID = @inventoryID"; string itemName = item.Name; if (item.Name.Length > 64) @@ -537,16 +550,16 @@ namespace OpenSim.Data.MSSQL command.Parameters.Add(database.CreateParameter("inventoryNextPermissions", item.NextPermissions)); command.Parameters.Add(database.CreateParameter("inventoryCurrentPermissions", item.CurrentPermissions)); command.Parameters.Add(database.CreateParameter("invType", item.InvType)); - command.Parameters.Add(database.CreateParameter("creatorID", item.CreatorIdAsUuid)); + command.Parameters.Add(database.CreateParameter("creatorID", item.CreatorId)); command.Parameters.Add(database.CreateParameter("inventoryBasePermissions", item.BasePermissions)); command.Parameters.Add(database.CreateParameter("inventoryEveryOnePermissions", item.EveryOnePermissions)); + command.Parameters.Add(database.CreateParameter("inventoryGroupPermissions", item.GroupPermissions)); command.Parameters.Add(database.CreateParameter("salePrice", item.SalePrice)); command.Parameters.Add(database.CreateParameter("saleType", item.SaleType)); command.Parameters.Add(database.CreateParameter("creationDate", item.CreationDate)); command.Parameters.Add(database.CreateParameter("groupID", item.GroupID)); command.Parameters.Add(database.CreateParameter("groupOwned", item.GroupOwned)); command.Parameters.Add(database.CreateParameter("flags", item.Flags)); - command.Parameters.Add(database.CreateParameter("keyInventoryID", item.ID)); conn.Open(); try { @@ -732,9 +745,9 @@ namespace OpenSim.Data.MSSQL try { InventoryFolderBase folder = new InventoryFolderBase(); - folder.Owner = new UUID((Guid)reader["agentID"]); - folder.ParentID = new UUID((Guid)reader["parentFolderID"]); - folder.ID = new UUID((Guid)reader["folderID"]); + folder.Owner = DBGuid.FromDB(reader["agentID"]); + folder.ParentID = DBGuid.FromDB(reader["parentFolderID"]); + folder.ID = DBGuid.FromDB(reader["folderID"]); folder.Name = (string)reader["folderName"]; folder.Type = (short)reader["type"]; folder.Version = Convert.ToUInt16(reader["version"]); @@ -760,24 +773,24 @@ namespace OpenSim.Data.MSSQL { InventoryItemBase item = new InventoryItemBase(); - item.ID = new UUID((Guid)reader["inventoryID"]); - item.AssetID = new UUID((Guid)reader["assetID"]); + item.ID = DBGuid.FromDB(reader["inventoryID"]); + item.AssetID = DBGuid.FromDB(reader["assetID"]); item.AssetType = Convert.ToInt32(reader["assetType"].ToString()); - item.Folder = new UUID((Guid)reader["parentFolderID"]); - item.Owner = new UUID((Guid)reader["avatarID"]); + item.Folder = DBGuid.FromDB(reader["parentFolderID"]); + item.Owner = DBGuid.FromDB(reader["avatarID"]); item.Name = reader["inventoryName"].ToString(); item.Description = reader["inventoryDescription"].ToString(); item.NextPermissions = Convert.ToUInt32(reader["inventoryNextPermissions"]); item.CurrentPermissions = Convert.ToUInt32(reader["inventoryCurrentPermissions"]); item.InvType = Convert.ToInt32(reader["invType"].ToString()); - item.CreatorId = ((Guid)reader["creatorID"]).ToString(); + item.CreatorId = reader["creatorID"].ToString(); item.BasePermissions = Convert.ToUInt32(reader["inventoryBasePermissions"]); item.EveryOnePermissions = Convert.ToUInt32(reader["inventoryEveryOnePermissions"]); item.GroupPermissions = Convert.ToUInt32(reader["inventoryGroupPermissions"]); item.SalePrice = Convert.ToInt32(reader["salePrice"]); item.SaleType = Convert.ToByte(reader["saleType"]); item.CreationDate = Convert.ToInt32(reader["creationDate"]); - item.GroupID = new UUID((Guid)reader["groupID"]); + item.GroupID = DBGuid.FromDB(reader["groupID"]); item.GroupOwned = Convert.ToBoolean(reader["groupOwned"]); item.Flags = Convert.ToUInt32(reader["flags"]); -- cgit v1.1