forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOracleDataClientBatchingBatcher.cs
172 lines (155 loc) · 5.21 KB
/
OracleDataClientBatchingBatcher.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using NHibernate.AdoNet.Util;
using NHibernate.Driver;
using NHibernate.Exceptions;
namespace NHibernate.AdoNet
{
/// <summary>
/// Summary description for OracleDataClientBatchingBatcher.
/// By Tomer Avissar
/// </summary>
public partial class OracleDataClientBatchingBatcher : AbstractBatcher
{
private int _batchSize;
private int _countOfCommands;
private int _totalExpectedRowsAffected;
private DbCommand _currentBatch;
private Dictionary<string, List<object>> _parameterValueListHashTable;
private Dictionary<string, bool> _parameterIsAllNullsHashTable;
private StringBuilder _currentBatchCommandsLog;
public OracleDataClientBatchingBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
: base(connectionManager, interceptor)
{
_batchSize = Factory.Settings.AdoBatchSize;
//we always create this, because we need to deal with a scenario in which
//the user change the logging configuration at runtime. Trying to put this
//behind an if(log.IsDebugEnabled) will cause a null reference exception
//at that point.
_currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}
public override void AddToBatch(IExpectation expectation)
{
bool firstOnBatch = true;
_totalExpectedRowsAffected += expectation.ExpectedRowCount;
string lineWithParameters = null;
var sqlStatementLogger = Factory.Settings.SqlStatementLogger;
if (sqlStatementLogger.IsDebugEnabled || Log.IsDebugEnabled())
{
lineWithParameters = sqlStatementLogger.GetCommandLineWithParameters(CurrentCommand);
var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic);
lineWithParameters = formatStyle.Formatter.Format(lineWithParameters);
_currentBatchCommandsLog.Append("command ")
.Append(_countOfCommands)
.Append(":")
.AppendLine(lineWithParameters);
}
if (Log.IsDebugEnabled())
{
Log.Debug("Adding to batch:{0}", lineWithParameters);
}
if (_currentBatch == null)
{
// use first command as the batching command
_currentBatch = CurrentCommand;
_parameterValueListHashTable = new Dictionary<string, List<object>>();
//oracle does not allow array containing all null values
// so this Dictionary is keeping track if all values are null or not
_parameterIsAllNullsHashTable = new Dictionary<string, bool>();
}
else
{
firstOnBatch = false;
}
foreach (DbParameter currentParameter in CurrentCommand.Parameters)
{
List<object> parameterValueList;
if (firstOnBatch)
{
parameterValueList = new List<object>();
_parameterValueListHashTable.Add(currentParameter.ParameterName, parameterValueList);
_parameterIsAllNullsHashTable.Add(currentParameter.ParameterName, true);
}
else
{
parameterValueList = _parameterValueListHashTable[currentParameter.ParameterName];
}
if (currentParameter.Value != DBNull.Value)
{
_parameterIsAllNullsHashTable[currentParameter.ParameterName] = false;
}
parameterValueList.Add(currentParameter.Value);
}
_countOfCommands++;
if (_countOfCommands >= _batchSize)
{
ExecuteBatchWithTiming(_currentBatch);
}
}
protected override void DoExecuteBatch(DbCommand ps)
{
if (_currentBatch != null)
{
int arraySize = 0;
_countOfCommands = 0;
Log.Info("Executing batch");
CheckReaders();
Prepare(_currentBatch);
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
Factory.Settings.SqlStatementLogger.LogBatchCommand(_currentBatchCommandsLog.ToString());
_currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}
foreach (DbParameter currentParameter in _currentBatch.Parameters)
{
List<object> parameterValueArray = _parameterValueListHashTable[currentParameter.ParameterName];
currentParameter.Value = parameterValueArray.ToArray();
arraySize = parameterValueArray.Count;
}
// setting the ArrayBindCount on the OracleCommand
// this value is not a part of the ADO.NET API.
// It's and ODP implementation, so it is being set by reflection
SetArrayBindCount(arraySize);
try
{
int rowsAffected;
try
{
rowsAffected = _currentBatch.ExecuteNonQuery();
}
catch (DbException e)
{
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
}
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
}
finally
{
// Cleaning up even if batched outcome is invalid
_totalExpectedRowsAffected = 0;
_currentBatch = null;
_parameterValueListHashTable = null;
}
}
}
protected override int CountOfStatementsInCurrentBatch
{
get { return _countOfCommands; }
}
private void SetArrayBindCount(int arraySize)
{
//TODO: cache the property info.
var command = Driver.UnwrapDbCommand(_currentBatch);
var objType = command.GetType();
var propInfo = objType.GetProperty("ArrayBindCount");
propInfo.SetValue(command, arraySize, null);
}
public override int BatchSize
{
get { return _batchSize; }
set { _batchSize = value; }
}
}
}