|
| 1 | +namespace Belin.Sql.Cmdlets; |
| 2 | + |
| 3 | +using System.Data; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Checks whether an entity with the specified primary key exists. |
| 7 | +/// </summary> |
| 8 | +[Cmdlet(VerbsDiagnostic.Test, "Object"), OutputType(typeof(bool))] |
| 9 | +public class TestObjectCommand: Cmdlet { |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// An array of types representing the number, order, and type of the parameters of the underlying method to invoke. |
| 13 | + /// </summary> |
| 14 | + private static readonly Type[] parameterTypes = [typeof(IDbConnection), typeof(object), typeof(CommandOptions)]; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// The type of object to check. |
| 18 | + /// </summary> |
| 19 | + [Parameter(Mandatory = true, Position = 1)] |
| 20 | + public required Type Class { get; set; } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The connection to the data source. |
| 24 | + /// </summary> |
| 25 | + [Parameter(Mandatory = true, Position = 0)] |
| 26 | + public required IDbConnection Connection { get; set; } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// The primary key value. |
| 30 | + /// </summary> |
| 31 | + [Parameter(Mandatory = true, Position = 2)] |
| 32 | + public required object Id { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The wait time, in seconds, before terminating the attempt to execute the command and generating an error. |
| 36 | + /// </summary> |
| 37 | + [Parameter, ValidateRange(ValidateRangeKind.Positive)] |
| 38 | + public int Timeout { get; set; } = 30; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The transaction to use, if any. |
| 42 | + /// </summary> |
| 43 | + [Parameter] |
| 44 | + public IDbTransaction? Transaction { get; set; } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Performs execution of this command. |
| 48 | + /// </summary> |
| 49 | + protected override void ProcessRecord() { |
| 50 | + var method = typeof(ConnectionExtensions).GetMethod(nameof(ConnectionExtensions.Exists), 1, parameterTypes)!.MakeGenericMethod(Class); |
| 51 | + var arguments = new object[] { Connection, Id, new CommandOptions { Timeout = Timeout, Transaction = Transaction } }; |
| 52 | + WriteObject(method.Invoke(null, arguments)); |
| 53 | + } |
| 54 | +} |
0 commit comments