Skip to content

Commit bd40eb9

Browse files
committed
Add the Test-Object cmdlet
1 parent 0ab2674 commit bd40eb9

6 files changed

Lines changed: 61 additions & 4 deletions

File tree

ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Added the `CommandBuilder` class.
1111
- Added the `ColumnInfo.GetValue()` method.
1212
- Added the `Delete()`, `Exists()` and `Find()` methods to the `ConnectionExtensions` class.
13-
- Added the `Find-Object` and `Remove-Object` cmdlets.
13+
- Added the `Find-Object`, `Remove-Object` and `Test-Object` cmdlets.
1414
- Made the `Mapper.GetTable()` method public.
1515

1616
## Version [2.0.0](https://github.com/cedx/sql.net/compare/v1.2.0...v2.0.0)

Sql.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"Approve-Transaction"
1919
"Close-Connection"
2020
"Deny-Transaction"
21+
"Find-Object"
2122
"Get-First"
2223
"Get-Mapper"
2324
"Get-Scalar"
@@ -29,6 +30,8 @@
2930
"New-Connection"
3031
"New-Parameter"
3132
"New-Transaction"
33+
"Remove-Object"
34+
"Test-Object"
3235
)
3336

3437
RequiredAssemblies = @(

src/Sql.Cmdlets/Find-Object.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class FindObjectCommand: Cmdlet {
1414
private static readonly Type[] parameterTypes = [typeof(IDbConnection), typeof(object), typeof(string[]), typeof(CommandOptions)];
1515

1616
/// <summary>
17-
/// The type of object to return.
17+
/// The type of object to find.
1818
/// </summary>
1919
[Parameter(Mandatory = true, Position = 1)]
2020
public required Type Class { get; set; }

src/Sql.Cmdlets/Test-Object.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

src/Sql/ConnectionExtensions.Async.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static async Task<IDataReader> ExecuteReaderAsync(this IDbConnection conn
8484
}
8585

8686
/// <summary>
87-
/// Returns a value indicating whether an entity with the specified primary key exists.
87+
/// Checks whether an entity with the specified primary key exists.
8888
/// </summary>
8989
/// <param name="connection">The connection to the data source.</param>
9090
/// <param name="id">The primary key value.</param>

src/Sql/ConnectionExtensions.Sync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static IDataReader ExecuteReader(this IDbConnection connection, string sq
7979
}
8080

8181
/// <summary>
82-
/// Returns a value indicating whether an entity with the specified primary key exists.
82+
/// Checks whether an entity with the specified primary key exists.
8383
/// </summary>
8484
/// <param name="connection">The connection to the data source.</param>
8585
/// <param name="id">The primary key value.</param>

0 commit comments

Comments
 (0)