@@ -5,9 +5,9 @@ namespace Belin.Sql.Cmdlets;
55using 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 ? ) > ) ) ]
1111public 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 ) ) ;
0 commit comments