|
| 1 | +namespace Belin.Sql.Cmdlets; |
| 2 | + |
| 3 | +using System.Data; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Updates the specified entity. |
| 7 | +/// </summary> |
| 8 | +[Cmdlet(VerbsData.Update, "Object"), OutputType(typeof(int))] |
| 9 | +public class UpdateObjectCommand: Cmdlet { |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// The list of columns to select. By default, all columns. |
| 13 | + /// </summary> |
| 14 | + [Parameter] |
| 15 | + public string[] Columns { get; set; } = []; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// The connection to the data source. |
| 19 | + /// </summary> |
| 20 | + [Parameter(Mandatory = true, Position = 0)] |
| 21 | + public required IDbConnection Connection { get; set; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The entity to update. |
| 25 | + /// </summary> |
| 26 | + [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true)] |
| 27 | + public required object InputObject { get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The wait time, in seconds, before terminating the attempt to execute the command and generating an error. |
| 31 | + /// </summary> |
| 32 | + [Parameter, ValidateRange(ValidateRangeKind.Positive)] |
| 33 | + public int Timeout { get; set; } = 30; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// The transaction to use, if any. |
| 37 | + /// </summary> |
| 38 | + [Parameter] |
| 39 | + public IDbTransaction? Transaction { get; set; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Performs execution of this command. |
| 43 | + /// </summary> |
| 44 | + protected override void ProcessRecord() { |
| 45 | + var entityType = InputObject.GetType(); |
| 46 | + var parameterTypes = new[] { typeof(IDbConnection), entityType, typeof(string[]), typeof(CommandOptions) }; |
| 47 | + var method = typeof(ConnectionExtensions).GetMethod(nameof(ConnectionExtensions.Update), 1, parameterTypes)!.MakeGenericMethod(entityType); |
| 48 | + var arguments = new object[] { Connection, InputObject, Columns, new CommandOptions { Timeout = Timeout, Transaction = Transaction } }; |
| 49 | + WriteObject(method.Invoke(null, arguments)); |
| 50 | + } |
| 51 | +} |
0 commit comments