forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordHandler.cs
More file actions
72 lines (60 loc) · 2.49 KB
/
RecordHandler.cs
File metadata and controls
72 lines (60 loc) · 2.49 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
using System;
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandling;
using Npgsql.TypeMapping;
namespace Npgsql.TypeHandlers
{
[TypeMapping("record")]
class RecordHandlerFactory : NpgsqlTypeHandlerFactory<object[]>
{
public override NpgsqlTypeHandler<object[]> Create(PostgresType pgType, NpgsqlConnection conn)
=> new RecordHandler(pgType, conn.Connector!.TypeMapper);
}
/// <summary>
/// Type handler for PostgreSQL record types.
/// </summary>
/// <remarks>
/// http://www.postgresql.org/docs/current/static/datatype-pseudo.html
///
/// Encoding (identical to composite):
/// A 32-bit integer with the number of columns, then for each column:
/// * An OID indicating the type of the column
/// * The length of the column(32-bit integer), or -1 if null
/// * The column data encoded as binary
/// </remarks>
class RecordHandler : NpgsqlTypeHandler<object[]>
{
readonly ConnectorTypeMapper _typeMapper;
public RecordHandler(PostgresType postgresType, ConnectorTypeMapper typeMapper)
: base(postgresType)
{
_typeMapper = typeMapper;
}
#region Read
public override async ValueTask<object[]> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
{
await buf.Ensure(4, async);
var fieldCount = buf.ReadInt32();
var result = new object[fieldCount];
for (var i = 0; i < fieldCount; i++)
{
await buf.Ensure(8, async);
var typeOID = buf.ReadUInt32();
var fieldLen = buf.ReadInt32();
if (fieldLen == -1) // Null field, simply skip it and leave at default
continue;
result[i] = await _typeMapper.GetByOID(typeOID).ReadAsObject(buf, fieldLen, async);
}
return result;
}
#endregion
#region Write (unsupported)
public override int ValidateAndGetLength(object[] value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
=> throw new NotSupportedException("Can't write record types");
public override Task Write(object[] value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> throw new NotSupportedException("Can't write record types");
#endregion
}
}