From 8714f592778e85af10f42bd7b9c4e3d73a3f72f2 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sun, 23 Nov 2025 00:35:17 +0000 Subject: [PATCH] a few changes to pgsql, Thx Tampa, (untested, may be bad) --- OpenSim/Data/PGSQL/PGSQLUserAccountData.cs | 13 +--- OpenSim/Data/PGSQL/PGSQLXAssetData.cs | 43 +++++------ OpenSim/Data/PGSQL/PGSQLXInventoryData.cs | 86 +++++++++------------- bin/System.Data.SQLite.dll.config | 2 +- 4 files changed, 61 insertions(+), 83 deletions(-) diff --git a/OpenSim/Data/PGSQL/PGSQLUserAccountData.cs b/OpenSim/Data/PGSQL/PGSQLUserAccountData.cs index b477550035..3dcb6a193a 100644 --- a/OpenSim/Data/PGSQL/PGSQLUserAccountData.cs +++ b/OpenSim/Data/PGSQL/PGSQLUserAccountData.cs @@ -26,11 +26,9 @@ */ using System; -using System.Collections; using System.Collections.Generic; using System.Data; using OpenMetaverse; -using OpenSim.Framework; using System.Text; using Npgsql; using log4net; @@ -295,17 +293,14 @@ namespace OpenSim.Data.PGSQL return new UserAccountData[0]; string sql = ""; - UUID scope_id; - UUID.TryParse(scopeID.ToString(), out scope_id); - using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) using (NpgsqlCommand cmd = new NpgsqlCommand()) { if (words.Length == 1) { sql = String.Format(@"select * from {0} where (""ScopeID""=:ScopeID or ""ScopeID""=:UUIDZero) and (LOWER(""FirstName"" COLLATE ""en_US.utf8"") like LOWER(:search) or LOWER(""LastName"" COLLATE ""en_US.utf8"") like LOWER(:search))", m_Realm); - cmd.Parameters.Add(m_database.CreateParameter("ScopeID", (UUID)scope_id)); - cmd.Parameters.Add (m_database.CreateParameter("UUIDZero", (UUID)UUID.Zero)); + cmd.Parameters.Add(m_database.CreateParameter("ScopeID", scopeID)); + cmd.Parameters.Add (m_database.CreateParameter("UUIDZero", UUID.Zero)); cmd.Parameters.Add(m_database.CreateParameter("search", "%" + words[0] + "%")); } else @@ -313,8 +308,8 @@ namespace OpenSim.Data.PGSQL sql = String.Format(@"select * from {0} where (""ScopeID""=:ScopeID or ""ScopeID""=:UUIDZero) and (LOWER(""FirstName"" COLLATE ""en_US.utf8"") like LOWER(:searchFirst) or LOWER(""LastName"" COLLATE ""en_US.utf8"") like LOWER(:searchLast))", m_Realm); cmd.Parameters.Add(m_database.CreateParameter("searchFirst", "%" + words[0] + "%")); cmd.Parameters.Add(m_database.CreateParameter("searchLast", "%" + words[1] + "%")); - cmd.Parameters.Add (m_database.CreateParameter("UUIDZero", (UUID)UUID.Zero)); - cmd.Parameters.Add(m_database.CreateParameter("ScopeID", (UUID)scope_id)); + cmd.Parameters.Add (m_database.CreateParameter("UUIDZero", UUID.Zero)); + cmd.Parameters.Add(m_database.CreateParameter("ScopeID", scopeID)); } cmd.Connection = conn; cmd.CommandText = sql; diff --git a/OpenSim/Data/PGSQL/PGSQLXAssetData.cs b/OpenSim/Data/PGSQL/PGSQLXAssetData.cs index e1195b15d4..3771c66439 100644 --- a/OpenSim/Data/PGSQL/PGSQLXAssetData.cs +++ b/OpenSim/Data/PGSQL/PGSQLXAssetData.cs @@ -32,11 +32,9 @@ using System.IO; using System.IO.Compression; using System.Reflection; using System.Security.Cryptography; -using System.Text; using log4net; using OpenMetaverse; using OpenSim.Framework; -using OpenSim.Data; using Npgsql; namespace OpenSim.Data.PGSQL @@ -58,7 +56,7 @@ namespace OpenSim.Data.PGSQL private bool m_enableCompression = false; private PGSQLManager m_database; private string m_connectionString; - private object m_dbLock = new object(); + private object m_dbLock = new(); /// /// We can reuse this for all hashing since all methods are single-threaded through m_dbBLock @@ -212,6 +210,9 @@ namespace OpenSim.Data.PGSQL { // m_log.DebugFormat("[XASSETS DB]: Storing asset {0} {1}", asset.Name, asset.ID); + if (!UUID.TryParse(asset.ID, out UUID asset_id)) + return; + lock (m_dbLock) { using (NpgsqlConnection dbcon = new NpgsqlConnection(m_connectionString)) @@ -244,7 +245,6 @@ namespace OpenSim.Data.PGSQL using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false)) { - // Console.WriteLine(WebUtil.CopyTo(new MemoryStream(asset.Data), compressionStream, int.MaxValue)); // We have to close the compression stream in order to make sure it writes everything out to the underlying memory output stream. compressionStream.Close(); byte[] compressedData = outputStream.ToArray(); @@ -254,12 +254,9 @@ namespace OpenSim.Data.PGSQL byte[] hash = hasher.ComputeHash(asset.Data); - UUID asset_id; - UUID.TryParse(asset.ID, out asset_id); - -// m_log.DebugFormat( -// "[XASSET DB]: Compressed data size for {0} {1}, hash {2} is {3}", -// asset.ID, asset.Name, hash, compressedData.Length); + //m_log.DebugFormat( + // "[XASSET DB]: Compressed data size for {0} {1}, hash {2} is {3}", + // asset.ID, asset.Name, hash, compressedData.Length); try { @@ -352,6 +349,9 @@ namespace OpenSim.Data.PGSQL if ((now - Utils.UnixTimeToDateTime(accessTime)).TotalDays < DaysBetweenAccessTimeUpdates) return; + if(!UUID.TryParse(assetMetadata.ID, out UUID asset_id)) + return; + lock (m_dbLock) { using (NpgsqlConnection dbcon = new NpgsqlConnection(m_connectionString)) @@ -362,14 +362,11 @@ namespace OpenSim.Data.PGSQL try { - UUID asset_id; - UUID.TryParse(assetMetadata.ID, out asset_id); - using (cmd) { // create unix epoch time - cmd.Parameters.Add(m_database.CreateParameter("id", asset_id)); - cmd.Parameters.Add(m_database.CreateParameter("access_time", (int)Utils.DateTimeToUnixTime(now))); + cmd.Parameters.Add(m_database.CreateParameter("ID", asset_id)); + cmd.Parameters.Add(m_database.CreateParameter("AccessTime", (int)Utils.DateTimeToUnixTime(now))); cmd.ExecuteNonQuery(); } } @@ -407,7 +404,7 @@ namespace OpenSim.Data.PGSQL { if (dbReader.Read()) { -// m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid); + // m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid); exists = true; } } @@ -431,7 +428,7 @@ namespace OpenSim.Data.PGSQL public bool[] AssetsExist(UUID[] uuids) { if (uuids.Length == 0) - return new bool[0]; + return []; HashSet exist = new HashSet(); @@ -467,7 +464,7 @@ namespace OpenSim.Data.PGSQL /// true if it exists, false otherwise. public bool ExistsAsset(UUID uuid) { -// m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid); + // m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid); bool assetExists = false; @@ -478,7 +475,7 @@ namespace OpenSim.Data.PGSQL dbcon.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand(@"SELECT id FROM XAssetsMeta WHERE id=:ID", dbcon)) { - cmd.Parameters.Add(m_database.CreateParameter("id", uuid)); + cmd.Parameters.Add(m_database.CreateParameter("ID", uuid)); try { @@ -493,7 +490,7 @@ namespace OpenSim.Data.PGSQL } catch (Exception e) { - m_log.Error(string.Format("[XASSETS DB]: PGSql failure fetching asset {0}", uuid), e); + m_log.Error($"[XASSETS DB]: PGSql failure fetching asset {uuid}", e); } } } @@ -522,9 +519,9 @@ namespace OpenSim.Data.PGSQL dbcon.Open(); using(NpgsqlCommand cmd = new NpgsqlCommand(@"SELECT name, description, access_time, ""AssetType"", temporary, id, asset_flags, creatorid FROM XAssetsMeta - LIMIT :start, :count",dbcon)) + LIMIT :start, :count", dbcon)) { - cmd.Parameters.Add(m_database.CreateParameter("start",start)); + cmd.Parameters.Add(m_database.CreateParameter("start", start)); cmd.Parameters.Add(m_database.CreateParameter("count", count)); try @@ -574,7 +571,7 @@ namespace OpenSim.Data.PGSQL using (NpgsqlCommand cmd = new NpgsqlCommand(@"delete from XAssetsMeta where id=:ID", dbcon)) { - cmd.Parameters.Add(m_database.CreateParameter(id, id)); + cmd.Parameters.Add(m_database.CreateParameter("ID", id)); cmd.ExecuteNonQuery(); } diff --git a/OpenSim/Data/PGSQL/PGSQLXInventoryData.cs b/OpenSim/Data/PGSQL/PGSQLXInventoryData.cs index e1a3cf08ee..a3da7fb8b4 100644 --- a/OpenSim/Data/PGSQL/PGSQLXInventoryData.cs +++ b/OpenSim/Data/PGSQL/PGSQLXInventoryData.cs @@ -26,42 +26,23 @@ */ using System; -using System.Collections; -using System.Collections.Generic; -using System.Data; using OpenMetaverse; -using OpenSim.Framework; -using System.Reflection; -using System.Text; -using log4net; using Npgsql; -using NpgsqlTypes; namespace OpenSim.Data.PGSQL { public class PGSQLXInventoryData : IXInventoryData { -// private static readonly ILog m_log = LogManager.GetLogger( -// MethodBase.GetCurrentMethod().DeclaringType); + // private static readonly ILog m_log = LogManager.GetLogger( + // MethodBase.GetCurrentMethod().DeclaringType); private PGSQLFolderHandler m_Folders; private PGSQLItemHandler m_Items; public PGSQLXInventoryData(string conn, string realm) { - m_Folders = new PGSQLFolderHandler( - conn, "inventoryfolders", "InventoryStore"); - m_Items = new PGSQLItemHandler( - conn, "inventoryitems", String.Empty); - } - - public static UUID str2UUID(string strUUID) - { - UUID newUUID = UUID.Zero; - - UUID.TryParse(strUUID, out newUUID); - - return newUUID; + m_Folders = new PGSQLFolderHandler(conn, "inventoryfolders", "InventoryStore"); + m_Items = new PGSQLItemHandler(conn, "inventoryitems", string.Empty); } public XInventoryFolder[] GetFolder(string field, string val) @@ -144,9 +125,14 @@ namespace OpenSim.Data.PGSQL { } - public bool MoveItem(string id, string newParent) + public bool MoveItem(string idstr, string newParentstr) { - XInventoryItem[] retrievedItems = Get(new string[] { "inventoryID" }, new string[] { id }); + if(!UUID.TryParse(idstr, out UUID id)) + return false; + if(!UUID.TryParse(newParentstr, out UUID newParent)) + return false; + + XInventoryItem[] retrievedItems = Get(["inventoryID"], [idstr]); if (retrievedItems.Length == 0) return false; @@ -156,7 +142,7 @@ namespace OpenSim.Data.PGSQL { using (NpgsqlCommand cmd = new NpgsqlCommand()) { - cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""inventoryID"" = :InventoryID", m_Realm); + cmd.CommandText = $"update {m_Realm} set parentFolderID = :ParentFolderID where inventoryID = :InventoryID"; cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newParent)); cmd.Parameters.Add(m_database.CreateParameter("InventoryID", id )); cmd.Connection = conn; @@ -175,18 +161,18 @@ namespace OpenSim.Data.PGSQL public XInventoryItem[] GetActiveGestures(string principalID) { + if(!UUID.TryParse(principalID, out UUID princID)) + return []; + using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) { using (NpgsqlCommand cmd = new NpgsqlCommand()) { -// cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1", m_Realm); + // cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1", m_Realm); - cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1"); + cmd.CommandText = "select * from inventoryitems where avatarID = :uuid and assetType = :type and flags = 1"; - UUID princID = UUID.Zero; - UUID.TryParse(principalID, out princID); - - cmd.Parameters.Add(m_database.CreateParameter("uuid", principalID)); + cmd.Parameters.Add(m_database.CreateParameter("uuid", princID)); cmd.Parameters.Add(m_database.CreateParameter("type", (int)AssetType.Gesture)); cmd.Connection = conn; conn.Open(); @@ -208,11 +194,11 @@ namespace OpenSim.Data.PGSQL and ""assetID"" = :AssetID group by ""assetID"" ", m_Realm); */ - cmd.CommandText = String.Format(@"select bit_or(""inventoryCurrentPermissions"") as ""inventoryCurrentPermissions"" + cmd.CommandText = @"select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions from inventoryitems - where ""avatarID""::uuid = :PrincipalID - and ""assetID""::uuid = :AssetID - group by ""assetID"" "); + where avatarID::uuid = :PrincipalID + and assetID::uuid = :AssetID + group by assetID"; cmd.Parameters.Add(m_database.CreateParameter("PrincipalID", principalID)); cmd.Parameters.Add(m_database.CreateParameter("AssetID", assetID)); @@ -255,7 +241,13 @@ namespace OpenSim.Data.PGSQL public bool MoveFolder(string id, string newParentFolderID) { - XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id }); + if(!UUID.TryParse(id, out UUID foldID)) + return false; + + if(!UUID.TryParse(newParentFolderID, out UUID newPar)) + return false; + + XInventoryFolder[] folders = Get(["folderID"], [id]); if (folders.Length == 0) return false; @@ -266,13 +258,8 @@ namespace OpenSim.Data.PGSQL { using (NpgsqlCommand cmd = new NpgsqlCommand()) { - UUID foldID = UUID.Zero; - UUID.TryParse(id, out foldID); - UUID newPar = UUID.Zero; - UUID.TryParse(newParentFolderID, out newPar); - - cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""folderID"" = :folderID", m_Realm); + cmd.CommandText = $"update {m_Realm} set parentFolderID = :ParentFolderID where folderID = :folderID"; cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newPar)); cmd.Parameters.Add(m_database.CreateParameter("folderID", foldID)); cmd.Connection = conn; @@ -311,20 +298,19 @@ namespace OpenSim.Data.PGSQL protected bool IncrementFolderVersion(string folderID) { -// m_log.DebugFormat("[PGSQL ITEM HANDLER]: Incrementing version on folder {0}", folderID); -// Util.PrintCallStack(); + //m_log.DebugFormat("[PGSQL ITEM HANDLER]: Incrementing version on folder {0}", folderID); + //Util.PrintCallStack(); - string sql = @"update inventoryfolders set version=version+1 where ""folderID"" = :folderID"; + if(!UUID.TryParse(folderID, out UUID foldID)) + return false; + + string sql = @"update inventoryfolders set version=version+1 where folderID = :folderID"; using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) { using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn)) { - UUID foldID = UUID.Zero; - UUID.TryParse(folderID, out foldID); - conn.Open(); - cmd.Parameters.Add( m_database.CreateParameter("folderID", foldID) ); try diff --git a/bin/System.Data.SQLite.dll.config b/bin/System.Data.SQLite.dll.config index 466aa6cb31..4daee4a254 100644 --- a/bin/System.Data.SQLite.dll.config +++ b/bin/System.Data.SQLite.dll.config @@ -3,5 +3,5 @@ - + \ No newline at end of file