Skip to content

Commit e60f2d3

Browse files
committed
Add the -SplitOn parameter to the Invoke-Query cmdlet
1 parent a8fe5df commit e60f2d3

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Version [1.1.0](https://github.com/cedx/sql.net/compare/v1.0.0...v1.1.0)
44
- Added support for mapping a single record to multiple objects.
5+
- Added the `-SplitOn` parameter to the `Invoke-Query` cmdlet.
56

67
## Version [1.0.0](https://github.com/cedx/sql.net/compare/v0.17.0...v1.0.0)
78
- First stable release.

src/Sql.Cmdlets/Invoke-Query.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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?>))]
1111
public 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

Comments
 (0)