Skip to content

Commit e633997

Browse files
committed
Add the -NoEnumerate and -Stream parameters to the Invoke-Query cmdlet
1 parent 6377cec commit e633997

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Breaking change: transformed the `CommandOptions` constructor into a parameterless constructor.
66
- Added the `QueryOptions` record.
77
- Added the `IEnumerable<T>.AsList()` extension method.
8+
- Added the `-NoEnumerate` and `-Stream` parameters to the `Invoke-Query` cmdlet.
89

910
## Version [1.2.0](https://github.com/cedx/sql.net/compare/v1.1.0...v1.2.0)
1011
- Simplified the constraint on generic type parameters.

src/Sql.Cmdlets/Invoke-Query.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace Belin.Sql.Cmdlets;
55
using System.Reflection;
66

77
/// <summary>
8-
/// Executes a parameterized SQL query and returns an array of objects whose properties correspond to the columns.
8+
/// Executes a parameterized SQL query and returns a sequence of objects whose properties correspond to the columns.
99
/// </summary>
10-
[Cmdlet(VerbsLifecycle.Invoke, "Query"), OutputType(typeof(object), typeof((object?, object?)))]
10+
[Cmdlet(VerbsLifecycle.Invoke, "Query"), OutputType(typeof(object), typeof((object?, object?)), typeof(IEnumerable<object>), typeof(IEnumerable<(object?, object?)>))]
1111
public class InvokeQueryCommand: Cmdlet {
1212

1313
/// <summary>
@@ -34,6 +34,12 @@ public class InvokeQueryCommand: Cmdlet {
3434
[Parameter(Mandatory = true, Position = 0)]
3535
public required IDbConnection Connection { get; set; }
3636

37+
/// <summary>
38+
/// Value indicating whether to prevent from enumerating the rows.
39+
/// </summary>
40+
[Parameter]
41+
public SwitchParameter NoEnumerate { get; set; }
42+
3743
/// <summary>
3844
/// The parameters of the SQL query.
3945
/// </summary>
@@ -46,6 +52,12 @@ public class InvokeQueryCommand: Cmdlet {
4652
[Parameter, ValidateNotNullOrWhiteSpace]
4753
public string SplitOn { get; set; } = "Id";
4854

55+
/// <summary>
56+
/// Value indicating whether to prevent from buffering the rows in memory.
57+
/// </summary>
58+
[Parameter]
59+
public SwitchParameter Stream { get; set; }
60+
4961
/// <summary>
5062
/// The wait time, in seconds, before terminating the attempt to execute the command and generating an error.
5163
/// </summary>
@@ -63,16 +75,15 @@ public class InvokeQueryCommand: Cmdlet {
6375
/// </summary>
6476
protected override void ProcessRecord() {
6577
Type[] types = As.Length <= 1
66-
? [typeof(IDbConnection), typeof(string), typeof(ParameterCollection), typeof(CommandOptions)]
67-
: [typeof(IDbConnection), typeof(string), typeof(ParameterCollection), typeof(string), typeof(CommandOptions)];
78+
? [typeof(IDbConnection), typeof(string), typeof(ParameterCollection), typeof(QueryOptions)]
79+
: [typeof(IDbConnection), typeof(string), typeof(ParameterCollection), typeof(string), typeof(QueryOptions)];
6880

69-
object?[] arguments = As.Length <= 1
70-
? [Connection, Command, Parameters, new CommandOptions { Timeout = Timeout, Transaction = Transaction, Type = CommandType }]
71-
: [Connection, Command, Parameters, SplitOn, new CommandOptions { Timeout = Timeout, Transaction = Transaction, Type = CommandType }];
81+
var queryOptions = new QueryOptions { Buffered = !Stream, Timeout = Timeout, Transaction = Transaction, Type = CommandType };
82+
object?[] arguments = As.Length <= 1 ? [Connection, Command, Parameters, queryOptions] : [Connection, Command, Parameters, SplitOn, queryOptions];
7283

7384
try {
7485
var method = typeof(ConnectionExtensions).GetMethod(nameof(ConnectionExtensions.Query), As.Length, types)!.MakeGenericMethod(As);
75-
WriteObject(method.Invoke(null, arguments), enumerateCollection: true);
86+
WriteObject(method.Invoke(null, arguments), enumerateCollection: !NoEnumerate);
7687
}
7788
catch (TargetInvocationException e) {
7889
WriteError(new ErrorRecord(e.InnerException, "Invoke-Query:TargetInvocationException", ErrorCategory.OperationStopped, null));

src/Sql/QueryOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Belin.Sql;
66
public record QueryOptions: CommandOptions {
77

88
/// <summary>
9-
/// Value indicating whether to buffer the results in memory.
9+
/// Value indicating whether to buffer the rows in memory.
1010
/// </summary>
1111
public bool Buffered { get; init; } = true;
1212
}

0 commit comments

Comments
 (0)