forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicResultSetsCommand.cs
325 lines (270 loc) · 7.24 KB
/
BasicResultSetsCommand.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
namespace NHibernate.Driver
{
public partial class BasicResultSetsCommand: IResultSetsCommand
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(BasicResultSetsCommand));
private SqlString _sqlString = SqlString.Empty;
public BasicResultSetsCommand(ISessionImplementor session)
{
Commands = new List<ISqlCommand>();
Session = session;
}
protected List<ISqlCommand> Commands { get; private set; }
protected ISessionImplementor Session { get; private set; }
public virtual void Append(ISqlCommand command)
{
Commands.Add(command);
_sqlString = null;
}
public bool HasQueries
{
get { return Commands.Count > 0; }
}
public virtual SqlString Sql => _sqlString ?? (_sqlString = GetSqlString());
private SqlString GetSqlString()
{
switch (Commands.Count)
{
case 0:
return SqlString.Empty;
case 1:
return Commands[0].Query;
}
var statementTerminator = Session.Factory.Dialect.StatementTerminator.ToString() + Environment.NewLine;
var builder = new SqlStringBuilder(Commands.Sum(c => c.Query.Count) + Commands.Count);
foreach (var command in Commands)
{
builder.Add(command.Query).Add(statementTerminator);
}
return builder.ToSqlString();
}
public virtual DbDataReader GetReader(int? commandTimeout)
{
var batcher = Session.Batcher;
SqlType[] sqlTypes = Commands.SelectMany(c => c.ParameterTypes).ToArray();
ForEachSqlCommand((sqlLoaderCommand, offset) => sqlLoaderCommand.ResetParametersIndexesForTheCommand(offset));
var command = batcher.PrepareQueryCommand(CommandType.Text, Sql, sqlTypes);
if (commandTimeout.HasValue)
{
command.CommandTimeout = commandTimeout.Value;
}
log.Info(command.CommandText);
BindParameters(command);
return BatcherDataReaderWrapper.Create(batcher, command);
}
protected virtual void BindParameters(DbCommand command)
{
var wholeQueryParametersList = Sql.GetParameters().ToList();
ForEachSqlCommand((sqlLoaderCommand, offset) => sqlLoaderCommand.Bind(command, wholeQueryParametersList, offset, Session));
}
/// <summary>
/// Execute the given <paramref name="actionToDo"/> for each command of the resultset.
/// </summary>
/// <param name="actionToDo">The action to perform where the first parameter is the <see cref="ISqlCommand"/> and the second parameter is the parameters offset of the <see cref="ISqlCommand"/>.</param>
protected void ForEachSqlCommand(Action<ISqlCommand, int> actionToDo)
{
var singleQueryParameterOffset = 0;
foreach (var sqlLoaderCommand in Commands)
{
actionToDo(sqlLoaderCommand, singleQueryParameterOffset);
singleQueryParameterOffset += sqlLoaderCommand.ParameterTypes.Length;
}
}
}
/// <summary>
/// Datareader wrapper with the same life cycle of its command (through the batcher)
/// </summary>
public partial class BatcherDataReaderWrapper: DbDataReader
{
private readonly IBatcher batcher;
private readonly DbCommand command;
private DbDataReader reader;
protected BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
{
if (batcher == null)
{
throw new ArgumentNullException("batcher");
}
if (command == null)
{
throw new ArgumentNullException("command");
}
this.batcher = batcher;
this.command = command;
}
public static BatcherDataReaderWrapper Create(IBatcher batcher, DbCommand command)
{
return new BatcherDataReaderWrapper(batcher, command)
{
reader = batcher.ExecuteReader(command)
};
}
public override string GetName(int i)
{
return reader.GetName(i);
}
public override string GetDataTypeName(int i)
{
return reader.GetDataTypeName(i);
}
public override IEnumerator GetEnumerator()
{
return reader.GetEnumerator();
}
public override System.Type GetFieldType(int i)
{
return reader.GetFieldType(i);
}
public override object GetValue(int i)
{
return reader.GetValue(i);
}
public override int GetValues(object[] values)
{
return reader.GetValues(values);
}
public override int GetOrdinal(string name)
{
return reader.GetOrdinal(name);
}
public override bool GetBoolean(int i)
{
return reader.GetBoolean(i);
}
public override byte GetByte(int i)
{
return reader.GetByte(i);
}
public override long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
return reader.GetBytes(i, fieldOffset, buffer, bufferoffset, length);
}
public override char GetChar(int i)
{
return reader.GetChar(i);
}
public override long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
return reader.GetChars(i, fieldoffset, buffer, bufferoffset, length);
}
public override Guid GetGuid(int i)
{
return reader.GetGuid(i);
}
public override short GetInt16(int i)
{
return reader.GetInt16(i);
}
public override int GetInt32(int i)
{
return reader.GetInt32(i);
}
public override long GetInt64(int i)
{
return reader.GetInt64(i);
}
public override float GetFloat(int i)
{
return reader.GetFloat(i);
}
public override double GetDouble(int i)
{
return reader.GetDouble(i);
}
public override string GetString(int i)
{
return reader.GetString(i);
}
public override decimal GetDecimal(int i)
{
return reader.GetDecimal(i);
}
public override DateTime GetDateTime(int i)
{
return reader.GetDateTime(i);
}
protected override DbDataReader GetDbDataReader(int ordinal)
{
return reader.GetData(ordinal);
}
public override bool IsDBNull(int i)
{
return reader.IsDBNull(i);
}
public override int FieldCount
{
get { return reader.FieldCount; }
}
public override bool HasRows
{
get { return reader.HasRows; }
}
public override object this[int i]
{
get { return reader[i]; }
}
public override object this[string name]
{
get { return reader[name]; }
}
public override bool Equals(object obj)
{
return reader.Equals(obj);
}
public override int GetHashCode()
{
return reader.GetHashCode();
}
public override void Close()
{
batcher.CloseCommand(command, reader);
}
public override DataTable GetSchemaTable()
{
return reader.GetSchemaTable();
}
public override bool NextResult()
{
return reader.NextResult();
}
public override bool Read()
{
return reader.Read();
}
public override Task<bool> ReadAsync(CancellationToken cancellationToken)
{
return reader.ReadAsync(cancellationToken);
}
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
{
return reader.IsDBNullAsync(ordinal, cancellationToken);
}
public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
{
return reader.NextResultAsync(cancellationToken);
}
public override int Depth
{
get { return reader.Depth; }
}
public override bool IsClosed
{
get { return reader.IsClosed; }
}
public override int RecordsAffected
{
get { return reader.RecordsAffected; }
}
}
}