forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextHandler.cs
More file actions
314 lines (270 loc) · 13.2 KB
/
TextHandler.cs
File metadata and controls
314 lines (270 loc) · 13.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandling;
using Npgsql.TypeMapping;
using NpgsqlTypes;
namespace Npgsql.TypeHandlers
{
/// <summary>
/// A factory for type handlers for PostgreSQL character data types (text, char, varchar, xml...).
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/datatype-character.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>
[TypeMapping("text", NpgsqlDbType.Text,
new[] { DbType.String, DbType.StringFixedLength, DbType.AnsiString, DbType.AnsiStringFixedLength },
new[] { typeof(string), typeof(char[]), typeof(char), typeof(ArraySegment<char>) },
DbType.String
)]
[TypeMapping("xml", NpgsqlDbType.Xml, dbType: DbType.Xml)]
[TypeMapping("character varying", NpgsqlDbType.Varchar, inferredDbType: DbType.String)]
[TypeMapping("character", NpgsqlDbType.Char, inferredDbType: DbType.String)]
[TypeMapping("name", NpgsqlDbType.Name, inferredDbType: DbType.String)]
[TypeMapping("refcursor", NpgsqlDbType.Refcursor, inferredDbType: DbType.String)]
[TypeMapping("citext", NpgsqlDbType.Citext, inferredDbType: DbType.String)]
[TypeMapping("unknown")]
public class TextHandlerFactory : NpgsqlTypeHandlerFactory<string>
{
/// <inheritdoc />
public override NpgsqlTypeHandler<string> Create(PostgresType pgType, NpgsqlConnection conn)
=> new TextHandler(pgType, conn);
}
/// <summary>
/// A type handler for PostgreSQL character data types (text, char, varchar, xml...).
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/datatype-character.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 TextHandler : NpgsqlTypeHandler<string>, INpgsqlTypeHandler<char[]>, INpgsqlTypeHandler<ArraySegment<char>>,
INpgsqlTypeHandler<char>, INpgsqlTypeHandler<byte[]>, ITextReaderHandler
{
// Text types are handled a bit more efficiently when sent as text than as binary
// see https://github.com/npgsql/npgsql/issues/1210#issuecomment-235641670
internal override bool PreferTextWrite => true;
readonly Encoding _encoding;
#region State
readonly char[] _singleCharArray = new char[1];
#endregion
/// <inheritdoc />
protected internal TextHandler(PostgresType postgresType, NpgsqlConnection connection)
: this(postgresType, connection.Connector!.TextEncoding) { }
/// <inheritdoc />
protected internal TextHandler(PostgresType postgresType, Encoding encoding)
: base(postgresType) => _encoding = encoding;
#region Read
/// <inheritdoc />
public override ValueTask<string> Read(NpgsqlReadBuffer buf, int byteLen, bool async, FieldDescription? fieldDescription = null)
{
return buf.ReadBytesLeft >= byteLen
? new ValueTask<string>(buf.ReadString(byteLen))
: ReadLong();
async ValueTask<string> ReadLong()
{
if (byteLen <= buf.Size)
{
// The string's byte representation can fit in our read buffer, read it.
while (buf.ReadBytesLeft < byteLen)
await buf.ReadMore(async);
return buf.ReadString(byteLen);
}
// Bad case: the string's byte representation doesn't fit in our buffer.
// This is rare - will only happen in CommandBehavior.Sequential mode (otherwise the
// entire row is in memory). Tweaking the buffer length via the connection string can
// help avoid this.
// Allocate a temporary byte buffer to hold the entire string and read it in chunks.
var tempBuf = new byte[byteLen];
var pos = 0;
while (true)
{
var len = Math.Min(buf.ReadBytesLeft, byteLen - pos);
buf.ReadBytes(tempBuf, pos, len);
pos += len;
if (pos < byteLen)
{
await buf.ReadMore(async);
continue;
}
break;
}
return buf.TextEncoding.GetString(tempBuf);
}
}
async ValueTask<char[]> INpgsqlTypeHandler<char[]>.Read(NpgsqlReadBuffer buf, int byteLen, bool async, FieldDescription? fieldDescription)
{
if (byteLen <= buf.Size)
{
// The string's byte representation can fit in our read buffer, read it.
while (buf.ReadBytesLeft < byteLen)
await buf.ReadMore(async);
return buf.ReadChars(byteLen);
}
// TODO: The following can be optimized with Decoder - no need to allocate a byte[]
var tempBuf = new byte[byteLen];
var pos = 0;
while (true)
{
var len = Math.Min(buf.ReadBytesLeft, byteLen - pos);
buf.ReadBytes(tempBuf, pos, len);
pos += len;
if (pos < byteLen)
{
await buf.ReadMore(async);
continue;
}
break;
}
return buf.TextEncoding.GetChars(tempBuf);
}
async ValueTask<char> INpgsqlTypeHandler<char>.Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription)
{
// Make sure we have enough bytes in the buffer for a single character
var maxBytes = Math.Min(buf.TextEncoding.GetMaxByteCount(1), len);
while (buf.ReadBytesLeft < maxBytes)
await buf.ReadMore(async);
var decoder = buf.TextEncoding.GetDecoder();
decoder.Convert(buf.Buffer, buf.ReadPosition, maxBytes, _singleCharArray, 0, 1, true, out var bytesUsed, out var charsUsed, out _);
buf.Skip(len - bytesUsed);
if (charsUsed < 1)
throw new NpgsqlSafeReadException(new NpgsqlException("Could not read char - string was empty"));
return _singleCharArray[0];
}
ValueTask<ArraySegment<char>> INpgsqlTypeHandler<ArraySegment<char>>.Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription)
{
buf.Skip(len);
throw new NpgsqlSafeReadException(new NotSupportedException("Only writing ArraySegment<char> to PostgreSQL text is supported, no reading."));
}
ValueTask<byte[]> INpgsqlTypeHandler<byte[]>.Read(NpgsqlReadBuffer buf, int byteLen, bool async, FieldDescription? fieldDescription)
{
var bytes = new byte[byteLen];
if (buf.ReadBytesLeft >= byteLen)
{
buf.ReadBytes(bytes, 0, byteLen);
return new ValueTask<byte[]>(bytes);
}
return ReadLong();
async ValueTask<byte[]> ReadLong()
{
if (byteLen <= buf.Size)
{
// The bytes can fit in our read buffer, read it.
while (buf.ReadBytesLeft < byteLen)
await buf.ReadMore(async);
buf.ReadBytes(bytes, 0, byteLen);
return bytes;
}
// Bad case: the bytes don't fit in our buffer.
// This is rare - will only happen in CommandBehavior.Sequential mode (otherwise the
// entire row is in memory). Tweaking the buffer length via the connection string can
// help avoid this.
var pos = 0;
while (true)
{
var len = Math.Min(buf.ReadBytesLeft, byteLen - pos);
buf.ReadBytes(bytes, pos, len);
pos += len;
if (pos < byteLen)
{
await buf.ReadMore(async);
continue;
}
break;
}
return bytes;
}
}
#endregion
#region Write
/// <inheritdoc />
public override unsafe int ValidateAndGetLength(string value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{
if (lengthCache == null)
lengthCache = new NpgsqlLengthCache(1);
if (lengthCache.IsPopulated)
return lengthCache.Get();
if (parameter == null || parameter.Size <= 0 || parameter.Size >= value.Length)
return lengthCache.Set(_encoding.GetByteCount(value));
fixed (char* p = value)
return lengthCache.Set(_encoding.GetByteCount(p, parameter.Size));
}
/// <inheritdoc />
public virtual int ValidateAndGetLength(char[] value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{
if (lengthCache == null)
lengthCache = new NpgsqlLengthCache(1);
if (lengthCache.IsPopulated)
return lengthCache.Get();
return lengthCache.Set(
parameter == null || parameter.Size <= 0 || parameter.Size >= value.Length
? _encoding.GetByteCount(value)
: _encoding.GetByteCount(value, 0, parameter.Size)
);
}
/// <inheritdoc />
public virtual int ValidateAndGetLength(ArraySegment<char> value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{
if (lengthCache == null)
lengthCache = new NpgsqlLengthCache(1);
if (lengthCache.IsPopulated)
return lengthCache.Get();
if (parameter?.Size > 0)
throw new ArgumentException($"Parameter {parameter.ParameterName} is of type ArraySegment<char> and should not have its Size set", parameter.ParameterName);
return lengthCache.Set(value.Array is null ? 0 : _encoding.GetByteCount(value.Array, value.Offset, value.Count));
}
/// <inheritdoc />
public int ValidateAndGetLength(char value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{
_singleCharArray[0] = value;
return _encoding.GetByteCount(_singleCharArray);
}
/// <inheritdoc />
public int ValidateAndGetLength(byte[] value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
=> value.Length;
/// <inheritdoc />
public override Task Write(string value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> WriteString(value, buf, lengthCache!, parameter, async);
/// <inheritdoc />
public virtual Task Write(char[] value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
{
var charLen = parameter == null || parameter.Size <= 0 || parameter.Size >= value.Length
? value.Length
: parameter.Size;
return buf.WriteChars(value, 0, charLen, lengthCache!.GetLast(), async);
}
/// <inheritdoc />
public virtual Task Write(ArraySegment<char> value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> value.Array is null ? Task.CompletedTask : buf.WriteChars(value.Array, value.Offset, value.Count, lengthCache!.GetLast(), async);
Task WriteString(string str, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter? parameter, bool async)
{
var charLen = parameter == null || parameter.Size <= 0 || parameter.Size >= str.Length
? str.Length
: parameter.Size;
return buf.WriteString(str, charLen, lengthCache!.GetLast(), async);
}
/// <inheritdoc />
public Task Write(char value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
{
_singleCharArray[0] = value;
var len = _encoding.GetByteCount(_singleCharArray);
return buf.WriteChars(_singleCharArray, 0, 1, len, async);
}
/// <inheritdoc />
public Task Write(byte[] value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> buf.WriteBytesRaw(value, async);
#endregion
/// <inheritdoc />
public virtual TextReader GetTextReader(Stream stream) => new StreamReader(stream);
}
}