-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSqlCommandSetWrapper.cs
54 lines (45 loc) · 2.15 KB
/
SqlCommandSetWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using Microsoft.Data.SqlClient;
using System.Diagnostics;
using System.Linq.Expressions;
namespace SecurityDriven.TinyORM
{
internal sealed class SqlCommandSetWrapper : IDisposable
{
static Type commandSetType = typeof(SqlCommand).Assembly.GetType("Microsoft.Data.SqlClient.SqlCommandSet")
?? throw new Exception($"{nameof(SqlCommandSetWrapper)}: Could not find[Microsoft.Data.SqlClient.SqlCommandSet].");
static Func<object> commandSetCtor = Expression.Lambda<Func<object>>(Expression.New(commandSetType)).Compile();
object commandSetInstance;
Action<SqlCommand> appendDelegate;
Action disposeDelegate;
Func<int> executeNonQueryDelegate;
Func<SqlConnection> connectionGetDelegate;
Action<SqlConnection> connectionSetDelegate;
Action<int> commandTimeoutSetDelegate;
int commandCount;
public SqlCommandSetWrapper()
{
commandSetInstance = commandSetCtor();
appendDelegate = (Action<SqlCommand>)Delegate.CreateDelegate(typeof(Action<SqlCommand>), commandSetInstance, "Append");
disposeDelegate = (Action)Delegate.CreateDelegate(typeof(Action), commandSetInstance, "Dispose");
executeNonQueryDelegate = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), commandSetInstance, "ExecuteNonQuery");
connectionGetDelegate = (Func<SqlConnection>)Delegate.CreateDelegate(typeof(Func<SqlConnection>), commandSetInstance, "get_Connection");
connectionSetDelegate = (Action<SqlConnection>)Delegate.CreateDelegate(typeof(Action<SqlConnection>), commandSetInstance, "set_Connection");
commandTimeoutSetDelegate = (Action<int>)Delegate.CreateDelegate(typeof(Action<int>), commandSetInstance, "set_CommandTimeout");
}//ctor
public void Dispose() => disposeDelegate();
public int CommandTimeout { set { commandTimeoutSetDelegate(value); } }
public int CommandCount { get { return commandCount; } }
public SqlConnection Connection
{
get { return connectionGetDelegate(); }
set { connectionSetDelegate(value); }
}// Connection
public int ExecuteNonQuery() => executeNonQueryDelegate();
public void Append(SqlCommand command)
{
appendDelegate(command);
++commandCount;
}// Append
}//class SqlCommandSetWrapper
}//ns