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
403 lines (337 loc) · 16.8 KB
/
ArrayHandler.cs
File metadata and controls
403 lines (337 loc) · 16.8 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
#region License
// The PostgreSQL License
//
// Copyright (C) 2018 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using Npgsql.BackendMessages;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandling;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace Npgsql.TypeHandlers
{
public abstract class ArrayHandler : NpgsqlTypeHandler
{
internal static class IsArrayOf<TArray, TElement>
{
public static readonly bool Value = typeof(TArray).IsArray && typeof(TArray).GetElementType() == typeof(TElement);
}
/// <inheritdoc />
public override ArrayHandler CreateArrayHandler(PostgresType arrayBackendType)
=> throw new NotSupportedException();
/// <inheritdoc />
public override RangeHandler CreateRangeHandler(PostgresType 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
/// </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;
public ArrayHandler(NpgsqlTypeHandler elementHandler, int lowerBound = 1)
{
_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;
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;
protected async ValueTask<Array> ReadArray<T>(NpgsqlReadBuffer buf, bool async)
{
await buf.Ensure(12, async);
var dimensions = buf.ReadInt32();
buf.ReadInt32(); // Has nulls. Not populated by PG?
var elementOID = buf.ReadUInt32();
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 T[0]; // TODO: static instance
var result = Array.CreateInstance(typeof(T), dimLengths);
if (dimensions == 1)
{
var oneDimensional = (T[])result;
for (var i = 0; i < oneDimensional.Length; i++)
oneDimensional[i] = await _elementHandler.ReadWithLength<T>(buf, async);
return oneDimensional;
}
// Multidimensional
var indices = new int[dimensions];
while (true)
{
var element = await _elementHandler.ReadWithLength<T>(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]++;
}
}
}
protected async ValueTask<List<T>> ReadList<T>(NpgsqlReadBuffer buf, bool async)
{
await buf.Ensure(12, async);
var dimensions = buf.ReadInt32();
if (dimensions == 0)
return new List<T>();
if (dimensions > 1)
throw new NotSupportedException($"Can't read multidimensional array as List<{typeof(T).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<T>(length);
for (var i = 0; i < length; i++)
list.Add(await _elementHandler.ReadWithLength<T>(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)}");
protected internal override int ValidateAndGetLength<TAny>(TAny value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
=> ValidateAndGetLength(value, ref lengthCache);
protected internal override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter = null)
=> 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;
lengthCache.Set(0);
var elemLengthCache = lengthCache;
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
foreach (var element in value)
if (element != null && typeof(TElement) != typeof(DBNull))
try
{
len += _elementHandler.ValidateAndGetLength(element, ref lengthCache, null);
}
catch (Exception e)
{
throw MixedTypesOrJaggedArrayException(e);
}
lengthCache = elemLengthCache;
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;
lengthCache.Set(0);
var elemLengthCache = lengthCache;
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
foreach (var element in value)
if (element != null && element != DBNull.Value)
try
{
len += _elementHandler.ValidateObjectAndGetLength(element, ref lengthCache, null);
}
catch (Exception e)
{
throw MixedTypesOrJaggedArrayException(e);
}
lengthCache = elemLengthCache;
lengthCache.Lengths[pos] = len;
return len;
}
internal override Task WriteWithLengthInternal<TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
{
if (buf.WriteSpaceLeft < 4)
return WriteWithLengthLong();
if (value == null || typeof(TAny) == typeof(DBNull))
{
buf.WriteInt32(-1);
return PGUtil.CompletedTask;
}
return WriteWithLength();
async Task WriteWithLengthLong()
{
if (buf.WriteSpaceLeft < 4)
await buf.Flush(async);
if (value == null || typeof(TAny) == typeof(DBNull))
{
buf.WriteInt32(-1);
return;
}
await WriteWithLength();
}
Task WriteWithLength()
{
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[,]...)
protected internal override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
=> value == null || value is DBNull
? WriteWithLengthInternal<DBNull>(null, 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, 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(NpgsqlTypeHandler elementHandler)
: base(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)
=> await ReadArray<TElementPsv>(buf, async);
}
}