@@ -7,14 +7,14 @@ namespace Belin.Sql.Cmdlets;
77/// <summary>
88/// Executes a parameterized SQL query and returns an array of objects whose properties correspond to the columns.
99/// </summary>
10- [ Cmdlet ( VerbsLifecycle . Invoke , "Query" ) , OutputType ( typeof ( object ) ) ]
10+ [ Cmdlet ( VerbsLifecycle . Invoke , "Query" ) , OutputType ( typeof ( object ) , typeof ( Tuple < object ? , object ? > ) ) ]
1111public class InvokeQueryCommand : Cmdlet {
1212
1313 /// <summary>
1414 /// The type of objects to return.
1515 /// </summary>
16- [ Parameter ]
17- public Type As { get ; set ; } = typeof ( ExpandoObject ) ;
16+ [ Parameter , ValidateNotNullOrEmpty ]
17+ public Type [ ] As { get ; set ; } = [ typeof ( ExpandoObject ) ] ;
1818
1919 /// <summary>
2020 /// The SQL query to be executed.
@@ -40,6 +40,12 @@ public class InvokeQueryCommand: Cmdlet {
4040 [ Parameter ( Position = 2 ) ]
4141 public ParameterCollection Parameters { get ; set ; } = [ ] ;
4242
43+ /// <summary>
44+ /// The field from which to split and read a second object.
45+ /// </summary>
46+ [ Parameter , ValidateNotNullOrWhiteSpace ]
47+ public string SplitOn { get ; set ; } = "Id" ;
48+
4349 /// <summary>
4450 /// The wait time, in seconds, before terminating the attempt to execute the command and generating an error.
4551 /// </summary>
@@ -56,11 +62,17 @@ public class InvokeQueryCommand: Cmdlet {
5662 /// Performs execution of this command.
5763 /// </summary>
5864 protected override void ProcessRecord ( ) {
65+ 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 ) ] ;
68+
69+ object ? [ ] arguments = As . Length <= 1
70+ ? [ Connection , Command , Parameters , new CommandOptions ( Timeout , Transaction , CommandType ) ]
71+ : [ Connection , Command , Parameters , SplitOn , new CommandOptions ( Timeout , Transaction , CommandType ) ] ;
72+
5973 try {
60- var types = new [ ] { typeof ( IDbConnection ) , typeof ( string ) , typeof ( ParameterCollection ) , typeof ( CommandOptions ) } ;
61- var method = typeof ( ConnectionExtensions ) . GetMethod ( nameof ( ConnectionExtensions . Query ) , 1 , types ) ! . MakeGenericMethod ( As ) ;
62- var records = ( IEnumerable < object > ) method . Invoke ( null , [ Connection , Command , Parameters , new CommandOptions ( Timeout , Transaction , CommandType ) ] ) ! ;
63- WriteObject ( records , enumerateCollection : true ) ;
74+ var method = typeof ( ConnectionExtensions ) . GetMethod ( nameof ( ConnectionExtensions . Query ) , As . Length , types ) ! . MakeGenericMethod ( As ) ;
75+ WriteObject ( method . Invoke ( null , arguments ) , enumerateCollection : true ) ;
6476 }
6577 catch ( TargetInvocationException e ) {
6678 WriteError ( new ErrorRecord ( e . InnerException , "Invoke-Query:TargetInvocationException" , ErrorCategory . OperationStopped , null ) ) ;
0 commit comments