//
//
// PL/.NET (pldotnet) - PostgreSQL support for .NET C# and F# as
// procedural languages (PL)
//
//
// Copyright (c) 2023 Brick Abode
//
// This code is subject to the terms of the PostgreSQL License.
// The full text of the license can be found in the LICENSE file
// at the top level of the pldotnet repository.
//
//
using System;
using System.Linq;
namespace PlDotNET.Common
{
#nullable enable
///
/// Represents metadata and row-level data passed to a PostgreSQL trigger function,
/// including old and new row values, trigger context, and table identifiers.
///
/// The contents of the row before the triggering operation (for UPDATE or DELETE).
/// The contents of the row after the triggering operation (for INSERT or UPDATE).
/// The name of the trigger as defined in PostgreSQL.
/// Specifies whether the trigger fires BEFORE or AFTER the operation.
/// The level of the trigger: ROW or STATEMENT.
/// The type of DML operation that triggered this call: INSERT, UPDATE, or DELETE.
/// The OID (object identifier) of the table on which the trigger fired.
/// The name of the table that the trigger is defined on.
/// The schema that contains the table.
/// Arguments passed to the trigger from the PostgreSQL CREATE TRIGGER statement.
public class TriggerData(
object?[] oldRow,
object?[] newRow,
string triggerName,
string triggerWhen,
string triggerLevel,
string triggerEvent,
int relationId,
string tableName,
string tableSchema,
string[] arguments)
{
///
/// Gets or sets the values of the row before the triggering operation.
/// Only populated for UPDATE and DELETE triggers.
///
public object?[] OldRow { get; set; } = oldRow;
///
/// Gets or sets the values of the row after the triggering operation.
/// Only populated for INSERT and UPDATE triggers.
///
public object?[] NewRow { get; set; } = newRow;
///
/// Gets or sets the name of the trigger that was fired.
///
public string TriggerName { get; set; } = triggerName;
///
/// Gets or sets the timing of the trigger execution (e.g., BEFORE or AFTER).
///
public string TriggerWhen { get; set; } = triggerWhen;
///
/// Gets or sets the level at which the trigger was fired (e.g., ROW or STATEMENT).
///
public string TriggerLevel { get; set; } = triggerLevel;
///
/// Gets or sets the DML event that caused the trigger (INSERT, UPDATE, or DELETE).
///
public string TriggerEvent { get; set; } = triggerEvent;
///
/// Gets or sets the PostgreSQL internal relation OID (object identifier) of the table.
///
public int RelationId { get; set; } = relationId;
///
/// Gets or sets the name of the table associated with the trigger.
///
public string TableName { get; set; } = tableName;
///
/// Gets or sets the schema name of the table associated with the trigger.
///
public string TableSchema { get; set; } = tableSchema;
///
/// Gets or sets the array of arguments passed to the trigger.
///
public string[] Arguments { get; set; } = arguments;
///
/// Returns a string representation of the trigger data for debugging purposes.
/// Includes trigger metadata, old and new rows, and arguments.
///
/// A multi-line string summarizing the contents of this instance.
public override string ToString()
{
string newline = Environment.NewLine;
static string FormatItem(object? item) => $"- ({item?.GetType().Name ?? "null"}){item?.ToString() ?? "null"}";
return $"TriggerName: (string){this.TriggerName}\n" +
$"TriggerWhen: (string){this.TriggerWhen}\n" +
$"TriggerLevel: (string){this.TriggerLevel}\n" +
$"TriggerEvent : (string){this.TriggerEvent}\n" +
$"RelationId: (int){this.RelationId}\n" +
$"TableName: (string){this.TableName}\n" +
$"TableSchema: (string){this.TableSchema}\n" +
"OldRow:\n" +
string.Join(newline, this.OldRow.Select(FormatItem)) +
"\nNewRow:\n" +
string.Join(newline, this.NewRow.Select(FormatItem)) +
"\nArguments:\n" +
string.Join(newline, this.Arguments.Select(arg => $"- (string){arg}")) +
"\n";
}
}
#nullable disable
}