-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add [BatchSize] and pass thru to multi-row execute (#76)
- Loading branch information
Showing
10 changed files
with
365 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
|
||
namespace Dapper; | ||
|
||
/// <summary> | ||
/// Indicates the batch size to use when executing commands with a sequence of argument rows. | ||
/// </summary> | ||
[Conditional("DEBUG")] // not needed post-build, so: evaporate | ||
[ImmutableObject(true)] | ||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method, AllowMultiple = false)] | ||
public sealed class BatchSizeAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Indicates the batch size to use when executing commands with a sequence of argument row; a value of zero disables batch usage; a negative value uses a single batch for all rows. | ||
/// </summary> | ||
public BatchSizeAttribute(int batchSize) => _ = batchSize; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Dapper; | ||
using System.Data.Common; | ||
|
||
[module: DapperAot] | ||
|
||
public static class Foo | ||
{ | ||
[BatchSize(10)] // should be passed explicitly | ||
static void SomeCode(DbConnection connection, string sql, string bar) | ||
{ | ||
var objs = new[] { new { id = 12, bar }, new { id = 34, bar = "def" } }; | ||
|
||
connection.Execute("insert Foo (Id, Value) values (@id, @bar)", objs); | ||
} | ||
|
||
// no batch size, should be passed implicitly | ||
static void SomeOtherCode(DbConnection connection, string sql, string bar) | ||
{ | ||
var objs = new[] { new { id = 12, bar }, new { id = 34, bar = "def" } }; | ||
|
||
connection.Execute("insert Foo (Id, Value) values (@id, @bar)", objs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#nullable enable | ||
namespace Dapper.AOT // interceptors must be in a known namespace | ||
{ | ||
file static class DapperGeneratedInterceptors | ||
{ | ||
[global::System.Runtime.CompilerServices.InterceptsLocationAttribute("Interceptors\\BatchSize.input.cs", 13, 20)] | ||
internal static int Execute0(this global::System.Data.IDbConnection cnn, string sql, object? param, global::System.Data.IDbTransaction? transaction, int? commandTimeout, global::System.Data.CommandType? commandType) | ||
{ | ||
// Execute, HasParameters, Text, KnownParameters | ||
// takes parameter: global::<anonymous type: int id, string bar>[] | ||
// parameter map: bar id | ||
global::System.Diagnostics.Debug.Assert(!string.IsNullOrWhiteSpace(sql)); | ||
global::System.Diagnostics.Debug.Assert((commandType ?? global::Dapper.DapperAotExtensions.GetCommandType(sql)) == global::System.Data.CommandType.Text); | ||
global::System.Diagnostics.Debug.Assert(param is not null); | ||
|
||
return global::Dapper.DapperAotExtensions.Command(cnn, transaction, sql, global::System.Data.CommandType.Text, commandTimeout.GetValueOrDefault(), CommandFactory0.Instance).Execute((object?[])param!, batchSize: 10); | ||
|
||
} | ||
|
||
[global::System.Runtime.CompilerServices.InterceptsLocationAttribute("Interceptors\\BatchSize.input.cs", 21, 20)] | ||
internal static int Execute1(this global::System.Data.IDbConnection cnn, string sql, object? param, global::System.Data.IDbTransaction? transaction, int? commandTimeout, global::System.Data.CommandType? commandType) | ||
{ | ||
// Execute, HasParameters, Text, KnownParameters | ||
// takes parameter: global::<anonymous type: int id, string bar>[] | ||
// parameter map: bar id | ||
global::System.Diagnostics.Debug.Assert(!string.IsNullOrWhiteSpace(sql)); | ||
global::System.Diagnostics.Debug.Assert((commandType ?? global::Dapper.DapperAotExtensions.GetCommandType(sql)) == global::System.Data.CommandType.Text); | ||
global::System.Diagnostics.Debug.Assert(param is not null); | ||
|
||
return global::Dapper.DapperAotExtensions.Command(cnn, transaction, sql, global::System.Data.CommandType.Text, commandTimeout.GetValueOrDefault(), CommandFactory1.Instance).Execute((object?[])param!); | ||
|
||
} | ||
|
||
private class CommonCommandFactory<T> : global::Dapper.CommandFactory<T> | ||
{ | ||
public override global::System.Data.Common.DbCommand GetCommand(global::System.Data.Common.DbConnection connection, string sql, global::System.Data.CommandType commandType, T args) | ||
{ | ||
var cmd = base.GetCommand(connection, sql, commandType, args); | ||
// apply special per-provider command initialization logic for OracleCommand | ||
if (cmd is global::Oracle.ManagedDataAccess.Client.OracleCommand cmd0) | ||
{ | ||
cmd0.BindByName = true; | ||
cmd0.InitialLONGFetchSize = -1; | ||
|
||
} | ||
return cmd; | ||
} | ||
|
||
} | ||
|
||
private static readonly CommonCommandFactory<object?> DefaultCommandFactory = new(); | ||
|
||
private sealed class CommandFactory0 : CommonCommandFactory<object?> // <anonymous type: int id, string bar> | ||
{ | ||
internal static readonly CommandFactory0 Instance = new(); | ||
public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object? args) | ||
{ | ||
var typed = Cast(args, static () => new { id = default(int), bar = default(string)! }); // expected shape | ||
var ps = cmd.Parameters; | ||
global::System.Data.Common.DbParameter p; | ||
p = cmd.CreateParameter(); | ||
p.ParameterName = "id"; | ||
p.DbType = global::System.Data.DbType.Int32; | ||
p.Direction = global::System.Data.ParameterDirection.Input; | ||
p.Value = AsValue(typed.id); | ||
ps.Add(p); | ||
|
||
p = cmd.CreateParameter(); | ||
p.ParameterName = "bar"; | ||
p.DbType = global::System.Data.DbType.String; | ||
p.Size = -1; | ||
p.Direction = global::System.Data.ParameterDirection.Input; | ||
p.Value = AsValue(typed.bar); | ||
ps.Add(p); | ||
|
||
} | ||
public override void UpdateParameters(in global::Dapper.UnifiedCommand cmd, object? args) | ||
{ | ||
var typed = Cast(args, static () => new { id = default(int), bar = default(string)! }); // expected shape | ||
var ps = cmd.Parameters; | ||
ps[0].Value = AsValue(typed.id); | ||
ps[1].Value = AsValue(typed.bar); | ||
|
||
} | ||
public override bool CanPrepare => true; | ||
|
||
} | ||
|
||
private sealed class CommandFactory1 : CommonCommandFactory<object?> // <anonymous type: int id, string bar> | ||
{ | ||
internal static readonly CommandFactory1 Instance = new(); | ||
public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object? args) | ||
{ | ||
var typed = Cast(args, static () => new { id = default(int), bar = default(string)! }); // expected shape | ||
var ps = cmd.Parameters; | ||
global::System.Data.Common.DbParameter p; | ||
p = cmd.CreateParameter(); | ||
p.ParameterName = "id"; | ||
p.DbType = global::System.Data.DbType.Int32; | ||
p.Direction = global::System.Data.ParameterDirection.Input; | ||
p.Value = AsValue(typed.id); | ||
ps.Add(p); | ||
|
||
p = cmd.CreateParameter(); | ||
p.ParameterName = "bar"; | ||
p.DbType = global::System.Data.DbType.String; | ||
p.Size = -1; | ||
p.Direction = global::System.Data.ParameterDirection.Input; | ||
p.Value = AsValue(typed.bar); | ||
ps.Add(p); | ||
|
||
} | ||
public override void UpdateParameters(in global::Dapper.UnifiedCommand cmd, object? args) | ||
{ | ||
var typed = Cast(args, static () => new { id = default(int), bar = default(string)! }); // expected shape | ||
var ps = cmd.Parameters; | ||
ps[0].Value = AsValue(typed.id); | ||
ps[1].Value = AsValue(typed.bar); | ||
|
||
} | ||
public override bool CanPrepare => true; | ||
|
||
} | ||
|
||
|
||
} | ||
} | ||
namespace System.Runtime.CompilerServices | ||
{ | ||
// this type is needed by the compiler to implement interceptors - it doesn't need to | ||
// come from the runtime itself, though | ||
|
||
[global::System.Diagnostics.Conditional("DEBUG")] // not needed post-build, so: evaporate | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true)] | ||
sealed file class InterceptsLocationAttribute : global::System.Attribute | ||
{ | ||
public InterceptsLocationAttribute(string path, int lineNumber, int columnNumber) | ||
{ | ||
_ = path; | ||
_ = lineNumber; | ||
_ = columnNumber; | ||
} | ||
} | ||
} |
Oops, something went wrong.