From 72e0442ac8a76e44805e1f11363002785b7024dd Mon Sep 17 00:00:00 2001 From: Alexander van Delft Date: Thu, 13 Jun 2024 16:22:07 +0200 Subject: [PATCH] [REVERT] Commit about adding rawSessionInstant in CDP4TransactionManager --- CometServer/Helpers/Cdp4TransactionManager.cs | 177 +++++++++--------- .../Helpers/ICdp4TransactionManager.cs | 49 ++--- 2 files changed, 111 insertions(+), 115 deletions(-) diff --git a/CometServer/Helpers/Cdp4TransactionManager.cs b/CometServer/Helpers/Cdp4TransactionManager.cs index ffb9af33..78d0585e 100644 --- a/CometServer/Helpers/Cdp4TransactionManager.cs +++ b/CometServer/Helpers/Cdp4TransactionManager.cs @@ -44,9 +44,7 @@ namespace CometServer.Helpers using ServiceUtils = Services.Utils; /// - /// The purpose of the is provide a for - /// read and write operations to the database while configuring the database to properly process - /// any temporal database interactions + /// A wrapper class for the class, allowing temporal database interaction. /// public class Cdp4TransactionManager : ICdp4TransactionManager { @@ -110,11 +108,6 @@ public class Cdp4TransactionManager : ICdp4TransactionManager /// private bool isFullAccessGranted; - /// - /// Backing field for the cached rawSessionInstant value - /// - private object rawSessionInstant; - /// /// Gets or sets the iteration setup dao. /// @@ -130,14 +123,6 @@ public class Cdp4TransactionManager : ICdp4TransactionManager /// public IterationSetup IterationSetup { get; private set; } - /// - /// Initializes a new instance of the - /// - public Cdp4TransactionManager() - { - this.rawSessionInstant = null; - } - /// /// The setup transaction. /// @@ -183,17 +168,13 @@ public NpgsqlTransaction SetupTransaction(ref NpgsqlConnection connection, Crede } /// - /// Get the current session time instant value from the database. In case the most current data is to be - /// retrieved this value returns (+infinity) which translates to . In case - /// a request is made related to time-travel the corresponding to the period_end - /// is returned + /// Get the current session time instant. /// /// /// The current transaction to the database. /// /// - /// A that represents the session Instant (either or - /// the corresponding to the period_end + /// The . /// public DateTime GetSessionInstant(NpgsqlTransaction transaction) { @@ -201,31 +182,23 @@ public DateTime GetSessionInstant(NpgsqlTransaction transaction) } /// - /// Get the raw current session time instant value from the database. In case the most current data is to be - /// retrieved this value returns (+infinity) which translates to . In case - /// a request is made related to time-travel the corresponding to the period_end - /// is returned + /// Get the raw current session time instant value from the database. /// /// /// The current transaction to the database. /// /// - /// A that represents the session Instant (either or - /// the corresponding to the period_end + /// The . /// public object GetRawSessionInstant(NpgsqlTransaction transaction) { - if (this.rawSessionInstant == null) - { - using var command = new NpgsqlCommand( - "SELECT * FROM \"SiteDirectory\".\"get_session_instant\"();", - transaction.Connection, - transaction); - - this.rawSessionInstant = command.ExecuteScalar(); + using (var command = new NpgsqlCommand( + "SELECT * FROM \"SiteDirectory\".\"get_session_instant\"();", + transaction.Connection, + transaction)) + { + return command.ExecuteScalar(); } - - return this.rawSessionInstant; } /// @@ -283,41 +256,61 @@ public bool IsFullAccessEnabled() } /// - /// Get the timestamp in the form of a that the was - /// created using the method. + /// Get the session timeframe start time. /// /// - /// The active + /// The current transaction to the database. /// /// /// The . /// - public DateTime GetTransactionTime(NpgsqlTransaction transaction) + public DateTime GetSessionTimeFrameStart(NpgsqlTransaction transaction) { - using var command = new NpgsqlCommand( - $"SELECT * FROM \"SiteDirectory\".\"{"get_transaction_time"}\"();", + using (var command = new NpgsqlCommand( + string.Format("SELECT * FROM \"SiteDirectory\".\"{0}\"();", "get_session_timeframe_start"), transaction.Connection, - transaction); + transaction)) + { + return (DateTime)command.ExecuteScalar(); + } + } - return (DateTime)command.ExecuteScalar(); + /// + /// Get the current transaction time. + /// + /// + /// The current transaction to the database. + /// + /// + /// The . + /// + public DateTime GetTransactionTime(NpgsqlTransaction transaction) + { + using (var command = new NpgsqlCommand( + string.Format("SELECT * FROM \"SiteDirectory\".\"{0}\"();", "get_transaction_time"), + transaction.Connection, + transaction)) + { + return (DateTime)command.ExecuteScalar(); + } } /// - /// Sets the default temporal context (-infinity, +infinity) + /// The set default context. /// /// - /// The active + /// The current transaction to the database. /// public void SetDefaultContext(NpgsqlTransaction transaction) { var sqlBuilder = new StringBuilder(); - sqlBuilder.AppendFormat( "UPDATE {0} SET ({1}, {2}) = ('-infinity', 'infinity');", TransactionInfoTable, PeriodStartColumn, PeriodEndColumn); - - using var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction); - - command.ExecuteNonQuery(); + + using (var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction)) + { + command.ExecuteNonQuery(); + } } /// @@ -333,11 +326,12 @@ public void SetAuditLoggingState(NpgsqlTransaction transaction, bool enabled) { var sql = $"UPDATE {TransactionInfoTable} SET {TransactionAuditEnabled} = :{TransactionAuditEnabled};"; - using var command = new NpgsqlCommand(sql, transaction.Connection, transaction); - - command.Parameters.Add($"{TransactionAuditEnabled}", NpgsqlDbType.Boolean).Value = enabled; + using (var command = new NpgsqlCommand(sql, transaction.Connection, transaction)) + { + command.Parameters.Add($"{TransactionAuditEnabled}", NpgsqlDbType.Boolean).Value = enabled; - command.ExecuteNonQuery(); + command.ExecuteNonQuery(); + } } /// @@ -388,8 +382,6 @@ public void SetIterationContext(NpgsqlTransaction transaction, string partition, /// private NpgsqlTransaction GetTransaction(ref NpgsqlConnection connection, Credentials credentials) { - this.rawSessionInstant = null; - var transaction = this.SetupNewTransaction(ref connection); CreateTransactionInfoTable(transaction); CreateDefaultTransactionInfoEntry(transaction, credentials); @@ -425,11 +417,12 @@ private static void ApplyIterationContext(NpgsqlTransaction transaction, string sqlBuilder.AppendFormat("FROM \"{0}\".\"IterationRevisionLog\" iteration_log LEFT JOIN \"{0}\".\"RevisionRegistry\" revision_from ON iteration_log.\"FromRevision\" = revision_from.\"Revision\" LEFT JOIN \"{0}\".\"RevisionRegistry\" revision_to ON iteration_log.\"ToRevision\" = revision_to.\"Revision\") IterationLogRevision", partition); sqlBuilder.Append(" WHERE \"IterationIid\" = :iterationIid);"); - using var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction); - - command.Parameters.Add("iterationIid", NpgsqlDbType.Uuid).Value = iterationSetup.IterationIid; + using (var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction)) + { + command.Parameters.Add("iterationIid", NpgsqlDbType.Uuid).Value = iterationSetup.IterationIid; - command.ExecuteNonQuery(); + command.ExecuteNonQuery(); + } } /// @@ -479,56 +472,58 @@ private NpgsqlTransaction SetupNewTransaction(ref NpgsqlConnection connection) } /// - /// Create a transaction info table with transaction scope lifetime. + /// The create default transaction info entry. /// /// /// The current transaction to the database. /// - private static void CreateTransactionInfoTable(NpgsqlTransaction transaction) + /// + /// The credentials. + /// + private static void CreateDefaultTransactionInfoEntry(NpgsqlTransaction transaction, Credentials credentials) { - // setup transaction_info table that is valid only for this transaction + // insert actor from the request credentials otherwise use default (null) user var sqlBuilder = new StringBuilder(); + var isCredentialSet = credentials != null; - sqlBuilder.AppendFormat("CREATE TEMPORARY TABLE {0} (", TransactionInfoTable); - sqlBuilder.AppendFormat("{0} uuid, ", UserIdColumn); - sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT '-infinity', ", PeriodStartColumn); - sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT 'infinity', ", PeriodEndColumn); - sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT 'infinity', ", InstantColumn); - sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT statement_timestamp(), ", TransactionTimeColumn); - sqlBuilder.AppendFormat("{0} boolean NOT NULL DEFAULT 'true'", TransactionAuditEnabled); - sqlBuilder.Append(") ON COMMIT DROP;"); + sqlBuilder.AppendFormat("INSERT INTO {0} ({1}, {2})", TransactionInfoTable, UserIdColumn, TransactionTimeColumn); + sqlBuilder.AppendFormat(" VALUES({0}, statement_timestamp());", isCredentialSet ? $":{UserIdColumn}" : "null"); - using var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction); + using (var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction)) + { + if (isCredentialSet) + { + command.Parameters.Add(UserIdColumn, NpgsqlDbType.Uuid).Value = credentials.Person.Iid; + } - command.ExecuteNonQuery(); + command.ExecuteNonQuery(); + } } /// - /// The create default transaction info entry. + /// Create a transaction info table with transaction scope lifetime. /// /// /// The current transaction to the database. /// - /// - /// The credentials. - /// - private static void CreateDefaultTransactionInfoEntry(NpgsqlTransaction transaction, Credentials credentials) + private static void CreateTransactionInfoTable(NpgsqlTransaction transaction) { - // insert actor from the request credentials otherwise use default (null) user + // setup transaction_info table that is valid only for this transaction var sqlBuilder = new StringBuilder(); - var isCredentialSet = credentials != null; - sqlBuilder.AppendFormat("INSERT INTO {0} ({1}, {2})", TransactionInfoTable, UserIdColumn, TransactionTimeColumn); - sqlBuilder.AppendFormat(" VALUES({0}, statement_timestamp());", isCredentialSet ? $":{UserIdColumn}" : "null"); - - using var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction); + sqlBuilder.AppendFormat("CREATE TEMPORARY TABLE {0} (", TransactionInfoTable); + sqlBuilder.AppendFormat("{0} uuid, ", UserIdColumn); + sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT '-infinity', ", PeriodStartColumn); + sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT 'infinity', ", PeriodEndColumn); + sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT 'infinity', ", InstantColumn); + sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT statement_timestamp(), ", TransactionTimeColumn); + sqlBuilder.AppendFormat("{0} boolean NOT NULL DEFAULT 'true'", TransactionAuditEnabled); + sqlBuilder.Append(") ON COMMIT DROP;"); - if (isCredentialSet) + using (var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction)) { - command.Parameters.Add(UserIdColumn, NpgsqlDbType.Uuid).Value = credentials.Person.Iid; + command.ExecuteNonQuery(); } - - command.ExecuteNonQuery(); } } } diff --git a/CometServer/Helpers/ICdp4TransactionManager.cs b/CometServer/Helpers/ICdp4TransactionManager.cs index c26fac6a..ced3d2fa 100644 --- a/CometServer/Helpers/ICdp4TransactionManager.cs +++ b/CometServer/Helpers/ICdp4TransactionManager.cs @@ -28,14 +28,14 @@ namespace CometServer.Helpers using CDP4Common.DTO; + using CDP4Orm.Dao; + using CometServer.Authorization; using Npgsql; /// - /// The purpose of the is provide a for - /// read and write operations to the database while configuring the database to properly process - /// any temporal database interactions + /// A wrapper interface for the class, allowing temporal database interaction. /// public interface ICdp4TransactionManager { @@ -68,8 +68,7 @@ public interface ICdp4TransactionManager /// The user credential information for this request /// /// - /// the unique identifier of the which is used to retrieve and set the - /// corresponding + /// An optional iteration id that can be supplied to set the transaction context to the iteration /// /// /// The . @@ -77,44 +76,46 @@ public interface ICdp4TransactionManager NpgsqlTransaction SetupTransaction(ref NpgsqlConnection connection, Credentials credentials, Guid iterationIid); /// - /// Get the timestamp in the form of a that the was - /// created using the method. + /// Get the current transaction time. /// /// - /// The active + /// The transaction. /// /// /// The . /// DateTime GetTransactionTime(NpgsqlTransaction transaction); + + /// + /// Get the session timeframe start time. + /// + /// + /// The transaction. + /// + /// + /// The . + /// + DateTime GetSessionTimeFrameStart(NpgsqlTransaction transaction); /// - /// Get the current session time instant value from the database. In case the most current data is to be - /// retrieved this value returns (+infinity) which translates to . In case - /// a request is made related to time-travel the corresponding to the period_end - /// is returned + /// Get the current session time instant. /// /// - /// The current transaction to the database. + /// The transaction. /// /// - /// A that represents the session Instant (either or - /// the corresponding to the period_end + /// The . /// DateTime GetSessionInstant(NpgsqlTransaction transaction); /// - /// Get the raw current session time instant value from the database. In case the most current data is to be - /// retrieved this value returns (+infinity) which translates to . In case - /// a request is made related to time-travel the corresponding to the period_end - /// is returned + /// Get the raw current session time instant value from the database. /// /// - /// The current transaction to the database. + /// The transaction. /// /// - /// A that represents the session Instant (either or - /// the corresponding to the period_end + /// The current session instant as an . /// object GetRawSessionInstant(NpgsqlTransaction transaction); @@ -149,10 +150,10 @@ public interface ICdp4TransactionManager void SetAuditLoggingState(NpgsqlTransaction transaction, bool enabled); /// - /// Sets the default temporal context (-infinity, +infinity) + /// The set default context. /// /// - /// The active + /// The transaction. /// void SetDefaultContext(NpgsqlTransaction transaction);