forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlDefaultDataReader.cs
More file actions
293 lines (256 loc) · 10.2 KB
/
NpgsqlDefaultDataReader.cs
File metadata and controls
293 lines (256 loc) · 10.2 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Npgsql.BackendMessages;
using Npgsql.TypeHandlers;
using Npgsql.TypeHandling;
namespace Npgsql
{
/// <summary>
/// The default, non-sequential reader, which buffers entire rows in memory.
/// </summary>
#pragma warning disable CA1010
sealed class NpgsqlDefaultDataReader : NpgsqlDataReader
#pragma warning restore CA1010
{
/// <summary>
/// The number of columns in the current row
/// </summary>
int _numColumns;
[CanBeNull]
List<int> _columnOffsets;
int _endOffset;
int _column;
/// <summary>
/// List of all streams that have been opened on the current row, and need to be disposed of
/// when the row is consumed.
/// </summary>
[CanBeNull]
List<IDisposable> _streams;
internal NpgsqlDefaultDataReader(NpgsqlCommand command, CommandBehavior behavior, List<NpgsqlStatement> statements, Task sendTask)
: base(command, behavior, statements, sendTask) {}
internal override ValueTask<IBackendMessage> ReadMessage(bool async)
=> Connector.ReadMessage(async);
internal override Task<bool> NextResult(bool async)
{
var task = base.NextResult(async);
if (Command.Parameters.HasOutputParameters && StatementIndex == 0)
{
// Populate the output parameters from the first row of the first resultset
return task.ContinueWith((t, o) =>
{
if (HasRows)
PopulateOutputParameters();
return t.Result;
}, null);
}
return task;
}
/// <summary>
/// The first row in a stored procedure command that has output parameters needs to be traversed twice -
/// once for populating the output parameters and once for the actual result set traversal. So in this
/// case we can't be sequential.
/// </summary>
void PopulateOutputParameters()
{
Debug.Assert(Command.Parameters.Any(p => p.IsOutputDirection));
Debug.Assert(StatementIndex == 0);
Debug.Assert(RowDescription != null);
Debug.Assert(State == ReaderState.BeforeResult);
// Temporarily set our state to InResult to allow us to read the values
State = ReaderState.InResult;
var pending = new Queue<NpgsqlParameter>();
var taken = new List<int>();
foreach (var p in Command.Parameters.Where(p => p.IsOutputDirection))
{
if (RowDescription.TryGetFieldIndex(p.CleanName, out var idx))
{
// TODO: Provider-specific check?
p.Value = GetValue(idx);
taken.Add(idx);
}
else
pending.Enqueue(p);
}
for (var i = 0; pending.Count != 0 && i != _numColumns; ++i)
{
// TODO: Need to get the provider-specific value based on the out param's type
if (!taken.Contains(i))
pending.Dequeue().Value = GetValue(i);
}
State = ReaderState.BeforeResult; // Set the state back
}
internal override Task ConsumeRow(bool async)
{
Debug.Assert(State == ReaderState.InResult || State == ReaderState.BeforeResult);
Buffer.Seek(_endOffset, SeekOrigin.Begin);
if (_streams != null)
{
foreach (var stream in _streams)
stream.Dispose();
_streams.Clear();
}
return PGUtil.CompletedTask;
}
internal override void ProcessDataMessage(DataRowMessage dataMsg)
{
// The connector's buffer can actually change between DataRows:
// If a large DataRow exceeding the connector's current read buffer arrives, and we're
// reading in non-sequential mode, a new oversize buffer is allocated. We thus have to
// recapture the connector's buffer on each new DataRow.
Buffer = Connector.ReadBuffer;
_numColumns = Buffer.ReadInt16();
_column = -1;
Debug.Assert(RowDescription.NumFields == _numColumns);
if (_columnOffsets == null)
_columnOffsets = new List<int>(_numColumns);
else
_columnOffsets.Clear();
for (var i = 0; i < _numColumns; i++)
{
_columnOffsets.Add(Buffer.ReadPosition);
var len = Buffer.ReadInt32();
if (len != -1)
Buffer.Seek(len, SeekOrigin.Current);
}
_endOffset = Buffer.ReadPosition;
}
// We know the entire row is buffered in memory (non-sequential reader), so no I/O will be performed
public override Task<T> GetFieldValueAsync<T>(int column, CancellationToken cancellationToken)
=> Task.FromResult(GetFieldValue<T>(column));
public override T GetFieldValue<T>(int column)
{
CheckRowAndOrdinal(column);
SeekToColumn(column);
if (ColumnLen == -1)
throw new InvalidCastException("Column is null");
var fieldDescription = RowDescription[column];
try
{
return typeof(T) == typeof(object)
? (T)fieldDescription.Handler.ReadAsObject(Buffer, ColumnLen, fieldDescription)
: fieldDescription.Handler.Read<T>(Buffer, ColumnLen, fieldDescription);
}
catch (NpgsqlSafeReadException e)
{
throw e.InnerException;
}
catch
{
Connector.Break();
throw;
}
}
/// <summary>
/// Gets the value of the specified column as an instance of <see cref="object"/>.
/// </summary>
/// <param name="ordinal">The zero-based column ordinal.</param>
/// <returns>The value of the specified column.</returns>
public override object GetValue(int ordinal)
{
CheckRowAndOrdinal(ordinal);
SeekToColumn(ordinal);
if (ColumnLen == -1)
return DBNull.Value;
var fieldDescription = RowDescription[ordinal];
object result;
try
{
result = fieldDescription.Handler.ReadAsObject(Buffer, ColumnLen, fieldDescription);
}
catch (NpgsqlSafeReadException e)
{
throw e.InnerException;
}
catch
{
Connector.Break();
throw;
}
// Used for Entity Framework <= 6 compability
if (Command.ObjectResultTypes?[ordinal] != null)
{
var type = Command.ObjectResultTypes[ordinal];
result = type == typeof(DateTimeOffset)
? new DateTimeOffset((DateTime)result)
: Convert.ChangeType(result, type);
}
return result;
}
/// <summary>
/// Gets the value of the specified column as an instance of <see cref="object"/>.
/// </summary>
/// <param name="ordinal">The zero-based column ordinal.</param>
/// <returns>The value of the specified column.</returns>
public override object GetProviderSpecificValue(int ordinal)
{
CheckRowAndOrdinal(ordinal);
SeekToColumn(ordinal);
if (ColumnLen == -1)
return DBNull.Value;
var fieldDescription = RowDescription[ordinal];
object result;
try
{
// TODO: Maybe call a non-async method which would allow simple type handlers (and
// maybe also text) to read without going through async...
result = fieldDescription.Handler.ReadPsvAsObject(Buffer, ColumnLen, fieldDescription);
}
catch (NpgsqlSafeReadException e)
{
throw e.InnerException;
}
catch
{
Connector.Break();
throw;
}
return result;
}
/// <summary>
/// Gets a value that indicates whether the column contains nonexistent or missing values.
/// </summary>
/// <param name="ordinal">The zero-based column ordinal.</param>
/// <returns><b>true</b> if the specified column is equivalent to <see cref="DBNull"/>; otherwise <b>false</b>.</returns>
public override bool IsDBNull(int ordinal)
{
CheckRowAndOrdinal(ordinal);
SeekToColumn(ordinal);
return ColumnLen == -1;
}
internal override ValueTask<Stream> GetStreamInternal(int column, bool async)
{
SeekToColumn(column);
if (ColumnLen == -1)
throw new InvalidCastException("Column is null");
var s = new MemoryStream(Buffer.Buffer, Buffer.ReadPosition, ColumnLen, false, false);
if (_streams == null)
_streams = new List<IDisposable>();
_streams.Add(s);
return new ValueTask<Stream>(s);
}
void SeekToColumn(int column) => SeekToColumn(column, false);
internal override Task SeekToColumn(int column, bool async)
{
Buffer.Seek(_columnOffsets[column], SeekOrigin.Begin);
ColumnLen = Buffer.ReadInt32();
_column = column;
PosInColumn = 0;
return PGUtil.CompletedTask;
}
internal override Task SeekInColumn(int posInColumn, bool async)
{
if (posInColumn > ColumnLen)
posInColumn = ColumnLen;
Buffer.Seek(_columnOffsets[_column] + 4 + posInColumn, SeekOrigin.Begin);
PosInColumn = posInColumn;
return PGUtil.CompletedTask;
}
}
}