forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayHandler.cs
More file actions
408 lines (338 loc) · 17.4 KB
/
ArrayHandler.cs
File metadata and controls
408 lines (338 loc) · 17.4 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandling;
namespace Npgsql.TypeHandlers
{
/// <summary>
/// Non-generic base class for all type handlers which handle PostgreSQL arrays.
/// Extend from <see cref="ArrayHandler{TElement}"/> instead.
/// </summary>
/// <remarks>
/// http://www.postgresql.org/docs/current/static/arrays.html.
///
/// The type handler API allows customizing Npgsql's behavior in powerful ways. However, although it is public, it
/// should be considered somewhat unstable, and may change in breaking ways, including in non-major releases.
/// Use it at your own risk.
/// </remarks>
public abstract class ArrayHandler : NpgsqlTypeHandler
{
/// <inheritdoc />
protected ArrayHandler(PostgresType arrayPostgresType) : base(arrayPostgresType) {}
internal static class IsArrayOf<TArray, TElement>
{
public static readonly bool Value = typeof(TArray).IsArray && typeof(TArray).GetElementType() == typeof(TElement);
}
/// <inheritdoc />
public override ArrayHandler CreateArrayHandler(PostgresArrayType arrayBackendType)
=> throw new NotSupportedException();
/// <inheritdoc />
public override RangeHandler CreateRangeHandler(PostgresRangeType rangeBackendType)
=> throw new NotSupportedException();
}
/// <summary>
/// Base class for all type handlers which handle PostgreSQL arrays.
/// </summary>
/// <remarks>
/// http://www.postgresql.org/docs/current/static/arrays.html.
///
/// The type handler API allows customizing Npgsql's behavior in powerful ways. However, although it is public, it
/// should be considered somewhat unstable, and may change in breaking ways, including in non-major releases.
/// Use it at your own risk.
/// </remarks>
public class ArrayHandler<TElement> : ArrayHandler
{
readonly int _lowerBound; // The lower bound value sent to the backend when writing arrays. Normally 1 (the PG default) but is 0 for OIDVector.
readonly NpgsqlTypeHandler _elementHandler;
/// <inheritdoc />
public ArrayHandler(PostgresType arrayPostgresType, NpgsqlTypeHandler elementHandler, int lowerBound = 1)
: base(arrayPostgresType)
{
_lowerBound = lowerBound;
_elementHandler = elementHandler;
}
internal override Type GetFieldType(FieldDescription? fieldDescription = null) => typeof(Array);
internal override Type GetProviderSpecificFieldType(FieldDescription? fieldDescription = null) => typeof(Array);
#region Read
internal override TAny Read<TAny>(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> Read<TAny>(buf, len, false, fieldDescription).Result;
/// <inheritdoc />
protected internal override async ValueTask<TAny> Read<TAny>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
{
if (IsArrayOf<TAny, TElement>.Value)
return (TAny)(object)await ReadArray<TElement>(buf, async);
if (typeof(TAny) == typeof(List<TElement>))
return (TAny)(object)await ReadList<TElement>(buf, async);
buf.Skip(len); // Perform this in sync for performance
throw new NpgsqlSafeReadException(new InvalidCastException(fieldDescription == null
? $"Can't cast database type to {typeof(TAny).Name}"
: $"Can't cast database type {fieldDescription.Handler.PgDisplayName} to {typeof(TAny).Name}"
));
}
internal override async ValueTask<object> ReadAsObject(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
=> await ReadArray<TElement>(buf, async);
internal override object ReadAsObject(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> ReadArray<TElement>(buf, false).Result;
/// <summary>
/// Reads an array of element type <typeparamref name="TAnyElement"/> from the given buffer <paramref name="buf"/>.
/// </summary>
protected async ValueTask<Array> ReadArray<TAnyElement>(NpgsqlReadBuffer buf, bool async)
{
await buf.Ensure(12, async);
var dimensions = buf.ReadInt32();
buf.ReadInt32(); // Has nulls. Not populated by PG?
buf.ReadUInt32(); // Element OID
var dimLengths = new int[dimensions];
await buf.Ensure(dimensions * 8, async);
for (var i = 0; i < dimensions; i++)
{
dimLengths[i] = buf.ReadInt32();
buf.ReadInt32(); // We don't care about the lower bounds
}
if (dimensions == 0)
return new TAnyElement[0]; // TODO: static instance
var result = Array.CreateInstance(typeof(TAnyElement), dimLengths);
if (dimensions == 1)
{
var oneDimensional = (TAnyElement[])result;
for (var i = 0; i < oneDimensional.Length; i++)
oneDimensional[i] = await _elementHandler.ReadWithLength<TAnyElement>(buf, async);
return oneDimensional;
}
// Multidimensional
var indices = new int[dimensions];
while (true)
{
var element = await _elementHandler.ReadWithLength<TAnyElement>(buf, async);
result.SetValue(element, indices);
// TODO: Overly complicated/inefficient...
indices[dimensions - 1]++;
for (var dim = dimensions - 1; dim >= 0; dim--)
{
if (indices[dim] <= result.GetUpperBound(dim))
continue;
if (dim == 0)
return result;
for (var j = dim; j < dimensions; j++)
indices[j] = result.GetLowerBound(j);
indices[dim - 1]++;
}
}
}
/// <summary>
/// Reads an array of element type <typeparamref name="TAnyElement"/> from the given buffer <paramref name="buf"/>.
/// </summary>
protected async ValueTask<List<TAnyElement>> ReadList<TAnyElement>(NpgsqlReadBuffer buf, bool async)
{
await buf.Ensure(12, async);
var dimensions = buf.ReadInt32();
if (dimensions == 0)
return new List<TAnyElement>();
if (dimensions > 1)
throw new NotSupportedException($"Can't read multidimensional array as List<{typeof(TAnyElement).Name}>");
buf.ReadInt32(); // Has nulls. Not populated by PG?
buf.ReadUInt32(); // Element OID. Ignored.
await buf.Ensure(8, async);
var length = buf.ReadInt32();
buf.ReadInt32(); // We don't care about the lower bounds
var list = new List<TAnyElement>(length);
for (var i = 0; i < length; i++)
list.Add(await _elementHandler.ReadWithLength<TAnyElement>(buf, async));
return list;
}
#endregion
#region Write
static Exception MixedTypesOrJaggedArrayException(Exception innerException)
=> new Exception("While trying to write an array, one of its elements failed validation. " +
"You may be trying to mix types in a non-generic IList, or to write a jagged array.", innerException);
static Exception CantWriteTypeException(Type type)
=> new InvalidCastException($"Can't write type {type} as an array of {typeof(TElement)}");
// Since TAny isn't constrained to class? or struct (C# doesn't have a non-nullable constraint that doesn't limit us to either struct or class),
// we must use the bang operator here to tell the compiler that a null value will never be returned.
/// <inheritdoc />
protected internal override int ValidateAndGetLength<TAny>(TAny value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
=> ValidateAndGetLength(value!, ref lengthCache);
/// <inheritdoc />
protected internal override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
=> ValidateAndGetLength(value!, ref lengthCache);
int ValidateAndGetLength(object value, ref NpgsqlLengthCache? lengthCache)
{
if (lengthCache == null)
lengthCache = new NpgsqlLengthCache(1);
if (lengthCache.IsPopulated)
return lengthCache.Get();
if (value is ICollection<TElement> generic)
return ValidateAndGetLengthGeneric(generic, ref lengthCache);
if (value is ICollection nonGeneric)
return ValidateAndGetLengthNonGeneric(nonGeneric, ref lengthCache);
throw CantWriteTypeException(value.GetType());
}
// Handle single-dimensional arrays and generic IList<T>
int ValidateAndGetLengthGeneric(ICollection<TElement> value, ref NpgsqlLengthCache lengthCache)
{
// Leave empty slot for the entire array length, and go ahead an populate the element slots
var pos = lengthCache.Position;
var len =
4 + // dimensions
4 + // has_nulls (unused)
4 + // type OID
1 * 8 + // number of dimensions (1) * (length + lower bound)
4 * value.Count; // sum of element lengths
lengthCache.Set(0);
NpgsqlLengthCache? elemLengthCache = lengthCache;
foreach (var element in value)
if (element != null && typeof(TElement) != typeof(DBNull))
try
{
len += _elementHandler.ValidateAndGetLength(element, ref elemLengthCache, null);
}
catch (Exception e)
{
throw MixedTypesOrJaggedArrayException(e);
}
lengthCache.Lengths[pos] = len;
return len;
}
// Take care of multi-dimensional arrays and non-generic IList, we have no choice but to box/unbox
int ValidateAndGetLengthNonGeneric(ICollection value, ref NpgsqlLengthCache lengthCache)
{
var asMultidimensional = value as Array;
var dimensions = asMultidimensional?.Rank ?? 1;
// Leave empty slot for the entire array length, and go ahead an populate the element slots
var pos = lengthCache.Position;
var len =
4 + // dimensions
4 + // has_nulls (unused)
4 + // type OID
dimensions * 8 + // number of dimensions * (length + lower bound)
4 * value.Count; // sum of element lengths
lengthCache.Set(0);
NpgsqlLengthCache? elemLengthCache = lengthCache;
foreach (var element in value)
if (element != null && element != DBNull.Value)
try
{
len += _elementHandler.ValidateObjectAndGetLength(element, ref elemLengthCache, null);
}
catch (Exception e)
{
throw MixedTypesOrJaggedArrayException(e);
}
lengthCache.Lengths[pos] = len;
return len;
}
internal override Task WriteWithLengthInternal<TAny>([AllowNull] TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
{
if (buf.WriteSpaceLeft < 4)
return WriteWithLengthLong();
return WriteWithLength();
async Task WriteWithLengthLong()
{
await buf.Flush(async);
await WriteWithLength();
}
Task WriteWithLength()
{
if (value == null || typeof(TAny) == typeof(DBNull))
{
buf.WriteInt32(-1);
return Task.CompletedTask;
}
buf.WriteInt32(ValidateAndGetLength(value, ref lengthCache, parameter));
if (value is ICollection<TElement> list)
return WriteGeneric(list, buf, lengthCache, async);
if (value is ICollection nonGeneric)
return WriteNonGeneric(nonGeneric, buf, lengthCache, async);
throw CantWriteTypeException(value.GetType());
}
}
// The default WriteObjectWithLength casts the type handler to INpgsqlTypeHandler<T>, but that's not sufficient for
// us (need to handle many types of T, e.g. int[], int[,]...)
/// <inheritdoc />
protected internal override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> value is DBNull
? WriteWithLengthInternal(DBNull.Value, buf, lengthCache, parameter, async)
: WriteWithLengthInternal(value, buf, lengthCache, parameter, async);
async Task WriteGeneric(ICollection<TElement> value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, bool async)
{
var len =
4 + // dimensions
4 + // has_nulls (unused)
4 + // type OID
1 * 8; // number of dimensions (1) * (length + lower bound)
if (buf.WriteSpaceLeft < len)
{
await buf.Flush(async);
Debug.Assert(buf.WriteSpaceLeft >= len, "Buffer too small for header");
}
buf.WriteInt32(1);
buf.WriteInt32(1); // has_nulls = 1. Not actually used by the backend.
buf.WriteUInt32(_elementHandler.PostgresType.OID);
buf.WriteInt32(value.Count);
buf.WriteInt32(_lowerBound); // We don't map .NET lower bounds to PG
foreach (var element in value)
await _elementHandler.WriteWithLengthInternal(element, buf, lengthCache, null, async);
}
async Task WriteNonGeneric(ICollection value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, bool async)
{
var asArray = value as Array;
var dimensions = asArray?.Rank ?? 1;
var len =
4 + // ndim
4 + // has_nulls
4 + // element_oid
dimensions * 8; // dim (4) + lBound (4)
if (buf.WriteSpaceLeft < len)
{
await buf.Flush(async);
Debug.Assert(buf.WriteSpaceLeft >= len, "Buffer too small for header");
}
buf.WriteInt32(dimensions);
buf.WriteInt32(1); // HasNulls=1. Not actually used by the backend.
buf.WriteUInt32(_elementHandler.PostgresType.OID);
if (asArray != null)
{
for (var i = 0; i < dimensions; i++)
{
buf.WriteInt32(asArray.GetLength(i));
buf.WriteInt32(_lowerBound); // We don't map .NET lower bounds to PG
}
}
else
{
buf.WriteInt32(value.Count);
buf.WriteInt32(_lowerBound); // We don't map .NET lower bounds to PG
}
foreach (var element in value)
await _elementHandler.WriteObjectWithLength(element ?? DBNull.Value, buf, lengthCache, null, async);
}
#endregion
}
/// <remarks>
/// http://www.postgresql.org/docs/current/static/arrays.html
/// </remarks>
/// <typeparam name="TElement">The .NET type contained as an element within this array</typeparam>
/// <typeparam name="TElementPsv">The .NET provider-specific type contained as an element within this array</typeparam>
class ArrayHandlerWithPsv<TElement, TElementPsv> : ArrayHandler<TElement>
{
public ArrayHandlerWithPsv(PostgresType arrayPostgresType, NpgsqlTypeHandler elementHandler)
: base(arrayPostgresType, elementHandler) { }
protected internal override async ValueTask<TAny> Read<TAny>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
{
if (IsArrayOf<TAny, TElementPsv>.Value)
return (TAny)(object)await ReadArray<TElementPsv>(buf, async);
if (typeof(TAny) == typeof(List<TElementPsv>))
return (TAny)(object)await ReadList<TElementPsv>(buf, async);
return await base.Read<TAny>(buf, len, async, fieldDescription);
}
internal override object ReadPsvAsObject(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> ReadPsvAsObject(buf, len, false, fieldDescription).Result;
internal override async ValueTask<object> ReadPsvAsObject(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
=> await ReadArray<TElementPsv>(buf, async);
}
}