forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySqlClientSqlCommandSet.cs
82 lines (71 loc) · 2.02 KB
/
MySqlClientSqlCommandSet.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;
namespace NHibernate.AdoNet
{
using Action = System.Action;
public class MySqlClientSqlCommandSet : IDisposable
{
private static readonly System.Type adapterType;
private readonly object instance;
private readonly Action doInitialise;
private readonly Action<int> batchSizeSetter;
private readonly Func<IDbCommand, int> doAppend;
private readonly Func<int> doExecuteNonQuery;
private readonly Action doDispose;
private int countOfCommands;
static MySqlClientSqlCommandSet()
{
var sysData = Assembly.Load("MySql.Data");
adapterType = sysData.GetType("MySql.Data.MySqlClient.MySqlDataAdapter");
Debug.Assert(adapterType != null, "Could not find MySqlDataAdapter!");
}
public MySqlClientSqlCommandSet(int batchSize)
{
instance = Activator.CreateInstance(adapterType, true);
doInitialise = (Action) Delegate.CreateDelegate(typeof (Action), instance, "InitializeBatching");
batchSizeSetter = (Action<int>) Delegate.CreateDelegate(typeof (Action<int>), instance, "set_UpdateBatchSize");
doAppend = (Func<IDbCommand, int>) Delegate.CreateDelegate(typeof (Func<IDbCommand, int>), instance, "AddToBatch");
doExecuteNonQuery = (Func<int>) Delegate.CreateDelegate(typeof (Func<int>), instance, "ExecuteBatch");
doDispose = (Action)Delegate.CreateDelegate(typeof(Action), instance, "Dispose");
Initialise(batchSize);
}
private void Initialise(int batchSize)
{
doInitialise();
batchSizeSetter(batchSize);
}
public void Append(IDbCommand command)
{
doAppend(command);
countOfCommands++;
}
public void Dispose()
{
doDispose();
}
public int ExecuteNonQuery()
{
try
{
if (CountOfCommands == 0)
{
return 0;
}
return doExecuteNonQuery();
}
catch (Exception exception)
{
throw new HibernateException("An exception occured when executing batch queries", exception);
}
}
public int CountOfCommands
{
get
{
return countOfCommands;
}
}
}
}