Skip to content

Commit 858298f

Browse files
committed
Add the Remove-Object cmdlet
1 parent c83629d commit 858298f

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/Sql.Cmdlets/Find-Object.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class FindObjectCommand: Cmdlet {
1616
/// <summary>
1717
/// The type of object to return.
1818
/// </summary>
19-
[Parameter(Mandatory = true)]
19+
[Parameter(Mandatory = true, Position = 1)]
2020
public required Type Class { get; set; }
2121

2222
/// <summary>
@@ -33,7 +33,7 @@ public class FindObjectCommand: Cmdlet {
3333
/// <summary>
3434
/// The primary key value.
3535
/// </summary>
36-
[Parameter(Mandatory = true)]
36+
[Parameter(Mandatory = true, Position = 2)]
3737
public required object Id { get; set; }
3838

3939
/// <summary>

src/Sql.Cmdlets/Remove-Object.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Belin.Sql.Cmdlets;
2+
3+
using System.Data;
4+
5+
/// <summary>
6+
/// Deletes the specified entity.
7+
/// </summary>
8+
[Cmdlet(VerbsCommon.Remove, "Object"), OutputType(typeof(bool))]
9+
public class RemoveObjectCommand: Cmdlet {
10+
11+
/// <summary>
12+
/// The connection to the data source.
13+
/// </summary>
14+
[Parameter(Mandatory = true, Position = 0)]
15+
public required IDbConnection Connection { get; set; }
16+
17+
/// <summary>
18+
/// The entity to be deleted.
19+
/// </summary>
20+
[Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true)]
21+
public required object InputObject { get; set; }
22+
23+
/// <summary>
24+
/// The wait time, in seconds, before terminating the attempt to execute the command and generating an error.
25+
/// </summary>
26+
[Parameter, ValidateRange(ValidateRangeKind.Positive)]
27+
public int Timeout { get; set; } = 30;
28+
29+
/// <summary>
30+
/// The transaction to use, if any.
31+
/// </summary>
32+
[Parameter]
33+
public IDbTransaction? Transaction { get; set; }
34+
35+
/// <summary>
36+
/// Performs execution of this command.
37+
/// </summary>
38+
protected override void ProcessRecord() {
39+
var entityType = InputObject.GetType();
40+
var parameterTypes = new[] { typeof(IDbConnection), entityType, typeof(CommandOptions) };
41+
var method = typeof(ConnectionExtensions).GetMethod(nameof(ConnectionExtensions.Delete), 1, parameterTypes)!.MakeGenericMethod(entityType);
42+
var arguments = new object[] { Connection, InputObject, new CommandOptions { Timeout = Timeout, Transaction = Transaction } };
43+
WriteObject(method.Invoke(null, arguments));
44+
}
45+
}

0 commit comments

Comments
 (0)