-
Notifications
You must be signed in to change notification settings - Fork 890
Expand file tree
/
Copy pathRowDescriptionMessage.cs
More file actions
413 lines (351 loc) · 16 KB
/
Copy pathRowDescriptionMessage.cs
File metadata and controls
413 lines (351 loc) · 16 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
409
410
411
412
413
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Threading;
using Npgsql.Internal;
using Npgsql.Internal.Postgres;
using Npgsql.PostgresTypes;
using Npgsql.Replication.PgOutput.Messages;
namespace Npgsql.BackendMessages;
readonly struct ReadConversionContext(PgConcreteTypeInfo typeInfo, PgFieldBinding binding)
{
public bool IsDefault => TypeInfo is null;
public PgConcreteTypeInfo TypeInfo { get; } = typeInfo;
public PgFieldBinding Binding { get; } = binding;
}
/// <summary>
/// A RowDescription message sent from the backend.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/static/protocol-message-formats.html
/// </remarks>
sealed class RowDescriptionMessage : IBackendMessage
{
// We should really have CompareOptions.IgnoreKanaType here, but see
// https://github.com/dotnet/corefx/issues/12518#issuecomment-389658716
static readonly StringComparer InvariantIgnoreCaseAndKanaWidthComparer =
CultureInfo.InvariantCulture.CompareInfo.GetStringComparer(
CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType);
readonly bool _connectorOwned;
FieldDescription?[] _fields;
readonly Dictionary<string, int> _nameIndex;
Dictionary<string, int>? _insensitiveIndex;
ReadConversionContext[]? _lastConverterInfoCache;
internal RowDescriptionMessage(bool connectorOwned, int numFields = 10)
{
_connectorOwned = connectorOwned;
_fields = new FieldDescription[numFields];
_nameIndex = new Dictionary<string, int>();
}
RowDescriptionMessage(RowDescriptionMessage source)
{
Count = source.Count;
_fields = new FieldDescription?[Count];
for (var i = 0; i < Count; i++)
_fields[i] = source._fields[i]!.Clone();
_nameIndex = new Dictionary<string, int>(source._nameIndex);
if (source._insensitiveIndex?.Count > 0)
_insensitiveIndex = new Dictionary<string, int>(source._insensitiveIndex, InvariantIgnoreCaseAndKanaWidthComparer);
}
internal RowDescriptionMessage Load(NpgsqlReadBuffer buf, PgSerializerOptions options)
{
_nameIndex.Clear();
_insensitiveIndex?.Clear();
var numFields = Count = buf.ReadInt16();
if (_fields.Length < numFields)
{
var oldFields = _fields;
_fields = new FieldDescription[numFields];
Array.Copy(oldFields, _fields, oldFields.Length);
}
for (var i = 0; i < numFields; ++i)
{
var field = _fields[i] ??= new();
field.Populate(
options,
name: buf.ReadNullTerminatedString(),
tableOID: buf.ReadUInt32(),
columnAttributeNumber: buf.ReadInt16(),
oid: buf.ReadUInt32(),
typeSize: buf.ReadInt16(),
typeModifier: buf.ReadInt32(),
dataFormat: DataFormatUtils.Create(buf.ReadInt16())
);
_nameIndex.TryAdd(field.Name, i);
}
return this;
}
internal static RowDescriptionMessage CreateForReplication(
PgSerializerOptions options, uint tableOID, DataFormat dataFormat, IReadOnlyList<RelationMessage.Column> columns)
{
var msg = new RowDescriptionMessage(false, columns.Count);
var numFields = msg.Count = columns.Count;
for (var i = 0; i < numFields; ++i)
{
var field = msg._fields[i] = new();
var column = columns[i];
field.Populate(
options,
name: column.ColumnName,
tableOID: tableOID,
columnAttributeNumber: checked((short)i),
oid: column.DataTypeId,
typeSize: 0, // TODO: Confirm we don't have this in replication
typeModifier: column.TypeModifier,
dataFormat: dataFormat
);
if (!msg._nameIndex.ContainsKey(field.Name))
msg._nameIndex.Add(field.Name, i);
}
return msg;
}
public FieldDescription this[int ordinal]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((uint)ordinal >= (uint)Count)
{
ThrowHelper.ThrowIndexOutOfRangeException("Ordinal is out of range, value must be between 0 and {0} (exclusive).", Count);
return default!;
}
Debug.Assert(_fields[ordinal] != null);
return _fields[ordinal]!;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal void GetConversionContext(int ordinal, Type type, ref ReadConversionContext result)
=> this[ordinal].GetConversionContext(type, ref result);
internal void SetColumnInfoCache(ReadOnlySpan<ReadConversionContext> values)
{
if (_connectorOwned || _lastConverterInfoCache is not null)
return;
Interlocked.CompareExchange(ref _lastConverterInfoCache, values.ToArray(), null);
}
internal void LoadColumnInfoCache(PgSerializerOptions options, ReadConversionContext[] values)
{
if (_lastConverterInfoCache is not { } cache)
return;
// If the options have changed (for instance due to ReloadTypes) we need to invalidate the cache.
if (Count > 0 && !ReferenceEquals(options, _fields[0]!._serializerOptions))
{
Interlocked.CompareExchange(ref _lastConverterInfoCache, null, cache);
return;
}
cache.CopyTo(values.AsSpan());
}
public int Count { get; private set; }
/// <summary>
/// Given a string name, returns the field's ordinal index in the row.
/// </summary>
internal int GetFieldIndex(string name)
{
if (!TryGetFieldIndex(name, out var ret))
ThrowHelper.ThrowIndexOutOfRangeException($"Field not found in row: {name}");
return ret;
}
/// <summary>
/// Given a string name, returns the field's ordinal index in the row.
/// </summary>
internal bool TryGetFieldIndex(string name, out int fieldIndex)
{
if (_nameIndex.TryGetValue(name, out fieldIndex))
return true;
if (_insensitiveIndex is null || _insensitiveIndex.Count == 0)
{
if (_insensitiveIndex == null)
_insensitiveIndex = new Dictionary<string, int>(InvariantIgnoreCaseAndKanaWidthComparer);
foreach (var kv in _nameIndex)
_insensitiveIndex.TryAdd(kv.Key, kv.Value);
}
return _insensitiveIndex.TryGetValue(name, out fieldIndex);
}
public BackendMessageCode Code => BackendMessageCode.RowDescription;
internal RowDescriptionMessage Clone() => new(this);
}
/// <summary>
/// A descriptive record on a single field received from PostgreSQL.
/// See RowDescription in https://www.postgresql.org/docs/current/static/protocol-message-formats.html
/// </summary>
public sealed class FieldDescription
{
#pragma warning disable CS8618 // Lazy-initialized type
internal FieldDescription() { }
internal FieldDescription(uint oid)
: this("?", 0, 0, oid, 0, 0, DataFormat.Binary) { }
internal FieldDescription(
string name, uint tableOID, short columnAttributeNumber,
uint oid, short typeSize, int typeModifier, DataFormat dataFormat)
{
Name = name;
TableOID = tableOID;
ColumnAttributeNumber = columnAttributeNumber;
TypeOID = oid;
TypeSize = typeSize;
TypeModifier = typeModifier;
DataFormat = dataFormat;
}
#pragma warning restore CS8618
internal FieldDescription(FieldDescription source)
{
_serializerOptions = source._serializerOptions;
Name = source.Name;
TableOID = source.TableOID;
ColumnAttributeNumber = source.ColumnAttributeNumber;
TypeOID = source.TypeOID;
TypeSize = source.TypeSize;
TypeModifier = source.TypeModifier;
DataFormat = source.DataFormat;
PostgresType = source.PostgresType;
Field = source.Field;
_objectConversionContext = source._objectConversionContext;
}
internal void Populate(
PgSerializerOptions serializerOptions, string name, uint tableOID, short columnAttributeNumber,
uint oid, short typeSize, int typeModifier, DataFormat dataFormat
)
{
_serializerOptions = serializerOptions;
Name = name;
TableOID = tableOID;
ColumnAttributeNumber = columnAttributeNumber;
TypeOID = oid;
TypeSize = typeSize;
TypeModifier = typeModifier;
DataFormat = dataFormat;
PostgresType = _serializerOptions.DatabaseInfo.FindPostgresType((Oid)TypeOID)?.GetRepresentationalType() ?? UnknownBackendType.Instance;
Field = new(Name, _serializerOptions.ToCanonicalTypeId(PostgresType), TypeModifier);
_objectConversionContext = default;
}
/// <summary>
/// The field name.
/// </summary>
internal string Name { get; set; }
/// <summary>
/// The object ID of the field's data type.
/// </summary>
internal uint TypeOID { get; set; }
/// <summary>
/// The data type size (see pg_type.typlen). Note that negative values denote variable-width types.
/// </summary>
public short TypeSize { get; set; }
/// <summary>
/// The type modifier (see pg_attribute.atttypmod). The meaning of the modifier is type-specific.
/// </summary>
public int TypeModifier { get; set; }
/// <summary>
/// If the field can be identified as a column of a specific table, the object ID of the table; otherwise zero.
/// </summary>
internal uint TableOID { get; set; }
/// <summary>
/// If the field can be identified as a column of a specific table, the attribute number of the column; otherwise zero.
/// </summary>
internal short ColumnAttributeNumber { get; set; }
/// <summary>
/// The format code being used for the field.
/// Currently will be text or binary.
/// In a RowDescription returned from the statement variant of Describe, the format code is not yet known and will always be zero.
/// </summary>
internal DataFormat DataFormat { get; set; }
/// <summary>
/// Whether this field's data was requested in text format because the user opted into UnknownResultType
/// (via NpgsqlCommand.UnknownResultTypeList or AllResultTypesAreUnknown). Bindings for such fields are
/// expected to reinterpret the text bytes through a converter that could potentially only support binary formats.
/// </summary>
/// <remarks>
/// DataFormat.Text today exclusively signals that we executed with an UnknownResultTypeList.
/// If we ever want to fully support DataFormat.Text we'll need to flow UnknownResultType status separately.
/// </remarks>
internal bool IsUnknownResultType => DataFormat is DataFormat.Text;
internal Field Field { get; private set; }
internal string TypeDisplayName => PostgresType.GetDisplayNameWithFacets(TypeModifier);
internal PostgresType PostgresType { get; private set; }
internal Type FieldType => ObjectConversionContext.TypeInfo.Type;
ReadConversionContext _objectConversionContext;
internal ReadConversionContext ObjectConversionContext
{
get
{
if (!_objectConversionContext.IsDefault)
return _objectConversionContext;
GetInfoAndBind(null, ref _objectConversionContext);
return _objectConversionContext;
}
}
internal PgSerializerOptions _serializerOptions;
internal FieldDescription Clone()
{
var field = new FieldDescription(this);
return field;
}
internal void GetConversionContext(Type type, ref ReadConversionContext result) => GetInfoAndBind(type, ref result);
void GetInfoAndBind(Type? type, ref ReadConversionContext result)
{
Debug.Assert(result.IsDefault || (
ReferenceEquals(_serializerOptions, result.TypeInfo.Options) && (
IsUnknownResultType && result.TypeInfo.PgTypeId == _serializerOptions.TextPgTypeId ||
// Normal resolution
result.TypeInfo.PgTypeId == _serializerOptions.ToCanonicalTypeId(PostgresType))
), "Cache is bleeding over");
if (result is { IsDefault: false, TypeInfo.Type: var typeToConvert } && typeToConvert == type)
return;
var objectInfo = DataFormat is DataFormat.Text && type is not null ? ObjectConversionContext : _objectConversionContext;
if (objectInfo.TypeInfo is not null && (typeof(object) == type || objectInfo.TypeInfo.Type == type))
{
result = objectInfo;
return;
}
Core(type, out result);
if (!result.IsDefault && result.Binding.DataFormat != DataFormat)
ThrowHelper.ThrowInvalidOperationException(
$"Binding for column '{Name}' produced format '{result.Binding.DataFormat}' but the field format is '{DataFormat}'.");
[MethodImpl(MethodImplOptions.NoInlining)]
void Core(Type? type, out ReadConversionContext lastReadConversionContext)
{
PgFieldBinding binding;
switch (DataFormat)
{
case DataFormat.Text when IsUnknownResultType:
{
// Resolve the converter against pg_catalog.text, UnknownResultType reads text bytes
// for any column type. Every pg_catalog.text mapping we own declares text-format support, so a converter that
// can't bind to text here throws and surfaces as a missing mapping rather than getting silently reinterpreted.
var typeInfo = AdoSerializerHelpers.GetTypeInfoForReading(type ?? typeof(string), _serializerOptions.TextPgTypeId, _serializerOptions);
var concreteTypeInfo = typeInfo.MakeConcreteForField(
new ProviderFieldContext { Name = Field.Name, TypeModifier = Field.TypeModifier });
if (!concreteTypeInfo.SupportsReading)
AdoSerializerHelpers.ThrowReadingNotSupported(type, _serializerOptions, _serializerOptions.TextPgTypeId, resolved: true);
binding = concreteTypeInfo.BindField(DataFormat.Text);
lastReadConversionContext = new(concreteTypeInfo, binding);
break;
}
case DataFormat.Binary or DataFormat.Text:
{
var typeInfo = AdoSerializerHelpers.GetTypeInfoForReading(type ?? typeof(object), _serializerOptions.ToCanonicalTypeId(PostgresType), _serializerOptions);
var concreteTypeInfo = typeInfo.MakeConcreteForField(
new ProviderFieldContext { Name = Field.Name, TypeModifier = Field.TypeModifier });
if (!concreteTypeInfo.SupportsReading)
AdoSerializerHelpers.ThrowReadingNotSupported(type, _serializerOptions, _serializerOptions.ToCanonicalTypeId(PostgresType), resolved: true);
// If we don't support the DataFormat we'll just throw.
binding = concreteTypeInfo.BindField(DataFormat);
lastReadConversionContext = new(concreteTypeInfo, binding);
break;
}
default:
ThrowHelper.ThrowUnreachableException("Unknown data format {0}", DataFormat);
lastReadConversionContext = default;
break;
}
// We delay initializing ObjectOrDefaultInfo until after the first lookup (unless it is itself the first lookup).
// When passed in an unsupported type it allows the error to be more specific, instead of just having object/null to deal with.
if (_objectConversionContext.TypeInfo is null && type is not null)
_ = ObjectConversionContext;
}
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
public override string ToString() => Name + $"({PostgresType.DisplayName})";
}